PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/ui/lib/timeline/comment.php

https://gitlab.com/alexprowars/bitrix
PHP | 241 lines | 214 code | 27 blank | 0 comment | 12 complexity | af9e593fc6ea92c61623efb4e4110bbf MD5 | raw file
  1. <?php
  2. namespace Bitrix\UI\Timeline;
  3. use Bitrix\Main\Engine\Response\Component;
  4. use Bitrix\Main\Loader;
  5. use Bitrix\Main\ModuleManager;
  6. use Bitrix\Main\UserTable;
  7. use Bitrix\Rpa\Driver;
  8. class Comment
  9. {
  10. protected $filesUserFieldEntityId;
  11. protected $filesUserFieldName;
  12. public function __construct(string $filesUserFieldEntityId, string $filesUserFieldName)
  13. {
  14. $this->filesUserFieldEntityId = $filesUserFieldEntityId;
  15. $this->filesUserFieldName = $filesUserFieldName;
  16. }
  17. public function getVisualEditorResponse(string $name, int $id = 0, string $text = ''): Component
  18. {
  19. $formId = 'ui-timeline-comment-'.$name;
  20. $fileFields = $this->getFileUserFields($id);
  21. $isUploadFilesAvailable = isset($fileFields[$this->filesUserFieldName]);
  22. $params = [
  23. 'SELECTOR_VERSION' => 2,
  24. 'FORM_ID' => $formId,
  25. 'SHOW_MORE' => 'N',
  26. 'PARSER' => [
  27. 'Bold', 'Italic', 'Underline', 'Strike',
  28. 'ForeColor', 'FontList', 'FontSizeList', 'RemoveFormat',
  29. 'Quote', 'Code', 'InsertCut',
  30. 'CreateLink', 'Image', 'Table', 'Justify',
  31. 'InsertOrderedList', 'InsertUnorderedList',
  32. 'SmileList', 'Source', 'UploadImage', 'InputVideo', 'MentionUser'
  33. ],
  34. 'BUTTONS' => [
  35. ($isUploadFilesAvailable ? 'UploadImage' : null),
  36. 'CreateLink',
  37. 'InputVideo',
  38. 'Quote',
  39. 'MentionUser'
  40. ],
  41. 'TEXT' => [
  42. 'NAME' => 'MESSAGE',
  43. 'VALUE' => $text,
  44. 'HEIGHT' => '120px'
  45. ],
  46. 'LHE' => [
  47. 'id' => $name,
  48. 'documentCSS' => 'body {color:#434343;background:#F7FBE9}',
  49. 'jsObjName' => $name,
  50. 'width' => '100%',
  51. 'minBodyWidth' => '100%',
  52. 'normalBodyWidth' => '100%',
  53. 'height' => 100,
  54. 'minBodyHeight' => 100,
  55. 'showTaskbars' => false,
  56. 'showNodeNavi' => false,
  57. 'autoResize' => true,
  58. 'autoResizeOffset' => 50,
  59. 'bbCode' => true,
  60. 'saveOnBlur' => false,
  61. 'bAllowPhp' => false,
  62. 'lazyLoad' => true,
  63. 'limitPhpAccess' => false,
  64. 'setFocusAfterShow' => true,
  65. 'askBeforeUnloadPage' => false,
  66. 'useFileDialogs' => false,
  67. 'controlsMap' => [
  68. ['id' => 'Bold', 'compact' => true, 'sort' => 10],
  69. ['id' => 'Italic', 'compact' => true, 'sort' => 20],
  70. ['id' => 'Underline', 'compact' => true, 'sort' => 30],
  71. ['id' => 'Strikeout', 'compact' => true, 'sort' => 40],
  72. ['id' => 'RemoveFormat', 'compact' => true, 'sort' => 50],
  73. ['id' => 'Color', 'compact' => true, 'sort' => 60],
  74. ['id' => 'FontSelector', 'compact' => false, 'sort' => 70],
  75. ['id' => 'FontSize', 'compact' => false, 'sort' => 80],
  76. ['separator' => true, 'compact' => false, 'sort' => 90],
  77. ['id' => 'OrderedList', 'compact' => true, 'sort' => 100],
  78. ['id' => 'UnorderedList', 'compact' => true, 'sort' => 110],
  79. ['id' => 'AlignList', 'compact' => false, 'sort' => 120],
  80. ['separator' => true, 'compact' => false, 'sort' => 130],
  81. ['id' => 'InsertLink', 'compact' => true, 'sort' => 140, 'wrap' => 'bx-b-link-'.$formId],
  82. ['id' => 'InsertImage', 'compact' => false, 'sort' => 150],
  83. ['id' => 'InsertVideo', 'compact' => true, 'sort' => 160, 'wrap' => 'bx-b-video-'.$formId],
  84. ['id' => 'InsertTable', 'compact' => false, 'sort' => 170],
  85. ['id' => 'Code', 'compact' => true, 'sort' => 180],
  86. ['id' => 'Quote', 'compact' => true, 'sort' => 190, 'wrap' => 'bx-b-quote-'.$formId],
  87. ['separator' => true, 'compact' => false, 'sort' => 200],
  88. ['id' => 'BbCode', 'compact' => true, 'sort' => 220],
  89. ['id' => 'More', 'compact' => true, 'sort' => 230],
  90. ],
  91. ],
  92. 'USE_CLIENT_DATABASE' => 'Y',
  93. 'FILES' => [
  94. 'VALUE' => [],
  95. 'DEL_LINK' => '',
  96. 'SHOW' => 'N'
  97. ],
  98. 'UPLOAD_FILE' => $isUploadFilesAvailable,
  99. 'UPLOAD_FILE_PARAMS' => ['width' => 400, 'height' => 400],
  100. 'UPLOAD_WEBDAV_ELEMENT' => $fileFields[$this->filesUserFieldName] ?? false,
  101. ];
  102. return new Component('bitrix:main.post.form', '', $params);
  103. }
  104. public function saveFiles(int $id, array $files): bool
  105. {
  106. $manager = $this->getUserFieldManager();
  107. if($manager)
  108. {
  109. $data = [
  110. $this->filesUserFieldName => $files,
  111. ];
  112. if ($manager->CheckFields($this->filesUserFieldEntityId, $id, $data))
  113. {
  114. return (bool) $manager->Update($this->filesUserFieldEntityId, $id, $data);
  115. }
  116. }
  117. return false;
  118. }
  119. public function getFileUserFields(int $id = 0): array
  120. {
  121. $manager = $this->getUserFieldManager();
  122. if($manager && ModuleManager::isModuleInstalled('disk'))
  123. {
  124. $fileFields = $manager->GetUserFields($this->filesUserFieldEntityId, $id);
  125. if(isset($fileFields[$this->filesUserFieldName]))
  126. {
  127. $fileFields[$this->filesUserFieldName]['~EDIT_FORM_LABEL'] = $this->filesUserFieldName;
  128. $fileFields[$this->filesUserFieldName]['TAG'] = 'DOCUMENT ID';
  129. }
  130. return $fileFields;
  131. }
  132. return [];
  133. }
  134. public function getFilesContentResponse(int $id): ?Component
  135. {
  136. $fileFields = $this->getFileUserFields($id);
  137. if($fileFields && !empty($fileFields[$this->filesUserFieldName]['VALUE']))
  138. {
  139. return new Component('bitrix:system.field.view',
  140. $fileFields[$this->filesUserFieldName]["USER_TYPE"]["USER_TYPE_ID"],
  141. [
  142. "PUBLIC_MODE" => false,
  143. "ENABLE_AUTO_BINDING_VIEWER" => true,
  144. "LAZYLOAD" => 'Y',
  145. 'arUserField' => $fileFields[$this->filesUserFieldName],
  146. ]
  147. );
  148. }
  149. return null;
  150. }
  151. public function sendMentions(
  152. int $id,
  153. int $fromUserId,
  154. string $text,
  155. string $message,
  156. array $previouslyMentionedUserIds = []
  157. ): array
  158. {
  159. $messageIds = [];
  160. $parser = new CommentParser();
  161. $mentionedUserIds = $parser->getMentionedUserIds($text);
  162. $mentionedUserIds = array_filter(
  163. $mentionedUserIds,
  164. static function($userId) use ($fromUserId, $previouslyMentionedUserIds) {
  165. $userId = (int)$userId;
  166. return (
  167. $userId !== $fromUserId
  168. && !in_array($userId, $previouslyMentionedUserIds, true)
  169. );
  170. }
  171. );
  172. if(empty($mentionedUserIds))
  173. {
  174. return $messageIds;
  175. }
  176. if(!Loader::includeModule('im'))
  177. {
  178. return $messageIds;
  179. }
  180. foreach ($mentionedUserIds as $userId)
  181. {
  182. $userId = (int)$userId;
  183. $messageIds[] = \CIMNotify::Add([
  184. 'TO_USER_ID' => $userId,
  185. 'FROM_USER_ID' => $fromUserId,
  186. 'NOTIFY_TYPE' => IM_NOTIFY_FROM,
  187. 'NOTIFY_MODULE' => Driver::MODULE_ID,
  188. 'NOTIFY_TAG' => 'RPA|MESSAGE_TIMELINE_MENTION|' . $id,
  189. 'NOTIFY_MESSAGE' => $message,
  190. ]);
  191. }
  192. return $messageIds;
  193. }
  194. public function getUserGenderSuffix(int $userId): string
  195. {
  196. $userData = UserTable::getList([
  197. 'select' => ['PERSONAL_GENDER'],
  198. 'filter' => [
  199. '=ID' => $userId,
  200. ],
  201. 'limit' => 1,
  202. ])->fetch();
  203. if($userData && !empty($userData['PERSONAL_GENDER']))
  204. {
  205. return '_'.$userData['PERSONAL_GENDER'];
  206. }
  207. return '';
  208. }
  209. protected function getUserFieldManager(): ?\CUserTypeManager
  210. {
  211. global $USER_FIELD_MANAGER;
  212. return $USER_FIELD_MANAGER;
  213. }
  214. }