Update center added alert message

This commit is contained in:
Cüneyt Şentürk 2023-04-25 09:46:22 +03:00
parent 3400f006af
commit 8fbee840ea
10 changed files with 163 additions and 28 deletions

View File

@ -125,6 +125,6 @@ class DownloadModule extends Command
$version = Versions::getLatestVersion($url, $current); $version = Versions::getLatestVersion($url, $current);
} }
return $version; return $version?->latest;
} }
} }

View File

@ -91,7 +91,7 @@ class Update extends Command
public function getNewVersion() 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() public function getOldVersion()

View File

@ -48,7 +48,9 @@ class Updates extends Controller
$m->name = $row->getName(); $m->name = $row->getName();
$m->alias = $row->get('alias'); $m->alias = $row->get('alias');
$m->installed = $row->get('version'); $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; $modules[] = $m;
} }

View File

@ -45,7 +45,7 @@ class ShowInNotifications
$new->notifiable_type = "users"; $new->notifiable_type = "users";
$new->notifiable_id = user()->id; $new->notifiable_id = user()->id;
$new->data = [ $new->data = [
'title' => $name . ' (v' . $update . ')', 'title' => $name . ' (v' . $update?->latest . ')',
'description' => trans('install.update.' . $prefix, ['module' => $name, 'url' => route('updates.index')]), 'description' => trans('install.update.' . $prefix, ['module' => $name, 'url' => route('updates.index')]),
]; ];
$new->created_at = \Carbon\Carbon::now(); $new->created_at = \Carbon\Carbon::now();

View File

@ -44,7 +44,7 @@ class UpdateExtraModules
} }
$installed_version = $extra_module->get('version'); $installed_version = $extra_module->get('version');
$latest_version = Versions::latest($alias); $latest_version = Versions::latest($alias)?->latest;
// Skip if no update available // Skip if no update available
if (version_compare($installed_version, $latest_version, '>=')) { if (version_compare($installed_version, $latest_version, '>=')) {

View File

@ -55,15 +55,24 @@ trait SiteApi
return $response; 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 []; return [];
} }
$body = json_decode($response->getBody()); $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 []; return [];
} }

View File

