BxCnlModule.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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(
  164. 'object_id' => $aContentEvent['content_id']
  165. ));
  166. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added';
  167. if(method_exists($oModule, 'serviceActAsProfile') && $oModule->serviceActAsProfile())
  168. $aRv['lang_key'] = '_bx_channels_ntfs_txt_subobject_added_profile';
  169. $aRv['entry_url'] = $this->serviceGetLink($aContentEvent['cnl_id']);
  170. return $aRv;
  171. }
  172. return '';
  173. }
  174. public function serviceBrowseAuthor($iProfileId = 0, $aParams = array())
  175. {
  176. $CNF = &$this->_oConfig->CNF;
  177. if(empty($iProfileId))
  178. $iProfileId = bx_get_logged_profile_id();
  179. $sResult = isset($aParams['empty_message']) && (bool)$aParams['empty_message'] === true ? MsgBox(_t('_Empty')) : '';
  180. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  181. $aProfile = $oConnection->getConnectedContent($iProfileId);
  182. $aVars = array();
  183. foreach ($aProfile as $iProfileId) {
  184. $oProfile = BxDolProfile::getInstance($iProfileId);
  185. if (!$oProfile || $oProfile->getModule() != $this->getName())
  186. continue;
  187. $iContentId = $oProfile->getContentId();
  188. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  189. if (isset($aContentInfo[$CNF['FIELD_NAME']]))
  190. 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)));
  191. }
  192. if(empty($aVars) || !is_array($aVars))
  193. return $sResult;
  194. return $this->_oTemplate->parseHtmlByName('my_channels.html', array(
  195. 'bx_if:show_list' => array(
  196. 'condition' => count($aVars) > 0,
  197. 'content' => array(
  198. 'bx_repeat:items' => $aVars
  199. )
  200. )
  201. ));
  202. }
  203. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  204. {
  205. return true;
  206. }
  207. public function serviceGetFollowingChannelsNames($iProfileId)
  208. {
  209. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  210. return array();
  211. if (!($aIds = $oConn->getConnectedContent($iProfileId)))
  212. return array();
  213. $a = array();
  214. foreach ($aIds as $iId) {
  215. if (!($oProfile = BxDolProfile::getInstance($iId)))
  216. continue;
  217. if ($oProfile->getModule() != $this->getName())
  218. continue;
  219. $a[] = $oProfile->getDisplayName();
  220. }
  221. return $a;
  222. }
  223. public function checkAllowedCompose (&$aDataEntry, $isPerformAction = false)
  224. {
  225. return _t('_sys_txt_access_denied');
  226. }
  227. public function followLabels($sModule, $iContentId)
  228. {
  229. if (!getParam('bx_channels_labels_autofollow'))
  230. return;
  231. if (!($oModuleProfiles = BxDolModule::getInstance($sModule)))
  232. return;
  233. if (!($aContentInfo = $oModuleProfiles->_oDb->getContentInfoById($iContentId)) || !($oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModule)))
  234. return;
  235. $CNF = &$oModuleProfiles->_oConfig->CNF;
  236. if (!$oModuleProfiles->serviceActAsProfile() || !isset($CNF['FIELD_LABELS']) || empty($aContentInfo[$CNF['FIELD_ID']]))
  237. return;
  238. if (!($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) || !$oMetatags->keywordsIsEnabled() || !($aLabels = $oMetatags->keywordsGet($aContentInfo[$CNF['FIELD_ID']])))
  239. return;
  240. if (!($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  241. return;
  242. foreach ($aLabels as $sLabel) {
  243. if (!($iContentIdChannel = $this->_oDb->getChannelIdByName($sLabel)))
  244. continue;
  245. if (!($oProfileChannel = BxDolProfile::getInstanceByContentAndType($iContentIdChannel, 'bx_channels')))
  246. continue;
  247. $oConn->addConnection($oProfile->id(), $oProfileChannel->id());
  248. }
  249. }
  250. }
  251. /** @} */