akaunting/app/Traits/Modules.php

619 lines
14 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;
2021-09-07 10:33:34 +03:00
use App\Utilities\Date;
2017-09-19 12:32:06 +03:00
use App\Utilities\Info;
2021-09-07 10:33:34 +03:00
use Illuminate\Support\Facades\Cache;
2017-09-14 22:21:00 +03:00
trait Modules
{
2022-12-20 16:01:33 +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
]
];
2022-06-01 10:15:55 +03:00
if (! $response = static::getResponse('POST', 'token/check', $data)) {
2019-12-04 18:10:38 +03:00
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
2020-05-20 21:14:49 +03:00
public function getModules($data = [])
2017-09-14 22:21:00 +03:00
{
$key = 'apps.app.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$items = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($items)) {
2019-11-19 16:52:44 +03:00
return $items;
}
2019-12-04 18:10:38 +03:00
$items = static::getResponseData('GET', 'apps/items');
2019-11-19 16:52:44 +03:00
2020-05-20 21:14:49 +03:00
Cache::put($key, $items, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
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
}
2022-06-01 10:15:55 +03:00
public function getModuleIsubscribe($alias)
{
if (! $response = static::getResponse('GET', 'apps/' . $alias . '/isubscribe')) {
return [];
}
$body = json_decode($response->getBody());
if (! is_object($body)) {
return [];
}
return $body;
}
public function getModuleDocumentation($alias, $data = [])
{
$key = 'apps.' . $alias . '.docs.' . $this->getDataKeyOfModules($data);
2020-05-20 21:14:49 +03:00
$documentation = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($documentation)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $documentation, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
return $documentation;
}
2021-05-31 14:22:20 +03:00
public function getModuleReleases($alias, $data = [])
{
$key = 'apps.' . $alias . '.releases.' . $this->getDataKeyOfModules($data);
$releases = Cache::get($key);
2022-06-01 10:15:55 +03:00
if (! empty($releases)) {
2021-05-31 14:22:20 +03:00
return $releases;
}
$releases = static::getResponseData('GET', 'apps/' . $alias . '/releases', $data);
Cache::put($key, $releases, Date::now()->addHour());
return $releases;
}
2018-09-20 18:35:24 +03:00
public function getModuleReviews($alias, $data = [])
{
$key = 'apps.' . $alias . '.reviews.' . $this->getDataKeyOfModules($data);
2020-05-20 21:14:49 +03:00
$reviews = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($reviews)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $reviews, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
return $reviews;
2018-09-20 18:35:24 +03:00
}
2022-06-01 10:15:55 +03:00
public function getModuleTestimonials($alias, $data = [])
{
$key = 'apps.' . $alias . '.testimonials.' . $this->getDataKeyOfModules($data);
$testimonials = Cache::get($key);
if (! empty($testimonials)) {
return $testimonials;
}
$testimonials = static::getResponseData('GET', 'apps/' . $alias . '/testimonials', $data);
Cache::put($key, $testimonials, Date::now()->addHour());
return $testimonials;
}
public function getBannersOfModules($data = [])
{
$key = 'apps.banners.' . $this->getDataKeyOfModules($data);
$banners = Cache::get($key);
if (! empty($banners)) {
return $banners;
}
$banners = static::getResponseData('GET', 'apps/banners');
Cache::put($key, $banners, Date::now()->addHour());
return $banners;
}
public function getCategoriesOfModules($data = [])
2017-09-14 22:21:00 +03:00
{
$key = 'apps.categories.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$categories = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($categories)) {
2019-11-19 16:52:44 +03:00
return $categories;
}
2019-12-04 18:10:38 +03:00
$categories = static::getResponseData('GET', 'apps/categories');
2019-11-19 16:52:44 +03:00
2020-05-20 21:14:49 +03:00
Cache::put($key, $categories, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
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
{
$key = 'apps.categories.' . $alias . '.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$category = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($category)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $category, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
return $category;
2017-09-14 22:21:00 +03:00
}
public function getVendorsOfModules($data = [])
2018-10-03 12:10:59 +03:00
{
$key = 'apps.vendors.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$vendors = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($vendors)) {
2019-11-19 16:52:44 +03:00
return $vendors;
}
2019-12-04 18:10:38 +03:00
$vendors = static::getResponseData('GET', 'apps/vendors');
2019-11-19 16:52:44 +03:00
2020-05-20 21:14:49 +03:00
Cache::put($key, $vendors, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
return $vendors;
2018-10-03 12:10:59 +03:00
}
public function getModulesByVendor($alias, $data = [])
{
$key = 'apps.vendors.' . $alias . '.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$vendor = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($vendor)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $vendor, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
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);
}
2020-05-20 21:14:49 +03:00
public function getInstalledModules()
{
2021-04-16 00:59:43 +03:00
$key = 'apps.installed.' . company_id();
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) {
2022-06-01 10:15:55 +03:00
if (! $this->moduleExists($module->alias)) {
2020-05-17 02:46:03 +03:00
return;
}
2022-06-01 10:15:55 +03:00
if (! $result = $this->getModule($module->alias)) {
2020-05-17 02:46:03 +03:00
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 = [])
{
$key = 'apps.pre_sale.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$pre_sale = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($pre_sale)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $pre_sale, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
return $pre_sale;
2019-01-30 18:39:39 +03:00
}
public function getPaidModules($data = [])
2017-09-14 22:21:00 +03:00
{
$key = 'apps.paid.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$paid = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($paid)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $paid, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
return $paid;
2017-09-14 22:21:00 +03:00
}
public function getNewModules($data = [])
2017-09-14 22:21:00 +03:00
{
$key = 'apps.new.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$new = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($new)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $new, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
return $new;
2017-09-14 22:21:00 +03:00
}
public function getFreeModules($data = [])
2017-09-14 22:21:00 +03:00
{
$key = 'apps.free.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$free = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($free)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $free, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
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
{
$key = 'apps.featured.' . $this->getDataKeyOfModules($data);
2020-05-20 01:01:06 +03:00
2020-05-20 21:14:49 +03:00
$featured = Cache::get($key);
2019-11-19 16:52:44 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($featured)) {
2019-11-19 16:52:44 +03:00
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
2020-05-20 21:14:49 +03:00
Cache::put($key, $featured, Date::now()->addHour());
2019-11-19 16:52:44 +03:00
return $featured;
2017-09-14 22:21:00 +03:00
}
2022-06-01 10:15:55 +03:00
public function getPopularModules($data = [])
{
$key = 'apps.popular.' . $this->getDataKeyOfModules($data);
$popular = Cache::get($key);
if (! empty($popular)) {
return $popular;
}
$popular = static::getResponseData('GET', 'apps/popular', $data);
Cache::put($key, $popular, Date::now()->addHour());
return $popular;
}
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);
}
2022-06-01 10:15:55 +03:00
public function getTestimonialModules($data = [])
{
$key = 'apps.testimonials.' . $this->getDataKeyOfModules($data);
$testimonials = Cache::get($key);
if (! empty($testimonials)) {
return $testimonials;
}
$testimonials = static::getResponseData('GET', 'apps/testimonials', $data);
Cache::put($key, $testimonials, Date::now()->addHour());
return $testimonials;
}
public function getWidgetsOfModules($data = [])
{
$key = 'apps.widgets.' . $this->getDataKeyOfModules($data);
$widgets = Cache::get($key);
if (! empty($widgets)) {
return $widgets;
}
$widgets = static::getResponseData('GET', 'apps/widgets');
Cache::put($key, $widgets, Date::now()->addHour());
return $widgets;
}
public function getModulesByWidget($alias, $data = [])
{
$key = 'apps.widgets.' . $alias . '.' . $this->getDataKeyOfModules($data);
$widget = Cache::get($key);
if (! empty($widget)) {
return $widget;
}
$widget = static::getResponseData('GET', 'apps/widgets/' . $alias, $data);
Cache::put($key, $widget, Date::now()->addHour());
return $widget;
}
2017-09-14 22:21:00 +03:00
public function getCoreVersion()
{
$data['query'] = Info::all();
2022-06-01 10:15:55 +03:00
if (! $response = static::getResponse('GET', 'core/version', $data)) {
2019-12-04 18:10:38 +03:00
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
}
2018-12-24 16:53:10 +03:00
public function moduleExists($alias)
{
2022-06-01 10:15:55 +03:00
if (! module($alias) instanceof \Akaunting\Module\Module) {
2020-05-17 02:46:03 +03:00
return false;
}
return true;
}
2021-11-19 11:33:44 +03:00
public function moduleIsEnabled($alias): bool
2020-05-17 02:46:03 +03:00
{
2021-11-19 11:33:44 +03:00
if (! $this->moduleExists($alias)) {
2020-05-17 02:46:03 +03:00
return false;
}
2018-12-24 16:53:10 +03:00
// Check if module is installed in cloud
2023-06-06 13:46:39 +03:00
if (request()->getHost() == 'app.akaunting.com' || request()->getHost() == 'localhost') {
$modules = Cache::get('cloud.companies.' . company_id() . '.modules.installed', []);
if (in_array($alias, $modules)) {
return true;
}
}
if (module($alias)->disabled()) {
return false;
}
2021-11-19 11:33:44 +03:00
if (! Module::alias($alias)->enabled()->first()) {
2020-05-17 02:46:03 +03:00
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
}
2021-11-19 11:33:44 +03:00
public function moduleIsDisabled($alias): bool
2021-11-12 11:05:35 +03:00
{
return ! $this->moduleIsEnabled($alias);
}
2018-05-25 16:57:58 +03:00
public function loadSuggestions()
{
2020-05-20 21:14:49 +03:00
$key = 'apps.suggestions';
$data = Cache::get($key);
2018-05-25 16:57:58 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($data)) {
2018-05-25 16:57:58 +03:00
return $data;
}
$data = [];
2022-06-01 10:15:55 +03:00
if (! $suggestions = static::getResponseData('GET', 'apps/suggestions')) {
2019-12-04 18:10:38 +03:00
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
}
2020-05-20 21:14:49 +03:00
Cache::put($key, $data, Date::now()->addHour(6));
2018-05-25 16:57:58 +03:00
return $data;
}
2018-11-16 18:33:39 +03:00
public function loadNotifications()
{
2020-05-20 21:14:49 +03:00
$key = 'apps.notifications';
$data = Cache::get($key);
2018-11-16 18:33:39 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($data)) {
2018-11-16 18:33:39 +03:00
return $data;
}
$data = [];
2022-06-01 10:15:55 +03:00
if (! $notifications = static::getResponseData('GET', 'apps/notifications')) {
2019-12-04 18:10:38 +03:00
return $data;
2018-11-16 18:33:39 +03:00
}
foreach ($notifications as $notification) {
$data[$notification->path][] = $notification;
}
2020-05-20 21:14:49 +03:00
Cache::put($key, $data, Date::now()->addHour(6));
2018-11-16 18:33:39 +03:00
return $data;
}
2022-06-01 10:15:55 +03:00
public function loadTips()
{
$key = 'apps.tips';
$data = Cache::get($key);
if (! empty($data)) {
return $data;
}
$data = [];
if (! $tips = static::getResponseData('GET', 'apps/tips')) {
return $data;
}
foreach ($tips as $tip) {
$data[$tip->path][] = $tip;
}
Cache::put($key, $data, Date::now()->addHour(6));
return $data;
}
2018-05-25 16:57:58 +03:00
public function getSuggestions($path)
{
2020-05-20 21:14:49 +03:00
$key = 'apps.suggestions';
$data = Cache::get($key);
2018-05-25 16:57:58 +03:00
if (empty($data)) {
$data = $this->loadSuggestions();
}
2022-06-01 10:15:55 +03:00
if (! empty($data) && array_key_exists($path, $data)) {
2018-05-25 16:57:58 +03:00
return $data[$path];
}
return false;
}
public function getNotifications($path): array
2018-11-16 18:33:39 +03:00
{
2020-05-20 21:14:49 +03:00
$key = 'apps.notifications';
$data = Cache::get($key);
2018-11-16 18:33:39 +03:00
if (empty($data)) {
$data = $this->loadNotifications();
}
2022-06-01 10:15:55 +03:00
if (! empty($data) && array_key_exists($path, $data)) {
return (array) $data[$path];
}
return [];
}
public function getTips($path): array
{
$key = 'apps.tips';
$data = Cache::get($key);
if (empty($data)) {
$data = $this->loadTips();
}
if (! empty($data) && array_key_exists($path, $data)) {
return (array) $data[$path];
2018-11-16 18:33:39 +03:00
}
return [];
2018-11-16 18:33:39 +03:00
}
2020-05-20 21:14:49 +03:00
public function getPageNumberOfModules($data = [])
2020-05-20 21:14:49 +03:00
{
if (empty($data['query']) || empty($data['query']['page'])) {
return 1;
}
return $data['query']['page'];
}
2020-06-17 23:07:31 +03:00
public function getDataKeyOfModules($data = [])
2020-06-17 23:07:31 +03:00
{
$result = 'language.' . language()->getShortCode() . '.page.' . $this->getPageNumberOfModules($data);
2020-06-17 23:07:31 +03:00
if (isset($data['query']['page'])) {
unset($data['query']['page']);
}
if (isset($data['query'])){
foreach($data['query'] as $key => $value) {
2020-07-02 10:09:23 +03:00
$result .= '.' . $key . '.' . $value;
2020-06-17 23:07:31 +03:00
}
}
return $result;
2020-07-02 10:09:23 +03:00
}
2017-09-14 22:21:00 +03:00
}