BxCnlModule.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. function serviceSearchResultByHashtag($iContentId = 0)
  94. {
  95. $CNF = &$this->_oConfig->CNF;
  96. $oSearch = new BxTemplSearch();
  97. $oSearch->setLiveSearch(0);
  98. $oSearch->setMetaType('keyword');
  99. $aContentInfo = $this->_oDb->getContentInfoById(bx_get('id'));
  100. $_GET['keyword'] = $aContentInfo[$CNF['FIELD_NAME']];
  101. $sCode = $oSearch->response();
  102. if (!$sCode)
  103. $sCode = $oSearch->getEmptyResult();
  104. return $sCode;
  105. }
  106. /**
  107. * Data for Timeline module
  108. */
  109. public function serviceGetTimelineData()
  110. {
  111. $sModule = $this->_aModule['name'];
  112. return array(
  113. 'handlers' => array(
  114. 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' => ''),
  115. array('group' => $sModule . '_hastag', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_deleted')
  116. ),
  117. 'alerts' => array(
  118. array('unit' => $sModule, 'action' => 'hashtag_added'),
  119. array('unit' => $sModule, 'action' => 'hashtag_deleted')
  120. )
  121. );
  122. }
  123. public function serviceGetTimelinePostHashtag($aEvent, $aBrowseParams = array())
  124. {
  125. if(empty($aEvent) || !is_array($aEvent))
  126. return false;
  127. $aEventContent = $this->_oDb->getContentById($aEvent['object_id']);
  128. if(empty($aEventContent) || !is_array($aEventContent))
  129. return false;
  130. if(!BxDolRequest::serviceExists($aEventContent['module_name'], 'get_timeline_post'))
  131. return false;
  132. $iEventOwnerId = (int)(is_array($aEvent['owner_id']) ? array_shift($aEvent['owner_id']) : $aEvent['owner_id']);
  133. return BxDolService::call($aEventContent['module_name'], 'get_timeline_post', array(array('owner_id' => $iEventOwnerId, 'object_id' => $aEventContent['content_id'])));
  134. }
  135. /**
  136. * Data for Notifications module
  137. */
  138. public function serviceGetNotificationsData()
  139. {
  140. $a = parent::serviceGetNotificationsData();
  141. $sModule = $this->_aModule['name'];
  142. $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');
  143. $a['handlers'][] = array('group' => $sModule . '_hastag_notif', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'hashtag_deleted_notif');
  144. $a['settings'][] = array('group' => 'hastag', 'unit' => $sModule, 'action' => 'hashtag_added_notif', 'types' => array('follow_context'));
  145. $a['alerts'][] = array('unit' => $sModule, 'action' => 'hashtag_added_notif');
  146. $a['alerts'][] = array('unit' => $sModule, 'action' => 'hashtag_deleted_notif');
  147. return $a;
  148. }
  149. public function serviceGetNotificationsPostHashtag($aEvent)
  150. {
  151. if(empty($aEvent) || !is_array($aEvent))
  152. return '';
  153. $aContentEvent = $this->_oDb->getContentById($aEvent['subobject_id']);
  154. if(empty($aContentEvent) || !is_array($aContentEvent))
  155. return '';
  156. $oModule = BxDolModule::getInstance($aContentEvent['module_name']);
  157. if ($oModule){
  158. if (isset($oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW'])){
  159. $oPrivacy = BxDolPrivacy::getObjectInstance($oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  160. if (!$oPrivacy->check($aContentEvent['content_id']))
  161. return '';
  162. }
  163. $aRv = $oModule->serviceGetNotificationsPost(array('object_id' => $aContentEvent['content_id']));
  164. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added';
  165. $aRv['channel_url'] = $this->serviceGetLink($aContentEvent['cnl_id']);
  166. return $aRv;
  167. }
  168. return '';
  169. }
  170. public function serviceBrowseMyChannels($aParams = array())
  171. {
  172. $sResult = isset($aParams['empty_message']) && (bool)$aParams['empty_message'] === true ? MsgBox(_t('_Empty')) : '';
  173. if(!isLogged())
  174. return $sResult;
  175. $CNF = &$this->_oConfig->CNF;
  176. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  177. $aProfile = $oConnection->getConnectedContent(bx_get_logged_profile_id());
  178. $aVars = array();
  179. foreach ($aProfile as $iProfileId) {
  180. $oProfile = BxDolProfile::getInstance($iProfileId);
  181. if (!$oProfile || $oProfile->getModule() != $this->getName())
  182. continue;
  183. $iContentId = $oProfile->getContentId();
  184. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  185. if (isset($aContentInfo[$CNF['FIELD_NAME']]))
  186. 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)));
  187. }
  188. if(empty($aVars) || !is_array($aVars))
  189. return $sResult;
  190. return $this->_oTemplate->parseHtmlByName('my_channels.html',
  191. array('bx_if:show_list' => array(
  192. 'condition' => count($aVars) > 0,
  193. 'content' => array('bx_repeat:items' => $aVars))
  194. ));
  195. }
  196. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  197. {
  198. return true;
  199. }
  200. public function serviceGetFollowingChannelsNames($iProfileId)
  201. {
  202. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  203. return array();
  204. if (!($aIds = $oConn->getConnectedContent($iProfileId)))
  205. return array();
  206. $a = array();
  207. foreach ($aIds as $iId) {
  208. if (!($oProfile = BxDolProfile::getInstance($iId)))
  209. continue;
  210. if ($oProfile->getModule() != $this->getName())
  211. continue;
  212. $a[] = $oProfile->getDisplayName();
  213. }
  214. return $a;
  215. }
  216. public function checkAllowedCompose (&$aDataEntry, $isPerformAction = false)
  217. {
  218. return _t('_sys_txt_access_denied');
  219. }
  220. public function followLabels($sModule, $iContentId)
  221. {
  222. if (!getParam('bx_channels_labels_autofollow'))
  223. return;
  224. if (!($oModuleProfiles = BxDolModule::getInstance($sModule)))
  225. return;
  226. if (!($aContentInfo = $oModuleProfiles->_oDb->getContentInfoById($iContentId)) || !($oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModule)))
  227. return;
  228. $CNF = &$oModuleProfiles->_oConfig->CNF;
  229. if (!$oModuleProfiles->serviceActAsProfile() || !isset($CNF['FIELD_LABELS']) || empty($aContentInfo[$CNF['FIELD_ID']]))
  230. return;
  231. if (!($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) || !$oMetatags->keywordsIsEnabled() || !($aLabels = $oMetatags->keywordsGet($aContentInfo[$CNF['FIELD_ID']])))
  232. return;
  233. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  234. return;
  235. foreach ($aLabels as $sLabel) {
  236. if (!($iContentIdChannel = $this->_oDb->getChannelIdByName($sLabel)))
  237. continue;
  238. if (!($oProfileChannel = BxDolProfile::getInstanceByContentAndType($iContentIdChannel, 'bx_channels')))
  239. continue;
  240. $oConn->addConnection($oProfile->id(), $oProfileChannel->id());
  241. }
  242. }
  243. }
  244. /** @} */