Added Suggestion module on pages

This commit is contained in:
cuneytsenturk
2018-05-25 16:57:58 +03:00
parent 2b9721195d
commit 272b46e8a9
6 changed files with 110 additions and 1 deletions

View File

@ -9,6 +9,8 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Module;
use ZipArchive;
use Cache;
use Date;
trait Modules
{
@ -311,6 +313,53 @@ trait Modules
];
}
public function loadSuggestions()
{
// Get data from cache
$data = Cache::get('suggestions');
if (!empty($data)) {
return $data;
}
$data = [];
$url = 'apps/suggestions';
$response = $this->getRemote($url, 'GET', ['timeout' => 30, 'referer' => true]);
// Bad response
if ($response->getStatusCode() != 200) {
return false;
}
$suggestions = json_decode($response->getBody())->data;
foreach ($suggestions as $suggestion) {
$data[$suggestion->path] = $suggestion;
}
Cache::put('suggestions', $data, Date::now()->addHour(6));
return $data;
}
public function getSuggestions($path)
{
// Get data from cache
$data = Cache::get('suggestions');
if (empty($data)) {
$data = $this->loadSuggestions();
}
if (array_key_exists($path, $data)) {
return $data[$path];
}
return false;
}
protected function getRemote($path, $method = 'GET', $data = array())
{
$base = 'https://akaunting.com/api/';