1
0

BxBaseCmtsServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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($iCmtId)) !== false)
  139. $aVotes = array(
  140. 'system' => $oVotes->getSystemName(),
  141. 'object_id' => $iCmtId,
  142. 'count' => $aCmt['votes']
  143. );
  144. //--- Reactions
  145. $aReactions = array();
  146. if(($oReactions = $oCmts->getReactionObject($iCmtId)) !== false)
  147. $aReactions = array(
  148. 'system' => $oReactions->getSystemName(),
  149. 'object_id' => $iCmtId,
  150. 'count' => $aCmt['rvotes']
  151. );
  152. //--- Scores
  153. $aScores = array();
  154. if(($oScores = $oCmts->getScoreObject($iCmtId)) !== false)
  155. $aScores = array(
  156. 'system' => $oScores->getSystemName(),
  157. 'object_id' => $iCmtId,
  158. 'score' => $aCmt['score']
  159. );
  160. //--- Reports
  161. $aReports = array();
  162. if(($oReports = $oCmts->getReportObject($iCmtId)) !== false)
  163. $aReports = array(
  164. 'system' => $oReports->getSystemName(),
  165. 'object_id' => $iCmtId,
  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 vote for Notifications module
  208. */
  209. public function serviceGetNotificationsVote($aEvent)
  210. {
  211. $iCmtIdUnique = (int)$aEvent['object_id'];
  212. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  213. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  214. return array();
  215. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  216. if(!$oCmts || !$oCmts->isEnabled())
  217. return array();
  218. $oVote = $oCmts->getVoteObject($iCmtIdUnique);
  219. if(!$oVote)
  220. return array();
  221. $iCmtId = (int)$aCmtInfo['cmt_id'];
  222. $sCmtUrl = $oCmts->serviceGetLink($iCmtId);
  223. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  224. return array(
  225. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  226. 'entry_url' => $sCmtUrl,
  227. 'entry_caption' => $sCmtCaption,
  228. 'entry_author' => $aCmtInfo['author_id'],
  229. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_vote_single'),
  230. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  231. );
  232. }
  233. /**
  234. * Comment reaction for Notifications module
  235. */
  236. public function serviceGetNotificationsReaction($aEvent)
  237. {
  238. $iCmtIdUnique = (int)$aEvent['object_id'];
  239. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  240. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  241. return array();
  242. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  243. if(!$oCmts || !$oCmts->isEnabled())
  244. return array();
  245. $oReaction = $oCmts->getReactionObject($iCmtIdUnique);
  246. if(!$oReaction)
  247. return array();
  248. $aSubentry = $oReaction->getTrackBy(array('type' => 'id', 'id' => (int)$aEvent['subobject_id']));
  249. if(empty($aSubentry) || !is_array($aSubentry))
  250. return array();
  251. $aSubentrySampleParams = array();
  252. $aReaction = $oReaction->getReaction($aSubentry['reaction']);
  253. if(!empty($aReaction['title']))
  254. $aSubentrySampleParams[] = $aReaction['title'];
  255. else
  256. $aSubentrySampleParams[] = '_undefined';
  257. $iCmtId = (int)$aCmtInfo['cmt_id'];
  258. $sCmtUrl = $oCmts->serviceGetLink($iCmtId);
  259. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  260. return array(
  261. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  262. 'entry_url' => $sCmtUrl,
  263. 'entry_caption' => $sCmtCaption,
  264. 'entry_author' => $aCmtInfo['author_id'],
  265. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_reaction_single'),
  266. 'subentry_sample_params' => $aSubentrySampleParams,
  267. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  268. );
  269. }
  270. /**
  271. * Comment score -> vote up for Notifications module
  272. */
  273. public function serviceGetNotificationsScoreUp($aEvent)
  274. {
  275. return $this->_serviceGetNotificationsScore('up', $aEvent);
  276. }
  277. /**
  278. * Comment score -> vote down for Notifications module
  279. */
  280. public function serviceGetNotificationsScoreDown($aEvent)
  281. {
  282. return $this->_serviceGetNotificationsScore('down', $aEvent);
  283. }
  284. protected function _serviceGetNotificationsScore($sType, $aEvent)
  285. {
  286. $iCmtIdUnique = (int)$aEvent['object_id'];
  287. $aCmtInfo = BxDolCmts::getGlobalInfo($iCmtIdUnique);
  288. if(empty($aCmtInfo) || !is_array($aCmtInfo))
  289. return array();
  290. $oCmts = BxDolCmts::getObjectInstance($aCmtInfo['system_name'], 0, false);
  291. if(!$oCmts || !$oCmts->isEnabled())
  292. return array();
  293. $oScore = $oCmts->getScoreObject($iCmtIdUnique);
  294. if(!$oScore)
  295. return array();
  296. $iCmtId = (int)$aCmtInfo['cmt_id'];
  297. $sCmtUrl = $oCmts->serviceGetLink($iCmtId);
  298. $sCmtCaption = strmaxtextlen($oCmts->serviceGetText($iCmtId), 20, '...');
  299. return array(
  300. 'entry_sample' => $oCmts->getLanguageKey('txt_sample_single'),
  301. 'entry_url' => $sCmtUrl,
  302. 'entry_caption' => $sCmtCaption,
  303. 'entry_author' => $aCmtInfo['author_id'],
  304. 'subentry_sample' => $oCmts->getLanguageKey('txt_sample_score_' . $sType . '_single'),
  305. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  306. );
  307. }
  308. }
  309. /** @} */