BxBaseCmtsServices.php 25 KB

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