BxDolEditor.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Standard WYSIWYG editor view.
  11. * @see BxDolEditor::attachEditor
  12. */
  13. define('BX_EDITOR_STANDARD', 1);
  14. /**
  15. * Full WYSIWYG editor view. If not supported by editor, standard view is used.
  16. * @see BxDolEditor::attachEditor
  17. */
  18. define('BX_EDITOR_FULL', 2);
  19. /**
  20. * Mini WYSIWYG editor view. If not supported by editor, standard view is used.
  21. * @see BxDolEditor::attachEditor
  22. */
  23. define('BX_EDITOR_MINI', 3);
  24. /**
  25. * WYSIWYG editors.
  26. *
  27. * Site owner can choose which visual editor can be user on the site.
  28. *
  29. * Default visual editor is stored in 'sys_editor_default' setting option.
  30. *
  31. * @section editor_create Creating the Editor object:
  32. *
  33. *
  34. * Add record to 'sys_objects_editor' table:
  35. *
  36. * - object: name of the editor object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; for example: bx_blogs - custom editor in blogs module.
  37. * - title: title of the editor, displayed in the studio.
  38. * - skin: editor skin, if editor suports custom/multiple skins.
  39. * - override_class_name: user defined class name which is derived from one of base editor classes.
  40. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  41. *
  42. *
  43. * @section example Example of usage
  44. *
  45. * Apply visual editor to textarea:
  46. *
  47. * @code
  48. * echo '<textarea id="my_textarea" rows="20" cols="80">some text here</textarea>'; // print text area element
  49. *
  50. * $oEditor = BxDolEditor::getObjectInstance(); // get default editor object instance
  51. * if ($oEditor) // check if editor is available for using
  52. * echo $oEditor->attachEditor ('#my_textarea', BX_EDITOR_STANDARD); // output HTML which will automatically apply editor to textarea element by its id
  53. * @endcode
  54. *
  55. */
  56. class BxDolEditor extends BxDolFactory implements iBxDolFactoryObject
  57. {
  58. protected $_sObject;
  59. protected $_aObject;
  60. protected $_sConfCustom = '';
  61. protected $_sButtonsCustom = false;
  62. protected $_sPluginsCustom = false;
  63. /**
  64. * Constructor
  65. * @param $aObject array of editor options
  66. */
  67. protected function __construct($aObject)
  68. {
  69. parent::__construct();
  70. $this->_sObject = $aObject['object'];
  71. $this->_aObject = $aObject;
  72. }
  73. /**
  74. * Get editor object instance by object name
  75. * @param $sObject object name
  76. * @return object instance or false on error
  77. */
  78. static public function getObjectInstance($sObject = false, $oTemplate = false)
  79. {
  80. if (!$sObject)
  81. $sObject = getParam('sys_editor_default');
  82. if (isset($GLOBALS['bxDolClasses']['BxDolEditor!'.$sObject]))
  83. return $GLOBALS['bxDolClasses']['BxDolEditor!'.$sObject];
  84. $aObject = BxDolEditorQuery::getEditorObject($sObject);
  85. if (!$aObject || !is_array($aObject))
  86. return false;
  87. if (empty($aObject['override_class_name']))
  88. return false;
  89. $sClass = $aObject['override_class_name'];
  90. if (!empty($aObject['override_class_file']))
  91. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  92. $o = new $sClass($aObject, $oTemplate);
  93. return ($GLOBALS['bxDolClasses']['BxDolEditor!'.$sObject] = $o);
  94. }
  95. /**
  96. * Get object name
  97. */
  98. public function getObjectName ()
  99. {
  100. return $this->_sObject;
  101. }
  102. /**
  103. * Set custom editor configuration options
  104. */
  105. public function setCustomConf ($s)
  106. {
  107. $this->_sConfCustom = $s;
  108. }
  109. /**
  110. * Set custom plugins
  111. */
  112. public function setCustomPlugins ($sPlugins)
  113. {
  114. $this->_sPluginsCustom = $sPlugins;
  115. }
  116. /**
  117. * Set custom toolbar
  118. */
  119. public function setCustomToolbarButtons ($sButtons)
  120. {
  121. $this->_sButtonsCustom = $sButtons;
  122. }
  123. /**
  124. * Get minimal width which is neede for editor for the provided view mode
  125. */
  126. public function getWidth ($iViewMode)
  127. {
  128. // override this function in particular editor class
  129. }
  130. /**
  131. * Attach editor to HTML element, in most cases - textarea.
  132. * @param $sSelector - jQuery selector to attach editor to.
  133. * @param $iViewMode - editor view mode: BX_EDITOR_STANDARD, BX_EDITOR_MINI, BX_EDITOR_FULL
  134. * @param $bDynamicMode - is AJAX mode or not, the HTML with editor area is loaded synamically.
  135. */
  136. public function attachEditor ($sSelector, $iViewMode, $bDynamicMode = false)
  137. {
  138. // override this function in particular editor class
  139. }
  140. /**
  141. * Add css/js files which are needed for editor display and functionality.
  142. */
  143. protected function _addJsCss ($bDynamicMode = false)
  144. {
  145. // override this function in particular editor class
  146. }
  147. /**
  148. * Replace provided markers string.
  149. * @param $s - string to replace markers in
  150. * @param $a - markers array
  151. * @return string with replaces markers
  152. */
  153. protected function _replaceMarkers ($s, $a)
  154. {
  155. if (empty($s) || empty($a) || !is_array($a))
  156. return $s;
  157. return bx_replace_markers($s, $a);
  158. }
  159. }
  160. /** @} */