PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Engine/View/Helper/TinyMce.php

https://github.com/shopaholiccompany/shopaholic
PHP | 236 lines | 193 code | 27 blank | 16 comment | 22 complexity | afb1e4d14e0369ed7e04c47c926589ef MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Application_Core
  6. * @package Core
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: TinyMce.php 7281 2010-09-03 03:46:33Z john $
  10. */
  11. /**
  12. * @category Application_Core
  13. * @package Core
  14. * @copyright Copyright 2006-2010 Webligo Developments
  15. * @license http://www.socialengine.net/license/
  16. */
  17. class Engine_View_Helper_TinyMce extends Zend_View_Helper_Abstract
  18. {
  19. protected $_enabled = false;
  20. protected $_defaultScript = 'externals/tinymce/tiny_mce.js';
  21. protected $_html = true;
  22. protected $_bbcode = false;
  23. protected $_supported = array(
  24. 'mode' => array('textareas', 'specific_textareas', 'exact', 'none'),
  25. 'theme' => array('simple', 'advanced'),
  26. 'format' => array('html', 'xhtml'),
  27. 'languages' => array(
  28. 'en', 'ar', 'ca', 'el', 'fr', 'hy', 'ka', 'ml', 'pl', 'si', 'te', 'vi',
  29. 'az', 'ch', 'gl', 'ia', 'kl', 'mn', 'ps', 'sk', 'th', 'zh', 'be', 'cs',
  30. 'es', 'gu', 'id', 'ko', 'ms', 'pt', 'sl', 'tr', 'zu', 'bg', 'cy', 'et',
  31. 'he', 'ii', 'lb', 'nb', 'ro', 'sq', 'tt', 'bn', 'da', 'eu', 'hi', 'is',
  32. 'lt', 'nl', 'ru', 'sr', 'tw', 'br', 'de', 'fa', 'hr', 'it', 'lv', 'nn',
  33. 'sc', 'sv', 'uk', 'bs', 'dv', 'fi', 'hu', 'ja', 'mk', 'no', 'se', 'ta',
  34. 'ur',
  35. ),
  36. 'directionality' => array(
  37. 'rtl', 'ltr',
  38. ),
  39. 'plugins' => array('style', 'layer', 'table', 'save',
  40. 'advhr', 'advimage', 'advlink', 'emotions',
  41. 'iespell', 'insertdatetime', 'preview', 'media',
  42. 'searchreplace', 'print', 'contextmenu', 'paste',
  43. 'directionality', 'fullscreen', 'noneditable', 'visualchars',
  44. 'nonbreakfing', 'xhtmlxtras', 'imagemanager', 'filemanager', 'template'
  45. ));
  46. protected $_config = array('mode' => 'textareas',
  47. 'plugins' => 'emotions, table, fullscreen, media, preview, paste',
  48. 'theme' => 'advanced',
  49. 'theme_advanced_buttons1' => 'undo, redo, cleanup, removeformat, pasteword, |, code, media, image, fullscreen,preview',
  50. 'theme_advanced_buttons2' => '',
  51. 'theme_advanced_buttons3' => '',
  52. 'theme_advanced_toolbar_align' => 'left',
  53. 'theme_advanced_toolbar_location' => 'top',
  54. 'element_format' => 'html',
  55. 'height' => '225px'
  56. );
  57. protected $_scriptPath;
  58. protected $_scriptFile;
  59. protected $_useCompressor = false;
  60. public function __set($name, $value)
  61. {
  62. $method = 'set' . $name;
  63. if( !method_exists($this, $method) ) {
  64. throw new Engine_Exception('Invalid tinyMce property');
  65. }
  66. $this->$method($value);
  67. }
  68. public function __get($name)
  69. {
  70. $method = 'get' . $name;
  71. if( !method_exists($this, $method) ) {
  72. throw new Engine_Exception('Invalid tinyMce property');
  73. }
  74. return $this->$method();
  75. }
  76. public function setOptions(array $options)
  77. {
  78. $methods = get_class_methods($this);
  79. foreach( $options as $key => $value ) {
  80. $method = 'set' . ucfirst($key);
  81. if( in_array($method, $methods) ) {
  82. $this->$method($value);
  83. } else {
  84. $this->_config[$key] = $value;
  85. }
  86. }
  87. return $this;
  88. }
  89. public function TinyMce()
  90. {
  91. return $this;
  92. }
  93. public function setBbcode($value)
  94. {
  95. $this->_bbcode = ($value == "1");
  96. $this->updateSettings();
  97. }
  98. public function setHtml($value)
  99. {
  100. $this->_html = ($value == "1");
  101. $this->updateSettings();
  102. }
  103. public function setLanguage($language)
  104. {
  105. if( !in_array($language, $this->_supported['languages']) ) {
  106. list($language) = explode('_', $language);
  107. if( !in_array($language, $this->_supported['languages']) ) {
  108. return $this;
  109. }
  110. }
  111. $this->_config['language'] = $language;
  112. return $this;
  113. }
  114. public function setDirectionality($directionality)
  115. {
  116. if( in_array($directionality, $this->_supported['directionality']) ) {
  117. $this->_config['directionality'] = $directionality;
  118. }
  119. return $this;
  120. }
  121. public function updateSettings()
  122. {
  123. if( $this->_bbcode && (!$this->_html) ) {
  124. $this->_config['plugins'] .= ', bbcode';
  125. // $this->_config['theme_advanced_styles'] = "Code=codeStyle;Quote=quoteStyle";
  126. $this->_config['content_css'] = "bbcode.css";
  127. $this->_config['entity_encoding'] = "raw";
  128. $this->_config['add_unload_trigger'] = 0;
  129. $this->_config['remove_linebreaks'] = 0;
  130. $this->_config['theme_advanced_buttons1'] = 'bold,italic,underline,undo,redo,link,unlink,image,forecolor,removeformat,cleanup,code';
  131. }
  132. if( $this->_html ) {
  133. $this->_config['theme_advanced_buttons1'] = "fontselect, fontsizeselect, bold, italic, underline, strikethrough,forecolor,backcolor, |, justifyleft, justifycenter, justifyright, justifyfull, |, bullist, numlist, |, outdent, indent, blockquote";
  134. }
  135. if( (!$this->_html) && (!$this->_bbcode) ) {
  136. $this->_config['theme_advanced_buttons1'] = '';
  137. }
  138. }
  139. public function setScriptPath($path)
  140. {
  141. $this->_scriptPath = rtrim($path, '/');
  142. return $this;
  143. }
  144. public function setScriptFile($file)
  145. {
  146. $this->_scriptFile = (string) $file;
  147. }
  148. public function useCompressor($switch)
  149. {
  150. $this->_useCompressor = (bool) $switch;
  151. return $this;
  152. }
  153. public function render()
  154. {
  155. if( false === $this->_enabled ) {
  156. $this->_renderScript();
  157. $this->_renderCompressor();
  158. $this->_renderEditor();
  159. }
  160. $this->_enabled = true;
  161. }
  162. protected function _renderScript()
  163. {
  164. if( null === $this->_scriptFile ) {
  165. $script = $this->view->baseUrl() . '/' . $this->_defaultScript;
  166. } else {
  167. if( null === $this->_scriptPath ) {
  168. $this->_scriptPath = $this->view->baseUrl();
  169. }
  170. $script = $this->_scriptPath . '/' . $this->_scriptFile;
  171. }
  172. $this->view->headScript()->appendFile($script);
  173. return $this;
  174. }
  175. protected function _renderCompressor()
  176. {
  177. if( false === $this->_useCompressor ) {
  178. return;
  179. }
  180. $script = 'tinyMCE_GZ.init({' . PHP_EOL
  181. . 'themes: "' . implode(',', $this->_supportedTheme) . '"' . PHP_EOL
  182. . 'plugins: "' . implode(',', $this->_supportedPlugins) . '"' . PHP_EOL
  183. . 'languages: "' . implode(',', $this->_supportedLanguages) . '"' . PHP_EOL
  184. . 'disk_cache: true' . PHP_EOL
  185. . 'debug: false' . PHP_EOL
  186. . '});';
  187. $this->view->headScript()->appendScript($script);
  188. return $this;
  189. }
  190. protected function _renderEditor()
  191. {
  192. $script = 'tinyMCE.init({' . PHP_EOL;
  193. $length = count($this->_config);
  194. $i = 0;
  195. foreach( $this->_config as $name => $value ) {
  196. if( is_array($value) ) {
  197. $value = implode(',', $value);
  198. }
  199. if( !is_bool($value) ) {
  200. $value = '"' . $value . '"';
  201. }
  202. $script .= $name . ': ' . $value . ($i == $length - 1 ? '' : ',') . PHP_EOL;
  203. $i++;
  204. }
  205. $script .= '});';
  206. $this->view->headScript()->appendScript($script);
  207. return $this;
  208. }
  209. }