BxCnlModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. * @defdroup Channels Channels
  7. * @indroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Channels profiles module.
  13. */
  14. class BxCnlModule extends BxBaseModGroupsModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. public function checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  21. {
  22. return parent::_checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  23. }
  24. /**
  25. * Process Hash Tag
  26. *
  27. * @param string $sHashtag - hashtag to be processed.
  28. * @param string $sModuleName - module name.
  29. * @param integer $iContentId - ID of the content which has the hashtag.
  30. * @param integer $iAuthorId - action's author id.
  31. */
  32. function processHashtag($sHashtag, $sModuleName, $iContentId, $iAuthorId)
  33. {
  34. /*
  35. * Note! For now metatag object name is used here as module name, because usually it's equal to module's name. This should be changed in Ticket #1596
  36. * For now if module cannot be created then a channel for such tag shouldn't be created too.
  37. */
  38. $oModule = BxDolModule::getInstance($sModuleName);
  39. if(empty($oModule))
  40. return;
  41. /*
  42. * Use content's author profile when Author ID wasn't provided.
  43. * Usually it happens when tags were processed with cron.
  44. */
  45. if(empty($iAuthorId))
  46. $iAuthorId = BxDolService::call($sModuleName, 'get_author', array($iContentId));
  47. $aCheck = checkActionModule($this->_iProfileId, 'create channel auto', $this->getName(), false);
  48. $mixedCnlId = $this->_oDb->getChannelIdByName($sHashtag);
  49. if (empty($mixedCnlId) && ($aCheck[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)){
  50. $CNF = &$this->_oConfig->CNF;
  51. $oAccountQuery = BxDolAccountQuery::getInstance();
  52. $aOperators = $oAccountQuery->getOperators();
  53. if(count($aOperators) > 0){
  54. $oProfile = BxDolProfile::getInstanceByAccount($aOperators[0]);
  55. $aContent = $this->serviceEntityAdd($oProfile->id(), array($CNF['FIELD_NAME'] => $sHashtag));
  56. checkActionModule($this->_iProfileId, 'create channel auto', $this->getName(), true);
  57. if (isset($aContent['content']) && isset($aContent['content']['id']))
  58. $mixedCnlId = $aContent['content']['id'];
  59. }
  60. }
  61. if(empty($mixedCnlId) || (int)$this->_oDb->checkContentInChannel($iContentId, $mixedCnlId, $sModuleName, $iAuthorId) != 0)
  62. return;
  63. $iId = (int)$this->_oDb->addContentToChannel($iContentId, $mixedCnlId, $sModuleName, $iAuthorId);
  64. if($iId == 0)
  65. return;
  66. $oCnlProfile = BxDolProfile::getInstanceByContentAndType($mixedCnlId, $this->_oConfig->getName());
  67. if(!$oCnlProfile)
  68. return;
  69. $iCnlProfileId = $oCnlProfile->id();
  70. bx_alert($this->_aModule['name'], 'hashtag_added', $iId, $iCnlProfileId, array(
  71. 'object_author_id' => $iAuthorId,
  72. 'privacy_view' => -$iCnlProfileId,
  73. 'timeline_group' => array(
  74. 'by' => $sModuleName . '_' . $iAuthorId . '_' . $iContentId,
  75. 'field' => 'owner_id'
  76. )
  77. ));
  78. bx_alert($this->_aModule['name'], 'hashtag_added_notif', $mixedCnlId, $iCnlProfileId, array(
  79. 'object_author_id' => $iAuthorId,
  80. 'privacy_view' => -$iCnlProfileId,
  81. 'subobject_id' => $iId,
  82. 'content_id' => $iContentId,
  83. 'content_author_id' => $iAuthorId
  84. ));
  85. }
  86. function removeContentFromChannel($iContentId, $sModuleName)
  87. {
  88. $oDolProfileQuery = BxDolProfileQuery::getInstance();
  89. $aData = $this->_oDb->getDataByContent($iContentId, $sModuleName);
  90. foreach ($aData as $aRow) {
  91. $iProfileInfo = $oDolProfileQuery->getProfileByContentAndType($aRow['cnl_id'], $this->_aModule['name']);
  92. if(is_array($iProfileInfo)){
  93. bx_alert($this->_aModule['name'], 'hashtag_deleted', $aRow['id'], $iProfileInfo['id']);
  94. bx_alert($this->_aModule['name'], 'hashtag_deleted_notif', $aRow['cnl_id'], $iProfileInfo['id'], array('subobject_id' => $aRow['id']));
  95. }
  96. }
  97. return $this->_oDb->removeContentFromChannel($iContentId, $sModuleName);
  98. }
  99. public function serviceEntityBreadcrumb($iContentId = 0)
  100. {
  101. if(!$iContentId)
  102. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  103. if(!$iContentId)
  104. return false;
  105. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  106. if(!$aContentInfo)
  107. return false;
  108. return $this->_oTemplate->entryBreadcrumb($aContentInfo);
  109. }
  110. public function serviceEntityParent($iContentId = 0)
  111. {
  112. return $this->_serviceTemplateFunc('entryParent', $iContentId);
  113. }
  114. public function serviceEntityChilds($iContentId = 0)
  115. {
  116. return $this->_serviceTemplateFunc('entryChilds', $iContentId);
  117. }
  118. function serviceSearchResultByHashtag($iContentId = 0)
  119. {
  120. $CNF = &$this->_oConfig->CNF;
  121. $oSearch = new BxTemplSearch();
  122. $oSearch->setLiveSearch(0);
  123. $oSearch->setMetaType('keyword');
  124. $aContentInfo = $this->_oDb->getContentInfoById(bx_get('id'));
  125. $_GET['keyword'] = $aContentInfo[$CNF['FIELD_NAME']];
  126. $sCode = $oSearch->response();
  127. if (!$sCode)
  128. $sCode = $oSearch->getEmptyResult();
  129. return $sCode;
  130. }
  131. /**
  132. * Data for Timeline module
  133. */
  134. public function serviceGetTimelineData()
  135. {
  136. $sModule = $this->_aModule['name'];
  137. return array(
  138. 'handlers' => array(
  139. array('group' => $sModule . '_hastag', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_added', 'module_name' => $sModule, 'module_method' => 'get_timeline_post_hashtag', 'module_class' => 'Module', 'groupable' => 0, 'group_by' => ''),
  140. array('group' => $sModule . '_hastag', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_deleted')
  141. ),
  142. 'alerts' => array(
  143. array('unit' => $sModule, 'action' => 'hashtag_added'),
  144. array('unit' => $sModule, 'action' => 'hashtag_deleted')
  145. )
  146. );
  147. }
  148. public function serviceGetTimelinePostAllowedView ($aEvent)
  149. {
  150. $sError = _t('_sys_txt_access_denied');
  151. if(empty($aEvent) || !is_array($aEvent))
  152. return $sError;
  153. $aEventContent = $this->_oDb->getContentById($aEvent['object_id']);
  154. if(empty($aEventContent) || !is_array($aEventContent))
  155. return $sError;
  156. if(!BxDolRequest::serviceExists($aEventContent['module_name'], 'get_timeline_post_allowed_view'))
  157. return $sError;
  158. return BxDolService::call($aEventContent['module_name'], 'get_timeline_post_allowed_view', array(array('id' => $aEvent['id'], 'owner_id' => $aEventContent['author_id'], 'object_id' => $aEventContent['content_id'])));
  159. }
  160. public function serviceGetTimelinePostHashtag($aEvent, $aBrowseParams = array())
  161. {
  162. if(empty($aEvent) || !is_array($aEvent))
  163. return false;
  164. $aEventContent = $this->_oDb->getContentById($aEvent['object_id']);
  165. if(empty($aEventContent) || !is_array($aEventContent))
  166. return false;
  167. if(!BxDolRequest::serviceExists($aEventContent['module_name'], 'get_timeline_post'))
  168. return false;
  169. return BxDolService::call($aEventContent['module_name'], 'get_timeline_post', array(array('id' => $aEvent['id'], 'owner_id' => $aEventContent['author_id'], 'object_id' => $aEventContent['content_id'])));
  170. }
  171. /**
  172. * Data for Notifications module
  173. */
  174. public function serviceGetNotificationsData()
  175. {
  176. $a = parent::serviceGetNotificationsData();
  177. $sModule = $this->_aModule['name'];
  178. $a['handlers'][] = array('group' => $sModule . '_hastag_notif', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_added_notif', 'module_name' => $sModule, 'module_method' => 'get_notifications_post_hashtag', 'module_class' => 'Module');
  179. $a['handlers'][] = array('group' => $sModule . '_hastag_notif', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_deleted_notif');
  180. $a['settings'][] = array('group' => 'hastag', 'unit' => $sModule, 'action' => 'hashtag_added_notif', 'types' => array('follow_context'));
  181. $a['alerts'][] = array('unit' => $sModule, 'action' => 'hashtag_added_notif');
  182. $a['alerts'][] = array('unit' => $sModule, 'action' => 'hashtag_deleted_notif');
  183. return $a;
  184. }
  185. public function serviceGetNotificationsPostHashtag($aEvent)
  186. {
  187. if(empty($aEvent) || !is_array($aEvent))
  188. return '';
  189. $aContentEvent = $this->_oDb->getContentById($aEvent['subobject_id']);
  190. if(empty($aContentEvent) || !is_array($aContentEvent))
  191. return '';
  192. $oModule = BxDolModule::getInstance($aContentEvent['module_name']);
  193. if ($oModule) {
  194. if (isset($oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW'])){
  195. $oPrivacy = BxDolPrivacy::getObjectInstance($oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  196. if (!$oPrivacy->check($aContentEvent['content_id']))
  197. return '';
  198. }
  199. $aRv = $oModule->serviceGetNotificationsPost(array(
  200. 'object_id' => $aContentEvent['content_id']
  201. ));
  202. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added';
  203. if(method_exists($oModule, 'serviceActAsProfile') && $oModule->serviceActAsProfile())
  204. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added_profile';
  205. $aRv['entry_url'] = $this->serviceGetLink($aContentEvent['cnl_id']);
  206. return $aRv;
  207. }
  208. return '';
  209. }
  210. public function serviceBrowseAuthor($iProfileId = 0, $aParams = array())
  211. {
  212. $CNF = &$this->_oConfig->CNF;
  213. if(empty($iProfileId))
  214. $iProfileId = bx_get_logged_profile_id();
  215. $sResult = isset($aParams['empty_message']) && (bool)$aParams['empty_message'] === true ? MsgBox(_t('_Empty')) : '';
  216. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  217. $aProfile = $oConnection->getConnectedContent($iProfileId);
  218. $aVars = array();
  219. foreach ($aProfile as $iProfileId) {
  220. $oProfile = BxDolProfile::getInstance($iProfileId);
  221. if (!$oProfile || $oProfile->getModule() != $this->getName())
  222. continue;
  223. $iContentId = $oProfile->getContentId();
  224. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  225. if (isset($aContentInfo[$CNF['FIELD_NAME']]))
  226. array_push($aVars, array('title' => $aContentInfo[$CNF['FIELD_NAME']], 'link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId)));
  227. }
  228. if(empty($aVars) || !is_array($aVars))
  229. return $sResult;
  230. return $this->_oTemplate->parseHtmlByName('my_channels.html', array(
  231. 'bx_if:show_list' => array(
  232. 'condition' => count($aVars) > 0,
  233. 'content' => array(
  234. 'bx_repeat:items' => $aVars
  235. )
  236. )
  237. ));
  238. }
  239. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  240. {
  241. return true;
  242. }
  243. public function serviceGetFollowingChannelsNames($iProfileId)
  244. {
  245. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  246. return array();
  247. if (!($aIds = $oConn->getConnectedContent($iProfileId)))
  248. return array();
  249. $a = array();
  250. foreach ($aIds as $iId) {
  251. if (!($oProfile = BxDolProfile::getInstance($iId)))
  252. continue;
  253. if ($oProfile->getModule() != $this->getName())
  254. continue;
  255. $a[] = $oProfile->getDisplayName();
  256. }
  257. return $a;
  258. }
  259. public function checkAllowedCompose (&$aDataEntry, $isPerformAction = false)
  260. {
  261. return _t('_sys_txt_access_denied');
  262. }
  263. public function followLabels($sModule, $iContentId)
  264. {
  265. if (!getParam('bx_channels_labels_autofollow'))
  266. return;
  267. if (!($oModuleProfiles = BxDolModule::getInstance($sModule)))
  268. return;
  269. if (!($aContentInfo = $oModuleProfiles->_oDb->getContentInfoById($iContentId)) || !($oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModule)))
  270. return;
  271. $CNF = &$oModuleProfiles->_oConfig->CNF;
  272. if (!$oModuleProfiles->serviceActAsProfile() || !isset($CNF['FIELD_LABELS']) || empty($aContentInfo[$CNF['FIELD_ID']]))
  273. return;
  274. if (!($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) || !$oMetatags->keywordsIsEnabled() || !($aLabels = $oMetatags->keywordsGet($aContentInfo[$CNF['FIELD_ID']])))
  275. return;
  276. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  277. return;
  278. foreach ($aLabels as $sLabel) {
  279. if (!($iContentIdChannel = $this->_oDb->getChannelIdByName($sLabel)))
  280. continue;
  281. if (!($oProfileChannel = BxDolProfile::getInstanceByContentAndType($iContentIdChannel, 'bx_channels')))
  282. continue;
  283. $oConn->addConnection($oProfile->id(), $oProfileChannel->id());
  284. }
  285. }
  286. /** Returns profile's subscribed channels React Jot
  287. *
  288. * @param int $iProfileId
  289. * @param array $aParams
  290. * @return array
  291. */
  292. public function serviceBrowseAuthorChannels($iProfileId = 0, $aParams = array())
  293. {
  294. $CNF = &$this->_oConfig->CNF;
  295. if(empty($iProfileId))
  296. $iProfileId = bx_get_logged_profile_id();
  297. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  298. $aProfile = $oConnection->getConnectedContent($iProfileId);
  299. $aVars = array();
  300. foreach ($aProfile as $iProfileId) {
  301. $oProfile = BxDolProfile::getInstance($iProfileId);
  302. if (!$oProfile || $oProfile->getModule() != $this->getName())
  303. continue;
  304. $iContentId = $oProfile->getContentId();
  305. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  306. if (isset($aContentInfo[$CNF['FIELD_NAME']]))
  307. array_push($aVars, array(
  308. 'title' => $aContentInfo[$CNF['FIELD_NAME']],
  309. 'link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId),
  310. 'followers' => $oConnection -> getConnectedInitiatorsCount($iProfileId),
  311. 'id' => $iProfileId,
  312. 'icon' => $oProfile -> getThumb()
  313. ));
  314. }
  315. return $aVars;
  316. }
  317. }
  318. /** @} */