Real data integration of GPM into admin

This commit is contained in:
Djamil Legato
2014-10-07 17:01:40 -07:00
parent 4ee61d45c3
commit 118b3cae7f
7 changed files with 190 additions and 47 deletions

View File

@@ -1,10 +1,11 @@
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\GPM\GPM;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use RocketTheme\Toolbox\File\File;
use RocketTheme\Toolbox\Session\Session;
@@ -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],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000],
'onTwigSiteVariables' => ['onTwigSiteVariables', 1000]
'onTwigSiteVariables' => ['onTwigSiteVariables', 1000],
'onTask.GPM' => ['onTaskGPM', 0]
]);
// Change login behavior.

View File

@@ -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
@@ -8,11 +13,93 @@ $(function()
$('input.fancy').selectize({
delimiter: ',',
persist: false,
create: function(input) {
create: function (input) {
return {
value: 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 = '<i class="fa fa-bullhorn"></i> ';
content = 'Grav <b>v{available}</b> is now available! <span class="less">(Current: v{version})</span> ',
button = '<button class="button button-small secondary" data-gpm-update="grav">Update Grav Now</button>';
content = jQuery.substitute(content, {available: grav.available, version: grav.version});
$('[data-gpm-grav]').addClass('grav').html('<p>' + icon + content + button + '</p>');
}
if (resources.total > 0) {
var length,
icon = '<i class="fa fa-bullhorn"></i>',
content = '{updates} of your {type} have an <strong>update available</strong>',
button = '<button class="button button-small secondary">Update {Type}</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('<p>' + icon + content + button + '</p>');
var plugin, url;
$.each(resources.plugins, function (key, value) {
plugin = $('[data-gpm-plugin="' + key + '"] .gpm-name');
url = plugin.find('a');
plugin.append('<a href="' + url.attr('href') + '"><span class="badge update">Update available!</span></a>');
});
}
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('<p>' + icon + content + button + '</p>');
var theme, url;
$.each(resources.themes, function (key, value) {
theme = $('[data-gpm-theme="' + key + '"]');
url = theme.find('.gpm-name a');
theme.append('<div class="gpm-ribbon"><a href="' + url.attr('href') + '">UPDATE</a></div>');
});
}
// 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 = '<strong>v{available}</strong> of this ' + type + ' is now available!';
content = jQuery.substitute(content, {available: resources[type + 's'][slug].available});
button = jQuery.substitute(button, {Type: Type});
$(details).html('<p>' + icon + content + button + '</p>');
}
}
}, 'json');
});

View File

@@ -49,17 +49,17 @@
<div class="titlebar secondary-accent">
{% block titlebar %}{% endblock %}
</div>
{% if true or grav_update and grav_update['available'] > grav_update['current'] %}
<div class="grav-update">
<div class="grav-update" data-gpm-grav>
{% if admin.gpm.grav.isUpdatable() %}
<p>
<i class="fa fa-bullhorn"></i>
Grav <b>v{{ grav_update['available'] }}</b> is now available! <span class="less">(Current: v.{{ grav_update['current'] }})</span>
Grav <b>v{{ admin.gpm.grav.getVersion() }}</b> is now available! <span class="less">(Current: v{{ constant('GRAV_VERSION') }})</span>
<button class="button button-small secondary">Update Grav Now</button>
</p>
</div>
{% endif %}
</div>
<div class="content-padding">
<div>
{% include 'partials/messages.html.twig' %}

View File

@@ -1,9 +1,13 @@
<div class="grav-update plugins">
{% set gpm = admin.gpm() %}
<div class="grav-update plugin" data-gpm-plugin="{{ admin.route }}">
{% if gpm.isPluginUpdatable(admin.route) %}
{% set remote = gpm.getRepositoryPlugin(admin.route) %}
<p>
<i class="fa fa-bullhorn"></i>
<strong>v{{ plugin.version }}</strong> of this plugin is now available!
<button class="button button-small secondary">Update Plugin</button>
<strong>v{{ remote.available }}</strong> of this plugin is now available!
<button class="button button-small secondary" data-download="{{ remote.zipball_url }}">Update Plugin</button>
</p>
{% endif %}
</div>
<h1>
@@ -64,6 +68,7 @@
<td>{{ plugin.license }}</td>
</tr>
{% endif %}
{% if plugin.description %}
<tr>
<td>Description:</td>

