HtmlEmailFooterView.php
5 years ago
HtmlReportEmailHeaderView.php
5 years ago
MethodCallExpression.php
5 years ago
OneClickDone.php
5 years ago
RenderTokenParser.php
5 years ago
UIControl.php
5 years ago
ViewInterface.php
5 years ago
UIControl.php
180 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\View; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\View; |
| 13 | |
| 14 | /** |
| 15 | * Base type of UI controls. |
| 16 | * |
| 17 | * The JavaScript companion class can be found in plugins/CoreHome/javascripts/uiControl.js. |
| 18 | * |
| 19 | * @api |
| 20 | */ |
| 21 | class UIControl extends \Piwik\View |
| 22 | { |
| 23 | /** |
| 24 | * The Twig template file that generates the control's HTML. |
| 25 | * |
| 26 | * Derived classes must set this constant. |
| 27 | */ |
| 28 | const TEMPLATE = ''; |
| 29 | |
| 30 | /** |
| 31 | * The CSS class that is used to map the root element of this control with the JavaScript class. |
| 32 | * |
| 33 | * This field must be set prior to rendering. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $cssIdentifier = null; |
| 38 | |
| 39 | /** |
| 40 | * The name of the JavaScript class that handles the behavior of this control. |
| 41 | * |
| 42 | * This field must be set prior to rendering. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public $jsClass = null; |
| 47 | |
| 48 | /** |
| 49 | * The JavaScript module that contains the JavaScript class. |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | public $jsNamespace = 'piwik/UI'; |
| 54 | |
| 55 | /** |
| 56 | * Extra CSS class(es) for the root element. |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
| 60 | public $cssClass = ""; |
| 61 | |
| 62 | /** |
| 63 | * HTML Attributes for the root element |
| 64 | * |
| 65 | * @var string |
| 66 | */ |
| 67 | public $htmlAttributes = array(); |
| 68 | |
| 69 | /** |
| 70 | * The inner view that renders the actual control content. |
| 71 | * |
| 72 | * @var View |
| 73 | */ |
| 74 | private $innerView = null; |
| 75 | |
| 76 | /** |
| 77 | * Constructor. |
| 78 | */ |
| 79 | public function __construct() |
| 80 | { |
| 81 | $this->innerView = new View(static::TEMPLATE); |
| 82 | |
| 83 | parent::__construct("@CoreHome\_uiControl"); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Sets a variable. See {@link View::__set()}. |
| 88 | */ |
| 89 | public function __set($key, $val) |
| 90 | { |
| 91 | $this->innerView->__set($key, $val); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gets a view variable. See {@link View::__get()}. |
| 96 | */ |
| 97 | public function &__get($key) |
| 98 | { |
| 99 | return $this->innerView->__get($key); |
| 100 | } |
| 101 | |
| 102 | public function __isset($key) |
| 103 | { |
| 104 | return isset($this->innerView->templateVars[$key]); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Renders the control view within a containing <div> that is used by the UIControl JavaScript |
| 109 | * class. |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | public function render() |
| 114 | { |
| 115 | if ($this->cssIdentifier === null) { |
| 116 | throw new Exception("All UIControls must set a cssIdentifier property"); |
| 117 | } |
| 118 | |
| 119 | if ($this->jsClass === null) { |
| 120 | throw new Exception("All UIControls must set a jsClass property"); |
| 121 | } |
| 122 | |
| 123 | return parent::render(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * See {@link View::getTemplateVars()}. |
| 128 | */ |
| 129 | public function getTemplateVars($override = array()) |
| 130 | { |
| 131 | $this->templateVars['implView'] = $this->innerView; |
| 132 | $this->templateVars['cssIdentifier'] = $this->cssIdentifier; |
| 133 | $this->templateVars['cssClass'] = $this->cssClass; |
| 134 | $this->templateVars['jsClass'] = $this->jsClass; |
| 135 | $this->templateVars['htmlAttributes'] = $this->htmlAttributes; |
| 136 | $this->templateVars['jsNamespace'] = $this->jsNamespace; |
| 137 | $this->templateVars['implOverride'] = $override; |
| 138 | |
| 139 | $innerTemplateVars = $this->innerView->getTemplateVars($override); |
| 140 | |
| 141 | $this->templateVars['clientSideProperties'] = array(); |
| 142 | foreach ($this->getClientSideProperties() as $name) { |
| 143 | $this->templateVars['clientSideProperties'][$name] = $innerTemplateVars[$name]; |
| 144 | } |
| 145 | |
| 146 | $this->templateVars['clientSideParameters'] = array(); |
| 147 | foreach ($this->getClientSideParameters() as $name) { |
| 148 | $this->templateVars['clientSideParameters'][$name] = $innerTemplateVars[$name]; |
| 149 | } |
| 150 | |
| 151 | return parent::getTemplateVars($override); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns the array of property names whose values are passed to the UIControl JavaScript class. |
| 156 | * |
| 157 | * Should be overridden by descendants. |
| 158 | * |
| 159 | * @return array |
| 160 | */ |
| 161 | public function getClientSideProperties() |
| 162 | { |
| 163 | return array(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns an array of property names whose values are passed to the UIControl JavaScript class. |
| 168 | * These values differ from those in {@link $clientSideProperties} in that they are meant to passed as |
| 169 | * request parameters when the JavaScript code makes an AJAX request. |
| 170 | * |
| 171 | * Should be overridden by descendants. |
| 172 | * |
| 173 | * @return array |
| 174 | */ |
| 175 | public function getClientSideParameters() |
| 176 | { |
| 177 | return array(); |
| 178 | } |
| 179 | } |
| 180 |