BxBaseCmtsServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 serviceGetBlockView($sSystem = '', $iObjectId = 0, $iCommentId = 0)
  19. {
  20. if(empty($sSystem) && ($sSystem = bx_get('sys')) !== false)
  21. $sSystem = bx_process_input($sSystem);
  22. if(empty($iObjectId) && ($iObjectId = bx_get('id')) !== false)
  23. $iObjectId = bx_process_input($iObjectId, BX_DATA_INT);
  24. if(empty($iCommentId) && ($iCommentId = bx_get('cmt_id')) !== false)
  25. $iCommentId = bx_process_input($iCommentId, BX_DATA_INT);
  26. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iObjectId, true);
  27. if(!$oCmts)
  28. return '';
  29. return $oCmts->getCommentBlock($iCommentId);
  30. }
  31. public function serviceAlertResponseSysCmtsImagesFileDeleted($oAlert)
  32. {
  33. $iUniqId = (int)$oAlert->aExtras['ghost']['content_id'];
  34. $aCmt = BxDolCmts::getGlobalInfo($iUniqId);
  35. if(empty($aCmt) || !is_array($aCmt))
  36. return;
  37. $oCmts = BxDolCmts::getObjectInstance($aCmt['system_name'], 0, false);
  38. if(!$oCmts)
  39. return;
  40. $oCmts->getQueryObject()->deleteImages($aCmt['system_id'], false, $oAlert->iObject);
  41. }
  42. public function serviceGetMenuItemAddonVote($sSystem, $iId, $iCmtId)
  43. {
  44. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iId);
  45. $oVote = $oCmts->getVoteObject($iCmtId);
  46. if($oVote !== false)
  47. return $oVote->getCounter();
  48. return '';
  49. }
  50. public function serviceGetLiveUpdate($sSystem, $iContentId, $iProfileId, $iCount = 0)
  51. {
  52. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iContentId);
  53. if(!$oCmts || !$oCmts->isEnabled())
  54. return false;
  55. $sKey = $oCmts->getNotificationId();
  56. bx_import('BxDolSession');
  57. if((int)BxDolSession::getInstance()->getValue($sKey) == 1)
  58. return false;
  59. $iCountNew = $oCmts->getCommentsCount($iContentId, -1, BX_CMT_FILTER_OTHERS);
  60. if($iCountNew == $iCount)
  61. return false;
  62. return array(
  63. 'count' => $iCountNew, // required
  64. 'method' => $oCmts->getJsObjectName() . '.showLiveUpdate(oData)', // required
  65. 'data' => array(
  66. 'code' => $oCmts->getLiveUpdate($iCount, $iCountNew)
  67. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  68. );
  69. }
  70. public function serviceGetLiveUpdates($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() . '.showLiveUpdates(oData)', // required
  85. 'data' => array(
  86. 'code' => $oCmts->getLiveUpdates($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 serviceManageTools($sType = 'common')
  91. {
  92. $oGrid = BxDolGrid::getObjectInstance('sys_cmts_administration');
  93. if(!$oGrid)
  94. return '';
  95. $oTemplate = BxDolTemplate::getInstance();
  96. $oTemplate->addJs(array('BxDolCmtsManageTools.js'));
  97. $oTemplate->addCss(array('cmts_manage_tools.css'));
  98. $oTemplate->addJsTranslation(array('_sys_grid_search'));
  99. return array(
  100. 'content' => $oGrid->getCode()
  101. );
  102. }
  103. /**
  104. * Comment Added for Timeline module
  105. */
  106. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  107. {
  108. $bCache = true;
  109. $iCmtUniqId = $aEvent['object_id'];
  110. $oCmts = BxDolCmts::getObjectInstanceByUniqId($iCmtUniqId);
  111. if(!$oCmts)
  112. return false;
  113. $aCmt = BxDolCmtsQuery::getCommentExtendedByUniqId($iCmtUniqId);
  114. if(empty($aCmt) || !is_array($aCmt))
  115. return false;
  116. $iCmtId = (int)$aCmt['cmt_id'];
  117. $iUserId = bx_get_logged_profile_id();
  118. $iAuthorId = (int)$aCmt['cmt_author_id'];
  119. $iAuthorIdAbs = abs($iAuthorId);
  120. 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)
  121. return false;
  122. $sCmtUrl = $oCmts->getViewUrl($iCmtId);
  123. $sSample = '_cmt_txt_sample_comment_single_with_article';
  124. $sSampleWoArticle = '_cmt_txt_sample_comment_single';
  125. $sAction = '';
  126. $sActionCustom = '_cmt_txt_added_sample_custom';
  127. $aActionCustomMarkers = array(
  128. 'sample' => $sSample,
  129. 'sample_url' => $sCmtUrl,
  130. 'psample' => strip_tags($oCmts->getObjectTitle()),
  131. 'psample_url' => $oCmts->getBaseUrl(),
  132. );
  133. $iOwnerId = $iAuthorIdAbs;
  134. if(isset($aEvent['object_privacy_view']) && (int)$aEvent['object_privacy_view'] < 0)
  135. $iOwnerId = abs($aEvent['object_privacy_view']);
  136. //--- Votes
  137. $aVotes = array();
  138. if(($oVotes = $oCmts->getVoteObject($iCmtUniqId)) !== false)
  139. $aVotes = array(
  140. 'system' => $oVotes->getSystemName(),
  141. 'object_id' => $iCmtUniqId,
  142. 'count' => $aCmt['votes']
  143. );
  144. //--- Reactions
  145. $aReactions = array();
  146. if(($oReactions = $oCmts->getReactionObject($iCmtUniqId)) !== false)
  147. $aReactions = array(
  148. 'system' => $oReactions->getSystemName(),
  149. 'object_id' => $iCmtUniqId,
  150. 'count' => $aCmt['rvotes']
  151. );
  152. //--- Scores
  153. $aScores = array();
  154. if(($oScores = $oCmts->getScoreObject($iCmtUniqId)) !== false)
  155. $aScores = array(
  156. 'system' => $oScores->getSystemName(),
  157. 'object_id' => $iCmtUniqId,
  158. 'score' => $aCmt['score']
  159. );
  160. //--- Reports
  161. $aReports = array();
  162. if(($oReports = $oCmts->getReportObject($iCmtUniqId)) !== false)
  163. $aReports = array(
  164. 'system' => $oReports->getSystemName(),
  165. 'object_id' => $iCmtUniqId,
  166. 'count' => $aCmt['reports']
  167. );
  168. return array(
  169. '_cache' => $bCache,
  170. 'owner_id' => $iOwnerId,
  171. 'object_owner_id' => $iAuthorId,
  172. 'icon' => 'comments',
  173. 'sample' => $sSample,
  174. 'sample_wo_article' => $sSampleWoArticle,
  175. 'sample_action' => $sAction,
  176. 'sample_action_custom' => array(
  177. 'content' => $sActionCustom,
  178. 'markers' => $aActionCustomMarkers
  179. ),
  180. 'url' => $sCmtUrl,
  181. 'content' => array(
  182. 'sample' => $sSample,
  183. 'sample_wo_article' => $sSampleWoArticle,
  184. 'sample_action' => $sAction,
  185. 'url' => $sCmtUrl,
  186. 'title' => '',
  187. 'text' => $oCmts->getViewText($aCmt),
  188. 'images' => array(),
  189. 'images_attach' => array(),
  190. 'videos' => array(),
  191. 'videos_attach' => array(),
  192. 'files' => array(),
  193. 'files_attach' => $oCmts->getAttachments($iCmtId)
  194. ), //a string to display or array to parse default template before displaying.
  195. 'date' => $aCmt['cmt_time'],
  196. 'views' => array(),
  197. 'votes' => $aVotes,
  198. 'reactions' => $aReactions,
  199. 'scores' => $aScores,
  200. 'reports' => $aReports,
  201. 'comments' => array(),
  202. 'title' => !empty($aCmt['cmt_text']) ? $aCmt['cmt_text'] : '', //may be empty.
  203. 'description' => '' //may be empty.
  204. );
  205. }
  206. /**
  207. * Comment added for Notifications module
  208. */
  209. public function serviceGetNotificationsCommentAdded($aEvent)
  210. {
  211. $aCommentGi = BxDolCmts::getGlobalInfo($aEvent['subobject_id']);
  212. if(empty($aCommentGi) || !is_array($aCommentGi))
  213. return [];
  214. $iContentId = (int)$aEvent['object_id'];
  215. $oComment = BxDolCmts::getObjectInstance($aCommentGi['system_name'], $iContentId);
  216. if(!$oComment)
  217. return [];
  218. $aCommentSystem = $oComment->getSystemInfo();
  219. $oContentModule = BxDolModule::getInstance($aCommentSystem['module']);
  220. if(!$oContentModule)
  221. return [];
  222. $aContentInfo = $oContentModule->_oDb->getContentInfoById($iContentId);
  223. if(empty($aContentInfo) || !is_array($aContentInfo))
  224. return [];
  225. $CNF = &$oContentModule->_oConfig->CNF;
  226. if(empty($CNF['FIELD_ID']) || empty($CNF['FIELD_AUTHOR']) || !(isset($CNF['FIELD_TITLE']) || isset($CNF['FIELD_TEXT'])) || empty($CNF['URI_VIEW_ENTRY']))
  227. return [];
  228. $sEntryUrl = '{bx_url_root}' . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  229. $sEntryCaption = isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...');
  230. return array(
  231. 'entry_sample' => $CNF['T']['txt_sample_single'],
  232. 'entry_url' => $sEntryUrl,
  233. 'entry_caption' => $sEntryCaption,
  234. 'subentry_sample' => $CNF['T']['txt_sample_comment_single'],
  235. 'subentry_url' => '{bx_url_root}' . $oComment->getViewUrl((int)$aCommentGi['cmt_id'], false),
  236. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  237. );
  238. }
  239. /**
  240. * Comment vote for Notifications module
  241. */
  242. public function serviceGetNotificationsVote($aEvent)
  243. {
  244. $iCmtIdUnique = (int)$aEvent['object_id'];
  245. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  246. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  247. return array();
  248. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  249. if(!$oCmts || !$oCmts->isEnabled())
  250. return array();
  251. $oVote = $oCmts->getVoteObject($iCmtIdUnique);
  252. if(!$oVote)
  253. return array();
  254. $iCmtId = (int)$aCmtInfo['cmt_id'];
  255. $sCmtUrl = str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oCmts->serviceGetLink($iCmtId));
  256. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  257. return array(
  258. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  259. 'entry_url' => $sCmtUrl,
  260. 'entry_caption' => $sCmtCaption,
  261. 'entry_author' => $aCmtInfo['author_id'],
  262. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_vote_single'),
  263. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  264. );
  265. }
  266. /**
  267. * Comment reaction for Notifications module
  268. */
  269. public function serviceGetNotificationsReaction($aEvent)
  270. {
  271. $iCmtIdUnique = (int)$aEvent['object_id'];
  272. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  273. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  274. return array();
  275. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  276. if(!$oCmts || !$oCmts->isEnabled())
  277. return array();
  278. $oReaction = $oCmts->getReactionObject($iCmtIdUnique);
  279. if(!$oReaction)
  280. return array();
  281. $aSubentry = $oReaction->getTrackBy(array('type' => 'id', 'id' => (int)$aEvent['subobject_id']));
  282. if(empty($aSubentry) || !is_array($aSubentry))
  283. return array();
  284. $aSubentrySampleParams = array();
  285. $aReaction = $oReaction->getReaction($aSubentry['reaction']);
  286. if(!empty($aReaction['title']))
  287. $aSubentrySampleParams[] = $aReaction['title'];
  288. else
  289. $aSubentrySampleParams[] = '_undefined';
  290. $iCmtId = (int)$aCmtInfo['cmt_id'];
  291. $sCmtUrl = str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oCmts->serviceGetLink($iCmtId));
  292. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  293. return array(
  294. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  295. 'entry_url' => $sCmtUrl,
  296. 'entry_caption' => $sCmtCaption,
  297. 'entry_author' => $aCmtInfo['author_id'],
  298. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_reaction_single'),
  299. 'subentry_sample_params' => $aSubentrySampleParams,
  300. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  301. );
  302. }
  303. /**
  304. * Comment score -> vote up for Notifications module
  305. */
  306. public function serviceGetNotificationsScoreUp($aEvent)
  307. {
  308. return $this->_serviceGetNotificationsScore('up', $aEvent);
  309. }
  310. /**
  311. * Comment score -> vote down for Notifications module
  312. */
  313. public function serviceGetNotificationsScoreDown($aEvent)
  314. {
  315. return $this->_serviceGetNotificationsScore('down', $aEvent);
  316. }
  317. protected function _serviceGetNotificationsScore($sType, $aEvent)
  318. {
  319. $iCmtIdUnique = (int)$aEvent['object_id'];
  320. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  321. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  322. return array();
  323. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  324. if(!$oCmts || !$oCmts->isEnabled())
  325. return array();
  326. $oScore = $oCmts->getScoreObject($iCmtIdUnique);
  327. if(!$oScore)
  328. return array();
  329. $iCmtId = (int)$aCmtInfo['cmt_id'];
  330. $sCmtUrl = str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oCmts->serviceGetLink($iCmtId));
  331. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  332. return array(
  333. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  334. 'entry_url' => $sCmtUrl,
  335. 'entry_caption' => $sCmtCaption,
  336. 'entry_author' => $aCmtInfo['author_id'],
  337. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_score_' . $sType . '_single'),
  338. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  339. );
  340. }
  341. public function serviceGetDataApi($aParams)
  342. {
  343. if(is_string($aParams))
  344. $aParams = json_decode($aParams, true);
  345. $aParams['parent_id'] = !isset($aParams['parent_id']) ? 0 : $aParams['parent_id'];
  346. $aParams['start_from'] = !isset($aParams['start_from']) ? 0 : $aParams['start_from'];
  347. $aParams['order_way'] = !isset($aParams['order_way']) ? 'desc' : $aParams['order_way'];
  348. $aParams['is_form'] = !isset($aParams['is_form']) ? true : $aParams['is_form'];
  349. $aParams['insert'] = 'before';
  350. $oCmts = BxDolCmts::getObjectInstance($aParams['module'], $aParams['object_id']);
  351. if (!$oCmts || !$oCmts->isEnabled())
  352. return false;
  353. $aRv = [
  354. 'id' => '1',
  355. 'url' => '/api.php?r=system/get_data_api/TemplCmtsServices/&params[]=',
  356. 'type' => 'comments'
  357. ];
  358. $oForm = $oCmts->getFormPost($aParams['parent_id']);
  359. $bIsList = false;
  360. if (isset($oForm['form']) && $aParams['is_form']){
  361. $aRv['form'] = ['id' => 'cmt_form', 'type' => 'form', 'name' => 'comment', 'data' => $oForm['form']->getCodeAPI(), 'request' => ['immutable' => true]];
  362. // add view (form + new comment)
  363. if($oForm['form']->isSubmittedAndValid()){
  364. $aParams['insert'] = $aParams['order_way'] == 'desc' ? 'before' : 'after';
  365. $aParams['comment_id'] = $oForm['res'];
  366. $bIsList = true;
  367. }
  368. // default view (form + list)
  369. if (!$oForm['form']->isSubmitted()){
  370. $bIsList = true;
  371. }
  372. }
  373. else{
  374. // list only view
  375. $bIsList = true;
  376. $aParams['insert'] = 'after';
  377. }
  378. if ($bIsList)
  379. $aRv['browse'] = $this->serviceGetCommentsApi($oCmts, $aParams);
  380. return $aRv;
  381. }
  382. public function serviceGetCommentsApi($oCmts, $aParams)
  383. {
  384. $mixedResult = $oCmts->isViewAllowed();
  385. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  386. return $mixedResult; // TODO: error checking
  387. $aBp = !isset($aParams['aBp']) ? [] : $aParams['aBp'];
  388. $aDp = ['in_designbox' => false, 'show_empty' => false];
  389. $aBp['type'] = 'head';
  390. $oCmts->getParams($aBp, $aDp);
  391. $oCmts->prepareParams($aBp, $aDp);
  392. $aBp['order']['way'] = $aParams['order_way'];
  393. $aBp['order_way'] = $aParams['order_way'];
  394. $aBp['start'] = $aParams['start_from'] ;
  395. $aPp = $aBp['per_view'];
  396. $aBp['per_view'] = $aBp['per_view'] + 1;
  397. if (isset($aParams['per_view'])){
  398. $aBp['per_view'] = $aParams['per_view'];
  399. }
  400. $aCmts = isset($aParams['comment_id']) ? [['cmt_id' => $aParams['comment_id']]] : $oCmts->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp[($aBp['init_view'] != -1 ? 'init' : 'per') . '_view']);
  401. $aParams['start_from'] = 0;
  402. if (count($aCmts) == $aBp['per_view']){
  403. $aBp['per_view'] = $aPp;
  404. $aCmts = array_slice($aCmts, 0, $aBp['per_view']);
  405. $aParams['start_from'] = $aBp['start'] + $aBp['per_view'];
  406. }
  407. $aCmtsRv = [];
  408. foreach ($aCmts as $aCmt) {
  409. $aCmtsRv[] = $oCmts->getCommentStructure($aCmt['cmt_id'], $aBp, $aDp);
  410. }
  411. $aData = [
  412. 'unit' => 'comments',
  413. 'start' => $aParams['start_from'],
  414. 'order' => $aParams['order_way'],
  415. 'module' => $oCmts->getSystemName(),
  416. 'object_id' => $oCmts->getId(),
  417. 'data' => $aCmtsRv
  418. ];
  419. if (isset($aParams['mode']) && $aParams['mode'] == 'feed')
  420. return $aData;
  421. $aRv = [
  422. 'id' => 'cmt_list',
  423. 'type' => 'browse',
  424. 'insert' => $aParams['insert'],
  425. 'data' => $aData
  426. ];
  427. if (isset($aParams['comment_id'])){
  428. $aRv['new'] = $aParams['comment_id'];
  429. }
  430. return $aRv;
  431. }
  432. }
  433. /** @} */