Helper.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 0.2.9
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View;
  16. use Cake\Core\Configure;
  17. use Cake\Core\InstanceConfigTrait;
  18. use Cake\Core\Plugin;
  19. use Cake\Event\EventListener;
  20. use Cake\Routing\Router;
  21. use Cake\Utility\Inflector;
  22. /**
  23. * Abstract base class for all other Helpers in CakePHP.
  24. * Provides common methods and features.
  25. *
  26. * ## Callback methods
  27. *
  28. * Helpers support a number of callback methods. These callbacks allow you to hook into
  29. * the various view lifecycle events and either modify existing view content or perform
  30. * other application specific logic. The events are not implemented by this base class, as
  31. * implementing a callback method subscribes a helper to the related event. The callback methods
  32. * are as follows:
  33. *
  34. * - `beforeRender(Event $event, $viewFile)` - beforeRender is called before the view file is rendered.
  35. * - `afterRender(Event $event, $viewFile)` - afterRender is called after the view file is rendered
  36. * but before the layout has been rendered.
  37. * - beforeLayout(Event $event, $layoutFile)` - beforeLayout is called before the layout is rendered.
  38. * - `afterLayout(Event $event, $layoutFile)` - afterLayout is called after the layout has rendered.
  39. * - `beforeRenderFile(Event $event, $viewFile)` - Called before any view fragment is rendered.
  40. * - `afterRenderFile(Event $event, $viewFile, $content)` - Called after any view fragment is rendered.
  41. * If a listener returns a non-null value, the output of the rendered file will be set to that.
  42. *
  43. */
  44. class Helper implements EventListener {
  45. use InstanceConfigTrait;
  46. /**
  47. * List of helpers used by this helper
  48. *
  49. * @var array
  50. */
  51. public $helpers = array();
  52. /**
  53. * Default config for this helper.
  54. *
  55. * @var array
  56. */
  57. protected $_defaultConfig = [];
  58. /**
  59. * A helper lookup table used to lazy load helper objects.
  60. *
  61. * @var array
  62. */
  63. protected $_helperMap = array();
  64. /**
  65. * The current theme name if any.
  66. *
  67. * @var string
  68. */
  69. public $theme = null;
  70. /**
  71. * Request object
  72. *
  73. * @var \Cake\Network\Request
  74. */
  75. public $request = null;
  76. /**
  77. * Plugin path
  78. *
  79. * @var string
  80. */
  81. public $plugin = null;
  82. /**
  83. * Holds the fields array('field_name' => array('type' => 'string', 'length' => 100),
  84. * primaryKey and validates array('field_name')
  85. *
  86. * @var array
  87. */
  88. public $fieldset = array();
  89. /**
  90. * Holds tag templates.
  91. *
  92. * @var array
  93. */
  94. public $tags = array();
  95. /**
  96. * The View instance this helper is attached to
  97. *
  98. * @var View
  99. */
  100. protected $_View;
  101. /**
  102. * Default Constructor
  103. *
  104. * @param View $View The View this helper is being attached to.
  105. * @param array $config Configuration settings for the helper.
  106. */
  107. public function __construct(View $View, array $config = array()) {
  108. $this->_View = $View;
  109. $this->request = $View->request;
  110. $this->config($config);
  111. if (!empty($this->helpers)) {
  112. $this->_helperMap = $View->helpers()->normalizeArray($this->helpers);
  113. }
  114. }
  115. /**
  116. * Provide non fatal errors on missing method calls.
  117. *
  118. * @param string $method Method to invoke
  119. * @param array $params Array of params for the method.
  120. * @return void
  121. */
  122. public function __call($method, $params) {
  123. trigger_error(sprintf('Method %1$s::%2$s does not exist', get_class($this), $method), E_USER_WARNING);
  124. }
  125. /**
  126. * Lazy loads helpers.
  127. *
  128. * @param string $name Name of the property being accessed.
  129. * @return mixed Helper or property found at $name
  130. * @deprecated Accessing request properties through this method is deprecated and will be removed in 3.0.
  131. */
  132. public function __get($name) {
  133. if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
  134. $config = ['enabled' => false] + (array)$this->_helperMap[$name]['config'];
  135. $this->{$name} = $this->_View->addHelper($this->_helperMap[$name]['class'], $config);
  136. }
  137. if (isset($this->{$name})) {
  138. return $this->{$name};
  139. }
  140. }
  141. /**
  142. * Returns a string to be used as onclick handler for confirm dialogs.
  143. *
  144. * @param string $message Message to be displayed
  145. * @param string $okCode Code to be executed after user chose 'OK'
  146. * @param string $cancelCode Code to be executed after user chose 'Cancel'
  147. * @param array $options Array of options
  148. * @return string onclick JS code
  149. */
  150. protected function _confirm($message, $okCode, $cancelCode = '', $options = array()) {
  151. $message = json_encode($message);
  152. $confirm = "if (confirm({$message})) { {$okCode} } {$cancelCode}";
  153. if (isset($options['escape']) && $options['escape'] === false) {
  154. $confirm = h($confirm);
  155. }
  156. return $confirm;
  157. }
  158. /**
  159. * Adds the given class to the element options
  160. *
  161. * @param array $options Array options/attributes to add a class to
  162. * @param string $class The class name being added.
  163. * @param string $key the key to use for class.
  164. * @return array Array of options with $key set.
  165. */
  166. public function addClass(array $options = array(), $class = null, $key = 'class') {
  167. if (isset($options[$key]) && trim($options[$key])) {
  168. $options[$key] .= ' ' . $class;
  169. } else {
  170. $options[$key] = $class;
  171. }
  172. return $options;
  173. }
  174. /**
  175. * Get the View callbacks this helper is interested in.
  176. *
  177. * By defining one of the callback methods a helper is assumed
  178. * to be interested in the related event.
  179. *
  180. * Override this method if you need to add non-conventional event listeners.
  181. * Or if you want helpers to listen to non-standard events.
  182. *
  183. * @return array
  184. */
  185. public function implementedEvents() {
  186. $eventMap = [
  187. 'View.beforeRenderFile' => 'beforeRenderFile',
  188. 'View.afterRenderFile' => 'afterRenderFile',
  189. 'View.beforeRender' => 'beforeRender',
  190. 'View.afterRender' => 'afterRender',
  191. 'View.beforeLayout' => 'beforeLayout',
  192. 'View.afterLayout' => 'afterLayout'
  193. ];
  194. $events = [];
  195. foreach ($eventMap as $event => $method) {
  196. if (method_exists($this, $method)) {
  197. $events[$event] = $method;
  198. }
  199. }
  200. return $events;
  201. }
  202. }