2013-06-09 22:09:00 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
2014-05-27 20:18:06 +02:00
|
|
|
* This file is part of the phpBB Forum Software package.
|
|
|
|
*
|
|
|
|
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0)
|
|
|
|
*
|
|
|
|
* For full copyright and license information, please see
|
|
|
|
* the docs/CREDITS.txt file.
|
2013-06-09 22:09:00 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
namespace phpbb\template\twig;
|
|
|
|
|
|
|
|
class extension extends \Twig_Extension
|
2013-06-09 22:09:00 -05:00
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
/** @var \phpbb\template\context */
|
2013-07-02 12:17:56 -05:00
|
|
|
protected $context;
|
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
/** @var \phpbb\user */
|
2013-07-01 09:32:21 -05:00
|
|
|
protected $user;
|
|
|
|
|
2013-07-05 09:56:25 -05:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2013-09-10 14:01:09 +02:00
|
|
|
* @param \phpbb\template\context $context
|
|
|
|
* @param \phpbb\user $user
|
|
|
|
* @return \phpbb\template\twig\extension
|
2013-07-05 09:56:25 -05:00
|
|
|
*/
|
2013-09-10 14:01:09 +02:00
|
|
|
public function __construct(\phpbb\template\context $context, $user)
|
2013-07-01 09:32:21 -05:00
|
|
|
{
|
2013-07-02 12:17:56 -05:00
|
|
|
$this->context = $context;
|
2013-07-01 09:32:21 -05:00
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:56:25 -05:00
|
|
|
/**
|
|
|
|
* Get the name of this extension
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-09 22:09:00 -05:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'phpbb';
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:37:29 +01:00
|
|
|
/**
|
|
|
|
* Returns the token parser instance to add to the existing list.
|
|
|
|
*
|
|
|
|
* @return array An array of Twig_TokenParser instances
|
|
|
|
*/
|
2013-06-09 22:09:00 -05:00
|
|
|
public function getTokenParsers()
|
|
|
|
{
|
|
|
|
return array(
|
2013-09-10 14:01:09 +02:00
|
|
|
new \phpbb\template\twig\tokenparser\defineparser,
|
|
|
|
new \phpbb\template\twig\tokenparser\includeparser,
|
|
|
|
new \phpbb\template\twig\tokenparser\includejs,
|
|
|
|
new \phpbb\template\twig\tokenparser\includecss,
|
|
|
|
new \phpbb\template\twig\tokenparser\event,
|
|
|
|
new \phpbb\template\twig\tokenparser\includephp,
|
|
|
|
new \phpbb\template\twig\tokenparser\php,
|
2013-06-09 22:09:00 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:37:29 +01:00
|
|
|
/**
|
|
|
|
* Returns a list of filters to add to the existing list.
|
|
|
|
*
|
|
|
|
* @return array An array of filters
|
|
|
|
*/
|
|
|
|
public function getFilters()
|
|
|
|
{
|
2013-07-04 10:22:12 -05:00
|
|
|
return array(
|
2013-09-10 14:01:09 +02:00
|
|
|
new \Twig_SimpleFilter('subset', array($this, 'loop_subset'), array('needs_environment' => true)),
|
|
|
|
new \Twig_SimpleFilter('addslashes', 'addslashes'),
|
2013-06-28 15:40:30 -05:00
|
|
|
);
|
2013-10-30 13:37:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of global functions to add to the existing list.
|
|
|
|
*
|
|
|
|
* @return array An array of global functions
|
|
|
|
*/
|
|
|
|
public function getFunctions()
|
|
|
|
{
|
2013-07-04 10:22:12 -05:00
|
|
|
return array(
|
2013-09-10 14:01:09 +02:00
|
|
|
new \Twig_SimpleFunction('lang', array($this, 'lang')),
|
2013-07-01 09:32:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:37:29 +01:00
|
|
|
/**
|
|
|
|
* Returns a list of operators to add to the existing list.
|
|
|
|
*
|
|
|
|
* @return array An array of operators
|
|
|
|
*/
|
2013-06-09 22:09:00 -05:00
|
|
|
public function getOperators()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
2013-07-04 10:22:12 -05:00
|
|
|
'!' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'),
|
2013-07-01 09:18:58 -05:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
// precedence settings are copied from similar operators in Twig core extension
|
2013-09-10 14:01:09 +02:00
|
|
|
'||' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'&&' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
2013-07-01 09:18:58 -05:00
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
'eq' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Equal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
2013-06-12 12:48:37 -05:00
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
'ne' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'neq' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'<>' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
2013-06-12 12:48:37 -05:00
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
'===' => array('precedence' => 20, 'class' => '\phpbb\template\twig\node\expression\binary\equalequal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'!==' => array('precedence' => 20, 'class' => '\phpbb\template\twig\node\expression\binary\notequalequal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
2013-06-12 12:48:37 -05:00
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
'gt' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Greater', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'gte' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'ge' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'lt' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Less', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'lte' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
|
|
|
'le' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
2013-06-14 09:41:02 -05:00
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
'mod' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mod', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
|
2013-07-04 10:22:12 -05:00
|
|
|
),
|
|
|
|
);
|
2013-10-30 13:37:29 +01:00
|
|
|
}
|
2013-06-28 15:40:30 -05:00
|
|
|
|
|
|
|
/**
|
2013-10-30 13:37:29 +01:00
|
|
|
* Grabs a subset of a loop
|
|
|
|
*
|
2014-06-15 15:05:51 +02:00
|
|
|
* @param \Twig_Environment $env A Twig_Environment instance
|
2013-10-30 13:37:29 +01:00
|
|
|
* @param mixed $item A variable
|
|
|
|
* @param integer $start Start of the subset
|
|
|
|
* @param integer $end End of the subset
|
|
|
|
* @param Boolean $preserveKeys Whether to preserve key or not (when the input is an array)
|
|
|
|
*
|
|
|
|
* @return mixed The sliced variable
|
|
|
|
*/
|
2013-09-10 14:01:09 +02:00
|
|
|
function loop_subset(\Twig_Environment $env, $item, $start, $end = null, $preserveKeys = false)
|
2013-06-28 15:40:30 -05:00
|
|
|
{
|
2013-07-05 10:05:20 -05:00
|
|
|
// We do almost the same thing as Twig's slice (array_slice), except when $end is positive
|
2013-06-28 15:40:30 -05:00
|
|
|
if ($end >= 1)
|
|
|
|
{
|
|
|
|
// When end is > 1, subset will end on the last item in an array with the specified $end
|
|
|
|
// This is different from slice in that it is the number we end on rather than the number
|
2013-07-05 10:03:48 -05:00
|
|
|
// of items to grab (length)
|
2013-06-28 15:40:30 -05:00
|
|
|
|
|
|
|
// Start must always be the actual starting number for this calculation (not negative)
|
|
|
|
$start = ($start < 0) ? sizeof($item) + $start : $start;
|
|
|
|
$end = $end - $start;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always include the last element (this was the past design)
|
|
|
|
$end = ($end == -1 || $end === null) ? null : $end + 1;
|
|
|
|
|
|
|
|
return twig_slice($env, $item, $start, $end, $preserveKeys);
|
|
|
|
}
|
2013-07-02 12:17:56 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get output for a language variable (L_FOO, LA_FOO)
|
|
|
|
*
|
|
|
|
* This function checks to see if the language var was outputted to $context
|
|
|
|
* (e.g. in the ACP, L_TITLE)
|
|
|
|
* If not, we return the result of $user->lang()
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function lang()
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
$key = $args[0];
|
|
|
|
|
|
|
|
$context = $this->context->get_data_ref();
|
|
|
|
$context_vars = $context['.'][0];
|
|
|
|
|
|
|
|
if (isset($context_vars['L_' . $key]))
|
|
|
|
{
|
|
|
|
return $context_vars['L_' . $key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// LA_ is transformed into lang(\'$1\')|addslashes, so we should not
|
|
|
|
// need to check for it
|
|
|
|
|
|
|
|
return call_user_func_array(array($this->user, 'lang'), $args);
|
|
|
|
}
|
2013-06-09 22:09:00 -05:00
|
|
|
}
|