BxBaseCmtsServices.php 24 KB

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