PageRenderTime 102ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/php/html/formvalidator/form_validator_html_editor_options.class.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 266 lines | 160 code | 32 blank | 74 comment | 8 complexity | ac98adb9b78fa049c7c97b40761e4feb MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. namespace common\libraries;
  3. use admin\AdminDataManager;
  4. /**
  5. * The combination of options available for the FormValidatorHtmlEditor
  6. * Should be implemented for each specific editor to translate the generic option values
  7. *
  8. * @author Scaramanga
  9. */
  10. abstract class FormValidatorHtmlEditorOptions
  11. {
  12. /**
  13. * @var Array The array containing all the options
  14. */
  15. private $options;
  16. /**
  17. * The name of the toolbar set e.g. Basic, Wiki, Assessment
  18. */
  19. const OPTION_TOOLBAR = 'toolbar';
  20. /**
  21. * Name of the language to be used for the editor
  22. */
  23. const OPTION_LANGUAGE = 'language';
  24. /**
  25. * Name of the theme to be used for the editor
  26. */
  27. const OPTION_THEME = 'theme';
  28. /**
  29. * The width of the editor in pixels or percent
  30. */
  31. const OPTION_WIDTH = 'width';
  32. /**
  33. * The height of the editor in pixels
  34. */
  35. const OPTION_HEIGHT = 'height';
  36. /**
  37. * Whether or not the toolbar should be collapse by default
  38. */
  39. const OPTION_COLLAPSE_TOOLBAR = 'collapse_toolbar';
  40. /**
  41. * Path to the editors configuration file
  42. */
  43. const OPTION_CONFIGURATION = 'configuration';
  44. /**
  45. * Whether or not the content of the editor should be treated as a standalone page
  46. */
  47. const OPTION_FULL_PAGE = 'full_page';
  48. /**
  49. * Path to available templates for the editor
  50. */
  51. const OPTION_TEMPLATES = 'templates';
  52. /**
  53. * @param Array $options
  54. */
  55. function __construct($options)
  56. {
  57. $this->options = $options;
  58. $this->set_defaults();
  59. }
  60. /**
  61. * Returns the names of all available options
  62. *
  63. * @return Array The option names
  64. */
  65. function get_option_names()
  66. {
  67. return array(self :: OPTION_COLLAPSE_TOOLBAR, self :: OPTION_CONFIGURATION, self :: OPTION_FULL_PAGE,
  68. self :: OPTION_HEIGHT, self :: OPTION_LANGUAGE, self :: OPTION_TEMPLATES, self :: OPTION_THEME,
  69. self :: OPTION_TOOLBAR, self :: OPTION_WIDTH);
  70. }
  71. /**
  72. * Gets all options
  73. *
  74. * @return Array The options
  75. */
  76. function get_options()
  77. {
  78. return $this->options;
  79. }
  80. /**
  81. * Set the options
  82. *
  83. * @param Array $options
  84. */
  85. function set_options($options)
  86. {
  87. $this->options = $options;
  88. }
  89. /**
  90. * Get a specific option's value or null if the option isn't set
  91. *
  92. * @return mixed the option's value
  93. */
  94. function get_option($variable)
  95. {
  96. if (isset($this->options[$variable]))
  97. {
  98. return $this->options[$variable];
  99. }
  100. else
  101. {
  102. return null;
  103. }
  104. }
  105. /**
  106. * Sets a specific option
  107. *
  108. * @param String $variable
  109. * @param mixed $value
  110. */
  111. function set_option($variable, $value)
  112. {
  113. $this->options[$variable] = $value;
  114. }
  115. function get_mapping()
  116. {
  117. return array_combine($this->get_option_names(), $this->get_option_names());
  118. }
  119. /**
  120. * Process the generic options into editor specific ones
  121. */
  122. function render_options()
  123. {
  124. $javascript = array();
  125. $available_options = $this->get_option_names();
  126. $mapping = $this->get_mapping();
  127. foreach ($available_options as $available_option)
  128. {
  129. if (key_exists($available_option, $mapping))
  130. {
  131. $value = $this->get_option($available_option);
  132. if (isset($value))
  133. {
  134. $processing_function = 'process_' . $available_option;
  135. if (method_exists($this, $processing_function))
  136. {
  137. $value = call_user_func(array($this, $processing_function), $value);
  138. }
  139. $javascript[] = ' ' . $mapping[$available_option] . ' : ' . $this->format_for_javascript($value);
  140. }
  141. }
  142. }
  143. return implode(",\n", $javascript);
  144. }
  145. function set_defaults()
  146. {
  147. $available_options = $this->get_option_names();
  148. foreach ($available_options as $available_option)
  149. {
  150. $value = $this->get_option($available_option);
  151. if (! isset($value))
  152. {
  153. switch ($available_option)
  154. {
  155. case self :: OPTION_THEME :
  156. // $this->set_option($available_option, 'v2');
  157. $this->set_option($available_option, Theme :: get_theme());
  158. break;
  159. case self :: OPTION_LANGUAGE :
  160. $editor_lang = Translation :: get_language();
  161. $this->set_option($available_option, $editor_lang);
  162. break;
  163. case self :: OPTION_TOOLBAR :
  164. $this->set_option($available_option, 'Basic');
  165. break;
  166. case self :: OPTION_COLLAPSE_TOOLBAR :
  167. $this->set_option($available_option, false);
  168. break;
  169. case self :: OPTION_WIDTH :
  170. $this->set_option($available_option, 595);
  171. break;
  172. case self :: OPTION_HEIGHT :
  173. $this->set_option($available_option, 200);
  174. break;
  175. case self :: OPTION_FULL_PAGE :
  176. $this->set_option($available_option, false);
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. function format_for_javascript($value)
  183. {
  184. if (is_bool($value))
  185. {
  186. if ($value === true)
  187. {
  188. return 'true';
  189. }
  190. else
  191. {
  192. return 'false';
  193. }
  194. }
  195. elseif (is_int($value))
  196. {
  197. return $value;
  198. }
  199. elseif (is_array($value))
  200. {
  201. $elements = array();
  202. foreach ($value as $element)
  203. {
  204. $elements[] = self :: format_for_javascript($element);
  205. }
  206. return '[' . implode(',', $elements) . ']';
  207. }
  208. else
  209. {
  210. return '\'' . $value . '\'';
  211. }
  212. }
  213. /**
  214. * @param String $type
  215. * @param Array $options
  216. * @return FormValidatorHtmlEditorOptions
  217. */
  218. public static function factory($type, $options = array())
  219. {
  220. $file = dirname(__FILE__) . '/html_editor_options/' . $type . '_html_editor_options.class.php';
  221. $class = __NAMESPACE__ . '\\' . 'FormValidator' . Utilities :: underscores_to_camelcase($type) . 'HtmlEditorOptions';
  222. if (file_exists($file))
  223. {
  224. require_once ($file);
  225. return new $class($options);
  226. }
  227. else
  228. {
  229. return null;
  230. }
  231. }
  232. }
  233. ?>