View File

@@ -1,9 +1,13 @@
{% set gpm = admin.gpm() %}
{% set updatable = gpm.getUpdatablePlugins() %}
<div class="grav-update plugins">
{% if updatable |length %}
<p>
<i class="fa fa-bullhorn"></i>
One or more of your plugins has an <strong>update available</strong>.
{{ updatable |length }} of your plugins have an <strong>update available</strong>.
<button class="button button-small secondary">Update All Plugins</button>
</p>
{% endif %}
</div>
<h1>
@@ -14,11 +18,11 @@
{% for slug, package in admin.plugins %}
{% set plugin = package.toArray() %}
<tr>
<tr data-gpm-plugin="{{ slug|url_encode }}">
<td class="gpm-name">
<i class="fa fa-fw fa-{{ plugin.icon }}"></i>
<a href="{{ base_url_relative }}/plugins/{{ slug|url_encode }}">{{ plugin.name }}</a> <span class="gpm-version">v{{ plugin.version }}</span>
{% if plugin.update or slug == 'archives' %}
{% if gpm.isPluginUpdatable(slug) %}
<a href="{{ base_url_relative }}/plugins/{{ slug|url_encode }}"><span class="badge update">Update available!</span></a>
{% endif %}
</td>

View File

@@ -1,9 +1,13 @@
<div class="grav-update plugins">
{% set gpm = admin.gpm() %}
<div class="grav-update theme" data-gpm-theme="{{ admin.route }}">
{% if gpm.isThemeUpdatable(admin.route) %}
{% set remote = gpm.getRepositoryTheme(admin.route) %}
<p>
<i class="fa fa-bullhorn"></i>
<strong>v{{ theme.version }}</strong> of this theme is now available!
<button class="button button-small secondary">Update Theme</button>
<strong>v{{ remote.available }}</strong> of this theme is now available!
<button class="button button-small secondary" data-download="{{ remote.zipball_url }}">Update Theme</button>
</p>
{% endif %}
</div>
<h1>

View File

@@ -1,9 +1,13 @@
{% set gpm = admin.gpm() %}
{% set updatable = gpm.getUpdatableThemes() %}
<div class="grav-update themes">
{% if updatable |length %}
<p>
<i class="fa fa-bullhorn"></i>
One or more of your themes has an <strong>update available</strong>.
{{ updatable |length }} of your themes have an <strong>update available</strong>.
<button class="button button-small secondary">Update All Themes</button>
</p>
{% endif %}
</div>
<h1>
@@ -13,7 +17,7 @@
<div class="themes card-row grid fixed-blocks pure-g">
{% for slug, package in admin.themes %}
{% set theme = package.toArray() %}
<div class="theme card-item pure-u-1-3">
<div class="theme card-item pure-u-1-3" data-gpm-theme="{{ slug|url_encode }}">
<div class="gpm-name">
<i class="fa fa-fw fa-{{ theme.icon }}"></i>
<a href="{{ base_url_relative }}/themes/{{ slug|url_encode }}">{{ theme.name }}</a> <span class="gpm-version">v{{ theme.version }}</span>
@@ -30,7 +34,7 @@
<a href="{{ base_url_relative }}/themes/{{ slug }}/task:activate"><i class="fa fa-fw fa-toggle-off"></i></a>
</div>
{% endif %}
{% if config.get('system.pages.theme') == slug %}
{% if gpm.isThemeUpdatable(slug) %}
<div class="gpm-ribbon">
<a href="{{ base_url_relative }}/themes/{{ slug|url_encode }}">UPDATE</a>
</div>