BxBaseModGeneralSearchResult.php 8.6 KB

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