BxEditorModule.php 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 Editor integration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxEditorModule extends BxDolModule
  12. {
  13. public $_aButtons = [];
  14. function __construct(&$aModule)
  15. {
  16. parent::__construct($aModule);
  17. $this->_aButtons = [
  18. 'bold' => ['icon' => 'bold', 'text' => _t('_bx_editor_toolbar_action_bold'), 'command' => 'toggleBold()', 'checkOnSelect' => true],
  19. 'italic' => ['icon' => 'italic', 'text' => _t('_bx_editor_toolbar_action_italic'), 'command' => 'toggleItalic()', 'checkOnSelect' => true],
  20. 'underline' => ['icon' => 'underline', 'text' => _t('_bx_editor_toolbar_action_underline'), 'command' => 'toggleUnderline()', 'checkOnSelect' => true],
  21. 'strike' => ['icon' => 'strikethrough', 'text' => _t('_bx_editor_toolbar_action_strike'), 'command' => 'toggleStrike()', 'checkOnSelect' => true],
  22. 'subscript' => ['icon' => 'subscript', 'text' => _t('_bx_editor_toolbar_action_subscript'), 'command' => 'toggleSubscript()', 'checkOnSelect' => true],
  23. 'superscript' => ['icon' => 'superscript', 'text' => _t('_bx_editor_toolbar_action_superscript'), 'command' => 'toggleSuperscript()', 'checkOnSelect' => true],
  24. 'code' => ['icon' => 'code', 'text' => _t('_bx_editor_toolbar_action_code'), 'command' => 'toggleCode()', 'checkOnSelect' => true],
  25. 'highlight' => ['icon' => 'highlighter', 'text' => _t('_bx_editor_toolbar_action_highlight'), 'command' => 'toggleHighlight()', 'checkOnSelect' => true],
  26. 'indent' => ['icon' => 'indent', 'text' => _t('_bx_editor_toolbar_action_indent'), 'command' => 'setBlockquote()'],
  27. 'outdent' => ['icon' => 'outdent', 'text' => _t('_bx_editor_toolbar_action_outdent'), 'command' => 'unsetBlockquote()'],
  28. 'bulletList' => ['icon' => 'list-ul', 'text' => _t('_bx_editor_toolbar_action_bullet_list'), 'command' => 'toggleBulletList()', 'checkOnSelect' => true],
  29. 'orderedList' => ['icon' => 'list-ol', 'text' => _t('_bx_editor_toolbar_action_ordered_list'), 'command' => 'toggleOrderedList()', 'checkOnSelect' => true],
  30. 'codeBlock' => ['icon' => 'code', 'text' => _t('_bx_editor_toolbar_action_code_block'), 'command' => 'toggleCodeBlock()', 'checkOnSelect' => true],
  31. 'alignLeft' => ['icon' => 'align-left', 'text' => _t('_bx_editor_toolbar_action_align_left'), 'command' => 'setTextAlign("left")'],
  32. 'alignCenter' => ['icon' => 'align-center', 'text' => _t('_bx_editor_toolbar_action_align_center'), 'command' => 'setTextAlign("center")'],
  33. 'alignRight' => ['icon' => 'align-right', 'text' => _t('_bx_editor_toolbar_action_align_right'), 'command' => 'setTextAlign("right")'],
  34. 'alignJustify' => ['icon' => 'align-justify', 'text' => _t('_bx_editor_toolbar_action_align_justify'), 'command' => 'setTextAlign("justify")'],
  35. 'h1' => ['text' => _t('_bx_editor_toolbar_action_h1'), 'command' => 'toggleHeading({ level: 1 })'],
  36. 'h2' => ['text' => _t('_bx_editor_toolbar_action_h2'), 'command' => 'toggleHeading({ level: 2 })'],
  37. 'h3' => ['text' => _t('_bx_editor_toolbar_action_h3'), 'command' => 'toggleHeading({ level: 3 })'],
  38. 'h4' => ['text' => _t('_bx_editor_toolbar_action_h4'), 'command' => 'toggleHeading({ level: 4 })'],
  39. 'link' => ['icon' => 'link', 'text' => _t('_bx_editor_toolbar_action_link'), 'command' => 'bx_ex_editor_add_link(oEditor, oParams)', 'command_ex' => true],
  40. 'image' => ['icon' => 'image', 'text' => _t('_bx_editor_toolbar_action_image'), 'command' => 'bx_ex_editor_add_image(oEditor, oParams.bx_url_uploader)', 'command_ex' => true],
  41. 'embed' => ['icon' => 'share-alt', 'text' => _t('_bx_editor_toolbar_action_embed'), 'command' => 'bx_ex_editor_add_embed(oEditor, oParams)', 'command_ex' => true],
  42. ];
  43. }
  44. public function actionUpload()
  45. {
  46. header('Content-Type: application/json; charset=utf-8');
  47. if (!($oStorage = BxDolStorage::getObjectInstance('bx_editor_files'))) {
  48. echo json_encode(array('error' => '1'));
  49. exit;
  50. }
  51. $iProfileId = bx_get_logged_profile_id();
  52. if (!($iId = $oStorage->storeFileFromForm($_FILES['file'], false, $iProfileId))) {
  53. echo json_encode(array('error' => '1'));
  54. exit;
  55. }
  56. $oStorage->afterUploadCleanup($iId, $iProfileId);
  57. $aFileInfo = $oStorage->getFile($iId);
  58. if ($aFileInfo && in_array($aFileInfo['ext'], array('jpg', 'jpeg', 'jpe', 'png'))) {
  59. $oTranscoder = BxDolTranscoderImage::getObjectInstance('bx_editor_image');
  60. $sUrl = $oTranscoder->getFileUrl($iId);
  61. }
  62. else {
  63. $sUrl = $oStorage->getFileUrlById($iId);
  64. }
  65. echo json_encode(array('link' => $sUrl));
  66. }
  67. }
  68. /** @} */