PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/form-tiny-mce/View/TinyMce.php

https://github.com/juokaz/php-examples
PHP | 145 lines | 121 code | 19 blank | 5 comment | 10 complexity | 6765f6f6dd804d1c4093da7ab26d9e6f MD5 | raw file
  1. <?php
  2. /**
  3. * TinyMce view helper
  4. *
  5. * @original http://steven.macintyre.name/zend-framework-tinymce-view-helper/
  6. */
  7. class App_View_Helper_TinyMce extends Zend_View_Helper_Abstract
  8. {
  9. protected $_enabled = false;
  10. protected $_defaultScript = '/javascript/tiny_mce/tiny_mce.js';
  11. protected $_supported = array(
  12. 'mode' => array('textareas', 'specific_textareas', 'exact', 'none'),
  13. 'theme' => array('simple', 'advanced'),
  14. 'format' => array('html', 'xhtml'),
  15. 'languages' => array('en'),
  16. 'plugins' => array('style', 'layer', 'table', 'save',
  17. 'advhr', 'advimage', 'advlink', 'emotions',
  18. 'iespell', 'insertdatetime', 'preview', 'media',
  19. 'searchreplace', 'print', 'contextmenu', 'paste',
  20. 'directionality', 'fullscreen', 'noneditable', 'visualchars',
  21. 'nonbreaking', 'xhtmlxtras', 'imagemanager', 'filemanager','template'));
  22. protected $_config = array('mode' =>'textareas',
  23. 'theme' => 'simple',
  24. 'element_format' => 'html',
  25. 'gecko_spellcheck' => 'true');
  26. protected $_scriptPath;
  27. protected $_scriptFile;
  28. protected $_useCompressor = false;
  29. public function __set($name, $value)
  30. {
  31. $method = 'set' . $name;
  32. if (!method_exists($this, $method)) {
  33. throw new Zend_View_Exception('Invalid tinyMce property');
  34. }
  35. $this->$method($value);
  36. }
  37. public function __get($name)
  38. {
  39. $method = 'get' . $name;
  40. if (!method_exists($this, $method)) {
  41. throw new Zend_View_Exception('Invalid tinyMce property');
  42. }
  43. return $this->$method();
  44. }
  45. public function setOptions(array $options)
  46. {
  47. $methods = get_class_methods($this);
  48. foreach ($options as $key => $value) {
  49. $method = 'set' . ucfirst($key);
  50. if (in_array($method, $methods)) {
  51. $this->$method($value);
  52. } else {
  53. $this->_config[$key] = $value;
  54. }
  55. }
  56. return $this;
  57. }
  58. public function TinyMce ()
  59. {
  60. return $this;
  61. }
  62. public function setScriptPath ($path)
  63. {
  64. $this->_scriptPath = rtrim($path,'/');
  65. return $this;
  66. }
  67. public function setScriptFile ($file)
  68. {
  69. $this->_scriptFile = (string) $file;
  70. }
  71. public function setUseCompressor ($switch)
  72. {
  73. $this->_useCompressor = (bool) $switch;
  74. return $this;
  75. }
  76. public function render()
  77. {
  78. if (false === $this->_enabled) {
  79. $this->_renderScript();
  80. $this->_renderCompressor();
  81. $this->_renderEditor();
  82. }
  83. $this->_enabled = true;
  84. }
  85. protected function _renderScript ()
  86. {
  87. if (null === $this->_scriptFile) {
  88. $script = $this->_defaultScript;
  89. } else {
  90. $script = $this->_scriptPath . '/' . $this->_scriptFile;
  91. }
  92. $this->view->headScript()->appendFile($this->view->baseUrl() . $script);
  93. return $this;
  94. }
  95. protected function _renderCompressor ()
  96. {
  97. if (false === $this->_useCompressor) {
  98. return;
  99. }
  100. $script = 'tinyMCE_GZ.init({' . PHP_EOL
  101. . 'themes: "' . implode(',', $this->_supported['theme']) . '",' . PHP_EOL
  102. . 'plugins: "'. implode(',', $this->_supported['plugins']) . '",' . PHP_EOL
  103. . 'languages: "' . implode(',', $this->_supported['languages']) . '",' . PHP_EOL
  104. . 'disk_cache: true,' . PHP_EOL
  105. . 'debug: false,' . PHP_EOL
  106. . '});';
  107. $this->view->headScript()->appendScript($script);
  108. return $this;
  109. }
  110. protected function _renderEditor ()
  111. {
  112. $script = 'tinyMCE.init({' . PHP_EOL;
  113. foreach ($this->_config as $name => $value) {
  114. if (is_array($value)) {
  115. $value = implode(',', $value);
  116. }
  117. if (!is_bool($value)) {
  118. $value = '"' . $value . '"';
  119. }
  120. $script .= $name . ': ' . $value . ',' . PHP_EOL;
  121. }
  122. $script .= '});';
  123. $this->view->headScript()->appendScript($script);
  124. return $this;
  125. }
  126. }