Skip to content

Commit

Permalink
Update center added alert message
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Apr 25, 2023
1 parent 3400f00 commit 8fbee84
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/DownloadModule.php
Expand Up @@ -125,6 +125,6 @@ protected function getVersion()
$version = Versions::getLatestVersion($url, $current);
}

return $version;
return $version?->latest;
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/Update.php
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Install/Updates.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/Menu/ShowInNotifications.php
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/Module/UpdateExtraModules.php
Expand Up @@ -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, '>=')) {
Expand Down
15 changes: 12 additions & 3 deletions app/Traits/SiteApi.php
Expand Up @@ -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 [];
}

Expand Down
54 changes: 41 additions & 13 deletions app/Utilities/Versions.php
Expand Up @@ -55,19 +55,19 @@ 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)
{
// Get data from cache
$versions = Cache::get('versions');

if (!empty($versions)) {
if (! empty($versions)) {
return $versions;
}

Expand All @@ -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;
}

Expand All @@ -107,23 +123,35 @@ 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()
{
// Get data from cache
$updates = Cache::get('updates');

if (!empty($updates)) {
if (! empty($updates)) {
return $updates;
}

Expand All @@ -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;
}

Expand Down
73 changes: 73 additions & 0 deletions app/View/Components/UpdateAlert.php
@@ -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;
}
}
5 changes: 5 additions & 0 deletions resources/views/components/update-alert.blade.php
@@ -0,0 +1,5 @@
@foreach ($alerts as $type => $messages)
@foreach ($messages as $message)
<x-alert :type="$type" :message="$message" />
@endforeach
@endforeach
32 changes: 25 additions & 7 deletions resources/views/install/updates/index.blade.php
Expand Up @@ -10,6 +10,8 @@
</x-slot>

<x-slot name="content">
<x-update-alert />

<div class="my-10">
<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">
Expand All @@ -31,9 +33,17 @@

<x-table.td kind="right" class="w-6/12" kind="cursor-none">
<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">
{{ trans('updates.update', ['version' => $core]) }}
</x-link>
@if (! $core->errors)
<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">
{{ 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">
{{ trans('updates.changelog') }}
Expand Down Expand Up @@ -76,7 +86,7 @@

<x-table.tbody>
@if ($modules)
@foreach($modules as $module)
@foreach ($modules as $module)
<x-table.tr>
<x-table.td class="w-3/12" kind="cursor-none">
{{ $module->name }}
Expand All @@ -91,9 +101,17 @@
</x-table.td>

<x-table.td class="w-3/12" kind="right">
<x-link href="{{ route('updates.run', ['alias' => $module->alias, 'version' => $module->latest]) }}" kind="primary">
{{ trans_choice('general.updates', 1) }}
</x-link>
@if (empty($module->errors))
<x-link href="{{ route('updates.run', ['alias' => $module->alias, 'version' => $module->latest]) }}" kind="primary">
{{ 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.tr>
@endforeach
Expand Down

0 comments on commit 8fbee84

Please sign in to comment.