Added AdminUtilities Smarty plugin, optimized templates

This commit is contained in:
franck
2013-09-03 15:43:05 +02:00
parent 69f88db090
commit fce528c41f
17 changed files with 452 additions and 177 deletions

View File

@@ -34,6 +34,8 @@ use Thelia\Model\LangQuery;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
use Thelia\Model\ConfigQuery;
use Thelia\Type\BooleanOrBothType;
use Thelia\Type\TypeCollection;
use Thelia\Type\EnumListType;
/**
* Config loop, to access configuration variables
@@ -59,7 +61,21 @@ class Config extends BaseI18nLoop
Argument::createIntListTypeArgument('exclude'),
Argument::createAnyTypeArgument('variable'),
Argument::createBooleanOrBothTypeArgument('hidden'),
Argument::createBooleanOrBothTypeArgument('secured')
Argument::createBooleanOrBothTypeArgument('secured'),
new Argument(
'order',
new TypeCollection(
new EnumListType(
array(
'id', 'id_reverse',
'name', 'name_reverse',
'title', 'title_reverse',
'value', 'value_reverse',
)
)
),
'name'
)
);
}
@@ -94,7 +110,39 @@ class Config extends BaseI18nLoop
if (! is_null($secured) && $secured != BooleanOrBothType::ANY)
$search->filterBySecured($secured ? 1 : 0);
$search->orderByName(Criteria::ASC);
$orders = $this->getOrder();
foreach($orders as $order) {
switch ($order) {
case 'id':
$search->orderById(Criteria::ASC);
break;
case 'id_reverse':
$search->orderById(Criteria::DESC);
break;
case 'name':
$search->orderByName(Criteria::ASC);
break;
case 'name_reverse':
$search->orderByName(Criteria::DESC);
break;
case 'title':
$search->addAscendingOrderByColumn('i18n_TITLE');
break;
case 'title_reverse':
$search->addDescendingOrderByColumn('i18n_TITLE');
break;
case 'value':
$search->orderByValue(Criteria::ASC);
break;
case 'value_reverse':
$search->orderByValue(Criteria::DESC);
break;
}
}
$results = $this->search($search, $pagination);

View File

@@ -66,8 +66,9 @@ class Currency extends BaseI18nLoop
'code', 'code_reverse',
'symbol', 'symbol_reverse',
'rate', 'rate_reverse',
'is_default', 'is_default_reverse',
'manual', 'manual_reverse')
)
)
),
'manual'
)
@@ -143,6 +144,13 @@ class Currency extends BaseI18nLoop
$search->orderByRate(Criteria::DESC);
break;
case 'is_default':
$search->orderByByDefault(Criteria::ASC);
break;
case 'is_default_reverse':
$search->orderByByDefault(Criteria::DESC);
break;
case 'manual':
$search->orderByPosition(Criteria::ASC);
break;

View File

@@ -0,0 +1,144 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Template\Smarty\Plugins;
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
use Thelia\Core\Template\Smarty\AbstractSmartyPlugin;
use Thelia\Tools\URL;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Security\SecurityContext;
/**
* This class implements variour admin template utilities
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
class AdminUtilities extends AbstractSmartyPlugin
{
private $securityContext;
public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;
}
public function generatePositionChangeBlock($params, &$smarty) {
// The required permissions
$permission = $this->getParam($params, 'permission');
// The base position change path
$path = $this->getParam($params, 'path');
// The URL parameter the object ID is assigned
$url_parameter = $this->getParam($params, 'url_parameter');
// The current object position
$position = $this->getParam($params, 'position');
// The object ID
$id = $this->getParam($params, 'id');
// The in place dition class
$in_place_edit_class = $this->getParam($params, 'in_place_edit_class');
/*
<a href="{url path='/admin/configuration/currencies/positionUp' currency_id=$ID}"><i class="icon-arrow-up"></i></a>
<span class="currencyPositionChange" data-id="{$ID}">{$POSITION}</span>
<a href="{url path='/admin/configuration/currencies/positionDown' currency_id=$ID}"><i class="icon-arrow-down"></i></a>
*/
if ($permissions == null || $this->securityContext->isGranted("ADMIN", array($permission))) {
return sprintf(
'<a href="%s"><i class="icon-arrow-up"></i></a><span class="%s" data-id="%s">%s</span><a href="%s"><i class="icon-arrow-down"></i></a>',
URL::absoluteUrl("$path/positionUp", array($url_parameter => $id)),
$in_place_edit_class,
$id,
$position,
URL::absoluteUrl("$path/positionDown", array($url_parameter => $id))
);
}
else {
return $position;
}
}
/**
* Generates the link of a sortable column header
*
* @param array $params
* @param unknown $smarty
* @return string no text is returned.
*/
public function generateSortableColumnHeader($params, &$smarty)
{
// The current order of the table
$current_order = $this->getParam($params, 'current_order');
// The column ascending order
$order = $this->getParam($params, 'order');
// The column descending order label
$reverse_order = $this->getParam($params, 'reverse_order');
// The order change path
$path = $this->getParam($params, 'path');
// The column label
$label = $this->getParam($params, 'label');
if ($current_order == $order) {
$icon = 'up';
$order_change = $reverse_order;
}
else if ($current_order == $reverse_order) {
$icon = 'down';
$order_change = $order;
}
else {
$order_change = $order;
}
if (! empty($icon))
$output = sprintf('<i class="icon icon-chevron-%s"></i> ', $icon);
else
$output = '';
return sprintf('%s<a href="%s">%s</a>', $output, URL::absoluteUrl($path, array('order' => $order_change)), $label);
}
/**
* Define the various smarty plugins handled by this class
*
* @return an array of smarty plugin descriptors
*/
public function getPluginDescriptors()
{
return array(
new SmartyPluginDescriptor('function', 'admin_sortable_header', $this, 'generateSortableColumnHeader'),
new SmartyPluginDescriptor('function', 'admin_position_block' , $this, 'generatePositionChangeBlock'),
);
}
}