Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UIControl.php 4.2KB

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