BxBaseModGeneralSearchResult.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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]) || !$oDb->isValidFieldName($sName, true))
  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. $oDb = BxDolDb::getInstance();
  199. if(empty($aFilter['field']) || !$oDb->isValidFieldName($aFilter['field'], true) || empty($aFilter['value']))
  200. return;
  201. $aOperators = ['=', '!=', '<>', '<', '>', '<=', '>=', 'like', 'in', 'not in', 'between'];
  202. $sOperator = '=';
  203. if(isset($aFilter['operator'])) {
  204. if(!is_string($aFilter['operator']))
  205. return;
  206. $sOperator = strtolower(trim($aFilter['operator']));
  207. if(!in_array($sOperator, $aOperators, true))
  208. return;
  209. }
  210. $sTable = $this->aCurrent['table'] ?? '';
  211. if(isset($aFilter['table'])) {
  212. if(!is_string($aFilter['table']))
  213. return;
  214. switch($aFilter['table']) {
  215. case 'table':
  216. $sTable = $this->aCurrent['table'] ?? '';
  217. break;
  218. case 'tableSearch':
  219. $sTable = $this->aCurrent['tableSearch'] ?? '';
  220. break;
  221. default:
  222. return;
  223. }
  224. }
  225. if(!$oDb->isValidFieldName($sTable, true))
  226. return;
  227. if(!$oDb->isFieldExists($sTable, $aFilter['field']))
  228. return;
  229. $mixedValue = $aFilter['value'];
  230. if(in_array($sOperator, ['like', '=', '!=', '<>', '<', '>', '<=', '>='], true) && is_array($mixedValue))
  231. return;
  232. if($sOperator === 'between' && !is_array($mixedValue))
  233. return;
  234. $this->aCurrent['restriction']['filter'] = [
  235. 'value' => $mixedValue,
  236. 'field' => $aFilter['field'],
  237. 'operator' => $sOperator,
  238. 'table' => $sTable,
  239. ];
  240. }
  241. /**
  242. * Add conditions for private content
  243. */
  244. protected function addConditionsForPrivateContent($CNF, $oProfile, $aCustomGroup = array())
  245. {
  246. // default is bProcessPrivateContent = 1,
  247. // so private items are shown as empty boxes with "Private" title
  248. // we can show public content when privacy object is available
  249. if(empty($CNF['OBJECT_PRIVACY_VIEW']))
  250. return;
  251. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
  252. if(!$oPrivacy)
  253. return;
  254. // for posts in some context we need to show all items, not only public content
  255. if (!empty($this->aCurrent['restriction']['context']['value']) || !empty($this->aCurrent['restriction']['author']['value'])) {
  256. $this->setProcessPrivateContent(true);
  257. return;
  258. }
  259. // build condition to show only public content
  260. $aCondition = $oPrivacy->getContentPublicAsCondition($oProfile ? $oProfile->id() : 0, $aCustomGroup);
  261. if(empty($aCondition) || !is_array($aCondition))
  262. return;
  263. if(isset($aCondition['restriction'])) {
  264. $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aCondition['restriction']);
  265. $this->aPrivateConditionsIndexes['restriction'] = array_keys($aCondition['restriction']);
  266. }
  267. if(isset($aCondition['join'])) {
  268. $this->aCurrent['join'] = array_merge($this->aCurrent['join'], $aCondition['join']);
  269. $this->aPrivateConditionsIndexes['join'] = array_keys($aCondition['join']);
  270. }
  271. $this->setProcessPrivateContent(false);
  272. }
  273. function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true)
  274. {
  275. if($this->bShowcaseView)
  276. return '';
  277. $sPagination = parent::showPagination ($bAdmin, $bChangePage, $bPageReload);
  278. if(empty($sPagination))
  279. return '';
  280. return $sPagination;
  281. }
  282. protected function getItemPerPageInShowCase ()
  283. {
  284. $iPerPageInShowCase = (int)getParam('sys_per_page_browse_showcase');
  285. $CNF = &$this->oModule->_oConfig->CNF;
  286. if(isset($CNF['PARAM_PER_PAGE_BROWSE_SHOWCASE']))
  287. $iPerPageInShowCase = (int)getParam($CNF['PARAM_PER_PAGE_BROWSE_SHOWCASE']);
  288. if(!$iPerPageInShowCase)
  289. $iPerPageInShowCase = BX_SYS_PER_PAGE_BROWSE_SHOWCASE;
  290. return $iPerPageInShowCase;
  291. }
  292. function displayResultBlock()
  293. {
  294. if ($this->bShowcaseView) {
  295. $this->addContainerClass(array('bx-base-unit-showcase-wrapper'));
  296. $this->aCurrent['paginate']['perPage'] = $this->getItemPerPageInShowCase();
  297. $this->oModule->_oTemplate->addCss(array(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css'));
  298. $this->oModule->_oTemplate->addJs(array('flickity/flickity.pkgd.min.js','modules/base/general/js/|showcase.js'));
  299. }
  300. return parent::displayResultBlock();
  301. }
  302. function displaySearchBox ($sContent, $sPaginate = '')
  303. {
  304. $aResult = parent::displaySearchBox($sContent, $sPaginate);
  305. if(isset($this->_aParams['filters']) && is_array($this->_aParams['filters']))
  306. $aResult['buttons'] = [
  307. ['title' => _t('_Filters'), 'href' => 'javascript:void(0)', 'onclick' => 'javascript:' . $this->_aParams['filters']['onclick']]
  308. ];
  309. return $aResult;
  310. }
  311. function applyContainerId()
  312. {
  313. if(empty($this->aCurrent['name']) || empty($this->_sMode))
  314. return parent::applyContainerId();
  315. return str_replace('_', '-', $this->aCurrent['name'] . '-search-result-block-' . $this->_sMode);
  316. }
  317. function decodeDataAPI($a, $sMethod = 'getDataAPI')
  318. {
  319. if(!is_array($a))
  320. return $a;
  321. $bExtendedUnits = getParam('sys_api_extended_units') == 'on';
  322. foreach($a as $i => $r)
  323. $a[$i] = $this->oModule->$sMethod($r, ['extended' => $bExtendedUnits]);
  324. return $a;
  325. }
  326. protected function _updateCurrentForFollowedContexts($sMode, $aParams, &$oProfileContext)
  327. {
  328. $CNF = &$this->oModule->_oConfig->CNF;
  329. if(empty($aParams['followed_contexts']))
  330. return false;
  331. $iProfileId = (int)$aParams['followed_contexts'];
  332. $aContextTypes = array_keys(bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]));
  333. $aContextIds = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')->getConnectedContentByType($iProfileId, $aContextTypes);
  334. $this->aCurrent['restriction']['context'] = [
  335. 'value' => array_map(function($iValue) {
  336. return -$iValue;
  337. }, $aContextIds),
  338. 'field' => $CNF['FIELD_ALLOW_VIEW_TO'],
  339. 'operator' => 'in',
  340. ];
  341. if(!empty($aParams['per_page']))
  342. $this->aCurrent['paginate']['perPage'] = is_numeric($aParams['per_page']) ? (int)$aParams['per_page'] : (int)getParam($aParams['per_page']);
  343. $this->sBrowseUrl = '';
  344. $this->aCurrent['title'] = '';
  345. unset($this->aCurrent['rss']);
  346. return true;
  347. }
  348. function _getPseudFromParam ()
  349. {
  350. $mixedPseud = getParam($this->getModuleName() . '_browse_pseud');
  351. if(empty($mixedPseud))
  352. return false;
  353. if(($aPseud = json_decode($mixedPseud, true)) !== null)
  354. return $aPseud;
  355. return false;
  356. }
  357. }
  358. /** @} */