BxCnlModule.php 17 KB

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