wpWidgetFormRichText.class.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. class wpWidgetFormRichText extends sfWidgetFormTextarea {
  3. /**
  4. *
  5. * @param array $options An array of options
  6. * @param array $attributes An array of default HTML attributes
  7. * @see sfWidgetForm
  8. */
  9. protected function configure($options = array(), $attributes = array()) {
  10. $this->addOption('editor', 'tinymce');
  11. $this->addOption('tinymce_options', '');
  12. $this->addOption('tinymce_gzip', false);
  13. $this->addOption('css', false);
  14. if (isset($attributes['cols'])) {
  15. $this->addOption('cols', $attributes['cols']);
  16. }
  17. if (isset($attributes['rows'])) {
  18. $this->addOption('rows', $attributes['rows']);
  19. }
  20. parent::configure($options, $attributes);
  21. }
  22. /**
  23. *
  24. * @param string $name The element name
  25. * @param string $value The value displayed in this widget
  26. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  27. * @param array $errors An array of errors for the field
  28. * @return string An HTML tag string
  29. * @see sfWidgetForm
  30. */
  31. public function render($name, $value = null, $attributes = array(), $errors = array()) {
  32. $editorClass = 'sfRichTextEditor' . $this->toCanonicalCase($this->getOption('editor'));
  33. if (!class_exists($editorClass)) {
  34. throw new sfConfigurationException(sprintf('The rich text editor "%s" does not exist.', $editorClass));
  35. }
  36. $editor = new $editorClass();
  37. if (!in_array('sfRichTextEditor', class_parents($editor))) {
  38. throw new sfConfigurationException(sprintf('The editor "%s" must extend sfRichTextEditor.', $editor));
  39. }
  40. $attributes = array_merge($attributes, $this->getOptions());
  41. $editor->initialize($name, $value, $attributes);
  42. return $editor->toHTML();
  43. }
  44. /**
  45. * Converts a lower-case editor name to its canonical case
  46. *
  47. * @param string $editor
  48. * @return string
  49. */
  50. private function toCanonicalCase($editor) {
  51. switch ($editor) {
  52. case 'tinymce':
  53. return 'TinyMCE';
  54. case 'fck':
  55. return 'FCK';
  56. }
  57. }
  58. }