diff --git a/admin.php b/admin.php index 705fee85..2d49159e 100644 --- a/admin.php +++ b/admin.php @@ -1,10 +1,11 @@ [['login', 100000], ['onPluginsInitialized', 1000]], - 'onShutdown' => ['onShutdown', 1000] + 'onShutdown' => ['onShutdown', 1000] ]; } @@ -98,7 +99,7 @@ class AdminPlugin extends Plugin } } - /** + /** * Initialize administration plugin if admin path matches. * * Disables system cache. @@ -177,7 +178,7 @@ class AdminPlugin extends Plugin public function onTwigTemplatePaths() { $this->theme = $this->config->get('plugins.admin.theme', 'grav'); - $this->grav['twig']->twig_paths = array(__DIR__ . '/themes/'.$this->theme.'/templates'); + $this->grav['twig']->twig_paths = array(__DIR__ . '/themes/' . $this->theme . '/templates'); } /** @@ -186,7 +187,7 @@ class AdminPlugin extends Plugin public function onTwigSiteVariables() { // TODO: use real plugin name instead - $theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/admin/themes/'.$this->theme; + $theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/admin/themes/' . $this->theme; $twig = $this->grav['twig']; // Dynamic type support @@ -199,13 +200,14 @@ class AdminPlugin extends Plugin $twig->twig_vars['location'] = $this->template; $twig->twig_vars['base_url_relative_frontend'] = $twig->twig_vars['base_url_relative']; $twig->twig_vars['base_url_relative'] .= - ($twig->twig_vars['base_url_relative'] != '/' ? '/' : '') . trim($this->config->get('plugins.admin.route'), '/'); + ($twig->twig_vars['base_url_relative'] != '/' ? '/' : '') . trim($this->config->get('plugins.admin.route'), + '/'); $twig->twig_vars['theme_url'] = $theme_url; $twig->twig_vars['base_url'] = $twig->twig_vars['base_url_relative']; $twig->twig_vars['admin'] = $this->admin; // fake grav update - $twig->twig_vars['grav_update'] = array('current'=>'0.9.1', 'available'=>'0.9.1'); + $twig->twig_vars['grav_update'] = array('current' => '0.9.1', 'available' => '0.9.1'); switch ($this->template) { case 'dashboard': @@ -213,7 +215,8 @@ class AdminPlugin extends Plugin break; case 'pages': $twig->twig_vars['file'] = File::instance($this->admin->page(true)->filePath()); - $twig->twig_vars['media_types'] = str_replace('defaults,', '', implode(',.', array_keys($this->config->get('media')))); + $twig->twig_vars['media_types'] = str_replace('defaults,', '', + implode(',.', array_keys($this->config->get('media')))); break; } } @@ -231,12 +234,48 @@ class AdminPlugin extends Plugin } } + public function onTaskGPM() + { + $action = $_POST['action']; // getUpdatable | getUpdatablePlugins | getUpdatableThemes | gravUpdates + + if (isset($this->grav['session'])) { + $this->grav['session']->close(); + } + + try { + $gpm = new GPM(); + + switch ($action) { + case 'getUpdates': + $resources_updates = $gpm->getUpdatable(); + $grav_updates = [ + "isUpdatable" => $gpm->grav->isUpdatable(), + "assets" => $gpm->grav->getAssets(), + "version" => GRAV_VERSION, + "available" => $gpm->grav->getVersion(), + "date" => $gpm->grav->getDate() + ]; + + echo json_encode([ + "success" => true, + "payload" => ["resources" => $resources_updates, "grav" => $grav_updates] + ]); + break; + } + } catch (\Exception $e) { + echo json_encode(["success" => false, "message" => $e->getMessage()]); + } + + exit; + } + protected function initializeAdmin() { $this->enable([ - 'onPagesInitialized' => ['onPagesInitialized', 1000], + 'onPagesInitialized' => ['onPagesInitialized', 1000], 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000], - 'onTwigSiteVariables' => ['onTwigSiteVariables', 1000] + 'onTwigSiteVariables' => ['onTwigSiteVariables', 1000], + 'onTask.GPM' => ['onTaskGPM', 0] ]); // Change login behavior. diff --git a/themes/grav/js/admin-all.js b/themes/grav/js/admin-all.js index d8894814..56b47ba9 100644 --- a/themes/grav/js/admin-all.js +++ b/themes/grav/js/admin-all.js @@ -1,5 +1,10 @@ -$(function() -{ +$(function () { + jQuery.substitute = function(str, sub) { + return str.replace(/\{(.+?)\}/g, function($0, $1) { + return $1 in sub ? sub[$1] : $0; + }); + }; + // selectize $('select.fancy').selectize({ createOnBlur: true @@ -7,12 +12,94 @@ $(function() $('input.fancy').selectize({ delimiter: ',', - persist: false, - create: function(input) { + persist: false, + create: function (input) { return { value: input, - text: input + text: input } } }); + + // GPM + $.post(window.location.href, { + task: 'GPM', + action: 'getUpdates' + }, function (response) { + if (!response.success) { + throw new Error(response.message); + } + + var grav = response.payload.grav, + resources = response.payload.resources; + + //console.log(grav, resources); + + // grav updatable + if (grav.isUpdatable) { + var icon = ' '; + content = 'Grav v{available} is now available! (Current: v{version}) ', + button = ''; + + content = jQuery.substitute(content, {available: grav.available, version: grav.version}); + $('[data-gpm-grav]').addClass('grav').html('
' + icon + content + button + '
'); + } + + if (resources.total > 0) { + var length, + icon = '', + content = '{updates} of your {type} have an update available', + button = '', + plugins = $('.grav-update.plugins'), + themes = $('.grav-update.themes'); + + // list page + if (plugins[0] && (length = Object.keys(resources.plugins).length)) { + content = jQuery.substitute(content, {updates: length, type: 'plugins'}); + button = jQuery.substitute(button, {Type: 'All Plugins'}); + plugins.html('' + icon + content + button + '
'); + + var plugin, url; + $.each(resources.plugins, function (key, value) { + plugin = $('[data-gpm-plugin="' + key + '"] .gpm-name'); + url = plugin.find('a'); + plugin.append('Update available!'); + + }); + } + + if (themes[0] && (length = Object.keys(resources.themes).length)) { + content = jQuery.substitute(content, {updates: length, type: 'themes'}); + button = jQuery.substitute(button, {Type: 'All Themes'}); + themes.html('' + icon + content + button + '
'); + + var theme, url; + $.each(resources.themes, function (key, value) { + theme = $('[data-gpm-theme="' + key + '"]'); + url = theme.find('.gpm-name a'); + theme.append(''); + }); + } + + // details page + var type = 'plugin', + details = $('.grav-update.plugin')[0]; + + if (!details) { + details = $('.grav-update.theme')[0]; + type = 'theme'; + } + + if (details){ + var slug = $('[data-gpm-' + type + ']').data('gpm-' + type), + Type = type.charAt(0).toUpperCase() + type.substring(1); + + content = 'v{available} of this ' + type + ' is now available!'; + content = jQuery.substitute(content, {available: resources[type + 's'][slug].available}); + button = jQuery.substitute(button, {Type: Type}); + $(details).html('' + icon + content + button + '
'); + } + } + + }, 'json'); }); diff --git a/themes/grav/templates/partials/base.html.twig b/themes/grav/templates/partials/base.html.twig index 49d54883..3625bf19 100644 --- a/themes/grav/templates/partials/base.html.twig +++ b/themes/grav/templates/partials/base.html.twig @@ -49,17 +49,17 @@ - {% if true or grav_update and grav_update['available'] > grav_update['current'] %} -- Grav v{{ grav_update['available'] }} is now available! (Current: v.{{ grav_update['current'] }}) + Grav v{{ admin.gpm.grav.getVersion() }} is now available! (Current: v{{ constant('GRAV_VERSION') }})
- -- - v{{ plugin.version }} of this plugin is now available! - -
+{% set gpm = admin.gpm() %} ++ + v{{ remote.available }} of this plugin is now available! + +
+{% endif %}- - One or more of your plugins has an update available. - -
+ {% if updatable |length %} ++ + {{ updatable |length }} of your plugins have an update available. + +
+ {% endif %}- - v{{ theme.version }} of this theme is now available! - -
+{% set gpm = admin.gpm() %} ++ + v{{ remote.available }} of this theme is now available! + +
+ {% endif %}- - One or more of your themes has an update available. - -
+ {% if updatable |length %} ++ + {{ updatable |length }} of your themes have an update available. + +
+ {% endif %}