From 118b3cae7f45433fc09bb01ec95f7eb920482684 Mon Sep 17 00:00:00 2001 From: Djamil Legato Date: Tue, 7 Oct 2014 17:01:40 -0700 Subject: [PATCH] Real data integration of GPM into admin --- admin.php | 61 +++++++++--- themes/grav/js/admin-all.js | 97 ++++++++++++++++++- themes/grav/templates/partials/base.html.twig | 10 +- .../partials/plugins-details.html.twig | 17 ++-- .../templates/partials/plugins-list.html.twig | 18 ++-- .../partials/themes-details.html.twig | 16 +-- .../templates/partials/themes-list.html.twig | 18 ++-- 7 files changed, 190 insertions(+), 47 deletions(-) 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('
UPDATE
'); + }); + } + + // 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 @@
{% block titlebar %}{% endblock %}
- {% if true or grav_update and grav_update['available'] > grav_update['current'] %} -
+
+ {% if admin.gpm.grav.isUpdatable() %}

- 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') }})

- -
{% endif %} +
+
{% include 'partials/messages.html.twig' %} diff --git a/themes/grav/templates/partials/plugins-details.html.twig b/themes/grav/templates/partials/plugins-details.html.twig index 429f22dd..2a185f2d 100644 --- a/themes/grav/templates/partials/plugins-details.html.twig +++ b/themes/grav/templates/partials/plugins-details.html.twig @@ -1,9 +1,13 @@ -
-

- - v{{ plugin.version }} of this plugin is now available! - -

+{% set gpm = admin.gpm() %} +
+{% if gpm.isPluginUpdatable(admin.route) %} + {% set remote = gpm.getRepositoryPlugin(admin.route) %} +

+ + v{{ remote.available }} of this plugin is now available! + +

+{% endif %}

@@ -64,6 +68,7 @@ {{ plugin.license }} {% endif %} + {% if plugin.description %} Description: diff --git a/themes/grav/templates/partials/plugins-list.html.twig b/themes/grav/templates/partials/plugins-list.html.twig index efaf9373..e151890c 100644 --- a/themes/grav/templates/partials/plugins-list.html.twig +++ b/themes/grav/templates/partials/plugins-list.html.twig @@ -1,9 +1,13 @@ +{% set gpm = admin.gpm() %} +{% set updatable = gpm.getUpdatablePlugins() %}
-

- - One or more of your plugins has an update available. - -

+ {% if updatable |length %} +

+ + {{ updatable |length }} of your plugins have an update available. + +

+ {% endif %}

@@ -14,11 +18,11 @@ {% for slug, package in admin.plugins %} {% set plugin = package.toArray() %} - + {{ plugin.name }} v{{ plugin.version }} - {% if plugin.update or slug == 'archives' %} + {% if gpm.isPluginUpdatable(slug) %} Update available! {% endif %} diff --git a/themes/grav/templates/partials/themes-details.html.twig b/themes/grav/templates/partials/themes-details.html.twig index 7279722f..921f39ee 100644 --- a/themes/grav/templates/partials/themes-details.html.twig +++ b/themes/grav/templates/partials/themes-details.html.twig @@ -1,9 +1,13 @@ -
-

- - v{{ theme.version }} of this theme is now available! - -

+{% set gpm = admin.gpm() %} +
+ {% if gpm.isThemeUpdatable(admin.route) %} + {% set remote = gpm.getRepositoryTheme(admin.route) %} +

+ + v{{ remote.available }} of this theme is now available! + +

+ {% endif %}

diff --git a/themes/grav/templates/partials/themes-list.html.twig b/themes/grav/templates/partials/themes-list.html.twig index bfabff21..dc74131f 100644 --- a/themes/grav/templates/partials/themes-list.html.twig +++ b/themes/grav/templates/partials/themes-list.html.twig @@ -1,9 +1,13 @@ +{% set gpm = admin.gpm() %} +{% set updatable = gpm.getUpdatableThemes() %}
-

- - One or more of your themes has an update available. - -

+ {% if updatable |length %} +

+ + {{ updatable |length }} of your themes have an update available. + +

+ {% endif %}

@@ -13,7 +17,7 @@
{% for slug, package in admin.themes %} {% set theme = package.toArray() %} -
+
{{ theme.name }} v{{ theme.version }} @@ -30,7 +34,7 @@
{% endif %} - {% if config.get('system.pages.theme') == slug %} + {% if gpm.isThemeUpdatable(slug) %}