BxBaseCmtsServices.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System services related to Comments.
  11. */
  12. class BxBaseCmtsServices extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function serviceGetAuthor($iCmtUniqId)
  19. {
  20. $oCmts = BxDolCmts::getObjectInstanceByUniqId($iCmtUniqId);
  21. if(!$oCmts)
  22. return [];
  23. $aCmt = BxDolCmtsQuery::getCommentExtendedByUniqId($iCmtUniqId);
  24. if(empty($aCmt) || !is_array($aCmt))
  25. return [];
  26. return $oCmts->serviceGetAuthor((int)$aCmt['cmt_id']);
  27. }
  28. public function serviceGetBlockContent($sSystem = '', $iObjectId = 0, $iCommentId = 0)
  29. {
  30. $bIsApi = bx_is_api();
  31. if(empty($sSystem) && ($sSystem = bx_get('sys')) !== false)
  32. $sSystem = bx_process_input($sSystem);
  33. if(empty($iObjectId) && ($iObjectId = bx_get('id')) !== false)
  34. $iObjectId = bx_process_input($iObjectId, BX_DATA_INT);
  35. if(empty($iCommentId) && ($iCommentId = bx_get('cmt_id')) !== false)
  36. $iCommentId = bx_process_input($iCommentId, BX_DATA_INT);
  37. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iObjectId, true);
  38. if(!$oCmts || !$oCmts->isEnabled())
  39. return $bIsApi ? [] : '';
  40. if($bIsApi) {
  41. $aSystem = $oCmts->getSystemInfo();
  42. return [bx_api_get_block('comment_content', [
  43. 'author' => $oCmts->getObjectAuthorId(),
  44. 'module' => $aSystem['module'],
  45. 'title' => bx_is_srv($aSystem['module'], 'get_title') ? bx_srv($aSystem['module'], 'get_title', [$iObjectId]) : '',
  46. 'text' => bx_is_srv($aSystem['module'], 'get_text') ? bx_srv($aSystem['module'], 'get_text', [$iObjectId]) : '',
  47. 'link' => bx_api_get_relative_url($oCmts->getBaseUrl())
  48. ])];
  49. }
  50. return '';
  51. }
  52. public function serviceGetBlockAuthor($sSystem = '', $iObjectId = 0, $iCommentId = 0)
  53. {
  54. if(empty($sSystem) && ($sSystem = bx_get('sys')) !== false)
  55. $sSystem = bx_process_input($sSystem);
  56. if(empty($iObjectId) && ($iObjectId = bx_get('id')) !== false)
  57. $iObjectId = bx_process_input($iObjectId, BX_DATA_INT);
  58. if(empty($iCommentId) && ($iCommentId = bx_get('cmt_id')) !== false)
  59. $iCommentId = bx_process_input($iCommentId, BX_DATA_INT);
  60. if(bx_is_api()) {
  61. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iObjectId, true);
  62. $aCmt = $oCmts->getCommentRow($iCommentId);
  63. return [bx_api_get_block('entity_author', [
  64. 'author_data' => BxDolProfile::getData($aCmt['cmt_author_id']),
  65. 'entry_date' => $aCmt['cmt_time'],
  66. 'menu_manage' => [],
  67. 'text' => bx_process_output(strip_tags($oCmts->getObjectTitle($iObjectId))),
  68. 'url' => bx_api_get_relative_url($oCmts->getBaseUrl())
  69. ])];
  70. }
  71. return '';
  72. }
  73. public function serviceGetInfo($iCmtUniqId, $bSearchableFieldsOnly = true)
  74. {
  75. $oCmts = BxDolCmts::getObjectInstanceByUniqId($iCmtUniqId);
  76. if(!$oCmts)
  77. return [];
  78. $aCmt = BxDolCmtsQuery::getCommentExtendedByUniqId($iCmtUniqId);
  79. if(empty($aCmt) || !is_array($aCmt))
  80. return [];
  81. return $oCmts->serviceGetInfo((int)$aCmt['cmt_id'], $bSearchableFieldsOnly);
  82. }
  83. public function serviceGetBlockView($sSystem = '', $iObjectId = 0, $iCommentId = 0, $iParentsDepthMax = false)
  84. {
  85. $bIsApi = bx_is_api();
  86. if(empty($sSystem) && ($sSystem = bx_get('sys')) !== false)
  87. $sSystem = bx_process_input($sSystem);
  88. if(empty($iObjectId) && ($iObjectId = bx_get('id')) !== false)
  89. $iObjectId = bx_process_input($iObjectId, BX_DATA_INT);
  90. if(empty($iCommentId) && ($iCommentId = bx_get('cmt_id')) !== false)
  91. $iCommentId = bx_process_input($iCommentId, BX_DATA_INT);
  92. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iObjectId, true);
  93. if(!$oCmts)
  94. return $bIsApi ? [] : '';
  95. $iCmtStartFrom = $iCommentId;
  96. if($iParentsDepthMax !== false) {
  97. $aCmtParents = $oCmts->getCommentParents($iCommentId, $iParentsDepthMax);
  98. if(!empty($aCmtParents) && is_array($aCmtParents))
  99. $iCmtStartFrom = array_pop($aCmtParents);
  100. }
  101. if($bIsApi) {
  102. $aRv = $this->serviceGetDataApi([
  103. 'module' => $sSystem,
  104. 'object_id' => $iObjectId,
  105. 'comment_id' => $iCmtStartFrom
  106. ]);
  107. if(isset($aRv['browse']) && is_array($aRv['browse'])) {
  108. $aRv['browse']['data']['total_count'] = 0;
  109. if($iParentsDepthMax !== false)
  110. $aRv['browse']['data']['highlighted'] = $iCommentId;
  111. }
  112. return [$aRv];
  113. }
  114. return $oCmts->getCommentBlock($iCmtStartFrom);
  115. }
  116. public function serviceAlertResponseSysCmtsImagesFileDeleted($oAlert)
  117. {
  118. $iUniqId = (int)$oAlert->aExtras['ghost']['content_id'];
  119. $aCmt = BxDolCmts::getGlobalInfo($iUniqId);
  120. if(empty($aCmt) || !is_array($aCmt))
  121. return;
  122. $oCmts = BxDolCmts::getObjectInstance($aCmt['system_name'], 0, false);
  123. if(!$oCmts)
  124. return;
  125. $oCmts->getQueryObject()->deleteImages($aCmt['system_id'], false, $oAlert->iObject);
  126. }
  127. public function serviceGetMenuItemAddonVote($sSystem, $iId, $iCmtId)
  128. {
  129. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iId);
  130. $oVote = $oCmts->getVoteObject($oCmts->getCommentUniqId($iCmtId));
  131. if($oVote !== false)
  132. return $oVote->getCounter();
  133. return '';
  134. }
  135. public function serviceGetLiveUpdate($sSystem, $iContentId, $iProfileId, $iCount = 0)
  136. {
  137. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iContentId);
  138. if(!$oCmts || !$oCmts->isEnabled())
  139. return false;
  140. $sKey = $oCmts->getNotificationId();
  141. bx_import('BxDolSession');
  142. if((int)BxDolSession::getInstance()->getValue($sKey) == 1)
  143. return false;
  144. $iCountNew = $oCmts->getCommentsCount($iContentId, -1, BX_CMT_FILTER_OTHERS);
  145. if($iCountNew == $iCount)
  146. return false;
  147. return array(
  148. 'count' => $iCountNew, // required
  149. 'method' => $oCmts->getJsObjectName() . '.showLiveUpdate(oData)', // required
  150. 'data' => array(
  151. 'code' => $oCmts->getLiveUpdate($iCount, $iCountNew)
  152. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  153. );
  154. }
  155. public function serviceGetLiveUpdates($sSystem, $iContentId, $iProfileId, $iCount = 0)
  156. {
  157. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iContentId);
  158. if(!$oCmts || !$oCmts->isEnabled())
  159. return false;
  160. $sKey = $oCmts->getNotificationId();
  161. bx_import('BxDolSession');
  162. if((int)BxDolSession::getInstance()->getValue($sKey) == 1)
  163. return false;
  164. $iCountNew = $oCmts->getCommentsCount($iContentId, -1, BX_CMT_FILTER_OTHERS);
  165. if($iCountNew == $iCount)
  166. return false;
  167. return array(
  168. 'count' => $iCountNew, // required
  169. 'method' => $oCmts->getJsObjectName() . '.showLiveUpdates(oData)', // required
  170. 'data' => array(
  171. 'code' => $oCmts->getLiveUpdates($iCount, $iCountNew)
  172. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  173. );
  174. }
  175. public function serviceManageTools($sType = 'common')
  176. {
  177. $oGrid = BxDolGrid::getObjectInstance('sys_cmts_administration');
  178. if(!$oGrid)
  179. return '';
  180. $oTemplate = BxDolTemplate::getInstance();
  181. $oTemplate->addJs(array('BxDolCmtsManageTools.js'));
  182. $oTemplate->addCss(array('cmts_manage_tools.css'));
  183. $oTemplate->addJsTranslation(array('_sys_grid_search'));
  184. return array(
  185. 'content' => $oGrid->getCode()
  186. );
  187. }
  188. public function serviceGetMenuAddonManageTools()
  189. {
  190. $iNumTotal = BxDolCmts::getGlobalNumByParams();
  191. $iNum1 = BxDolCmts::getGlobalNumByParams([[
  192. 'key' => 'status_admin',
  193. 'value' => 'pending',
  194. 'operator' => '='
  195. ]]);
  196. $iNum2 = BxDolCmts::getGlobalNumByParams([[
  197. 'key' => 'reports',
  198. 'value' => '0',
  199. 'operator' => '>'
  200. ]]);
  201. return [
  202. 'counter1_value' => $iNum1,
  203. 'counter1_caption' => _t('_sys_menu_dashboard_manage_tools_addon_counter1_caption_profile_default'),
  204. 'counter2_value' => $iNum2,
  205. 'counter3_value' => $iNumTotal
  206. ];
  207. }
  208. /**
  209. * Comment Added for Timeline module
  210. */
  211. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  212. {
  213. $bCache = true;
  214. $iCmtUniqId = $aEvent['object_id'];
  215. $oCmts = BxDolCmts::getObjectInstanceByUniqId($iCmtUniqId);
  216. if(!$oCmts)
  217. return false;
  218. $aCmt = BxDolCmtsQuery::getCommentExtendedByUniqId($iCmtUniqId);
  219. if(empty($aCmt) || !is_array($aCmt))
  220. return false;
  221. $iCmtId = (int)$aCmt['cmt_id'];
  222. $iUserId = bx_get_logged_profile_id();
  223. $iAuthorId = (int)$aCmt['cmt_author_id'];
  224. $iAuthorIdAbs = abs($iAuthorId);
  225. if($iAuthorId < 0 && ((is_numeric($aEvent['owner_id']) && $iAuthorIdAbs == (int)$aEvent['owner_id']) || (is_array($aEvent['owner_id']) && in_array($iAuthorIdAbs, $aEvent['owner_id']))) && $iAuthorIdAbs != $iUserId)
  226. return false;
  227. $sCmtUrl = $oCmts->getViewUrl($iCmtId);
  228. $sSample = '_cmt_txt_sample_comment_single_with_article';
  229. $sSampleWoArticle = '_cmt_txt_sample_comment_single';
  230. $sAction = '';
  231. $sActionCustom = '_cmt_txt_added_sample_custom';
  232. $aActionCustomMarkers = array(
  233. 'sample' => $sSample,
  234. 'sample_url' => $sCmtUrl,
  235. 'psample' => strip_tags($oCmts->getObjectTitle()),
  236. 'psample_url' => $oCmts->getBaseUrl(),
  237. );
  238. $iOwnerId = $iAuthorIdAbs;
  239. if(isset($aEvent['object_privacy_view']) && (int)$aEvent['object_privacy_view'] < 0)
  240. $iOwnerId = abs($aEvent['object_privacy_view']);
  241. //--- Votes
  242. $aVotes = array();
  243. if(($oVotes = $oCmts->getVoteObject($iCmtUniqId)) !== false)
  244. $aVotes = array(
  245. 'system' => $oVotes->getSystemName(),
  246. 'object_id' => $iCmtUniqId,
  247. 'count' => $aCmt['votes']
  248. );
  249. //--- Reactions
  250. $aReactions = array();
  251. if(($oReactions = $oCmts->getReactionObject($iCmtUniqId)) !== false)
  252. $aReactions = array(
  253. 'system' => $oReactions->getSystemName(),
  254. 'object_id' => $iCmtUniqId,
  255. 'count' => $aCmt['rvotes']
  256. );
  257. //--- Scores
  258. $aScores = array();
  259. if(($oScores = $oCmts->getScoreObject($iCmtUniqId)) !== false)
  260. $aScores = array(
  261. 'system' => $oScores->getSystemName(),
  262. 'object_id' => $iCmtUniqId,
  263. 'score' => $aCmt['score']
  264. );
  265. //--- Reports
  266. $aReports = array();
  267. if(($oReports = $oCmts->getReportObject($iCmtUniqId)) !== false)
  268. $aReports = array(
  269. 'system' => $oReports->getSystemName(),
  270. 'object_id' => $iCmtUniqId,
  271. 'count' => $aCmt['reports']
  272. );
  273. return array(
  274. '_cache' => $bCache,
  275. 'owner_id' => $iOwnerId,
  276. 'object_owner_id' => $iAuthorId,
  277. 'icon' => 'comments',
  278. 'sample' => $sSample,
  279. 'sample_wo_article' => $sSampleWoArticle,
  280. 'sample_action' => $sAction,
  281. 'sample_action_custom' => array(
  282. 'content' => $sActionCustom,
  283. 'markers' => $aActionCustomMarkers
  284. ),
  285. 'url' => $sCmtUrl,
  286. 'content' => array(
  287. 'sample' => $sSample,
  288. 'sample_wo_article' => $sSampleWoArticle,
  289. 'sample_action' => $sAction,
  290. 'url' => $sCmtUrl,
  291. 'title' => '',
  292. 'text' => $oCmts->getViewText($aCmt),
  293. 'images' => array(),
  294. 'images_attach' => array(),
  295. 'videos' => array(),
  296. 'videos_attach' => array(),
  297. 'files' => array(),
  298. 'files_attach' => $oCmts->getAttachments($iCmtId)
  299. ), //a string to display or array to parse default template before displaying.
  300. 'date' => $aCmt['cmt_time'],
  301. 'views' => array(),
  302. 'votes' => $aVotes,
  303. 'reactions' => $aReactions,
  304. 'scores' => $aScores,
  305. 'reports' => $aReports,
  306. 'comments' => array(),
  307. 'title' => !empty($aCmt['cmt_text']) ? $aCmt['cmt_text'] : '', //may be empty.
  308. 'description' => '' //may be empty.
  309. );
  310. }
  311. /**
  312. * Comment added for Notifications module
  313. */
  314. public function serviceGetNotificationsCommentAdded($aEvent)
  315. {
  316. $aCommentGi = BxDolCmts::getGlobalInfo($aEvent['subobject_id']);
  317. if(empty($aCommentGi) || !is_array($aCommentGi))
  318. return [];
  319. $iContentId = (int)$aEvent['object_id'];
  320. $oComment = BxDolCmts::getObjectInstance($aCommentGi['system_name'], $iContentId);
  321. if(!$oComment)
  322. return [];
  323. $aCommentSystem = $oComment->getSystemInfo();
  324. $oContentModule = BxDolModule::getInstance($aCommentSystem['module']);
  325. if(!$oContentModule)
  326. return [];
  327. $aContentInfo = $oContentModule->_oDb->getContentInfoById($iContentId);
  328. if(empty($aContentInfo) || !is_array($aContentInfo))
  329. return [];
  330. $CNF = &$oContentModule->_oConfig->CNF;
  331. if(empty($CNF['FIELD_ID']) || empty($CNF['FIELD_AUTHOR']) || !(isset($CNF['FIELD_TITLE']) || isset($CNF['FIELD_TEXT'])) || empty($CNF['URI_VIEW_ENTRY']))
  332. return [];
  333. $sEntryUrl = '{bx_url_root}' . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  334. $sEntryCaption = isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...');
  335. $sEntrySummary = isset($aContentInfo[$CNF['FIELD_TEXT']]) ? $aContentInfo[$CNF['FIELD_TEXT']] : '';
  336. $iCommentId = (int)$aCommentGi['cmt_id'];
  337. return [
  338. 'entry_sample' => $CNF['T']['txt_sample_single'],
  339. 'entry_url' => $sEntryUrl,
  340. 'entry_caption' => $sEntryCaption,
  341. 'entry_summary' => $sEntrySummary,
  342. 'subentry_sample' => $CNF['T']['txt_sample_comment_single'],
  343. 'subentry_url' => $oComment->getItemUrl($iCommentId, '{bx_url_root}'),
  344. 'subentry_url_api' => $oComment->getItemUrlApi($iCommentId, '{bx_url_root}'),
  345. 'subentry_summary' => $oComment->getViewText($iCommentId),
  346. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  347. ];
  348. }
  349. /**
  350. * Comment vote for Notifications module
  351. */
  352. public function serviceGetNotificationsVote($aEvent)
  353. {
  354. $iCmtIdUnique = (int)$aEvent['object_id'];
  355. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  356. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  357. return array();
  358. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  359. if(!$oCmts || !$oCmts->isEnabled())
  360. return array();
  361. $oVote = $oCmts->getVoteObject($iCmtIdUnique);
  362. if(!$oVote)
  363. return array();
  364. $iCmtId = (int)$aCmtInfo['cmt_id'];
  365. $sCmtUrl = str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oCmts->serviceGetLink($iCmtId));
  366. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  367. $sCmtUrlApi = '';
  368. if(bx_is_api() && getParam('sys_api_comment_notif_link_content') == 'on') {
  369. $aCmt = $oCmts->getCommentsBy(['type' => 'uniq_id', 'uniq_id' => $iCmtIdUnique]);
  370. if(!empty($aCmt) && is_array($aCmt)) {
  371. $oCmts->init($aCmt['cmt_object_id']);
  372. $sCmtUrlApi = $oCmts->getBaseUrl('{bx_url_root}') . '#cid=' . $iCmtId;
  373. }
  374. }
  375. return array(
  376. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  377. 'entry_url' => $sCmtUrl,
  378. 'entry_url_api' => $sCmtUrlApi,
  379. 'entry_caption' => $sCmtCaption,
  380. 'entry_author' => $aCmtInfo['author_id'],
  381. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_vote_single'),
  382. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  383. );
  384. }
  385. /**
  386. * Comment reaction for Notifications module
  387. */
  388. public function serviceGetNotificationsReaction($aEvent)
  389. {
  390. $iCmtIdUnique = (int)$aEvent['object_id'];
  391. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  392. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  393. return array();
  394. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  395. if(!$oCmts || !$oCmts->isEnabled())
  396. return array();
  397. $oReaction = $oCmts->getReactionObject($iCmtIdUnique);
  398. if(!$oReaction)
  399. return array();
  400. $aSubentry = $oReaction->getTrackBy(array('type' => 'id', 'id' => (int)$aEvent['subobject_id']));
  401. if(empty($aSubentry) || !is_array($aSubentry))
  402. return array();
  403. $aSubentrySampleParams = array();
  404. $aReaction = $oReaction->getReaction($aSubentry['reaction']);
  405. if(!empty($aReaction['title']))
  406. $aSubentrySampleParams[] = $aReaction['title'];
  407. else
  408. $aSubentrySampleParams[] = '_undefined';
  409. $iCmtId = (int)$aCmtInfo['cmt_id'];
  410. $sCmtUrl = str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oCmts->serviceGetLink($iCmtId));
  411. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  412. $sCmtUrlApi = '';
  413. if(bx_is_api() && getParam('sys_api_comment_notif_link_content') == 'on') {
  414. $aCmt = $oCmts->getCommentsBy(['type' => 'uniq_id', 'uniq_id' => $iCmtIdUnique]);
  415. if(!empty($aCmt) && is_array($aCmt)) {
  416. $oCmts->init($aCmt['cmt_object_id']);
  417. $sCmtUrlApi = $oCmts->getBaseUrl('{bx_url_root}') . '#cid=' . $iCmtId;
  418. }
  419. }
  420. return array(
  421. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  422. 'entry_url' => $sCmtUrl,
  423. 'entry_url_api' => $sCmtUrlApi,
  424. 'entry_caption' => $sCmtCaption,
  425. 'entry_author' => $aCmtInfo['author_id'],
  426. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_reaction_single'),
  427. 'subentry_sample_params' => $aSubentrySampleParams,
  428. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  429. );
  430. }
  431. /**
  432. * Comment score -> vote up for Notifications module
  433. */
  434. public function serviceGetNotificationsScoreUp($aEvent)
  435. {
  436. return $this->_serviceGetNotificationsScore('up', $aEvent);
  437. }
  438. /**
  439. * Comment score -> vote down for Notifications module
  440. */
  441. public function serviceGetNotificationsScoreDown($aEvent)
  442. {
  443. return $this->_serviceGetNotificationsScore('down', $aEvent);
  444. }
  445. protected function _serviceGetNotificationsScore($sType, $aEvent)
  446. {
  447. $iCmtIdUnique = (int)$aEvent['object_id'];
  448. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  449. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  450. return array();
  451. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  452. if(!$oCmts || !$oCmts->isEnabled())
  453. return array();
  454. $oScore = $oCmts->getScoreObject($iCmtIdUnique);
  455. if(!$oScore)
  456. return array();
  457. $iCmtId = (int)$aCmtInfo['cmt_id'];
  458. $sCmtUrl = str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oCmts->serviceGetLink($iCmtId));
  459. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  460. $sCmtUrlApi = '';
  461. if(bx_is_api() && getParam('sys_api_comment_notif_link_content') == 'on') {
  462. $aCmt = $oCmts->getCommentsBy(['type' => 'uniq_id', 'uniq_id' => $iCmtIdUnique]);
  463. if(!empty($aCmt) && is_array($aCmt)) {
  464. $oCmts->init($aCmt['cmt_object_id']);
  465. $sCmtUrlApi = $oCmts->getBaseUrl('{bx_url_root}') . '#cid=' . $iCmtId;
  466. }
  467. }
  468. return array(
  469. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  470. 'entry_url' => $sCmtUrl,
  471. 'entry_url_api' => $sCmtUrlApi,
  472. 'entry_caption' => $sCmtCaption,
  473. 'entry_author' => $aCmtInfo['author_id'],
  474. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_score_' . $sType . '_single'),
  475. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  476. );
  477. }
  478. /**
  479. * @page service Service Calls
  480. * @section bx_system_cmts System Services
  481. * @subsection bx_system_cmts-general General
  482. * @subsubsection bx_system_cmts-get_data_api get_data_api
  483. *
  484. * @code bx_srv('system', 'get_data_api', [], 'TemplCmtsServices'); @endcode
  485. *
  486. * Get comments data
  487. * @param $aParams array with paramenters :
  488. * "module":"bx_posts","object_id":3,"start_from":5,"order_way":"desc","is_form":false
  489. *
  490. * @see TemplCmtsServices::serviceGetDataApi
  491. */
  492. /**
  493. * @ref bx_system_cmts-get_data_api "get_data_api"
  494. * @api @ref bx_system_cmts-get_data_api "get_data_api"
  495. */
  496. public function serviceGetDataApi($aParams)
  497. {
  498. if(is_string($aParams))
  499. $aParams = json_decode($aParams, true);
  500. if (isset($aParams['action']) && isset($aParams['id']) && $aParams['action'] == 'remove'){
  501. $iCmtId = $aParams['id'];
  502. $oCmts = BxDolCmts::getObjectInstance($aParams['module'], $aParams['object_id']);
  503. $oCmts->remove($iCmtId);
  504. return 'Ok';
  505. }
  506. if (isset($aParams['action']) && isset($aParams['id']) && $aParams['action'] == 'edit'){
  507. $iCmtId = $aParams['id'];
  508. $oCmts = BxDolCmts::getObjectInstance($aParams['module'], $aParams['object_id']);
  509. $oForm = $oCmts->getFormEdit($iCmtId);
  510. if($oForm['form']->isSubmittedAndValid()){
  511. $aParams['comment_id'] = $oForm['res'];
  512. $aRv['browse'] = $this->serviceGetCommentsApi($oCmts, $aParams);
  513. return $aRv;
  514. }
  515. $aForm = $oForm['form']->getCodeAPI();
  516. $aForm['inputs']['cmt_text']['numLines'] = 1;
  517. $aForm['inputs']['cmt_text']['autoheight'] = true;
  518. if (isset($aForm['inputs']['cmt_controls'])){
  519. $aForm['inputs']['cmt_submit'] = $aForm['inputs']['cmt_controls'][0];
  520. unset($aForm['inputs']['cmt_controls']);
  521. }
  522. $aRv['form'] = ['id' => 'cmt_form', 'type' => 'form', 'name' => 'comment', 'data' => $aForm, 'request' => ['immutable' => true]];
  523. return $aRv;
  524. }
  525. $aParams['parent_id'] = !isset($aParams['parent_id']) ? 0 : $aParams['parent_id'];
  526. $aParams['start_from'] = !isset($aParams['start_from']) ? 0 : $aParams['start_from'];
  527. $aParams['order_way'] = !isset($aParams['order_way']) ? 'asc' : $aParams['order_way'];
  528. $aParams['is_form'] = !isset($aParams['is_form']) ? true : $aParams['is_form'];
  529. $aParams['insert'] = 'before';
  530. $oCmts = BxDolCmts::getObjectInstance($aParams['module'], $aParams['object_id']);
  531. if (!$oCmts || !$oCmts->isEnabled())
  532. return false;
  533. $aRv = [
  534. 'id' => '1',
  535. 'url' => '/api.php?r=system/get_data_api/TemplCmtsServices/&params[]=',
  536. 'type' => 'comments'
  537. ];
  538. $oForm = $oCmts->getFormPost($aParams['parent_id']);
  539. $bIsList = false;
  540. if (isset($oForm['form']) && $aParams['is_form']){
  541. $aForm = $oForm['form']->getCodeAPI();
  542. $aForm['inputs']['cmt_text']['numLines'] = 1;
  543. $aForm['inputs']['cmt_text']['autoheight'] = true;
  544. $aForm['inputs']['cmt_submit']['icon'] = 'contact';
  545. $aForm['inputs']['cmt_submit']['variant'] = 'text';
  546. $aForm['inputs']['cmt_submit']['icon_only'] = true;
  547. // add view (form + new comment)
  548. if($oForm['form']->isSubmittedAndValid()){
  549. $sInsert = $aParams['order_way'] == 'desc' ? 'after' : 'before';
  550. if (bx_get('cmt_parent_id') > 0)
  551. $sInsert = 'after';
  552. $aParams['insert'] = $sInsert;
  553. $aParams['comment_id'] = $oForm['res'];
  554. $bIsList = true;
  555. $aForm['inputs']['cmt_parent_id']['value'] = 0;
  556. }
  557. if (bx_get_logged_profile_id())
  558. $aRv['form'] = ['id' => 'cmt_form', 'type' => 'form', 'name' => 'comment', 'data' => $aForm, 'request' => ['immutable' => true]];
  559. // default view (form + list)
  560. if (!$oForm['form']->isSubmitted()){
  561. $bIsList = true;
  562. }
  563. }
  564. else{
  565. // list only view
  566. $bIsList = true;
  567. $aParams['insert'] = 'before';
  568. }
  569. if ($bIsList)
  570. $aRv['browse'] = $this->serviceGetCommentsApi($oCmts, $aParams);
  571. return $aRv;
  572. }
  573. public function serviceGetCommentsApi($oCmts, $aParams)
  574. {
  575. $mixedResult = $oCmts->isViewAllowed();
  576. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  577. return $mixedResult; // TODO: error checking
  578. if (isset($aParams['comment_id']))
  579. $aParams['comment_id'] = explode(',', $aParams['comment_id']);
  580. $aBp = !isset($aParams['aBp']) ? [] : $aParams['aBp'];
  581. $aDp = ['in_designbox' => false, 'show_empty' => false];
  582. $aDp['type'] = !getParam('sys_api_comments_flat') ? 'threaded' : 'flat';
  583. $aBp['type'] = 'head';
  584. $oCmts->getParams($aBp, $aDp);
  585. $oCmts->prepareParams($aBp, $aDp);
  586. $sOrderWay = !empty($aParams['order_way']) ? $aParams['order_way'] : 'desc';
  587. $aBp['order']['way'] = $sOrderWay;
  588. $aBp['order_way'] = $sOrderWay;
  589. $aBp['start'] = !empty($aParams['start_from']) ? (int)$aParams['start_from'] : 0;
  590. $aPp = $aBp['per_view'];
  591. $aBp['per_view'] = $aBp['per_view'] + 1;
  592. if (isset($aParams['per_view'])){
  593. $aBp['per_view'] = $aParams['per_view'];
  594. }
  595. $aCmts = isset($aParams['comment_id']) ? array_map(function($value) {return ['cmt_id' => $value];}, $aParams['comment_id']) : $oCmts->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp[($aBp['init_view'] != -1 ? 'init' : 'per') . '_view']);
  596. $aParams['start_from'] = 0;
  597. if (count($aCmts) == $aBp['per_view']){
  598. $aBp['per_view'] = $aPp;
  599. $aCmts = array_slice($aCmts, 0, $aBp['per_view']);
  600. $aParams['start_from'] = $aBp['start'] + $aBp['per_view'];
  601. }
  602. $aCmtsRv = [];
  603. foreach ($aCmts as $aCmt) {
  604. $aBp['order_way'] = 'asc';
  605. $aCmts = $oCmts->getCommentStructure($aCmt['cmt_id'], $aBp, $aDp);
  606. if($aCmts === false)
  607. continue;
  608. if(($aKeys = array_keys($aCmts)) && ($sKey = array_shift($aKeys)) && $aCmts[$sKey]['data']['cmt_parent_id'] > 0) {
  609. $aParent = $oCmts->getCommentSimple((int)$aCmts[$sKey]['data']['cmt_parent_id']);
  610. $aCmts[$sKey]['data']['cmt_parent'] = [
  611. 'data' => $aParent,
  612. 'author_data' => BxDolProfile::getData($aParent['cmt_author_id'])
  613. ];
  614. }
  615. $aCmtsRv[] = $aCmts;
  616. }
  617. $aData = [
  618. 'unit' => 'comments',
  619. 'start' => $aParams['start_from'],
  620. 'count' => count($aCmts),
  621. 'per_view' => $aBp['per_view'],
  622. 'total_count' => $oCmts->getCommentsCount(),
  623. 'order' => $sOrderWay,
  624. 'view' => $aDp['type'],
  625. 'module' => $oCmts->getSystemName(),
  626. 'object_id' => $oCmts->getId(),
  627. 'max_level' => $oCmts->getSystemInfo()['number_of_levels'],
  628. 'highlighted' => 0,
  629. 'data' => $aCmtsRv,
  630. ];
  631. if (isset($aParams['mode']) && $aParams['mode'] == 'feed')
  632. return $aData;
  633. $aRv = [
  634. 'id' => 'cmt_list',
  635. 'type' => 'browse',
  636. 'insert' => !empty($aParams['insert']) ? $aParams['insert'] : 'before',
  637. 'data' => $aData
  638. ];
  639. if (isset($aParams['comment_id'])){
  640. $aRv['new'] = $aParams['comment_id'];
  641. }
  642. return $aRv;
  643. }
  644. }
  645. /** @} */