sfRichTextEditorTinyMCE.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfRichTextEditorTinyMCE implements the TinyMCE rich text editor.
  11. *
  12. * <b>Options:</b>
  13. * - css - Path to the TinyMCE editor stylesheet
  14. *
  15. * <b>Css example:</b>
  16. * <code>
  17. * / * user: foo * / => without spaces. 'foo' is the name in the select box
  18. * .foobar
  19. * {
  20. * color: #f00;
  21. * }
  22. * </code>
  23. *
  24. * @package symfony
  25. * @subpackage helper
  26. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  27. * @version SVN: $Id: sfRichTextEditorTinyMCE.class.php 11783 2008-09-25 16:21:27Z fabien $
  28. */
  29. class sfRichTextEditorTinyMCE extends sfRichTextEditor
  30. {
  31. /**
  32. * Returns the rich text editor as HTML.
  33. *
  34. * @return string Rich text editor HTML representation
  35. */
  36. public function toHTML()
  37. {
  38. $options = $this->options;
  39. // we need to know the id for things the rich text editor
  40. // in advance of building the tag
  41. $id = _get_option($options, 'id', get_id_from_name($this->name, null));
  42. // use tinymce's gzipped js?
  43. $tinymce_file = _get_option($options, 'tinymce_gzip') ? '/tiny_mce_gzip.php' : '/tiny_mce.js';
  44. // tinymce installed?
  45. $js_path = sfConfig::get('sf_rich_text_js_dir') ? '/'.sfConfig::get('sf_rich_text_js_dir').$tinymce_file : '/sf/tinymce/js'.$tinymce_file;
  46. if (!is_readable(sfConfig::get('sf_web_dir').$js_path))
  47. {
  48. throw new sfConfigurationException('You must install TinyMCE to use this helper (see rich_text_js_dir settings).');
  49. }
  50. sfContext::getInstance()->getResponse()->addJavascript($js_path);
  51. require_once dirname(__FILE__).'/JavascriptBaseHelper.php';
  52. $tinymce_options = '';
  53. $style_selector = '';
  54. // custom CSS file?
  55. if ($css_file = _get_option($options, 'css'))
  56. {
  57. $css_path = stylesheet_path($css_file);
  58. sfContext::getInstance()->getResponse()->addStylesheet($css_path);
  59. $css = file_get_contents(sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.$css_path);
  60. $styles = array();
  61. preg_match_all('#^/\*\s*user:\s*(.+?)\s*\*/\s*\015?\012\s*\.([^\s]+)#Smi', $css, $matches, PREG_SET_ORDER);
  62. foreach ($matches as $match)
  63. {
  64. $styles[] = $match[1].'='.$match[2];
  65. }
  66. $tinymce_options .= ' content_css: "'.$css_path.'",'."\n";
  67. $tinymce_options .= ' theme_advanced_styles: "'.implode(';', $styles).'"'."\n";
  68. $style_selector = 'styleselect,separator,';
  69. }
  70. $culture = sfContext::getInstance()->getUser()->getCulture();
  71. $tinymce_js = '
  72. tinyMCE.init({
  73. mode: "exact",
  74. language: "'.strtolower(substr($culture, 0, 2)).'",
  75. elements: "'.$id.'",
  76. plugins: "table,advimage,advlink,flash",
  77. theme: "advanced",
  78. theme_advanced_toolbar_location: "top",
  79. theme_advanced_toolbar_align: "left",
  80. theme_advanced_path_location: "bottom",
  81. theme_advanced_buttons1: "'.$style_selector.'justifyleft,justifycenter,justifyright,justifyfull,separator,bold,italic,strikethrough,separator,sub,sup,separator,charmap",
  82. theme_advanced_buttons2: "bullist,numlist,separator,outdent,indent,separator,undo,redo,separator,link,unlink,image,flash,separator,cleanup,removeformat,separator,code",
  83. theme_advanced_buttons3: "tablecontrols",
  84. extended_valid_elements: "img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name]",
  85. relative_urls: false,
  86. debug: false
  87. '.($tinymce_options ? ','.$tinymce_options : '').'
  88. '.(isset($options['tinymce_options']) ? ','.$options['tinymce_options'] : '').'
  89. });';
  90. if (isset($options['tinymce_options']))
  91. {
  92. unset($options['tinymce_options']);
  93. }
  94. return
  95. content_tag('script', javascript_cdata_section($tinymce_js), array('type' => 'text/javascript')).
  96. content_tag('textarea', $this->content, array_merge(array('name' => $this->name, 'id' => $id), _convert_options($options)));
  97. }
  98. }