BxCnlModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. $aCheck = checkActionModule($this->_iProfileId, 'create channel auto', $this->getName(), false);
  42. $mixedCnlId = $this->_oDb->getChannelIdByName($sHashtag);
  43. if (empty($mixedCnlId) && ($aCheck[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED)){
  44. $CNF = &$this->_oConfig->CNF;
  45. $oAccountQuery = BxDolAccountQuery::getInstance();
  46. $aOperators = $oAccountQuery->getOperators();
  47. if(count($aOperators) > 0){
  48. $oProfile = BxDolProfile::getInstanceByAccount($aOperators[0]);
  49. $aContent = $this->serviceEntityAdd($oProfile->id(), array($CNF['FIELD_NAME'] => $sHashtag));
  50. checkActionModule($this->_iProfileId, 'create channel auto', $this->getName(), true);
  51. if (isset($aContent['content']) && isset($aContent['content']['id']))
  52. $mixedCnlId = $aContent['content']['id'];
  53. }
  54. }
  55. if(empty($mixedCnlId) || (int)$this->_oDb->checkContentInChannel($iContentId, $mixedCnlId, $sModuleName, $iAuthorId) != 0)
  56. return;
  57. $iId = (int)$this->_oDb->addContentToChannel($iContentId, $mixedCnlId, $sModuleName, $iAuthorId);
  58. if($iId == 0)
  59. return;
  60. $oCnlProfile = BxDolProfile::getInstanceByContentAndType($mixedCnlId, $this->_oConfig->getName());
  61. if(!$oCnlProfile)
  62. return;
  63. $iCnlProfileId = $oCnlProfile->id();
  64. bx_alert($this->_aModule['name'], 'hashtag_added', $iId, $iCnlProfileId, array(
  65. 'object_author_id' => $iAuthorId,
  66. 'privacy_view' => -$iCnlProfileId,
  67. 'timeline_group' => array(
  68. 'by' => $sModuleName . '_' . $iAuthorId . '_' . $iContentId,
  69. 'field' => 'owner_id'
  70. )
  71. ));
  72. bx_alert($this->_aModule['name'], 'hashtag_added_notif', $mixedCnlId, $iCnlProfileId, array(
  73. 'object_author_id' => $iAuthorId,
  74. 'privacy_view' => -$iCnlProfileId,
  75. 'subobject_id' => $iId,
  76. 'content_id' => $iContentId,
  77. 'content_author_id' => $iAuthorId
  78. ));
  79. }
  80. function removeContentFromChannel($iContentId, $sModuleName)
  81. {
  82. $oDolProfileQuery = BxDolProfileQuery::getInstance();
  83. $aData = $this->_oDb->getDataByContent($iContentId, $sModuleName);
  84. foreach ($aData as $aRow) {
  85. $iProfileInfo = $oDolProfileQuery->getProfileByContentAndType($aRow['cnl_id'], $this->_aModule['name']);
  86. if(is_array($iProfileInfo)){
  87. bx_alert($this->_aModule['name'], 'hashtag_deleted', $aRow['id'], $iProfileInfo['id']);
  88. bx_alert($this->_aModule['name'], 'hashtag_deleted_notif', $aRow['cnl_id'], $iProfileInfo['id'], array('subobject_id' => $aRow['id']));
  89. }
  90. }
  91. return $this->_oDb->removeContentFromChannel($iContentId, $sModuleName);
  92. }
  93. public function serviceEntityBreadcrumb($iContentId = 0)
  94. {
  95. if(!$iContentId)
  96. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  97. if(!$iContentId)
  98. return false;
  99. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  100. if(!$aContentInfo)
  101. return false;
  102. return $this->_oTemplate->entryBreadcrumb($aContentInfo);
  103. }
  104. public function serviceEntityParent($iContentId = 0)
  105. {
  106. return $this->_serviceTemplateFunc('entryParent', $iContentId);
  107. }
  108. public function serviceEntityChilds($iContentId = 0)
  109. {
  110. return $this->_serviceTemplateFunc('entryChilds', $iContentId);
  111. }
  112. function serviceSearchResultByHashtag($iContentId = 0)
  113. {
  114. $CNF = &$this->_oConfig->CNF;
  115. $oSearch = new BxTemplSearch();
  116. $oSearch->setLiveSearch(0);
  117. $oSearch->setMetaType('keyword');
  118. $aContentInfo = $this->_oDb->getContentInfoById(bx_get('id'));
  119. $_GET['keyword'] = $aContentInfo[$CNF['FIELD_NAME']];
  120. $sCode = $oSearch->response();
  121. if (!$sCode)
  122. $sCode = $oSearch->getEmptyResult();
  123. return $sCode;
  124. }
  125. /**
  126. * Data for Timeline module
  127. */
  128. public function serviceGetTimelineData()
  129. {
  130. $sModule = $this->_aModule['name'];
  131. return array(
  132. 'handlers' => array(
  133. 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' => ''),
  134. array('group' => $sModule . '_hastag', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_deleted')
  135. ),
  136. 'alerts' => array(
  137. array('unit' => $sModule, 'action' => 'hashtag_added'),
  138. array('unit' => $sModule, 'action' => 'hashtag_deleted')
  139. )
  140. );
  141. }
  142. public function serviceGetTimelinePostHashtag($aEvent, $aBrowseParams = array())
  143. {
  144. if(empty($aEvent) || !is_array($aEvent))
  145. return false;
  146. $aEventContent = $this->_oDb->getContentById($aEvent['object_id']);
  147. if(empty($aEventContent) || !is_array($aEventContent))
  148. return false;
  149. if(!BxDolRequest::serviceExists($aEventContent['module_name'], 'get_timeline_post'))
  150. return false;
  151. return BxDolService::call($aEventContent['module_name'], 'get_timeline_post', array(array('owner_id' => $aEventContent['author_id'], 'object_id' => $aEventContent['content_id'])));
  152. }
  153. /**
  154. * Data for Notifications module
  155. */
  156. public function serviceGetNotificationsData()
  157. {
  158. $a = parent::serviceGetNotificationsData();
  159. $sModule = $this->_aModule['name'];
  160. $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');
  161. $a['handlers'][] = array('group' => $sModule . '_hastag_notif', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_deleted_notif');
  162. $a['settings'][] = array('group' => 'hastag', 'unit' => $sModule, 'action' => 'hashtag_added_notif', 'types' => array('follow_context'));
  163. $a['alerts'][] = array('unit' => $sModule, 'action' => 'hashtag_added_notif');
  164. $a['alerts'][] = array('unit' => $sModule, 'action' => 'hashtag_deleted_notif');
  165. return $a;
  166. }
  167. public function serviceGetNotificationsPostHashtag($aEvent)
  168. {
  169. if(empty($aEvent) || !is_array($aEvent))
  170. return '';
  171. $aContentEvent = $this->_oDb->getContentById($aEvent['subobject_id']);
  172. if(empty($aContentEvent) || !is_array($aContentEvent))
  173. return '';
  174. $oModule = BxDolModule::getInstance($aContentEvent['module_name']);
  175. if ($oModule) {
  176. if (isset($oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW'])){
  177. $oPrivacy = BxDolPrivacy::getObjectInstance($oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  178. if (!$oPrivacy->check($aContentEvent['content_id']))
  179. return '';
  180. }
  181. $aRv = $oModule->serviceGetNotificationsPost(array(
  182. 'object_id' => $aContentEvent['content_id']
  183. ));
  184. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added';
  185. if(method_exists($oModule, 'serviceActAsProfile') && $oModule->serviceActAsProfile())
  186. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added_profile';
  187. $aRv['entry_url'] = $this->serviceGetLink($aContentEvent['cnl_id']);
  188. return $aRv;
  189. }
  190. return '';
  191. }
  192. public function serviceBrowseAuthor($iProfileId = 0, $aParams = array())
  193. {
  194. $CNF = &$this->_oConfig->CNF;
  195. if(empty($iProfileId))
  196. $iProfileId = bx_get_logged_profile_id();
  197. $sResult = isset($aParams['empty_message']) && (bool)$aParams['empty_message'] === true ? MsgBox(_t('_Empty')) : '';
  198. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  199. $aProfile = $oConnection->getConnectedContent($iProfileId);
  200. $aVars = array();
  201. foreach ($aProfile as $iProfileId) {
  202. $oProfile = BxDolProfile::getInstance($iProfileId);
  203. if (!$oProfile || $oProfile->getModule() != $this->getName())
  204. continue;
  205. $iContentId = $oProfile->getContentId();
  206. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  207. if (isset($aContentInfo[$CNF['FIELD_NAME']]))
  208. 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)));
  209. }
  210. if(empty($aVars) || !is_array($aVars))
  211. return $sResult;
  212. return $this->_oTemplate->parseHtmlByName('my_channels.html', array(
  213. 'bx_if:show_list' => array(
  214. 'condition' => count($aVars) > 0,
  215. 'content' => array(
  216. 'bx_repeat:items' => $aVars
  217. )
  218. )
  219. ));
  220. }
  221. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  222. {
  223. return true;
  224. }
  225. public function serviceGetFollowingChannelsNames($iProfileId)
  226. {
  227. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  228. return array();
  229. if (!($aIds = $oConn->getConnectedContent($iProfileId)))
  230. return array();
  231. $a = array();
  232. foreach ($aIds as $iId) {
  233. if (!($oProfile = BxDolProfile::getInstance($iId)))
  234. continue;
  235. if ($oProfile->getModule() != $this->getName())
  236. continue;
  237. $a[] = $oProfile->getDisplayName();
  238. }
  239. return $a;
  240. }
  241. public function checkAllowedCompose (&$aDataEntry, $isPerformAction = false)
  242. {
  243. return _t('_sys_txt_access_denied');
  244. }
  245. public function followLabels($sModule, $iContentId)
  246. {
  247. if (!getParam('bx_channels_labels_autofollow'))
  248. return;
  249. if (!($oModuleProfiles = BxDolModule::getInstance($sModule)))
  250. return;
  251. if (!($aContentInfo = $oModuleProfiles->_oDb->getContentInfoById($iContentId)) || !($oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModule)))
  252. return;
  253. $CNF = &$oModuleProfiles->_oConfig->CNF;
  254. if (!$oModuleProfiles->serviceActAsProfile() || !isset($CNF['FIELD_LABELS']) || empty($aContentInfo[$CNF['FIELD_ID']]))
  255. return;
  256. if (!($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) || !$oMetatags->keywordsIsEnabled() || !($aLabels = $oMetatags->keywordsGet($aContentInfo[$CNF['FIELD_ID']])))
  257. return;
  258. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  259. return;
  260. foreach ($aLabels as $sLabel) {
  261. if (!($iContentIdChannel = $this->_oDb->getChannelIdByName($sLabel)))
  262. continue;
  263. if (!($oProfileChannel = BxDolProfile::getInstanceByContentAndType($iContentIdChannel, 'bx_channels')))
  264. continue;
  265. $oConn->addConnection($oProfile->id(), $oProfileChannel->id());
  266. }
  267. }
  268. }
  269. /** @} */