Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Aug 24, 2015
2 parents 7df2f10 + 0badc53 commit 3da8844
Show file tree
Hide file tree
Showing 65 changed files with 2,313 additions and 456 deletions.
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
# v0.4.0
## 08/24/2015

1. [](#new)
* Multi-language Page support!!!
* Admin languages configurable per user
* Toastr messages for `check updates`
* new `tu` filter for admin translations
* Italian and German admin translations
* Added a save location in system and site configuration
* Page metadata now uses flexible array field
1. [](#improved)
* Allow subpages of modular pages to display in pages list
* Open external pages in new tabs
* Reworked `visibility` of pages
* Use `PLUGIN_ADMIN` prefix for translations
* Added link to gravatar.com to avoid confusion on avatar
* Limit page count to 200 in ordering field
* Fixed various Safari _flex_ issues
* Use `rawRoute()` for page links
* Minor `param separator` fixes
* Various CSS fixes
* Improved CodeMirror to force spaces
* Added **Selectize** dropdowns to various forms and modals
1. [](#bugfix)
* Fix for `Call to a member function path() on non-object` error
* Fixed dropdown z-index issues
* Correctly set the filename including language if set
* Fix for empty taxonomies on page save
* Fix for page not redirecting properly on folder change
* Fix for table headers styling
* Added missing translation strings
* Unique page counting in total page counts
* Fixed JS warning with page filtering and deleting


# v0.3.0
## 08/11/2015

Expand Down
41 changes: 39 additions & 2 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,12 @@ public function onTwigSiteVariables()

$twig->twig_vars['location'] = $this->template;
$twig->twig_vars['base_url_relative_frontend'] = $twig->twig_vars['base_url_relative'];
$twig->twig_vars['admin_route'] = trim($this->config->get('plugins.admin.route'), '/');
$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'] != '/' ? '/' : '') . $twig->twig_vars['admin_route'];
$twig->twig_vars['theme_url'] = '/user/plugins/admin/themes/' . $this->theme;
$twig->twig_vars['base_url'] = $twig->twig_vars['base_url_relative'];
$twig->twig_vars['base_path'] = GRAV_ROOT;
$twig->twig_vars['admin'] = $this->admin;

switch ($this->template) {
Expand Down Expand Up @@ -291,6 +292,7 @@ public function onTaskGPM()
protected function initializeAdmin()
{
$this->enable([
'onTwigExtensions' => ['onTwigExtensions', 1000],
'onPagesInitialized' => ['onPagesInitialized', 1000],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000],
'onTwigSiteVariables' => ['onTwigSiteVariables', 1000],
Expand Down Expand Up @@ -334,5 +336,40 @@ protected function initializeAdmin()

// Get theme for admin
$this->theme = $this->config->get('plugins.admin.theme', 'grav');

$assets = $this->grav['assets'];
$translations = 'if (!window.translations) window.translations = {}; ' . PHP_EOL . 'window.translations.PLUGIN_ADMIN = {};' . PHP_EOL;

$strings = ['EVERYTHING_UP_TO_DATE',
'UPDATES_ARE_AVAILABLE',
'IS_AVAILABLE_FOR_UPDATE',
'AND',
'IS_NOW_AVAILABLE',
'CURRENT',
'UPDATE_GRAV_NOW',
'TASK_COMPLETED',
'UPDATE',
'UPDATING_PLEASE_WAIT',
'GRAV_SYMBOLICALLY_LINKED',
'OF_YOUR',
'OF_THIS',
'HAVE_AN_UPDATE_AVAILABLE',
'UPDATE_AVAILABLE',
'DAYS'];

foreach($strings as $string) {
$translations .= 'translations.PLUGIN_ADMIN.' . $string .' = "' . $this->admin->translate('PLUGIN_ADMIN.' . $string) . '"; ' . PHP_EOL;;
}

$assets->addInlineJs($translations);
}

/**
* Add Twig Extensions
*/
public function onTwigExtensions()
{
require_once(__DIR__.'/twig/AdminTwigExtension.php');
$this->grav['twig']->twig->addExtension(new AdminTwigExtension());
}
}
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Admin Panel
version: 0.3.0
version: 0.4.0
description: Adds an advanced administration panel to manage your site
icon: empire
author:
Expand Down
166 changes: 142 additions & 24 deletions classes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\GPM\GPM;
use Grav\Common\Grav;
use Grav\Common\Language\LanguageCodes;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Plugins;
Expand Down Expand Up @@ -68,6 +69,11 @@ class Admin
*/
public $user;

/**
* @var Lang
*/
protected $lang;

/**
* @var Grav\Common\GPM\GPM
*/
Expand All @@ -87,10 +93,27 @@ public function __construct(Grav $grav, $base, $location, $route)
$this->base = $base;
$this->location = $location;
$this->route = $route;

$this->uri = $this->grav['uri'];
$this->session = $this->grav['session'];
$this->user = $this->grav['user'];
$language = $this->grav['language'];

if ($language->enabled()) {
$this->multilang = true;
$this->languages_enabled = $this->grav['config']->get('system.languages.supported', []);

//Set the currently active language for the admin
$language = $this->grav['uri']->param('lang');
if (!$language) {
if (!$this->session->admin_lang) $this->session->admin_lang = 'en';
$language = $this->session->admin_lang;
}
$this->grav['language']->setActive($language ?: 'en');
} else {
$this->grav['language']->setActive('en');
$this->multilang = false;
}

}

/**
Expand Down Expand Up @@ -142,6 +165,12 @@ public function authenticate($form)
{
if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) {
$user = User::load($form['username']);

//default to english if language not set
if (empty($user->language)) {
$user->set('language', 'en');
}

if ($user->exists()) {
$user->authenticated = true;

Expand All @@ -154,11 +183,8 @@ public function authenticate($form)
/** @var Grav $grav */
$grav = $this->grav;

$l = $this->grav['language'];

$this->setMessage($l->translate('LOGIN_LOGGED_IN'), 'info');
$this->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN', [$this->user->language]), 'info');

// $redirect_route =$this->getLoginRedirect() ?: $this->uri->route();
$redirect_route = $this->uri->route();
$grav->redirect($redirect_route);
}
Expand Down Expand Up @@ -220,7 +246,7 @@ public function page($route = false)
public function blueprints($type)
{
if ($this->blueprints === null) {
$this->blueprints = new Data\Blueprints($this->grav['locator']->findResource('blueprints://'));
$this->blueprints = new Data\Blueprints('blueprints://');
}

return $this->blueprints->get($type);
Expand Down Expand Up @@ -344,12 +370,17 @@ public function field($name)
*
* @return array
*/
public function routes()
public function routes($unique = false)
{
/** @var Pages $pages */
$pages = $this->grav['pages'];

return $pages->routes();
if ($unique) {
$routes = array_unique($pages->routes());
} else {
$routes = $pages->routes();
}
return $routes;
}

/**
Expand Down Expand Up @@ -514,7 +545,7 @@ protected function getPage($path)
return null;
}

$ppath = dirname($path);
$ppath = str_replace('\\', '/' , dirname($path));

// Find or create parent(s).
$parent = $this->getPage($ppath != '/' ? $ppath : '');
Expand All @@ -534,9 +565,23 @@ protected function getPage($path)
if (isset($this->session->{$page->route()})) {
// Found the type and header from the session.
$data = $this->session->{$page->route()};
$visible = isset($data['visible']) && $data['visible'] != '' ? (bool)$data['visible'] : $this->guessVisibility($page);

$header = ['title' => $data['title'], 'visible' => $visible];
$header = ['title' => $data['title']];

if (isset($data['visible'])) {
if ($data['visible'] == '' || $data['visible']) {
// if auto (ie '')
$children = $page->parent()->children();
foreach ($children as $child) {
if ($child->order()) {
// set page order
$page->order(1000);
break;
}
}
}

}

if ($data['type'] == 'modular') {
$header['body_classes'] = 'modular';
Expand All @@ -545,7 +590,7 @@ protected function getPage($path)
$name = $page->modular() ? str_replace('modular/', '', $data['type']) : $data['type'];
$page->name($name . '.md');
$page->header($header);
$page->frontmatter(Yaml::dump((array)$page->header()));
$page->frontmatter(Yaml::dump((array)$page->header(), 10, 2, false));
} else {
// Find out the type by looking at the parent.
$type = $parent->childType() ? $parent->childType() : $parent->blueprints()->get('child_type',
Expand All @@ -560,22 +605,34 @@ protected function getPage($path)
}

/**
* Guess the intended visibility status based on other sibling folders
* Return the languages available in the admin
*
* @param \Grav\Common\Page\Page $page
*
* @return bool
* @return array
*/
public function guessVisibility(Page $page)
public static function adminLanguages()
{
$children = $page->parent()->children();
foreach ($children as $child) {
if ($child->order()) {
return true;
}
$languages = [];
$lang_data = Yaml::parse(file_get_contents(__DIR__ . '/../languages.yaml'));
foreach ($lang_data as $lang => $values) {
$languages[$lang] = LanguageCodes::getNativeName($lang);
}
return $languages;
}

return false;
/**
* Return the languages available in the site
*
* @return array
*/
public static function siteLanguages()
{
$languages = [];
$lang_data = Grav::instance()['config']->get('system.languages.supported', []);

foreach ($lang_data as $index => $lang) {
$languages[$lang] = LanguageCodes::getNativeName($lang);
}
return $languages;
}

/**
Expand All @@ -585,7 +642,14 @@ public function guessVisibility(Page $page)
*/
public static function route()
{
return dirname('/' . Grav::instance()['admin']->route);
$pages = Grav::instance()['pages'];
$route = '/' . ltrim(Grav::instance()['admin']->route, '/');

$page = $pages->dispatch($route);
$parent = $page->parent();
$parent_route = $parent->rawRoute();

return $parent_route;
}

/**
Expand All @@ -604,6 +668,11 @@ public function isTeamGrav($info)
}
}

/**
* Renders phpinfo
*
* @return string The phpinfo() output
*/
function phpinfo() {
ob_start();
phpinfo();
Expand All @@ -613,4 +682,53 @@ function phpinfo() {
$pinfo = preg_replace( '%^.*<body>(.*)</body>.*$%ms','$1',$pinfo);
return $pinfo;
}

/**
* Translate a string to the user-defined language
*
* @param $string the string to translate
*/
public function translate($string) {
return $this->_translate($string, [$this->grav['user']->authenticated ? $this->grav['user']->language : 'en']);
}

public function _translate($args, Array $languages = null, $array_support = false, $html_out = false)
{
if (is_array($args)) {
$lookup = array_shift($args);
} else {
$lookup = $args;
$args = [];
}

if ($lookup) {
if (empty($languages) || reset($languages) == null) {
if ($this->grav['config']->get('system.languages.translations_fallback', true)) {
$languages = $this->grav['language']->getFallbackLanguages();
} else {
$languages = (array)$this->grav['language']->getDefault();
}
}
} else {
$languages = ['en'];
}

foreach ((array)$languages as $lang) {
$translation = $this->grav['language']->getTranslation($lang, $lookup, $array_support);

if (!$translation) {
$translation = $this->grav['language']->getTranslation($this->grav['language']->getDefault(), $lookup, $array_support);
}

if ($translation) {
if (count($args) >= 1) {
return vsprintf($translation, $args);
} else {
return $translation;
}
}
}

return $lookup;
}
}
Loading

0 comments on commit 3da8844

Please # to comment.