BxBaseModGeneralSearchResult.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. * @defgroup BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_SYS_PER_PAGE_BROWSE_SHOWCASE', 32);
  12. class BxBaseModGeneralSearchResult extends BxTemplSearchResult
  13. {
  14. protected $oModule;
  15. protected $sFilterName;
  16. protected $bShowcaseView = false;
  17. protected $aUnitViews = array();
  18. protected $sUnitViewDefault = 'gallery';
  19. function __construct($sMode = '', $aParams = array())
  20. {
  21. $this->_sMode = $sMode;
  22. $this->_aParams = $aParams;
  23. parent::__construct();
  24. }
  25. function getMain()
  26. {
  27. if(!$this->oModule)
  28. $this->oModule = BxDolModule::getInstance($this->getModuleName());
  29. return $this->oModule;
  30. }
  31. function getContentInfoObject()
  32. {
  33. return BxDolContentInfo::getObjectInstance($this->getContentInfoName());
  34. }
  35. function getFieldsOwn()
  36. {
  37. $mixedFields = getParam($this->getModuleName() . '_browse_fields_own');
  38. if(empty($mixedFields))
  39. return false;
  40. if(($aFields = json_decode($mixedFields, true)) !== null)
  41. return $aFields;
  42. $sDelimiter = ',';
  43. if(strpos($mixedFields, $sDelimiter) !== false)
  44. return explode($sDelimiter, $mixedFields);
  45. return false;
  46. }
  47. function getFieldsJoin($sJoin)
  48. {
  49. $mixedFields = getParam($this->getModuleName() . '_browse_fields_join');
  50. if(empty($mixedFields))
  51. return false;
  52. return ($aJoins = json_decode($mixedFields, true)) !== null && !empty($aJoins[$sJoin]) ? $aJoins[$sJoin] : false;
  53. }
  54. function getRssUnitLink (&$a)
  55. {
  56. $CNF = &$this->oModule->_oConfig->CNF;
  57. return bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $a[$CNF['FIELD_ID']]));
  58. }
  59. function getRssPageUrl ()
  60. {
  61. if (false === parent::getRssPageUrl())
  62. return false;
  63. $oPermalinks = BxDolPermalinks::getInstance();
  64. return bx_absolute_url($oPermalinks->permalink($this->aCurrent['rss']['link']));
  65. }
  66. function rss ()
  67. {
  68. if (!isset($this->aCurrent['rss']))
  69. return '';
  70. $this->aCurrent['paginate']['perPage'] = empty($this->oModule->_oConfig->CNF['PARAM_NUM_RSS']) ? 10 : getParam($this->oModule->_oConfig->CNF['PARAM_NUM_RSS']);
  71. return parent::rss();
  72. }
  73. function processingAPI ()
  74. {
  75. $aResult = parent::processingAPI();
  76. if(isset($this->_aParams['filters']) && is_array($this->_aParams['filters'])) {
  77. $oModule = $this->getMain();
  78. if(!empty($this->_aParams['filters']['values']) && is_array($this->_aParams['filters']['values']))
  79. $aResult['params']['filters'] = $this->_aParams['filters']['values'];
  80. $aResult['filters'] = $oModule->_oTemplate->getBrowsingFilters(['mode' => $this->_sMode]);
  81. }
  82. return $aResult;
  83. }
  84. protected function addCustomConditions($CNF, $oProfile, $sMode, $aParams)
  85. {
  86. $this->addConditionsForAuthorStatus($CNF);
  87. $this->addConditionsForCf($CNF);
  88. if(!empty($aParams['filter']) && is_array($aParams['filter']))
  89. $this->addConditionsForFilter($CNF, $sMode, $aParams);
  90. }
  91. protected function addConditionsForAuthorStatus($CNF)
  92. {
  93. if (empty($CNF['FIELD_AUTHOR']))
  94. return;
  95. $this->aCurrent['restriction']['statusAuthor'] = [
  96. 'value' => 'active',
  97. 'field' => 'status',
  98. 'operator' => '=',
  99. 'table' => 'sys_profiles',
  100. ];
  101. $this->aCurrent['join']['statusAuthor'] = [
  102. 'type' => 'INNER',
  103. 'table' => 'sys_profiles',
  104. 'mainField' => $CNF['FIELD_AUTHOR'],
  105. 'mainFieldFunc' => 'ABS',
  106. 'onField' => 'id',
  107. 'joinFields' => array(),
  108. ];
  109. }
  110. protected function addConditionsForCf($CNF)
  111. {
  112. if(empty($CNF['FIELD_CF']))
  113. return;
  114. $oCf = BxDolContentFilter::getInstance();
  115. if(!$oCf->isEnabled())
  116. return;
  117. $aConditions = $oCf->getConditions($this->aCurrent['table'], $CNF['FIELD_CF']);
  118. if(!empty($aConditions) && is_array($aConditions))
  119. $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aConditions);
  120. }
  121. protected function addConditionsForFilter($CNF, $sMode, $aParams)
  122. {
  123. $aFilter = $aParams['filter'];
  124. if(empty($aFilter['field']) || empty($aFilter['value']))
  125. return;
  126. $aRestriction = [
  127. 'value' => $aFilter['value'],
  128. 'field' => $aFilter['field'],
  129. 'operator' => '=',
  130. ];
  131. if(isset($aFilter['operator']))
  132. $aRestriction['operator'] = $aFilter['operator'];
  133. if(isset($aFilter['table']))
  134. switch($aFilter['table']) {
  135. case 'table':
  136. $aRestriction['table'] = $this->aCurrent['table'];
  137. break;
  138. case 'tableSearch':
  139. $aRestriction['table'] = $this->aCurrent['tableSearch'];
  140. break;
  141. default:
  142. $aRestriction['table'] = $aFilter['table'];
  143. }
  144. $this->aCurrent['restriction']['filter'] = $aRestriction;
  145. }
  146. /**
  147. * Add conditions for private content
  148. */
  149. protected function addConditionsForPrivateContent($CNF, $oProfile, $aCustomGroup = array())
  150. {
  151. // default is bProcessPrivateContent = 1,
  152. // so private items are shown as empty boxes with "Private" title
  153. // we can show public content when privacy object is available
  154. if(empty($CNF['OBJECT_PRIVACY_VIEW']))
  155. return;
  156. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
  157. if(!$oPrivacy)
  158. return;
  159. // for posts in some context we need to show all items, not only public content
  160. if (!empty($this->aCurrent['restriction']['context']['value']) || !empty($this->aCurrent['restriction']['author']['value'])) {
  161. $this->setProcessPrivateContent(true);
  162. return;
  163. }
  164. // build condition to show only public content
  165. $aCondition = $oPrivacy->getContentPublicAsCondition($oProfile ? $oProfile->id() : 0, $aCustomGroup);
  166. if(empty($aCondition) || !is_array($aCondition))
  167. return;
  168. if(isset($aCondition['restriction'])) {
  169. $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aCondition['restriction']);
  170. $this->aPrivateConditionsIndexes['restriction'] = array_keys($aCondition['restriction']);
  171. }
  172. if(isset($aCondition['join'])) {
  173. $this->aCurrent['join'] = array_merge($this->aCurrent['join'], $aCondition['join']);
  174. $this->aPrivateConditionsIndexes['join'] = array_keys($aCondition['join']);
  175. }
  176. $this->setProcessPrivateContent(false);
  177. }
  178. function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true)
  179. {
  180. if($this->bShowcaseView)
  181. return '';
  182. $sPagination = parent::showPagination ($bAdmin, $bChangePage, $bPageReload);
  183. if(empty($sPagination))
  184. return '';
  185. return $sPagination;
  186. }
  187. protected function getItemPerPageInShowCase ()
  188. {
  189. $iPerPageInShowCase = (int)getParam('sys_per_page_browse_showcase');
  190. $CNF = &$this->oModule->_oConfig->CNF;
  191. if(isset($CNF['PARAM_PER_PAGE_BROWSE_SHOWCASE']))
  192. $iPerPageInShowCase = (int)getParam($CNF['PARAM_PER_PAGE_BROWSE_SHOWCASE']);
  193. if(!$iPerPageInShowCase)
  194. $iPerPageInShowCase = BX_SYS_PER_PAGE_BROWSE_SHOWCASE;
  195. return $iPerPageInShowCase;
  196. }
  197. function displayResultBlock()
  198. {
  199. if ($this->bShowcaseView) {
  200. $this->addContainerClass(array('bx-base-unit-showcase-wrapper'));
  201. $this->aCurrent['paginate']['perPage'] = $this->getItemPerPageInShowCase();
  202. $this->oModule->_oTemplate->addCss(array(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css'));
  203. $this->oModule->_oTemplate->addJs(array('flickity/flickity.pkgd.min.js','modules/base/general/js/|showcase.js'));
  204. }
  205. return parent::displayResultBlock();
  206. }
  207. function displaySearchBox ($sContent, $sPaginate = '')
  208. {
  209. $aResult = parent::displaySearchBox($sContent, $sPaginate);
  210. if(isset($this->_aParams['filters']) && is_array($this->_aParams['filters']))
  211. $aResult['buttons'] = [
  212. ['title' => _t('_Filters'), 'href' => 'javascript:void(0)', 'onclick' => 'javascript:' . $this->_aParams['filters']['onclick']]
  213. ];
  214. return $aResult;
  215. }
  216. function applyContainerId()
  217. {
  218. if(empty($this->aCurrent['name']) || empty($this->_sMode))
  219. return parent::applyContainerId();
  220. return str_replace('_', '-', $this->aCurrent['name'] . '-search-result-block-' . $this->_sMode);
  221. }
  222. function decodeDataAPI($a)
  223. {
  224. if(!is_array($a))
  225. return $a;
  226. $bExtendedUnits = getParam('sys_api_extended_units') == 'on';
  227. foreach($a as $i => $r)
  228. $a[$i] = $this->oModule->getDataAPI($r, ['extended' => $bExtendedUnits]);
  229. return $a;
  230. }
  231. protected function _updateCurrentForFollowedContexts($sMode, $aParams, &$oProfileContext)
  232. {
  233. $CNF = &$this->oModule->_oConfig->CNF;
  234. if(empty($aParams['followed_contexts']))
  235. return false;
  236. $iProfileId = (int)$aParams['followed_contexts'];
  237. $aContextTypes = array_keys(bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]));
  238. $aContextIds = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')->getConnectedContentByType($iProfileId, $aContextTypes);
  239. $this->aCurrent['restriction']['context'] = [
  240. 'value' => array_map(function($iValue) {
  241. return -$iValue;
  242. }, $aContextIds),
  243. 'field' => $CNF['FIELD_ALLOW_VIEW_TO'],
  244. 'operator' => 'in',
  245. ];
  246. if(!empty($aParams['per_page']))
  247. $this->aCurrent['paginate']['perPage'] = is_numeric($aParams['per_page']) ? (int)$aParams['per_page'] : (int)getParam($aParams['per_page']);
  248. $this->sBrowseUrl = '';
  249. $this->aCurrent['title'] = '';
  250. unset($this->aCurrent['rss']);
  251. return true;
  252. }
  253. function _getPseudFromParam ()
  254. {
  255. $mixedPseud = getParam($this->getModuleName() . '_browse_pseud');
  256. if(empty($mixedPseud))
  257. return false;
  258. if(($aPseud = json_decode($mixedPseud, true)) !== null)
  259. return $aPseud;
  260. return false;
  261. }
  262. }
  263. /** @} */