BxCnlModule.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. $CNF = &$this->_oConfig->CNF;
  35. /*
  36. * 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
  37. * For now if module cannot be created then a channel for such tag shouldn't be created too.
  38. */
  39. $oModule = BxDolModule::getInstance($sModuleName);
  40. if(empty($oModule))
  41. return;
  42. /*
  43. * Use content's author profile when Author ID wasn't provided.
  44. * Usually it happens when tags were processed with cron.
  45. */
  46. if(empty($iAuthorId))
  47. $iAuthorId = BxDolService::call($sModuleName, 'get_author', array($iContentId));
  48. $aCheck = checkActionModule($this->_iProfileId, 'create channel auto', $this->getName(), false);
  49. $mixedCnlId = $this->_oDb->getChannelIdByName($sHashtag);
  50. if(empty($mixedCnlId) && ($aCheck[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)) {
  51. $iProfileId = (int)$this->_oDb->getParam($CNF['PARAM_DEFAULT_AUTHOR']);
  52. if(empty($iProfileId)) {
  53. $aOperators = BxDolAccountQuery::getInstance()->getOperators();
  54. if(count($aOperators) > 0)
  55. $iProfileId = BxDolProfile::getInstanceByAccount(array_shift($aOperators))->id();
  56. }
  57. $aContent = $this->serviceEntityAdd($iProfileId, array($CNF['FIELD_NAME'] => $sHashtag));
  58. checkActionModule($this->_iProfileId, 'create channel auto', $this->getName(), true);
  59. if(isset($aContent['content']) && isset($aContent['content']['id']))
  60. $mixedCnlId = $aContent['content']['id'];
  61. }
  62. if(empty($mixedCnlId) || (int)$this->_oDb->checkContentInChannel($iContentId, $mixedCnlId, $sModuleName, $iAuthorId) != 0)
  63. return;
  64. $iId = (int)$this->_oDb->addContentToChannel($iContentId, $mixedCnlId, $sModuleName, $iAuthorId);
  65. if($iId == 0)
  66. return;
  67. $oCnlProfile = BxDolProfile::getInstanceByContentAndType($mixedCnlId, $this->_oConfig->getName());
  68. if(!$oCnlProfile)
  69. return;
  70. $iCnlProfileId = $oCnlProfile->id();
  71. bx_alert($this->_aModule['name'], 'hashtag_added', $iId, $iCnlProfileId, array(
  72. 'object_author_id' => $iAuthorId,
  73. 'privacy_view' => -$iCnlProfileId,
  74. 'timeline_group' => array(
  75. 'by' => $sModuleName . '_' . $iAuthorId . '_' . $iContentId,
  76. 'field' => 'owner_id'
  77. )
  78. ));
  79. $aParams = array(
  80. 'object_author_id' => $iAuthorId,
  81. 'privacy_view' => -$iCnlProfileId,
  82. 'subobject_id' => $iId,
  83. 'content_id' => $iContentId,
  84. 'content_author_id' => $iAuthorId
  85. );
  86. bx_alert('system', 'prepare_alert_params', 0, 0, array('unit'=> $this->_aModule['name'], 'action' => 'hashtag_added_notif', 'object_id' => $mixedCnlId, 'sender_id' => $iCnlProfileId, 'extras' => &$aParams));
  87. bx_alert($this->_aModule['name'], 'hashtag_added_notif', $mixedCnlId, $iCnlProfileId, $aParams);
  88. }
  89. function removeContentFromChannel($iContentId, $sModuleName)
  90. {
  91. $oDolProfileQuery = BxDolProfileQuery::getInstance();
  92. $aData = $this->_oDb->getDataByContent($iContentId, $sModuleName);
  93. foreach ($aData as $aRow) {
  94. $iProfileInfo = $oDolProfileQuery->getProfileByContentAndType($aRow['cnl_id'], $this->_aModule['name']);
  95. if(is_array($iProfileInfo)){
  96. bx_alert($this->_aModule['name'], 'hashtag_deleted', $aRow['id'], $iProfileInfo['id']);
  97. bx_alert($this->_aModule['name'], 'hashtag_deleted_notif', $aRow['cnl_id'], $iProfileInfo['id'], array('subobject_id' => $aRow['id']));
  98. }
  99. }
  100. return $this->_oDb->removeContentFromChannel($iContentId, $sModuleName);
  101. }
  102. public function serviceGetWidgetNotices()
  103. {
  104. $CNF = &$this->_oConfig->CNF;
  105. $iResult = 0;
  106. if(empty($this->_oDb->getParam($CNF['PARAM_DEFAULT_AUTHOR'])))
  107. $iResult += 1;
  108. return $iResult;
  109. }
  110. public function serviceGetOptionsDefaultAuthor()
  111. {
  112. $aResult = array(
  113. array('key' => '', 'value' => _t('_Select_one'))
  114. );
  115. $aAccountsIds = BxDolAccountQuery::getInstance()->getOperators();
  116. foreach($aAccountsIds as $iAccountId) {
  117. $aProfilesIds = BxDolAccount::getInstance($iAccountId)->getProfilesIds();
  118. foreach($aProfilesIds as $iProfileId)
  119. $aResult[] = array(
  120. 'key' => $iProfileId,
  121. 'value' => BxDolProfile::getInstance($iProfileId)->getDisplayName()
  122. );
  123. }
  124. return $aResult;
  125. }
  126. public function serviceEntityBreadcrumb($iContentId = 0)
  127. {
  128. if(!$iContentId)
  129. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  130. if(!$iContentId)
  131. return false;
  132. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  133. if(!$aContentInfo)
  134. return false;
  135. return $this->_oTemplate->entryBreadcrumb($aContentInfo);
  136. }
  137. public function serviceEntityParent($iContentId = 0)
  138. {
  139. return $this->_serviceTemplateFunc('entryParent', $iContentId);
  140. }
  141. public function serviceEntityChilds($iContentId = 0)
  142. {
  143. return $this->_serviceTemplateFunc('entryChilds', $iContentId);
  144. }
  145. function serviceSearchResultByHashtag($iContentId = 0)
  146. {
  147. $CNF = &$this->_oConfig->CNF;
  148. $oSearch = new BxTemplSearch();
  149. $oSearch->setLiveSearch(0);
  150. $oSearch->setMetaType('keyword');
  151. $aContentInfo = $this->_oDb->getContentInfoById(bx_get('id'));
  152. $_GET['keyword'] = $aContentInfo[$CNF['FIELD_NAME']];
  153. $sCode = $oSearch->response();
  154. if (!$sCode)
  155. $sCode = $oSearch->getEmptyResult();
  156. return $sCode;
  157. }
  158. /**
  159. * Data for Timeline module
  160. */
  161. public function serviceGetTimelineData()
  162. {
  163. $sModule = $this->_aModule['name'];
  164. return array(
  165. 'handlers' => array(
  166. 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' => ''),
  167. array('group' => $sModule . '_hastag', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_deleted')
  168. ),
  169. 'alerts' => array(
  170. array('unit' => $sModule, 'action' => 'hashtag_added'),
  171. array('unit' => $sModule, 'action' => 'hashtag_deleted')
  172. )
  173. );
  174. }
  175. public function serviceGetTimelinePostAllowedView ($aEvent)
  176. {
  177. $sError = _t('_sys_txt_access_denied');
  178. if(empty($aEvent) || !is_array($aEvent))
  179. return $sError;
  180. $aEventContent = $this->_oDb->getContentById($aEvent['object_id']);
  181. if(empty($aEventContent) || !is_array($aEventContent))
  182. return $sError;
  183. if(!BxDolRequest::serviceExists($aEventContent['module_name'], 'get_timeline_post_allowed_view'))
  184. return $sError;
  185. 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'])));
  186. }
  187. public function serviceGetTimelinePostHashtag($aEvent, $aBrowseParams = array())
  188. {
  189. if(empty($aEvent) || !is_array($aEvent))
  190. return false;
  191. $aEventContent = $this->_oDb->getContentById($aEvent['object_id']);
  192. if(empty($aEventContent) || !is_array($aEventContent))
  193. return false;
  194. if(!BxDolRequest::serviceExists($aEventContent['module_name'], 'get_timeline_post'))
  195. return false;
  196. return BxDolService::call($aEventContent['module_name'], 'get_timeline_post', array(
  197. array(
  198. 'id' => $aEvent['id'],
  199. 'owner_id' => $aEventContent['author_id'],
  200. 'object_id' => $aEventContent['content_id']
  201. ),
  202. $aBrowseParams
  203. ));
  204. }
  205. /**
  206. * Data for Notifications module
  207. */
  208. public function serviceGetNotificationsData()
  209. {
  210. $a = parent::serviceGetNotificationsData();
  211. $sModule = $this->_aModule['name'];
  212. $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');
  213. $a['handlers'][] = array('group' => $sModule . '_hastag_notif', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_deleted_notif');
  214. $a['settings'][] = array('group' => 'hastag', 'unit' => $sModule, 'action' => 'hashtag_added_notif', 'types' => array('follow_context'));
  215. $a['alerts'][] = array('unit' => $sModule, 'action' => 'hashtag_added_notif');
  216. $a['alerts'][] = array('unit' => $sModule, 'action' => 'hashtag_deleted_notif');
  217. return $a;
  218. }
  219. public function serviceGetNotificationsPostHashtag($aEvent)
  220. {
  221. if(empty($aEvent) || !is_array($aEvent))
  222. return '';
  223. $aContentEvent = $this->_oDb->getContentById($aEvent['subobject_id']);
  224. if(empty($aContentEvent) || !is_array($aContentEvent))
  225. return '';
  226. $oModule = BxDolModule::getInstance($aContentEvent['module_name']);
  227. if ($oModule) {
  228. if (isset($oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW'])){
  229. $oPrivacy = BxDolPrivacy::getObjectInstance($oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  230. if (!$oPrivacy->check($aContentEvent['content_id']))
  231. return '';
  232. }
  233. $aRv = $oModule->serviceGetNotificationsPost(array(
  234. 'object_id' => $aContentEvent['content_id']
  235. ));
  236. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added';
  237. if(method_exists($oModule, 'serviceActAsProfile') && $oModule->serviceActAsProfile())
  238. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added_profile';
  239. $aRv['entry_url'] = $this->serviceGetLink($aContentEvent['cnl_id']);
  240. return $aRv;
  241. }
  242. return '';
  243. }
  244. public function serviceBrowseByLevel ($iLevelId = 0, $bDisplayEmptyMsg = false)
  245. {
  246. echo $iLevelId;
  247. return $this->_serviceBrowse ('level', array('level' => $iLevelId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  248. }
  249. public function serviceBrowseAuthor($iProfileId = 0, $aParams = array())
  250. {
  251. $CNF = &$this->_oConfig->CNF;
  252. if(empty($iProfileId))
  253. $iProfileId = bx_get_logged_profile_id();
  254. $sResult = isset($aParams['empty_message']) && (bool)$aParams['empty_message'] === true ? MsgBox(_t('_Empty')) : '';
  255. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  256. $aProfile = $oConnection->getConnectedContent($iProfileId);
  257. $aVars = array();
  258. foreach ($aProfile as $iProfileId) {
  259. $oProfile = BxDolProfile::getInstance($iProfileId);
  260. if (!$oProfile || $oProfile->getModule() != $this->getName())
  261. continue;
  262. $iContentId = $oProfile->getContentId();
  263. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  264. if (isset($aContentInfo[$CNF['FIELD_NAME']]))
  265. 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)));
  266. }
  267. if(empty($aVars) || !is_array($aVars))
  268. return $sResult;
  269. return $this->_oTemplate->parseHtmlByName('my_channels.html', array(
  270. 'bx_if:show_list' => array(
  271. 'condition' => count($aVars) > 0,
  272. 'content' => array(
  273. 'bx_repeat:items' => $aVars
  274. )
  275. )
  276. ));
  277. }
  278. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  279. {
  280. return true;
  281. }
  282. public function serviceGetFollowingChannelsNames($iProfileId)
  283. {
  284. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  285. return array();
  286. if (!($aIds = $oConn->getConnectedContent($iProfileId)))
  287. return array();
  288. $a = array();
  289. foreach ($aIds as $iId) {
  290. if (!($oProfile = BxDolProfile::getInstance($iId)))
  291. continue;
  292. if ($oProfile->getModule() != $this->getName())
  293. continue;
  294. $a[] = $oProfile->getDisplayName();
  295. }
  296. return $a;
  297. }
  298. public function checkAllowedCompose (&$aDataEntry, $isPerformAction = false)
  299. {
  300. return _t('_sys_txt_access_denied');
  301. }
  302. public function followLabels($sModule, $iContentId)
  303. {
  304. if (!getParam('bx_channels_labels_autofollow'))
  305. return;
  306. if (!($oModuleProfiles = BxDolModule::getInstance($sModule)))
  307. return;
  308. if (!($aContentInfo = $oModuleProfiles->_oDb->getContentInfoById($iContentId)) || !($oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModule)))
  309. return;
  310. $CNF = &$oModuleProfiles->_oConfig->CNF;
  311. if (!$oModuleProfiles->serviceActAsProfile() || !isset($CNF['FIELD_LABELS']) || empty($aContentInfo[$CNF['FIELD_ID']]))
  312. return;
  313. if (!($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) || !$oMetatags->keywordsIsEnabled() || !($aLabels = $oMetatags->keywordsGet($aContentInfo[$CNF['FIELD_ID']])))
  314. return;
  315. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  316. return;
  317. foreach ($aLabels as $sLabel) {
  318. if (!($iContentIdChannel = $this->_oDb->getChannelIdByName($sLabel)))
  319. continue;
  320. if (!($oProfileChannel = BxDolProfile::getInstanceByContentAndType($iContentIdChannel, 'bx_channels')))
  321. continue;
  322. $oConn->addConnection($oProfile->id(), $oProfileChannel->id());
  323. }
  324. }
  325. /** Returns profile's subscribed channels React Jot
  326. *
  327. * @param int $iProfileId
  328. * @param array $aParams
  329. * @return array
  330. */
  331. public function serviceBrowseAuthorChannels($iProfileId = 0, $aParams = array())
  332. {
  333. $CNF = &$this->_oConfig->CNF;
  334. if(empty($iProfileId))
  335. $iProfileId = bx_get_logged_profile_id();
  336. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  337. $aProfile = $oConnection->getConnectedContent($iProfileId);
  338. $aVars = array();
  339. foreach ($aProfile as $iProfileId) {
  340. $oProfile = BxDolProfile::getInstance($iProfileId);
  341. if (!$oProfile || $oProfile->getModule() != $this->getName())
  342. continue;
  343. $iContentId = $oProfile->getContentId();
  344. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  345. if (isset($aContentInfo[$CNF['FIELD_NAME']]))
  346. array_push($aVars, array(
  347. 'title' => $aContentInfo[$CNF['FIELD_NAME']],
  348. 'link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId),
  349. 'followers' => $oConnection -> getConnectedInitiatorsCount($iProfileId),
  350. 'id' => $iProfileId,
  351. 'icon' => $oProfile -> getThumb()
  352. ));
  353. }
  354. return $aVars;
  355. }
  356. }
  357. /** @} */