BxBaseCmtsServices.php 27 KB

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