From 8fbee840ea49a0d7ff6b709d253424a991f9e51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Tue, 25 Apr 2023 09:46:22 +0300 Subject: [PATCH] Update center added alert message --- app/Console/Commands/DownloadModule.php | 2 +- app/Console/Commands/Update.php | 2 +- app/Http/Controllers/Install/Updates.php | 4 +- app/Listeners/Menu/ShowInNotifications.php | 2 +- app/Listeners/Module/UpdateExtraModules.php | 2 +- app/Traits/SiteApi.php | 15 +++- app/Utilities/Versions.php | 54 ++++++++++---- app/View/Components/UpdateAlert.php | 73 +++++++++++++++++++ .../views/components/update-alert.blade.php | 5 ++ .../views/install/updates/index.blade.php | 32 ++++++-- 10 files changed, 163 insertions(+), 28 deletions(-) create mode 100644 app/View/Components/UpdateAlert.php create mode 100644 resources/views/components/update-alert.blade.php diff --git a/app/Console/Commands/DownloadModule.php b/app/Console/Commands/DownloadModule.php index 75e55163c23..dad683816fd 100644 --- a/app/Console/Commands/DownloadModule.php +++ b/app/Console/Commands/DownloadModule.php @@ -125,6 +125,6 @@ protected function getVersion() $version = Versions::getLatestVersion($url, $current); } - return $version; + return $version?->latest; } } diff --git a/app/Console/Commands/Update.php b/app/Console/Commands/Update.php index dfdf4da9a20..7b4beb77163 100644 --- a/app/Console/Commands/Update.php +++ b/app/Console/Commands/Update.php @@ -91,7 +91,7 @@ public function handle() public function getNewVersion() { - return ($this->argument('new') == 'latest') ? Versions::latest($this->alias) : $this->argument('new'); + return ($this->argument('new') == 'latest') ? Versions::latest($this->alias)?->latest : $this->argument('new'); } public function getOldVersion() diff --git a/app/Http/Controllers/Install/Updates.php b/app/Http/Controllers/Install/Updates.php index 86a2fe684e3..92181690b36 100644 --- a/app/Http/Controllers/Install/Updates.php +++ b/app/Http/Controllers/Install/Updates.php @@ -48,7 +48,9 @@ public function index() $m->name = $row->getName(); $m->alias = $row->get('alias'); $m->installed = $row->get('version'); - $m->latest = $updates[$alias]; + $m->latest = $updates[$alias]->latest; + $m->errors = $updates[$alias]->errors; + $m->message = $updates[$alias]->message; $modules[] = $m; } diff --git a/app/Listeners/Menu/ShowInNotifications.php b/app/Listeners/Menu/ShowInNotifications.php index 2b9942c814c..1b286dd7894 100644 --- a/app/Listeners/Menu/ShowInNotifications.php +++ b/app/Listeners/Menu/ShowInNotifications.php @@ -45,7 +45,7 @@ public function handle(Event $event) $new->notifiable_type = "users"; $new->notifiable_id = user()->id; $new->data = [ - 'title' => $name . ' (v' . $update . ')', + 'title' => $name . ' (v' . $update?->latest . ')', 'description' => trans('install.update.' . $prefix, ['module' => $name, 'url' => route('updates.index')]), ]; $new->created_at = \Carbon\Carbon::now(); diff --git a/app/Listeners/Module/UpdateExtraModules.php b/app/Listeners/Module/UpdateExtraModules.php index 73c5f2b3bb5..46f3f973a86 100644 --- a/app/Listeners/Module/UpdateExtraModules.php +++ b/app/Listeners/Module/UpdateExtraModules.php @@ -44,7 +44,7 @@ public function handle(Event $event) } $installed_version = $extra_module->get('version'); - $latest_version = Versions::latest($alias); + $latest_version = Versions::latest($alias)?->latest; // Skip if no update available if (version_compare($installed_version, $latest_version, '>=')) { diff --git a/app/Traits/SiteApi.php b/app/Traits/SiteApi.php index 5d73e0f0438..9fc4b0e6712 100644 --- a/app/Traits/SiteApi.php +++ b/app/Traits/SiteApi.php @@ -55,15 +55,24 @@ public static function getResponse($method, $path, $data = [], $status_code = 20 return $response; } - public static function getResponseData($method, $path, $data = [], $status_code = 200) + public static function getResponseBody($method, $path, $data = [], $status_code = 200) { - if (!$response = static::getResponse($method, $path, $data, $status_code)) { + if (! $response = static::getResponse($method, $path, $data, $status_code)) { return []; } $body = json_decode($response->getBody()); - if (!is_object($body)) { + return $body; + } + + public static function getResponseData($method, $path, $data = [], $status_code = 200) + { + if (! $body = static::getResponseBody($method, $path, $data, $status_code)) { + return []; + } + + if (! is_object($body)) { return []; } diff --git a/app/Utilities/Versions.php b/app/Utilities/Versions.php index fb0bb8588cf..3293200335d 100644 --- a/app/Utilities/Versions.php +++ b/app/Utilities/Versions.php @@ -55,11 +55,11 @@ public static function latest($alias) { $versions = static::all($alias); - if (empty($versions[$alias]) || empty($versions[$alias]->data)) { + if (empty($versions[$alias])) { return false; } - return $versions[$alias]->data->latest; + return $versions[$alias]; } public static function all($modules = null) @@ -67,7 +67,7 @@ public static function all($modules = null) // Get data from cache $versions = Cache::get('versions'); - if (!empty($versions)) { + if (! empty($versions)) { return $versions; } @@ -78,17 +78,33 @@ public static function all($modules = null) // Check core first $url = 'core/version/' . $info['akaunting'] . '/' . $info['php'] . '/' . $info['mysql'] . '/' . $info['companies']; - $versions['core'] = static::getLatestVersion($url, $info['akaunting']); - + # Installed modules start $modules = Arr::wrap($modules); + $installed_modules = []; + $module_version = '?modules='; + + foreach ($modules as $module) { + $alias = $module->get('alias'); + $version = $module->get('version'); + + $installed_modules[] = $alias; + } + + $module_version .= implode(',', $installed_modules); + + $url .= $module_version; + # Installed modules end + + $versions['core'] = static::getLatestVersion($url, $info['akaunting']); + // Then modules foreach ($modules as $module) { if (is_string($module)) { $module = module($module); } - if (!$module instanceof \Akaunting\Module\Module) { + if (! $module instanceof \Akaunting\Module\Module) { continue; } @@ -107,15 +123,27 @@ public static function all($modules = null) public static function getLatestVersion($url, $latest) { - if (!$data = static::getResponseData('GET', $url, ['timeout' => 10])) { - return $latest; + $version = new \stdClass(); + + $version->can_update = true; + $version->latest = $latest; + $version->errors = false; + $version->message = ''; + + if (! $body = static::getResponseBody('GET', $url, ['timeout' => 10])) { + return $version; } - if (!is_object($data)) { - return $latest; + if (! is_object($body)) { + return $version; } - return $data->latest; + $version->can_update = $body->success; + $version->latest = $body->data->latest; + $version->errors = $body->errors; + $version->message = $body->message; + + return $version; } public static function getUpdates() @@ -123,7 +151,7 @@ public static function getUpdates() // Get data from cache $updates = Cache::get('updates'); - if (!empty($updates)) { + if (! empty($updates)) { return $updates; } @@ -146,7 +174,7 @@ public static function getUpdates() $installed_version = $module->get('version'); } - if (version_compare($installed_version, $latest_version, '>=')) { + if (version_compare($installed_version, $latest_version->latest, '>=')) { continue; } diff --git a/app/View/Components/UpdateAlert.php b/app/View/Components/UpdateAlert.php new file mode 100644 index 00000000000..fddf47bae3f --- /dev/null +++ b/app/View/Components/UpdateAlert.php @@ -0,0 +1,73 @@ +alerts = $this->getAlerts($alerts); + } + + /** + * Get the view / contents that represent the component. + * + * @return \Illuminate\Contracts\View\View|string + */ + public function render() + { + return view('components.update-alert'); + } + + public function getAlerts() + { + $alerts = []; + + $updates = Versions::getUpdates(); + + if (! $updates) { + return $alerts; + } + + foreach ($updates as $alias => $update) { + if (! $update->errors) { + continue; + } + + foreach ($update->errors as $key => $error) { + switch ($key) { + case 'core': + $type = 'danger'; + break; + case 'expires': + case 'compatible': + $type = 'warning'; + break; + default: + $type = 'danger'; + } + + if (is_object($error) || is_array($error)) { + foreach ($error as $message) { + $alerts[$type][] = $message; + } + } else { + $alerts[$type][] = $error; + } + } + } + + return $alerts; + } +} diff --git a/resources/views/components/update-alert.blade.php b/resources/views/components/update-alert.blade.php new file mode 100644 index 00000000000..21913684212 --- /dev/null +++ b/resources/views/components/update-alert.blade.php @@ -0,0 +1,5 @@ +@foreach ($alerts as $type => $messages) + @foreach ($messages as $message) + + @endforeach +@endforeach diff --git a/resources/views/install/updates/index.blade.php b/resources/views/install/updates/index.blade.php index b400232635d..3ea5184ae61 100644 --- a/resources/views/install/updates/index.blade.php +++ b/resources/views/install/updates/index.blade.php @@ -10,6 +10,8 @@ + +
@@ -31,9 +33,17 @@ - - {{ trans('updates.update', ['version' => $core]) }} - + @if (! $core->errors) + + {{ trans('updates.update', ['version' => $core->latest]) }} + + @else + + + {{ trans('updates.update', ['version' => $core->latest]) }} + + + @endif {{ trans('updates.changelog') }} @@ -76,7 +86,7 @@ @if ($modules) - @foreach($modules as $module) + @foreach ($modules as $module) {{ $module->name }} @@ -91,9 +101,17 @@ - - {{ trans_choice('general.updates', 1) }} - + @if (empty($module->errors)) + + {{ trans_choice('general.updates', 1) }} + + @else + + + {{ trans_choice('general.updates', 1) }} + + + @endif @endforeach