PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/QuickApps/View/Helper/QaHtmlHelper.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 653 lines | 157 code | 74 blank | 422 comment | 2 complexity | 460b0b26e5cc5f3c9176c7d783aa632e MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Html Helper
  4. *
  5. * PHP version 5
  6. *
  7. * @package QuickApps.View.Helper
  8. * @version 1.0
  9. * @author Christopher Castro <chris@quickapps.es>
  10. * @link http://www.quickappscms.org
  11. */
  12. class QaHtmlHelper extends AppHelper {
  13. /**
  14. * Other helpers used by QaHtmlHelper
  15. *
  16. * @var array
  17. * @access public
  18. */
  19. var $helpers = array('CoreHtml' => array('className' => 'Html'), 'Table');
  20. /**
  21. * Constructor
  22. *
  23. * ### Settings
  24. *
  25. * - `configFile` A file containing an array of tags you wish to redefine.
  26. *
  27. * ### Customizing tag sets
  28. *
  29. * Using the `configFile` option you can redefine the tag HtmlHelper will use.
  30. * The file named should be compatible with HtmlHelper::loadConfig().
  31. *
  32. * @param View $View The View this helper is being attached to.
  33. * @param array $settings Configuration settings for the helper.
  34. */
  35. public function __construct(View $View, $settings = array()) {
  36. parent::__construct($View, $settings);
  37. if (!empty($settings['configFile'])) {
  38. $this->CoreHtml->loadConfig($settings['configFile']);
  39. }
  40. }
  41. /**
  42. * QuickApps implementation of TableHelper
  43. *
  44. * @var array
  45. * @access public
  46. */
  47. function table($data , $options) {
  48. $_data = compact('data', 'options');
  49. $this->hook('html_table_alter', $_data);
  50. extract($_data);
  51. return $this->Table->create($data, $options);
  52. }
  53. /**
  54. * Adds a link to the breadcrumbs array.
  55. *
  56. * @param string $name Text for link
  57. * @param string $link URL for link (if empty it won't be a link)
  58. * @param mixed $options Link attributes e.g. array('id'=>'selected')
  59. * @return void
  60. * @see HtmlHelper::link() for details on $options that can be used.
  61. */
  62. public function addCrumb($name, $link = null, $options = null) {
  63. $data = compact('name', 'link', 'options');
  64. $this->hook('html_add_crumb_alter', $data);
  65. extract($data);
  66. return $this->CoreHtml->addCrumb($name, $link, $options);
  67. }
  68. /**
  69. * Returns a doctype string.
  70. *
  71. * Possible doctypes:
  72. *
  73. * - html4-strict: HTML4 Strict.
  74. * - html4-trans: HTML4 Transitional.
  75. * - html4-frame: HTML4 Frameset.
  76. * - html5: HTML5.
  77. * - xhtml-strict: XHTML1 Strict.
  78. * - xhtml-trans: XHTML1 Transitional.
  79. * - xhtml-frame: XHTML1 Frameset.
  80. * - xhtml11: XHTML1.1.
  81. *
  82. * @param string $type Doctype to use.
  83. * @return string Doctype string
  84. * @access public
  85. * @link http://book.cakephp.org/view/1439/docType
  86. */
  87. public function docType($type = 'xhtml-strict') {
  88. $this->hook('html_doc_type_alter', $type);
  89. return $this->CoreHtml->docType($type);
  90. }
  91. /**
  92. * Creates a link to an external resource and handles basic meta tags
  93. *
  94. * ### Options
  95. *
  96. * - `inline` Whether or not the link element should be output inline, or in scripts_for_layout.
  97. *
  98. * @param string $type The title of the external resource
  99. * @param mixed $url The address of the external resource or string for content attribute
  100. * @param array $options Other attributes for the generated tag. If the type attribute is html,
  101. * rss, atom, or icon, the mime-type is returned.
  102. * @return string A completed `<link />` element.
  103. * @access public
  104. * @link http://book.cakephp.org/view/1438/meta
  105. */
  106. public function meta($type, $url = null, $options = array()) {
  107. $data = compact('type', 'url', 'options');
  108. $this->hook('html_meta_alter', $data);
  109. extract($data);
  110. return $this->CoreHtml->meta($type, $url, $options);
  111. }
  112. /**
  113. * Returns a charset META-tag.
  114. *
  115. * @param string $charset The character set to be used in the meta tag. If empty,
  116. * The App.encoding value will be used. Example: "utf-8".
  117. * @return string A meta tag containing the specified character set.
  118. * @access public
  119. * @link http://book.cakephp.org/view/1436/charset
  120. */
  121. public function charset($charset = null) {
  122. $this->hook('html_charset_alter', $charset);
  123. return $this->CoreHtml->charset($charset);
  124. }
  125. /**
  126. * Creates an HTML link.
  127. *
  128. * If $url starts with "http://" this is treated as an external link. Else,
  129. * it is treated as a path to controller/action and parsed with the
  130. * HtmlHelper::url() method.
  131. *
  132. * If the $url is empty, $title is used instead.
  133. *
  134. * ### Options
  135. *
  136. * - `escape` Set to false to disable escaping of title and attributes.
  137. *
  138. * @param string $title The content to be wrapped by <a> tags.
  139. * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  140. * @param array $options Array of HTML attributes.
  141. * @param string $confirmMessage JavaScript confirmation message.
  142. * @return string An `<a />` element.
  143. * @access public
  144. * @link http://book.cakephp.org/view/1442/link
  145. */
  146. public function link($title, $url = null, $options = array(), $confirmMessage = false) {
  147. $data = compact('title', 'url', 'options', 'confirmMessage');
  148. $this->hook('html_link_alter', $data);
  149. extract($data);
  150. return $this->CoreHtml->link($title, $url, $options, $confirmMessage);
  151. }
  152. /**
  153. * Creates a link element for CSS stylesheets.
  154. *
  155. * ### Options
  156. *
  157. * - `inline` If set to false, the generated tag appears in the head tag of the layout. Defaults to true
  158. *
  159. * @param mixed $path The name of a CSS style sheet or an array containing names of
  160. * CSS stylesheets. If `$path` is prefixed with '/', the path will be relative to the webroot
  161. * of your application. Otherwise, the path will be relative to your CSS path, usually webroot/css.
  162. * @param string $rel Rel attribute. Defaults to "stylesheet". If equal to 'import' the stylesheet will be imported.
  163. * @param array $options Array of HTML attributes.
  164. * @return string CSS <link /> or <style /> tag, depending on the type of link.
  165. * @access public
  166. * @link http://book.cakephp.org/view/1437/css
  167. */
  168. public function css($path, $rel = null, $options = array()) {
  169. $data = compact('path', 'rel', 'options');
  170. $this->hook('html_css_alter', $data);
  171. extract($data);
  172. return $this->CoreHtml->css($path, $rel, $options);
  173. }
  174. /**
  175. * Returns one or many `<script>` tags depending on the number of scripts given.
  176. *
  177. * If the filename is prefixed with "/", the path will be relative to the base path of your
  178. * application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
  179. *
  180. * Can include one or many Javascript files.
  181. *
  182. * ### Options
  183. *
  184. * - `inline` - Whether script should be output inline or into scripts_for_layout.
  185. * - `once` - Whether or not the script should be checked for uniqueness. If true scripts will only be
  186. * included once, use false to allow the same script to be included more than once per request.
  187. *
  188. * @param mixed $url String or array of javascript files to include
  189. * @param mixed $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value
  190. * @return mixed String of `<script />` tags or null if $inline is false or if $once is true and the file has been
  191. * included before.
  192. * @access public
  193. * @link http://book.cakephp.org/view/1589/script
  194. */
  195. public function script($url, $options = array()) {
  196. $data = compact('url', 'options');
  197. $this->hook('html_script_alter', $data);
  198. extract($data);
  199. return $this->CoreHtml->script($url, $options);
  200. }
  201. /**
  202. * Wrap $script in a script tag.
  203. *
  204. * ### Options
  205. *
  206. * - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
  207. * - `inline` (boolean) Whether or not the $script should be added to $scripts_for_layout or output inline
  208. *
  209. * @param string $script The script to wrap
  210. * @param array $options The options to use.
  211. * @return mixed string or null depending on the value of `$options['inline']`
  212. * @access public
  213. * @link http://book.cakephp.org/view/1604/scriptBlock
  214. */
  215. public function scriptBlock($script, $options = array()) {
  216. $data = compact('script', 'options');
  217. $this->hook('html_script_block_alter', $data);
  218. extract($data);
  219. return $this->CoreHtml->scriptBlock($script, $options);
  220. }
  221. /**
  222. * Begin a script block that captures output until HtmlHelper::scriptEnd()
  223. * is called. This capturing block will capture all output between the methods
  224. * and create a scriptBlock from it.
  225. *
  226. * ### Options
  227. *
  228. * - `safe` Whether the code block should contain a CDATA
  229. * - `inline` Should the generated script tag be output inline or in `$scripts_for_layout`
  230. *
  231. * @param array $options Options for the code block.
  232. * @return void
  233. * @access public
  234. * @link http://book.cakephp.org/view/1605/scriptStart
  235. */
  236. public function scriptStart($options = array()) {
  237. $this->hook('html_script_start_alter', $options);
  238. return $this->CoreHtml->scriptStart($options);
  239. }
  240. /**
  241. * End a Buffered section of Javascript capturing.
  242. * Generates a script tag inline or in `$scripts_for_layout` depending on the settings
  243. * used when the scriptBlock was started
  244. *
  245. * @return mixed depending on the settings of scriptStart() either a script tag or null
  246. * @access public
  247. * @link http://book.cakephp.org/view/1606/scriptEnd
  248. */
  249. public function scriptEnd() {
  250. $r = $this->CoreHtml->scriptEnd();
  251. $this->hook('html_script_end_alter', $r);
  252. return $r;
  253. }
  254. /**
  255. * Builds CSS style data from an array of CSS properties
  256. *
  257. * ### Usage:
  258. *
  259. * {{{
  260. * echo $html->style(array('margin' => '10px', 'padding' => '10px'), true);
  261. *
  262. * // creates
  263. * 'margin:10px;padding:10px;'
  264. * }}}
  265. *
  266. * @param array $data Style data array, keys will be used as property names, values as property values.
  267. * @param boolean $oneline Whether or not the style block should be displayed on one line.
  268. * @return string CSS styling data
  269. * @access public
  270. * @link http://book.cakephp.org/view/1440/style
  271. */
  272. public function style($data, $oneline = true) {
  273. $data = compact('data', 'oneline');
  274. $this->hook('html_style_alter', $data);
  275. extract($data);
  276. return $this->CoreHtml->style($data, $oneline);
  277. }
  278. /**
  279. * Returns the breadcrumb trail as a sequence of &raquo;-separated links.
  280. *
  281. * @param string $separator Text to separate crumbs.
  282. * @param string $startText This will be the first crumb, if false it defaults to first crumb in array
  283. * @return string Composed bread crumbs
  284. */
  285. public function getCrumbs($separator = '&raquo;', $startText = false) {
  286. $data = compact('separator', 'startText');
  287. $this->hook('html_get_crumbs_alter', $data);
  288. extract($data);
  289. return $this->CoreHtml->getCrumbs($separator, $startText);
  290. }
  291. /**
  292. * Returns breadcrumbs as a (x)html list
  293. *
  294. * This method uses HtmlHelper::tag() to generate list and its elements. Works
  295. * similiary to HtmlHelper::getCrumbs(), so it uses options which every
  296. * crumb was added with.
  297. *
  298. * @param array $options Array of html attributes to apply to the generated list elements.
  299. * @return string breadcrumbs html list
  300. * @access public
  301. */
  302. function getCrumbList($options = array()) {
  303. $this->hook('html_get_crumb_list_alter', $options);
  304. return $this->CoreHtml->getCrumbList($options);
  305. }
  306. /**
  307. * Creates a formatted IMG element. If `$options['url']` is provided, an image link will be
  308. * generated with the link pointed at `$options['url']`. This method will set an empty
  309. * alt attribute if one is not supplied.
  310. *
  311. * ### Usage
  312. *
  313. * Create a regular image:
  314. *
  315. * `echo $html->image('cake_icon.png', array('alt' => 'CakePHP'));`
  316. *
  317. * Create an image link:
  318. *
  319. * `echo $html->image('cake_icon.png', array('alt' => 'CakePHP', 'url' => 'http://cakephp.org'));`
  320. *
  321. * @param string $path Path to the image file, relative to the app/webroot/img/ directory.
  322. * @param array $options Array of HTML attributes.
  323. * @return string completed img tag
  324. * @access public
  325. * @link http://book.cakephp.org/view/1441/image
  326. */
  327. public function image($path, $options = array()) {
  328. $data = compact('path', 'options');
  329. $this->hook('html_image_alter', $data);
  330. extract($data);
  331. return $this->CoreHtml->image($path, $options);
  332. }
  333. /**
  334. * Returns a row of formatted and named TABLE headers.
  335. *
  336. * @param array $names Array of tablenames.
  337. * @param array $trOptions HTML options for TR elements.
  338. * @param array $thOptions HTML options for TH elements.
  339. * @return string Completed table headers
  340. * @access public
  341. * @link http://book.cakephp.org/view/1446/tableHeaders
  342. */
  343. public function tableHeaders($names, $trOptions = null, $thOptions = null) {
  344. $data = compact('names', 'trOptions', 'thOptions');
  345. $this->hook('html_table_headers_alter', $data);
  346. extract($data);
  347. return $this->CoreHtml->tableHeaders($names, $trOptions, $thOptions);
  348. }
  349. /**
  350. * Returns a formatted string of table rows (TR's with TD's in them).
  351. *
  352. * @param array $data Array of table data
  353. * @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
  354. * @param array $evenTrOptions HTML options for even TR elements
  355. * @param bool $useCount adds class "column-$i"
  356. * @param bool $continueOddEven If false, will use a non-static $count variable,
  357. * so that the odd/even count is reset to zero just for that call.
  358. * @return string Formatted HTML
  359. * @access public
  360. * @link http://book.cakephp.org/view/1447/tableCells
  361. */
  362. public function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
  363. $data = compact('data', 'oddTrOptions', 'evenTrOptions', 'useCount', 'continueOddEven');
  364. $this->hook('html_table_cells_alter', $data);
  365. extract($data);
  366. return $this->CoreHtml->tableCells($data, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
  367. }
  368. /**
  369. * Returns a formatted block tag, i.e DIV, SPAN, P.
  370. *
  371. * ### Options
  372. *
  373. * - `escape` Whether or not the contents should be html_entity escaped.
  374. *
  375. * @param string $name Tag name.
  376. * @param string $text String content that will appear inside the div element.
  377. * If null, only a start tag will be printed
  378. * @param array $options Additional HTML attributes of the DIV tag, see above.
  379. * @return string The formatted tag element
  380. * @access public
  381. * @link http://book.cakephp.org/view/1443/tag
  382. */
  383. public function tag($name, $text = null, $options = array()) {
  384. $data = compact('name', 'text', 'options');
  385. $this->hook('html_tag_alter', $data);
  386. extract($data);
  387. return $this->CoreHtml->tag($name, $text, $options);
  388. }
  389. /**
  390. * Returns a formatted existent block of $tags
  391. *
  392. * @param string $tag Tag name
  393. * @return string Formatted block
  394. */
  395. public function useTag($tag) {
  396. $args = func_get_args();
  397. array_shift($args);
  398. $data = compact('tag', 'args');
  399. $this->hook('html_useTag_alter', $data);
  400. extract($data);
  401. foreach ($args as &$arg) {
  402. if (is_array($arg)) {
  403. $arg = $this->CoreHtml->_parseAttributes($arg, null, ' ', '');
  404. }
  405. }
  406. $before = $this->hook('html_before_use_tag', $data, array('collectReturn' => true));
  407. $after = $this->hook('html_after_use_tag', $data, array('collectReturn' => true));
  408. return implode(' ', (array)$before) . vsprintf($this->CoreHtml->_tags[$tag], $args) . implode(' ', (array)$after);
  409. }
  410. /**
  411. * Returns a formatted DIV tag for HTML FORMs.
  412. *
  413. * ### Options
  414. *
  415. * - `escape` Whether or not the contents should be html_entity escaped.
  416. *
  417. * @param string $class CSS class name of the div element.
  418. * @param string $text String content that will appear inside the div element.
  419. * If null, only a start tag will be printed
  420. * @param array $options Additional HTML attributes of the DIV tag
  421. * @return string The formatted DIV element
  422. * @access public
  423. * @link http://book.cakephp.org/view/1444/div
  424. */
  425. public function div($class = null, $text = null, $options = array()) {
  426. $data = compact('class', 'text', 'options');
  427. $this->hook('html_div_alter', $data);
  428. extract($data);
  429. return $this->CoreHtml->div($class, $text, $options);
  430. }
  431. /**
  432. * Returns a formatted P tag.
  433. *
  434. * ### Options
  435. *
  436. * - `escape` Whether or not the contents should be html_entity escaped.
  437. *
  438. * @param string $class CSS class name of the p element.
  439. * @param string $text String content that will appear inside the p element.
  440. * @param array $options Additional HTML attributes of the P tag
  441. * @return string The formatted P element
  442. * @access public
  443. * @link http://book.cakephp.org/view/1445/para
  444. */
  445. public function para($class, $text, $options = array()) {
  446. $data = compact('class', 'text', 'options');
  447. $this->hook('html_para_alter', $data);
  448. extract($data);
  449. return $this->CoreHtml->para($class, $text, $options);
  450. }
  451. /**
  452. * Returns an audio/video element
  453. *
  454. * ### Usage
  455. *
  456. * Using an audio file:
  457. *
  458. * `echo $this->Html->media('audio.mp3', array('fullBase' => true));`
  459. *
  460. * Outputs:
  461. *
  462. * `<video src="http://www.somehost.com/files/audio.mp3">Fallback text</video>`
  463. *
  464. * Using a video file:
  465. *
  466. * `echo $this->Html->media('video.mp4', array('text' => 'Fallback text'));`
  467. *
  468. * Outputs:
  469. *
  470. * `<video src="/files/video.mp4">Fallback text</video>`
  471. *
  472. * Using multiple video files:
  473. *
  474. * {{{
  475. * echo $this->Html->media(
  476. * array('video.mp4', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
  477. * array('tag' => 'video', 'autoplay')
  478. * );
  479. * }}}
  480. *
  481. * Outputs:
  482. *
  483. * {{{
  484. * <video autoplay="autoplay">
  485. * <source src="/files/video.mp4" type="video/mp4"/>
  486. * <source src="/files/video.ogv" type="video/ogv; codecs='theora, vorbis'"/>
  487. * </video>
  488. * }}}
  489. *
  490. * ### Options
  491. *
  492. * - `tag` Type of media element to generate, either "audio" or "video".
  493. * If tag is not provided it's guessed based on file's mime type.
  494. * - `text` Text to include inside the audio/video tag
  495. * - `pathPrefix` Path prefix to use for relative urls, defaults to 'files/'
  496. * - `fullBase` If provided the src attribute will get a full address including domain name
  497. *
  498. * @param string|array $path Path to the video file, relative to the webroot/{$options['pathPrefix']} directory.
  499. * Or an array where each item itself can be a path string or an associate array containing keys `src` and `type`
  500. * @param array $options Array of HTML attributes, and special options above.
  501. * @return string Generated media element
  502. */
  503. public function media($path, $options = array()) {
  504. $data = compact('path', 'options');
  505. $this->hook('html_media_alter', $data);
  506. extract($data);
  507. return $this->CoreHtml->media($path, $options);
  508. }
  509. /**
  510. * Build a nested list (UL/OL) out of an associative array.
  511. *
  512. * @param array $list Set of elements to list
  513. * @param array $options Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag
  514. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  515. * @param string $tag Type of list tag to use (ol/ul)
  516. * @return string The nested list
  517. */
  518. public function nestedList($list, $options = array(), $itemOptions = array(), $tag = 'ul') {
  519. $data = compact('list', 'options', 'itemOptions', 'tag');
  520. $this->hook('html_nested_list_alter', $data);
  521. extract($data);
  522. return $this->CoreHtml->nestedList($list, $options, $itemOptions, $tag);
  523. }
  524. /**
  525. * Load Html configs
  526. *
  527. * @param mixed $configFile String with the config file (load using PhpReader) or an array with file and reader name
  528. * @param string $path Path with config file
  529. * @return mixed False to error or loaded configs
  530. */
  531. public function loadConfig($configFile, $path = CONFIGS) {
  532. $data = compact('configFile', 'path');
  533. $this->hook('html_load_config_alter', $data);
  534. extract($data);
  535. return $this->CoreHtml->loadConfig($configFile, $path);
  536. }
  537. /**
  538. * Returns a space-delimited string with items of the $options array. If a
  539. * key of $options array happens to be one of:
  540. *
  541. * - 'compact'
  542. * - 'checked'
  543. * - 'declare'
  544. * - 'readonly'
  545. * - 'disabled'
  546. * - 'selected'
  547. * - 'defer'
  548. * - 'ismap'
  549. * - 'nohref'
  550. * - 'noshade'
  551. * - 'nowrap'
  552. * - 'multiple'
  553. * - 'noresize'
  554. *
  555. * And its value is one of:
  556. *
  557. * - '1' (string)
  558. * - 1 (integer)
  559. * - true (boolean)
  560. * - 'true' (string)
  561. *
  562. * Then the value will be reset to be identical with key's name.
  563. * If the value is not one of these 3, the parameter is not output.
  564. *
  565. * 'escape' is a special option in that it controls the conversion of
  566. * attributes to their html-entity encoded equivalents. Set to false to disable html-encoding.
  567. *
  568. * If value for any option key is set to `null` or `false`, that option will be excluded from output.
  569. *
  570. * @param array $options Array of options.
  571. * @param array $exclude Array of options to be excluded, the options here will not be part of the return.
  572. * @param string $insertBefore String to be inserted before options.
  573. * @param string $insertAfter String to be inserted after options.
  574. * @return string Composed attributes.
  575. */
  576. public function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
  577. return $this->CoreHtml->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
  578. }
  579. }