BxBaseModGeneralSearchResult.php 9.7 KB

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