BxDolEditor.php 4.7 KB

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