diff options
Diffstat (limited to 'core/View')
-rw-r--r-- | core/View/RenderTokenParser.php | 82 | ||||
-rw-r--r-- | core/View/UIControl.php | 92 |
2 files changed, 173 insertions, 1 deletions
diff --git a/core/View/RenderTokenParser.php b/core/View/RenderTokenParser.php new file mode 100644 index 0000000000..14f1eb6806 --- /dev/null +++ b/core/View/RenderTokenParser.php @@ -0,0 +1,82 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\View; + +use Twig_Token; +use Twig_TokenParser; +use Twig_Node_Expression_MethodCall; +use Twig_Node_Expression_Array; +use Twig_Node_Include; +use Exception; + +/** + * Defines a new Twig tag that will render a Piwik View. + * + * Use the tag like this: + * + * {% render theView %} + * + * where `theView` is a variable referencing a View instance. + */ +class RenderTokenParser extends Twig_TokenParser +{ + /** + * Parses the Twig stream and creates a Twig_Node_Include instance that includes + * the View's template. + * + * @return Twig_Node_Include + */ + public function parse(Twig_Token $token) + { + $parser = $this->parser; + $stream = $parser->getStream(); + + $view = $parser->getExpressionParser()->parseExpression(); + + $variablesOverride = null; + if ($stream->nextIf(Twig_Token::NAME_TYPE, 'with')) { + $variablesOverride = $this->parser->getExpressionParser()->parseExpression(); + } + + $stream->expect(Twig_Token::BLOCK_END_TYPE); + + $viewTemplateExpr = new Twig_Node_Expression_MethodCall( + $view, + 'getTemplateFile', + new Twig_Node_Expression_Array(array(), $token->getLine()), + $token->getLine() + ); + + $variablesOverrideExpr = $variablesOverride === null ? array() : array($variablesOverride); + $variablesExpr = new Twig_Node_Expression_MethodCall( + $view, + 'getTemplateVars', + new Twig_Node_Expression_Array($variablesOverrideExpr, $token->getLine()), + $token->getLine() + ); + + return new Twig_Node_Include( + $viewTemplateExpr, + $variablesExpr, + $only = false, + $ignoreMissing = false, + $token->getLine() + ); + } + + /** + * Returns the tag identifier. + * + * @return string + */ + public function getTag() + { + return 'render'; + } +}
\ No newline at end of file diff --git a/core/View/UIControl.php b/core/View/UIControl.php index 90d0b0b331..39994aa828 100644 --- a/core/View/UIControl.php +++ b/core/View/UIControl.php @@ -8,6 +8,9 @@ */ namespace Piwik\View; +use Piwik\View; +use Exception; + /** * Base type of UI controls. * @@ -25,12 +28,99 @@ class UIControl extends \Piwik\View const TEMPLATE = ''; /** + * Holds the array of values that are passed to the UIControl JavaScript class. + * + * @var array + */ + public $clientSideProperties = array(); + + /** + * Holds an array of values that are passed to the UIControl JavaScript class. These values + * differ from those in {@link $clientSideProperties} in that they are meant to passed as + * request parameters when the JavaScript code makes an AJAX request. + * + * @var array + */ + public $clientSideParameters = array(); + + /** + * The CSS class that is used to map the root element of this control with the JavaScript class. + * + * This field must be set prior to rendering. + * + * @var string + */ + public $cssIdentifier = null; + + /** + * The name of the JavaScript class that handles the behavior of this control. + * + * The JavaScript class must exist in the **piwik/UI** JavaScript module (so it will exist in + * `window.piwik.UI`). + * + * This field must be set prior to rendering. + * + * @var string + */ + public $jsClass = null; + + /** + * Extra CSS class(es) for the root element. + * + * @var string + */ + public $cssClass = ""; + + /** + * Whether we are currently rendering the containing div or not. + */ + private $renderingContainer = false; + + /** * Constructor. */ - public function __construct() { + public function __construct() + { parent::__construct(static::TEMPLATE); $this->clientSideProperties = array(); $this->clientSideParameters = array(); } + + /** + * Renders the control view within a containing <div> that is used by the UIControl JavaScript + * class. + * + * @return string + */ + public function render() + { + if ($this->cssIdentifier === null) { + throw new Exception("All UIControls must set a cssIdentifier property"); + } + + if ($this->jsClass === null) { + throw new Exception("All UIControls must set a jsClass property"); + } + + if ($this->renderingContainer) { + return parent::render(); + } else { + $this->renderingContainer = true; + + $surroundingDivView = new View("@CoreHome\_uiControl"); + $surroundingDivView->clientSideProperties = $this->clientSideProperties; + $surroundingDivView->clientSideParameters = $this->clientSideParameters; + $surroundingDivView->implView = $this; + $surroundingDivView->cssIdentifier = $this->cssIdentifier; + $surroundingDivView->cssClass = $this->cssClass; + $surroundingDivView->jsClass = $this->jsClass; + + $result = $surroundingDivView->render(); + + $this->renderingContainer = false; + + return $result; + } + } }
\ No newline at end of file |