@ -55,11 +55,11 @@ class Versions
{ {
$versions = static::all($alias); $versions = static::all($alias);
if (empty($versions[$alias]) || empty($versions[$alias]->data)) { if (empty($versions[$alias])) {
return false; return false;
} }
return $versions[$alias]->data->latest; return $versions[$alias];
} }
public static function all($modules = null) public static function all($modules = null)
@ -67,7 +67,7 @@ class Versions
// Get data from cache // Get data from cache
$versions = Cache::get('versions'); $versions = Cache::get('versions');
if (!empty($versions)) { if (! empty($versions)) {
return $versions; return $versions;
} }
@ -78,17 +78,33 @@ class Versions
// Check core first // Check core first
$url = 'core/version/' . $info['akaunting'] . '/' . $info['php'] . '/' . $info['mysql'] . '/' . $info['companies']; $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); $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 // Then modules
foreach ($modules as $module) { foreach ($modules as $module) {
if (is_string($module)) { if (is_string($module)) {
$module = module($module); $module = module($module);
} }
if (!$module instanceof \Akaunting\Module\Module) { if (! $module instanceof \Akaunting\Module\Module) {
continue; continue;
} }
@ -107,15 +123,27 @@ class Versions
public static function getLatestVersion($url, $latest) public static function getLatestVersion($url, $latest)
{ {
if (!$data = static::getResponseData('GET', $url, ['timeout' => 10])) { $version = new \stdClass();
return $latest;
$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)) { if (! is_object($body)) {
return $latest; 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() public static function getUpdates()
@ -123,7 +151,7 @@ class Versions
// Get data from cache // Get data from cache
$updates = Cache::get('updates'); $updates = Cache::get('updates');
if (!empty($updates)) { if (! empty($updates)) {
return $updates; return $updates;
} }
@ -146,7 +174,7 @@ class Versions
$installed_version = $module->get('version'); $installed_version = $module->get('version');
} }
if (version_compare($installed_version, $latest_version, '>=')) { if (version_compare($installed_version, $latest_version->latest, '>=')) {
continue; continue;
} }

View File

@ -0,0 +1,73 @@
<?php
namespace App\View\Components;
use App\Abstracts\View\Component;
use App\Utilities\Versions;
use Illuminate\Support\Arr;
class UpdateAlert extends Component
{
public $alerts;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct(array $alerts = [])
{
$this->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;
}
}

View File

@ -0,0 +1,5 @@
@foreach ($alerts as $type => $messages)
@foreach ($messages as $message)
<x-alert :type="$type" :message="$message" />
@endforeach
@endforeach

View File

@ -10,6 +10,8 @@
</x-slot> </x-slot>
<x-slot name="content"> <x-slot name="content">
<x-update-alert />
<div class="my-10"> <div class="my-10">
<div class="flex items-center"> <div class="flex items-center">
<div class="relative px-4 text-sm text-center pb-2 text-purple font-medium border-purple transition-all after:absolute after:w-full after:h-0.5 after:left-0 after:right-0 after:bottom-0 after:bg-purple after:rounded-tl-md after:rounded-tr-md"> <div class="relative px-4 text-sm text-center pb-2 text-purple font-medium border-purple transition-all after:absolute after:w-full after:h-0.5 after:left-0 after:right-0 after:bottom-0 after:bg-purple after:rounded-tl-md after:rounded-tr-md">
@ -31,9 +33,17 @@
<x-table.td kind="right" class="w-6/12" kind="cursor-none"> <x-table.td kind="right" class="w-6/12" kind="cursor-none">
<x-slot name="first" class="flex justify-end" override="class"> <x-slot name="first" class="flex justify-end" override="class">
<x-link href="{{ route('updates.run', ['alias' => 'core', 'version' => $core]) }}" class="px-3 py-1.5 rounded-xl text-sm font-medium leading-6 ltr:mr-2 rtl:ml-2 bg-green text-white hover:bg-green-700 disabled:bg-green-100" override="class"> @if (! $core->errors)
{{ trans('updates.update', ['version' => $core]) }} <x-link href="{{ route('updates.run', ['alias' => 'core', 'version' => $core->latest]) }}" class="px-3 py-1.5 rounded-xl text-sm font-medium leading-6 ltr:mr-2 rtl:ml-2 bg-green text-white hover:bg-green-700 disabled:bg-green-100" override="class">
</x-link> {{ trans('updates.update', ['version' => $core->latest]) }}
</x-link>
@else
<x-tooltip id="tooltip-core-button" placement="top" :message="$core->message">
<x-button class="px-3 py-1.5 rounded-xl text-sm font-medium leading-6 ltr:mr-2 rtl:ml-2 text-white bg-green-300 cursor-default" override="class">
{{ trans('updates.update', ['version' => $core->latest]) }}
</x-button>
</x-tooltip>
@endif
<x-button @click="onChangelog"> <x-button @click="onChangelog">
{{ trans('updates.changelog') }} {{ trans('updates.changelog') }}
@ -76,7 +86,7 @@
<x-table.tbody> <x-table.tbody>
@if ($modules) @if ($modules)
@foreach($modules as $module) @foreach ($modules as $module)
<x-table.tr> <x-table.tr>
<x-table.td class="w-3/12" kind="cursor-none"> <x-table.td class="w-3/12" kind="cursor-none">
{{ $module->name }} {{ $module->name }}
@ -91,9 +101,17 @@
</x-table.td> </x-table.td>
<x-table.td class="w-3/12" kind="right"> <x-table.td class="w-3/12" kind="right">
<x-link href="{{ route('updates.run', ['alias' => $module->alias, 'version' => $module->latest]) }}" kind="primary"> @if (empty($module->errors))
{{ trans_choice('general.updates', 1) }} <x-link href="{{ route('updates.run', ['alias' => $module->alias, 'version' => $module->latest]) }}" kind="primary">
</x-link> {{ trans_choice('general.updates', 1) }}
</x-link>
@else
<x-tooltip id="tooltip-modules-{{ $module->alias }}" placement="top" :message="$module->message">
<x-button class="px-3 py-1.5 rounded-xl text-sm font-medium leading-6 text-white bg-green-300 cursor-default" override="class">
{{ trans_choice('general.updates', 1) }}
</x-button>
</x-tooltip>
@endif
</x-table.td> </x-table.td>
</x-table.tr> </x-table.tr>
@endforeach @endforeach