/views/helpers/markitup.php

https://github.com/sams/cakephp-markitup · PHP · 158 lines · 113 code · 11 blank · 34 comment · 10 complexity · 682377c161bbdeeea46fab28d9ab5b7c MD5 · raw file

  1. <?php
  2. class MarkitupHelper extends AppHelper {
  3. public $helpers = array('Core.Html', 'Core.Form', 'Core.Javascript');
  4. public $paths = array(
  5. 'css' => '/js/markitup/',
  6. 'js' => 'markitup/',
  7. );
  8. public $vendors = array('markdown' => 'Markitup.Markdown');
  9. public function __construct() {
  10. $paths = Configure::read('Markitup.paths');
  11. if (empty($paths)) {
  12. return;
  13. }
  14. if (is_string($paths)) {
  15. $paths = array('js' => $paths);
  16. }
  17. $this->paths = array_merge($this->paths, $paths);
  18. }
  19. /**
  20. * Generates a form textarea element complete with label and wrapper div with markItUp! applied.
  21. *
  22. * @param string $fieldName This should be "Modelname.fieldname"
  23. * @param array $settings
  24. * @return string An <textarea /> element.
  25. */
  26. public function editor($name, $settings = array()) {
  27. $this->Javascript->link($this->paths['js'] . 'jquery.markitup', false);
  28. $config = $this->_build($settings);
  29. $settings = $config['settings'];
  30. $default = $config['default'];
  31. $textarea = array_diff_key($settings, $default);
  32. $textarea = array_merge($textarea, array('type' => 'textarea'));
  33. $id = '#' . parent::domId($name);
  34. $out[] = '$(function() {';
  35. $out[] = ' $("' . $id . '").markItUp(';
  36. $out[] = ' ' . $settings['settings'] . ',';
  37. $out[] = ' {';
  38. $out[] = ' previewParserPath:"' . $settings['parser'] . '"';
  39. $out[] = ' }';
  40. $out[] = ' );';
  41. $out[] = '});';
  42. $this->Html->scriptBlock(join("\n", $out), array('inline' => false));
  43. return $this->output($this->Form->input($name, $textarea));
  44. }
  45. /**
  46. * Link to build markItUp! on a existing textfield
  47. *
  48. * @param string $title The content to be wrapped by <a> tags.
  49. * @param string $fieldName This should be "Modelname.fieldname" or specific domId as #id.
  50. * @param array $settings
  51. * @param array $htmlAttributes Array of HTML attributes.
  52. * @param string $confirmMessage JavaScript confirmation message.
  53. * @return string An <a /> element.
  54. */
  55. public function create($title, $fieldName = "", $settings = array(), $htmlAttributes = array(), $confirmMessage = false) {
  56. $id = ($fieldName{0} === '#') ? $fieldName : '#'.parent::domId($fieldName);
  57. $config = $this->_build($settings);
  58. $settings = $config['settings'];
  59. $htmlAttributes = array_merge($htmlAttributes, array('onclick' => 'jQuery("'.$id.'").markItUpRemove(); jQuery("'.$id.'").markItUp('.$settings['settings'].', { previewParserPath:"'.$settings['parser'].'" }); return false;'));
  60. return $this->Html->link($title, "#", $htmlAttributes, $confirmMessage, false);
  61. }
  62. /**
  63. * Link to destroy a markItUp! editor from a textfield
  64. * @param string $title The content to be wrapped by <a> tags.
  65. * @param string $fieldName This should be "Modelname.fieldname" or specific domId as #id.
  66. * @param array $htmlAttributes Array of HTML attributes.
  67. * @param string $confirmMessage JavaScript confirmation message.
  68. * @return string An <a /> element.
  69. */
  70. public function destroy($title, $fieldName = "", $htmlAttributes = array(), $confirmMessage = false) {
  71. $id = ($fieldName{0} === '#') ? $fieldName : '#'.parent::domId($fieldName);
  72. $htmlAttributes = array_merge($htmlAttributes, array('onclick' => 'jQuery("'.$id.'").markItUpRemove(); return false;'));
  73. return $this->Html->link($title, "#", $htmlAttributes, $confirmMessage, false);
  74. }
  75. /**
  76. * Link to add content to the focused textarea
  77. * @param string $title The content to be wrapped by <a> tags.
  78. * @param string $fieldName This should be "Modelname.fieldname" or specific domId as #id.
  79. * @param mixed $content String or array of markItUp! options (openWith, closeWith, replaceWith, placeHolder and more. See markItUp! documentation for more details : http://markitup.jaysalvat.com/documentation
  80. * @param array $htmlAttributes Array of HTML attributes.
  81. * @param string $confirmMessage JavaScript confirmation message.
  82. * @return string An <a /> element.
  83. */
  84. public function insert($title, $fieldName = null, $content = array(), $htmlAttributes = array(), $confirmMessage = false) {
  85. if (isset($fieldName)) {
  86. $content['target'] = ($fieldName{0} === '#') ? $fieldName : '#'.parent::domId($fieldName);
  87. }
  88. if (!is_array($content)) {
  89. $content['replaceWith'] = $content;
  90. }
  91. $properties = '';
  92. foreach($content as $k => $v) {
  93. $properties .= $k.':"'.addslashes($v).'",';
  94. }
  95. $properties = substr($properties, 0, -1);
  96. $htmlAttributes = array_merge($htmlAttributes, array('onclick' => '$.markItUp( { '.$properties.' } ); return false;'));
  97. return $this->Html->link($title, "#", $htmlAttributes, $confirmMessage, false);
  98. }
  99. public function parse($content, $parser = 'default') {
  100. $parsers = Configure::read('Markitup.vendors');
  101. if (empty($vendors)) {
  102. $vendors = array();
  103. }
  104. $vendors = array_merge($this->vendors, $vendors);
  105. if (array_key_exists($parser, $vendors)) {
  106. if (!is_array($vendors[$parser])) {
  107. $vendors[$parser] = array('class' => $vendors[$parser]);
  108. }
  109. extract($vendors[$parser]);
  110. $plugin = 'App';
  111. if (strpos($class, '.')) {
  112. list($plugin, $class) = explode('.', $class);
  113. }
  114. if (!isset($file)) {
  115. $file = null;
  116. }
  117. App::import('Vendor', $plugin . '.' . $class, null, null, $file);
  118. $content = $class($content);
  119. }
  120. echo $this->Html->css($this->paths['css'] . 'templates/preview', null, null, false);
  121. return $content;
  122. }
  123. protected function _build($settings) {
  124. $default = array(
  125. 'set' => 'default',
  126. 'skin' => 'simple',
  127. 'settings' => 'mySettings',
  128. 'parser' => array(
  129. 'plugin' => 'markitup',
  130. 'controller' => 'markitup',
  131. 'action' => 'preview',
  132. 'admin' => false,
  133. )
  134. );
  135. $settings = array_merge($default, $settings);
  136. if ($settings['parser']) {
  137. $settings['parser'] = $this->Html->url(Router::url(array_merge($settings['parser'], array($settings['set']))));
  138. }
  139. $this->Html->css(array(
  140. $this->paths['css'] . 'skins/' . $settings['skin'] . '/style',
  141. $this->paths['css'] . 'sets/' . $settings['set'] . '/style',
  142. ), null, array('inline' => false));
  143. $this->Html->script($this->paths['js'] . 'sets/' . $settings['set'] . '/set', array('inline' => false));
  144. return array('settings' => $settings, 'default' => $default);
  145. }
  146. }
  147. ?>