1
0

BxBaseCmtsServices.php 26 KB

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