akaunting/app/Traits/Modules.php

623 lines
15 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Traits;
2019-12-04 18:10:38 +03:00
use App\Models\Module\Module;
2019-11-16 10:21:14 +03:00
use App\Traits\SiteApi;
2019-12-03 15:41:56 +03:00
use App\Utilities\Console;
2017-09-19 12:32:06 +03:00
use App\Utilities\Info;
2017-09-14 22:21:00 +03:00
use Artisan;
2018-12-12 20:28:16 +03:00
use Cache;
use Date;
2017-09-14 22:21:00 +03:00
use File;
2019-11-16 10:21:14 +03:00
use Illuminate\Support\Str;
2017-09-19 12:32:06 +03:00
use ZipArchive;
2017-09-14 22:21:00 +03:00
trait Modules
{
2019-11-16 10:21:14 +03:00
use SiteApi;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
public function checkToken($apiKey)
2018-04-25 16:56:01 +03:00
{
$data = [
'form_params' => [
2019-11-16 10:21:14 +03:00
'token' => $apiKey,
2018-04-25 16:56:01 +03:00
]
];
2019-12-04 18:10:38 +03:00
if (!$response = static::getResponse('POST', 'token/check', $data)) {
return false;
2018-04-25 16:56:01 +03:00
}
2019-12-04 18:10:38 +03:00
$result = json_decode($response->getBody());
return $result->success ? true : false;
2018-04-25 16:56:01 +03:00
}
2019-11-16 10:21:14 +03:00
// Get All Modules
2017-09-14 22:21:00 +03:00
public function getModules()
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$items = Cache::get('apps.items');
if (!empty($items)) {
return $items;
}
2019-12-04 18:10:38 +03:00
$items = static::getResponseData('GET', 'apps/items');
2019-11-19 16:52:44 +03:00
Cache::put('apps.items', $items, Date::now()->addHour());
return $items;
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
// Get Module
2017-09-14 22:21:00 +03:00
public function getModule($alias)
{
2019-12-04 18:10:38 +03:00
$item = static::getResponseData('GET', 'apps/' . $alias);
2019-11-19 16:52:44 +03:00
return $item;
2017-09-14 22:21:00 +03:00
}
public function getDocumentation($alias)
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$documentation = Cache::get('apps.docs.' . $alias);
if (!empty($documentation)) {
return $documentation;
}
2019-12-04 18:10:38 +03:00
$documentation = static::getResponseData('GET', 'apps/docs/' . $alias);
2019-11-19 16:52:44 +03:00
Cache::put('apps.docs.' . $alias, $documentation, Date::now()->addHour());
return $documentation;
}
2018-09-20 18:35:24 +03:00
public function getModuleReviews($alias, $data = [])
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$reviews = Cache::get('apps.' . $alias . '.reviews');
if (!empty($reviews)) {
return $reviews;
}
2019-12-04 18:10:38 +03:00
$reviews = static::getResponseData('GET', 'apps/' . $alias . '/reviews', $data);
2019-11-19 16:52:44 +03:00
Cache::put('apps.' . $alias . '.reviews', $reviews, Date::now()->addHour());
return $reviews;
2018-09-20 18:35:24 +03:00
}
2017-09-14 22:21:00 +03:00
public function getCategories()
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$categories = Cache::get('apps.categories');
if (!empty($categories)) {
return $categories;
}
2019-12-04 18:10:38 +03:00
$categories = static::getResponseData('GET', 'apps/categories');
2019-11-19 16:52:44 +03:00
Cache::put('apps.categories', $categories, Date::now()->addHour());
return $categories;
2017-09-14 22:21:00 +03:00
}
2018-09-20 11:16:56 +03:00
public function getModulesByCategory($alias, $data = [])
2017-09-14 22:21:00 +03:00
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$category = Cache::get('apps.categories.' . $alias);
if (!empty($category)) {
return $category;
}
2019-12-04 18:10:38 +03:00
$category = static::getResponseData('GET', 'apps/categories/' . $alias, $data);
2019-11-19 16:52:44 +03:00
Cache::put('apps.categories.' . $alias, $category, Date::now()->addHour());
return $category;
2017-09-14 22:21:00 +03:00
}
2018-10-03 12:10:59 +03:00
public function getVendors()
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$vendors = Cache::get('apps.vendors');
if (!empty($vendors)) {
return $vendors;
}
2019-12-04 18:10:38 +03:00
$vendors = static::getResponseData('GET', 'apps/vendors');
2019-11-19 16:52:44 +03:00
Cache::put('apps.vendors', $vendors, Date::now()->addHour());
return $vendors;
2018-10-03 12:10:59 +03:00
}
public function getModulesByVendor($alias, $data = [])
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$vendor = Cache::get('apps.vendors.' . $alias);
if (!empty($vendor)) {
return $vendor;
}
2019-12-04 18:10:38 +03:00
$vendor = static::getResponseData('GET', 'apps/vendors/' . $alias, $data);
2019-11-19 16:52:44 +03:00
Cache::put('apps.vendors.' . $alias, $vendor, Date::now()->addHour());
return $vendor;
2018-10-03 12:10:59 +03:00
}
public function getMyModules($data = [])
{
2019-12-04 18:10:38 +03:00
return static::getResponseData('GET', 'apps/my', $data);
}
public function getInstalledModules($data = [])
{
$company_id = session('company_id');
2020-05-17 02:46:03 +03:00
$key = 'installed.' . $company_id . '.module';
2020-05-17 02:46:03 +03:00
if ($installed = Cache::get($key)) {
return $installed;
}
$installed = [];
2019-11-16 10:21:14 +03:00
2020-05-17 02:46:03 +03:00
Module::all()->each(function($module) use (&$installed) {
if (!$this->moduleExists($module->alias)) {
return;
}
2020-05-17 02:46:03 +03:00
if (!$result = $this->getModule($module->alias)) {
return;
}
2020-05-17 02:46:03 +03:00
$installed[] = $result;
});
Cache::put($key, $installed, Date::now()->addHour(6));
return $installed;
}
2019-01-30 18:39:39 +03:00
public function getPreSaleModules($data = [])
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$pre_sale = Cache::get('apps.pre_sale');
if (!empty($pre_sale)) {
return $pre_sale;
}
2019-12-04 18:10:38 +03:00
$pre_sale = static::getResponseData('GET', 'apps/pre_sale', $data);
2019-11-19 16:52:44 +03:00
Cache::put('apps.pre_sale', $pre_sale, Date::now()->addHour());
return $pre_sale;
2019-01-30 18:39:39 +03:00
}
public function getPaidModules($data = [])
2017-09-14 22:21:00 +03:00
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$paid = Cache::get('apps.paid');
if (!empty($paid)) {
return $paid;
}
2019-12-04 18:10:38 +03:00
$paid = static::getResponseData('GET', 'apps/paid', $data);
2019-11-19 16:52:44 +03:00
Cache::put('apps.paid', $paid, Date::now()->addHour());
return $paid;
2017-09-14 22:21:00 +03:00
}
public function getNewModules($data = [])
2017-09-14 22:21:00 +03:00
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$new = Cache::get('apps.new');
if (!empty($new)) {
return $new;
}
2019-12-04 18:10:38 +03:00
$new = static::getResponseData('GET', 'apps/new', $data);
2019-11-19 16:52:44 +03:00
Cache::put('apps.new', $new, Date::now()->addHour());
return $new;
2017-09-14 22:21:00 +03:00
}
public function getFreeModules($data = [])
2017-09-14 22:21:00 +03:00
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$free = Cache::get('apps.free');
if (!empty($free)) {
return $free;
}
2019-12-04 18:10:38 +03:00
$free = static::getResponseData('GET', 'apps/free', $data);
2019-11-19 16:52:44 +03:00
Cache::put('apps.free', $free, Date::now()->addHour());
return $free;
2018-04-25 19:28:10 +03:00
}
2019-11-19 16:52:44 +03:00
public function getFeaturedModules($data = [])
2018-04-25 19:28:10 +03:00
{
2019-11-19 16:52:44 +03:00
// Get data from cache
$featured = Cache::get('apps.featured');
if (!empty($featured)) {
return $featured;
}
2019-12-04 18:10:38 +03:00
$featured = static::getResponseData('GET', 'apps/featured', $data);
2019-11-19 16:52:44 +03:00
Cache::put('apps.featured', $featured, Date::now()->addHour());
return $featured;
2017-09-14 22:21:00 +03:00
}
2019-11-19 16:52:44 +03:00
public function getSearchModules($data = [])
{
2019-12-04 18:10:38 +03:00
return static::getResponseData('GET', 'apps/search', $data);
}
2017-09-14 22:21:00 +03:00
public function getCoreVersion()
{
$data['query'] = Info::all();
2019-12-04 18:10:38 +03:00
if (!$response = static::getResponse('GET', 'core/version', $data)) {
return [];
2017-09-14 22:21:00 +03:00
}
2019-12-04 18:10:38 +03:00
return $response->json();
2017-09-14 22:21:00 +03:00
}
public function downloadModule($path)
{
2020-05-07 17:17:04 +03:00
if (empty($path)) {
return [
'success' => false,
'error' => true,
'message' => trans('modules.errors.download', ['module' => '']),
'data' => null,
];
}
2019-12-04 18:10:38 +03:00
if (!$response = static::getResponse('GET', $path)) {
return [
'success' => false,
'error' => true,
2020-05-07 17:17:04 +03:00
'message' => trans('modules.errors.download', ['module' => '']),
2019-12-04 18:10:38 +03:00
'data' => null,
];
}
2017-09-14 22:21:00 +03:00
2019-12-04 18:10:38 +03:00
$file = $response->getBody()->getContents();
2017-09-14 22:21:00 +03:00
2019-12-04 18:10:38 +03:00
$path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp') . '/' . $path;
2017-09-14 22:21:00 +03:00
2019-12-04 18:10:38 +03:00
$file_path = $temp_path . '/upload.zip';
2017-09-14 22:21:00 +03:00
2019-12-04 18:10:38 +03:00
// Create tmp directory
if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path);
}
2017-09-14 22:21:00 +03:00
2019-12-04 18:10:38 +03:00
// Add content to the Zip file
$uploaded = is_int(file_put_contents($file_path, $file)) ? true : false;
2017-09-14 22:21:00 +03:00
2019-12-04 18:10:38 +03:00
if (!$uploaded) {
2017-10-11 16:01:23 +03:00
return [
2019-12-04 18:10:38 +03:00
'success' => false,
'error' => true,
2020-05-07 17:17:04 +03:00
'message' => trans('modules.errors.download', ['module' => '']),
2019-12-04 18:10:38 +03:00
'data' => null,
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
}
2017-10-11 16:01:23 +03:00
return [
2019-12-04 18:10:38 +03:00
'success' => true,
'error' => false,
2019-11-20 17:11:35 +03:00
'message' => null,
2019-12-04 18:10:38 +03:00
'data' => [
'path' => $path,
],
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
}
public function unzipModule($path)
{
2020-05-07 17:17:04 +03:00
if (empty($path)) {
return [
'success' => false,
'error' => true,
'message' => trans('modules.errors.unzip', ['module' => '']),
'data' => null,
];
}
2017-09-14 22:21:00 +03:00
$temp_path = storage_path('app/temp') . '/' . $path;
$file = $temp_path . '/upload.zip';
// Unzip the file
2017-09-19 12:32:06 +03:00
$zip = new ZipArchive();
if (!$zip->open($file) || !$zip->extractTo($temp_path)) {
return [
2017-09-14 22:21:00 +03:00
'success' => false,
2019-11-20 17:11:35 +03:00
'error' => true,
2020-05-07 17:17:04 +03:00
'message' => trans('modules.errors.unzip', ['module' => '']),
2017-09-14 22:21:00 +03:00
'data' => null,
2017-09-19 12:32:06 +03:00
];
2017-09-14 22:21:00 +03:00
}
2017-09-19 12:32:06 +03:00
$zip->close();
2017-09-14 22:21:00 +03:00
// Remove Zip
2017-09-19 12:32:06 +03:00
File::delete($file);
2017-09-14 22:21:00 +03:00
2017-09-19 12:32:06 +03:00
$data = [
2017-09-14 22:21:00 +03:00
'path' => $path
2017-09-19 12:32:06 +03:00
];
2017-09-14 22:21:00 +03:00
2017-09-19 12:32:06 +03:00
return [
2017-09-14 22:21:00 +03:00
'success' => true,
2019-11-20 17:11:35 +03:00
'error' => false,
'message' => null,
2017-09-14 22:21:00 +03:00
'data' => $data,
2017-09-19 12:32:06 +03:00
];
2017-09-14 22:21:00 +03:00
}
public function installModule($path)
{
2020-05-07 17:17:04 +03:00
if (empty($path)) {
return [
'success' => false,
'error' => true,
'message' => trans('modules.errors.finish', ['module' => '']),
'data' => null,
];
}
2017-09-14 22:21:00 +03:00
$temp_path = storage_path('app/temp') . '/' . $path;
2020-05-07 17:17:04 +03:00
$modules_path = config('module.paths.modules');
2017-09-14 22:21:00 +03:00
// Create modules directory
if (!File::isDirectory($modules_path)) {
File::makeDirectory($modules_path);
}
$module = json_decode(file_get_contents($temp_path . '/module.json'));
2019-11-16 10:21:14 +03:00
$module_path = $modules_path . '/' . Str::studly($module->alias);
2017-09-14 22:21:00 +03:00
// Create module directory
if (!File::isDirectory($module_path)) {
File::makeDirectory($module_path);
}
// Move all files/folders from temp path then delete it
File::copyDirectory($temp_path, $module_path);
File::deleteDirectory($temp_path);
2017-10-11 16:01:23 +03:00
$data = [
2019-11-16 10:21:14 +03:00
'path' => $path,
'name' => Str::studly($module->alias),
2020-02-03 18:57:31 +03:00
'alias' => $module->alias,
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
2019-12-03 15:41:56 +03:00
$company_id = session('company_id');
2020-02-03 18:57:31 +03:00
$locale = app()->getLocale();
2019-12-03 15:41:56 +03:00
Cache::forget('installed.' . $company_id . '.module');
2020-05-07 17:17:04 +03:00
$command = "module:install {$module->alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
$message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $module->alias]);
return [
'success' => false,
'error' => true,
'message' => $message,
'data' => null,
];
}
2019-12-03 15:41:56 +03:00
2017-10-11 16:01:23 +03:00
return [
2017-09-14 22:21:00 +03:00
'success' => true,
2020-05-07 17:17:04 +03:00
'redirect' => route('apps.app.show', $module->alias),
2019-11-20 17:11:35 +03:00
'error' => false,
'message' => null,
2017-09-14 22:21:00 +03:00
'data' => $data,
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
}
public function uninstallModule($alias)
{
2019-11-16 10:21:14 +03:00
$module = module($alias);
2017-09-14 22:21:00 +03:00
2017-10-11 16:01:23 +03:00
$data = [
2019-11-16 10:21:14 +03:00
'name' => $module->getName(),
2017-09-14 22:21:00 +03:00
'category' => $module->get('category'),
'version' => $module->get('version'),
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
2019-01-14 15:41:51 +03:00
Artisan::call('cache:clear');
2017-09-14 22:21:00 +03:00
$module->delete();
2019-01-14 15:41:51 +03:00
// Cache Data clear
File::deleteDirectory(storage_path('framework/cache/data'));
2017-09-14 22:21:00 +03:00
2017-10-11 16:01:23 +03:00
return [
2017-09-14 22:21:00 +03:00
'success' => true,
2019-11-20 17:11:35 +03:00
'error' => false,
'message' => null,
2019-12-03 15:41:56 +03:00
'data' => $data,
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
}
public function enableModule($alias)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
$module = module($alias);
2017-09-14 22:21:00 +03:00
2017-10-11 16:01:23 +03:00
$data = [
2019-11-16 10:21:14 +03:00
'name' => $module->getName(),
2017-09-14 22:21:00 +03:00
'category' => $module->get('category'),
'version' => $module->get('version'),
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
$module->enable();
Artisan::call('cache:clear');
2017-10-11 16:01:23 +03:00
return [
2017-09-14 22:21:00 +03:00
'success' => true,
2019-11-20 17:11:35 +03:00
'error' => false,
'message' => null,
2019-12-03 15:41:56 +03:00
'data' => $data,
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
}
public function disableModule($alias)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
$module = module($alias);
2017-09-14 22:21:00 +03:00
2017-10-11 16:01:23 +03:00
$data = [
2019-11-16 10:21:14 +03:00
'name' => $module->getName(),
'category' => $module->get('category'),
'version' => $module->get('version'),
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
$module->disable();
Artisan::call('cache:clear');
2017-10-11 16:01:23 +03:00
return [
2017-09-14 22:21:00 +03:00
'success' => true,
2019-11-20 17:11:35 +03:00
'error' => false,
'message' => null,
2019-12-03 15:41:56 +03:00
'data' => $data,
2017-10-11 16:01:23 +03:00
];
2017-09-14 22:21:00 +03:00
}
2018-12-24 16:53:10 +03:00
public function moduleExists($alias)
{
2020-05-17 02:46:03 +03:00
if (!module($alias) instanceof \Akaunting\Module\Module) {
return false;
}
return true;
}
public function moduleEnabled($alias)
{
if (!$this->moduleExists($alias)) {
return false;
}
2018-12-24 16:53:10 +03:00
2020-05-17 02:46:03 +03:00
if (!Module::alias($alias)->enabled()->first()) {
return false;
2018-12-24 16:53:10 +03:00
}
2020-05-17 02:46:03 +03:00
return true;
2018-12-24 16:53:10 +03:00
}
2018-05-25 16:57:58 +03:00
public function loadSuggestions()
{
// Get data from cache
$data = Cache::get('suggestions');
if (!empty($data)) {
return $data;
}
$data = [];
2019-12-04 18:10:38 +03:00
if (!$suggestions = static::getResponseData('GET', 'apps/suggestions')) {
return $data;
2018-05-25 16:57:58 +03:00
}
2019-12-04 18:10:38 +03:00
foreach ($suggestions as $suggestion) {
$data[$suggestion->path] = $suggestion;
2018-05-25 16:57:58 +03:00
}
Cache::put('suggestions', $data, Date::now()->addHour(6));
return $data;
}
2018-11-16 18:33:39 +03:00
public function loadNotifications()
{
// Get data from cache
$data = Cache::get('notifications');
if (!empty($data)) {
return $data;
}
$data = [];
2019-12-04 18:10:38 +03:00
if (!$notifications = static::getResponse('GET', 'apps/notifications')) {
return $data;
2018-11-16 18:33:39 +03:00
}
foreach ($notifications as $notification) {
$data[$notification->path][] = $notification;
}
Cache::put('notifications', $data, Date::now()->addHour(6));
return $data;
}
2018-05-25 16:57:58 +03:00
public function getSuggestions($path)
{
// Get data from cache
$data = Cache::get('suggestions');
if (empty($data)) {
$data = $this->loadSuggestions();
}
2018-06-13 15:07:43 +03:00
if (!empty($data) && array_key_exists($path, $data)) {
2018-05-25 16:57:58 +03:00
return $data[$path];
}
return false;
}
2018-11-16 18:33:39 +03:00
public function getNotifications($path)
{
// Get data from cache
$data = Cache::get('notifications');
if (empty($data)) {
$data = $this->loadNotifications();
}
if (!empty($data) && array_key_exists($path, $data)) {
return $data[$path];
}
return false;
}
2017-09-14 22:21:00 +03:00
}