BxBaseModGeneralSearchResult.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 ($bForceGetData = false)
  74. {
  75. $aResult = parent::processingAPI($bForceGetData);
  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. $mixedFilters = $oModule->_oTemplate->getBrowsingFilters(array_merge(['mode' => $this->_sMode], $this->_aParams));
  81. if($mixedFilters)
  82. $aResult['filters'] = $mixedFilters;
  83. }
  84. return $aResult;
  85. }
  86. protected function addCustomConditions($CNF, $oProfile, $sMode, $aParams)
  87. {
  88. $this->addConditionsForAuthorStatus($CNF);
  89. $this->addConditionsForCf($CNF);
  90. if(!empty($aParams['filter']) && is_array($aParams['filter']))
  91. $this->addConditionsForFilter($CNF, $sMode, $aParams);
  92. $this->addConditionsForSearchFilters($CNF, $aParams);
  93. }
  94. /**
  95. * Search-form values posted as params.filters ({title: "toy"}).
  96. * Operators come from the module's extended search fields, so a value
  97. * is applied only when that field is an active search field on the entries table.
  98. */
  99. protected function addConditionsForSearchFilters($CNF, $aParams)
  100. {
  101. $aValues = [];
  102. if(!empty($aParams['filters']['values']) && is_array($aParams['filters']['values']))
  103. $aValues = $aParams['filters']['values'];
  104. elseif(!empty($aParams['filters']) && is_array($aParams['filters']) && !isset($aParams['filters']['onclick']))
  105. $aValues = $aParams['filters'];
  106. if(!$aValues || !($sModule = $this->getModuleName()))
  107. return;
  108. $sTable = !empty($CNF['TABLE_ENTRIES']) ? $CNF['TABLE_ENTRIES'] : ($this->aCurrent['table'] ?? '');
  109. if($sTable === '')
  110. return;
  111. $oDb = BxDolDb::getInstance();
  112. $aFields = $oDb->getPairs(
  113. "SELECT `name`, `search_operator` FROM `sys_search_extended_fields` WHERE `object` = :object AND `active` = 1",
  114. 'name',
  115. 'search_operator',
  116. ['object' => $sModule]
  117. );
  118. if(empty($aFields) || !is_array($aFields))
  119. return;
  120. foreach($aValues as $sName => $mixedValue) {
  121. if(!is_string($sName) || !isset($aFields[$sName]) || !preg_match('/^[A-Za-z0-9_]+$/', $sName))
  122. continue;
  123. $sOperator = $aFields[$sName];
  124. if($sOperator === '' || $this->_isSearchFilterEmpty($mixedValue))
  125. continue;
  126. if(!$oDb->isFieldExists($sTable, $sName))
  127. continue;
  128. $aRestriction = [
  129. 'field' => $sName,
  130. 'table' => $sTable,
  131. 'operator' => $sOperator,
  132. 'value' => $mixedValue,
  133. ];
  134. switch($sOperator) {
  135. case 'like':
  136. case '=':
  137. if(is_array($mixedValue))
  138. continue 2;
  139. break;
  140. case 'in':
  141. case 'not in':
  142. $aRestriction['value'] = is_array($mixedValue) ? $mixedValue : [$mixedValue];
  143. break;
  144. case 'between':
  145. if(!is_array($mixedValue))
  146. continue 2;
  147. break;
  148. default:
  149. continue 2;
  150. }
  151. $this->aCurrent['restriction']['search_' . $sName] = $aRestriction;
  152. }
  153. }
  154. protected function _isSearchFilterEmpty($mixedValue)
  155. {
  156. if($mixedValue === null || $mixedValue === '' || $mixedValue === false)
  157. return true;
  158. if(!is_array($mixedValue))
  159. return false;
  160. foreach($mixedValue as $mixedItem)
  161. if(!$this->_isSearchFilterEmpty($mixedItem))
  162. return false;
  163. return true;
  164. }
  165. protected function addConditionsForAuthorStatus($CNF)
  166. {
  167. if (empty($CNF['FIELD_AUTHOR']))
  168. return;
  169. $this->aCurrent['restriction']['statusAuthor'] = [
  170. 'value' => 'active',
  171. 'field' => 'status',
  172. 'operator' => '=',
  173. 'table' => 'sys_profiles',
  174. ];
  175. $this->aCurrent['join']['statusAuthor'] = [
  176. 'type' => 'INNER',
  177. 'table' => 'sys_profiles',
  178. 'mainField' => $CNF['FIELD_AUTHOR'],
  179. 'mainFieldFunc' => 'ABS',
  180. 'onField' => 'id',
  181. 'joinFields' => array(),
  182. ];
  183. }
  184. protected function addConditionsForCf($CNF)
  185. {
  186. if(empty($CNF['FIELD_CF']))
  187. return;
  188. $oCf = BxDolContentFilter::getInstance();
  189. if(!$oCf->isEnabled())
  190. return;
  191. $aConditions = $oCf->getConditions($this->aCurrent['table'], $CNF['FIELD_CF']);
  192. if(!empty($aConditions) && is_array($aConditions))
  193. $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aConditions);
  194. }
  195. protected function addConditionsForFilter($CNF, $sMode, $aParams)
  196. {
  197. $aFilter = $aParams['filter'];
  198. if(empty($aFilter['field']) || empty($aFilter['value']))
  199. return;
  200. $aRestriction = [
  201. 'value' => $aFilter['value'],
  202. 'field' => $aFilter['field'],
  203. 'operator' => '=',
  204. ];
  205. if(isset($aFilter['operator']))
  206. $aRestriction['operator'] = $aFilter['operator'];
  207. if(isset($aFilter['table']))
  208. switch($aFilter['table']) {
  209. case 'table':
  210. $aRestriction['table'] = $this->aCurrent['table'];
  211. break;
  212. case 'tableSearch':
  213. $aRestriction['table'] = $this->aCurrent['tableSearch'];
  214. break;
  215. default:
  216. $aRestriction['table'] = $aFilter['table'];
  217. }
  218. $this->aCurrent['restriction']['filter'] = $aRestriction;
  219. }
  220. /**
  221. * Add conditions for private content
  222. */
  223. protected function addConditionsForPrivateContent($CNF, $oProfile, $aCustomGroup = array())
  224. {
  225. // default is bProcessPrivateContent = 1,
  226. // so private items are shown as empty boxes with "Private" title
  227. // we can show public content when privacy object is available
  228. if(empty($CNF['OBJECT_PRIVACY_VIEW']))
  229. return;
  230. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
  231. if(!$oPrivacy)
  232. return;
  233. // for posts in some context we need to show all items, not only public content
  234. if (!empty($this->aCurrent['restriction']['context']['value']) || !empty($this->aCurrent['restriction']['author']['value'])) {
  235. $this->setProcessPrivateContent(true);
  236. return;
  237. }
  238. // build condition to show only public content
  239. $aCondition = $oPrivacy->getContentPublicAsCondition($oProfile ? $oProfile->id() : 0, $aCustomGroup);
  240. if(empty($aCondition) || !is_array($aCondition))
  241. return;
  242. if(isset($aCondition['restriction'])) {
  243. $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aCondition['restriction']);
  244. $this->aPrivateConditionsIndexes['restriction'] = array_keys($aCondition['restriction']);
  245. }
  246. if(isset($aCondition['join'])) {
  247. $this->aCurrent['join'] = array_merge($this->aCurrent['join'], $aCondition['join']);
  248. $this->aPrivateConditionsIndexes['join'] = array_keys($aCondition['join']);
  249. }
  250. $this->setProcessPrivateContent(false);
  251. }
  252. function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true)
  253. {
  254. if($this->bShowcaseView)
  255. return '';
  256. $sPagination = parent::showPagination ($bAdmin, $bChangePage, $bPageReload);
  257. if(empty($sPagination))
  258. return '';
  259. return $sPagination;
  260. }
  261. protected function getItemPerPageInShowCase ()
  262. {
  263. $iPerPageInShowCase = (int)getParam('sys_per_page_browse_showcase');
  264. $CNF = &$this->oModule->_oConfig->CNF;
  265. if(isset($CNF['PARAM_PER_PAGE_BROWSE_SHOWCASE']))
  266. $iPerPageInShowCase = (int)getParam($CNF['PARAM_PER_PAGE_BROWSE_SHOWCASE']);
  267. if(!$iPerPageInShowCase)
  268. $iPerPageInShowCase = BX_SYS_PER_PAGE_BROWSE_SHOWCASE;
  269. return $iPerPageInShowCase;
  270. }
  271. function displayResultBlock()
  272. {
  273. if ($this->bShowcaseView) {
  274. $this->addContainerClass(array('bx-base-unit-showcase-wrapper'));
  275. $this->aCurrent['paginate']['perPage'] = $this->getItemPerPageInShowCase();
  276. $this->oModule->_oTemplate->addCss(array(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css'));
  277. $this->oModule->_oTemplate->addJs(array('flickity/flickity.pkgd.min.js','modules/base/general/js/|showcase.js'));
  278. }
  279. return parent::displayResultBlock();
  280. }
  281. function displaySearchBox ($sContent, $sPaginate = '')
  282. {
  283. $aResult = parent::displaySearchBox($sContent, $sPaginate);
  284. if(isset($this->_aParams['filters']) && is_array($this->_aParams['filters']))
  285. $aResult['buttons'] = [
  286. ['title' => _t('_Filters'), 'href' => 'javascript:void(0)', 'onclick' => 'javascript:' . $this->_aParams['filters']['onclick']]
  287. ];
  288. return $aResult;
  289. }
  290. function applyContainerId()
  291. {
  292. if(empty($this->aCurrent['name']) || empty($this->_sMode))
  293. return parent::applyContainerId();
  294. return str_replace('_', '-', $this->aCurrent['name'] . '-search-result-block-' . $this->_sMode);
  295. }
  296. function decodeDataAPI($a, $sMethod = 'getDataAPI')
  297. {
  298. if(!is_array($a))
  299. return $a;
  300. $bExtendedUnits = getParam('sys_api_extended_units') == 'on';
  301. foreach($a as $i => $r)
  302. $a[$i] = $this->oModule->$sMethod($r, ['extended' => $bExtendedUnits]);
  303. return $a;
  304. }
  305. protected function _updateCurrentForFollowedContexts($sMode, $aParams, &$oProfileContext)
  306. {
  307. $CNF = &$this->oModule->_oConfig->CNF;
  308. if(empty($aParams['followed_contexts']))
  309. return false;
  310. $iProfileId = (int)$aParams['followed_contexts'];
  311. $aContextTypes = array_keys(bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]));
  312. $aContextIds = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')->getConnectedContentByType($iProfileId, $aContextTypes);
  313. $this->aCurrent['restriction']['context'] = [
  314. 'value' => array_map(function($iValue) {
  315. return -$iValue;
  316. }, $aContextIds),
  317. 'field' => $CNF['FIELD_ALLOW_VIEW_TO'],
  318. 'operator' => 'in',
  319. ];
  320. if(!empty($aParams['per_page']))
  321. $this->aCurrent['paginate']['perPage'] = is_numeric($aParams['per_page']) ? (int)$aParams['per_page'] : (int)getParam($aParams['per_page']);
  322. $this->sBrowseUrl = '';
  323. $this->aCurrent['title'] = '';
  324. unset($this->aCurrent['rss']);
  325. return true;
  326. }
  327. function _getPseudFromParam ()
  328. {
  329. $mixedPseud = getParam($this->getModuleName() . '_browse_pseud');
  330. if(empty($mixedPseud))
  331. return false;
  332. if(($aPseud = json_decode($mixedPseud, true)) !== null)
  333. return $aPseud;
  334. return false;
  335. }
  336. }
  337. /** @} */