BxDolSearch.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. define('BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT', 10);
  9. /**
  10. * Search class for different content.
  11. *
  12. * Allows present content in main search area for all modules.
  13. *
  14. * To add taking part content from your module to search area you need add a record to `sys_objects_search` table:
  15. * \code
  16. * ID - autoincremented id for internal usage
  17. * ObjectName - your unique module name, with vendor prefix, lowercase and spaces are underscored
  18. * Title - language key for module's choicer in global search form
  19. * ClassName - class name which is responsible for search process ad showing result for found results
  20. * ClassPath - file where your ClassName is stored (if class is stored in template folder then you can use key {tmpl})
  21. * \endcode
  22. *
  23. * For using this class you should have unit of BxDolSearchResult class (see description below) with method processing which is responsible for processing of search request.
  24. *
  25. * Example of usage can be seen in the default Dolphin's modules (photos, sounds, events, sites, etc).
  26. *
  27. * Memberships/ACL:
  28. * Doesn't depend on user's membership.
  29. *
  30. * Alerts:
  31. * no alerts available
  32. *
  33. */
  34. class BxDolSearch extends BxDol {
  35. var $aClasses = array(); ///< array of all search classes
  36. var $aChoice = array(); ///< array of current search classes which were choosen in search area
  37. /**
  38. * Constructor
  39. * @param array $aChoice - array of choosen classes (will take a part only existing in `sys_objects_search` table)
  40. */
  41. function BxDolSearch ($aChoice = '') {
  42. parent::BxDol();
  43. $this->aClasses = BxDolDb::getInstance()->fromCache('sys_objects_search', 'getAllWithKey',
  44. 'SELECT `ID` as `id`,
  45. `Title` as `title`,
  46. `ClassName` as `class`,
  47. `ClassPath` as `file`,
  48. `ObjectName`
  49. FROM `sys_objects_search`', 'ObjectName'
  50. );
  51. if (is_array($aChoice) && !empty($aChoice)) {
  52. foreach ($aChoice as $sValue) {
  53. if (isset($this->aClasses[$sValue]))
  54. $this->aChoice[$sValue] = $this->aClasses[$sValue];
  55. }
  56. } else {
  57. $this->aChoice = $this->aClasses;
  58. }
  59. }
  60. /**
  61. * create units for all classes and calling their processing methods
  62. */
  63. function response () {
  64. $sCode = '';
  65. foreach ($this->aChoice as $sKey => $aValue) {
  66. if (!class_exists($aValue['class'])) {
  67. $sClassPath = str_replace('{tmpl}', BxDolTemplate::getInstance()->getCode(), $aValue['file']);
  68. require_once(BX_DIRECTORY_PATH_ROOT . $sClassPath);
  69. }
  70. $oEx = new $aValue['class']();
  71. $oEx->id = $aValue['id'];
  72. $sCode .= $oEx->processing();
  73. }
  74. return $sCode;
  75. }
  76. function getEmptyResult () {
  77. $sKey = _t('_Empty');
  78. return DesignBoxContent($sKey, MsgBox($sKey), 1);
  79. }
  80. }
  81. /*
  82. * Search class for processing search requests and displaying search results.
  83. *
  84. * Allows present content from modules on search params or internal (via fields of class) conditions.
  85. *
  86. * Example of usage (you can see example in any of default Dolhpin's modules):
  87. *
  88. * 1. Extends your own search class from this one (or from BxBaseSearchResult or BxBaseSearchResultSharedMedia classes)
  89. * 2. Set necessary fields of class (using as example BxFilesSearch):
  90. *
  91. * \code
  92. *
  93. * // main field of search class
  94. * $this->aCurrent = array(
  95. *
  96. * // name of module
  97. * 'name' => 'bx_files',
  98. *
  99. * // language key with name of module
  100. * 'title' => '_bx_files',
  101. *
  102. * // main content table
  103. * 'table' => 'bx_files_main',
  104. *
  105. * // array of all fields which can be choosen for select in result query
  106. * 'ownFields' => array('ID', 'Title', 'Uri', 'Desc', 'Date', 'Size', 'Ext', 'Views', 'Rate', 'RateCount', 'Type'),
  107. *
  108. * // array of fields which take a part in search by keyword (global search and related words only - in other cases leave it blank)
  109. * 'searchFields' => array('Title', 'Tags', 'Desc', 'Categories'),
  110. *
  111. * // array of join tables
  112. * // 'type' - type of join
  113. * // 'table' - join table
  114. * // 'mainField' - field from main table for 'on' condition
  115. * // 'onField' - field from joining table for 'on' condition
  116. * // 'joinFields' - array of fields from joining table
  117. *
  118. * 'join' => array(
  119. * 'profile' => array(
  120. * 'type' => 'left',
  121. * 'table' => 'Profiles',
  122. * 'mainField' => 'Owner',
  123. * 'onField' => 'ID',
  124. * 'joinFields' => array('NickName'),
  125. * ),
  126. * // ...
  127. * ),
  128. *
  129. * // array of search parameters
  130. * // 'value' - value of search parameter (can be number, string or array)
  131. * // 'field' - field which will take value of search, may be lefy as blank then in search query will be pasted to WHERE conditon via operator AGAINST
  132. * // 'operator' - operator between field and value (can be 'in', 'not in', '>', '<', 'against', 'like' and '=' by default),
  133. * // 'paramName' - GET param which will keep 'value' for pagianation
  134. *
  135. * 'restriction' => array(
  136. * 'activeStatus' => array('value'=>'approved', 'field'=>'Status', 'operator'=>'=', 'paramName' => 'status'),
  137. * 'albumType' => array('value'=>'', 'field'=>'Type', 'operator'=>'=', 'paramName'=>'albumType', 'table'=>'sys_albums'),
  138. * ),
  139. *
  140. * // array of pagination
  141. * // 'perPage' - units per page
  142. * // 'start' - show search results starting from this number
  143. * // 'num' - number of element to show on current page + 1
  144. *
  145. * 'paginate' => array('perPage' => 10, 'start' => 0, 'num' => 11),
  146. *
  147. * // sort mode - by default it was last (DESC by date)
  148. * 'sorting' => 'last',
  149. *
  150. * // mode of units presentation in search result, can be 'short' or 'full' view
  151. * 'view' => 'full',
  152. *
  153. * // field of id
  154. * 'ident' => 'ID',
  155. *
  156. * // rss feed array
  157. * 'rss' => array(
  158. * 'title' => '',
  159. * 'link' => '',
  160. * 'image' => '',
  161. * 'profile' => 0,
  162. * 'fields' => array (
  163. * 'Link' => '',
  164. * 'Title' => 'Title',
  165. * 'DateTimeUTS' => 'Date',
  166. * 'Desc' => 'Desc',
  167. * 'Photo' => '',
  168. * ),
  169. * ),
  170. * );
  171. *
  172. * // array of fields renamings
  173. * var $aPseud - filling in costructor by function _getPseud
  174. *
  175. * // unique identificator from `sys_objects_search` table
  176. * var $id;
  177. *
  178. * // indicator of rate connection
  179. * var $iRate
  180. *
  181. * // rate object field
  182. * var $oRate;
  183. *
  184. * // indicator of connection parts in display search data (for function addCustomParts)
  185. * var $bCustomParts;
  186. *
  187. * \endcode
  188. *
  189. * Memberships/ACL:
  190. * Doesn't depend on user's membership.
  191. *
  192. * Alerts:
  193. * no alerts available
  194. *
  195. */
  196. class BxDolSearchResult {
  197. var $aCurrent;
  198. var $aPseud;
  199. var $id;
  200. var $iRate = 1;
  201. var $oRate = null;
  202. var $bCustomParts = false;
  203. var $bDisplayEmptyMsg = false;
  204. var $sDisplayEmptyMsgKey = '';
  205. var $bProcessPrivateContent = true;
  206. protected $_aMarkers = array ();
  207. /**
  208. * constructor
  209. * filling identificator field
  210. */
  211. function BxDolSearchResult () {
  212. if (isset($this->aPseud['id']))
  213. $this->aCurrent['ident'] = $this->aPseud['id'];
  214. }
  215. /**
  216. * Display empty message if there is no content, custom empty message can be used.
  217. * @param $b - boolan value to enable or disable 'empty' message
  218. * @param $sLangKey [optional] - custom 'empty' message
  219. */
  220. public function setDisplayEmptyMsg($b, $sLangKey = '') {
  221. $this->bDisplayEmptyMsg = $b;
  222. if ($sLangKey)
  223. $this->sDisplayEmptyMsgKey = $sLangKey;
  224. }
  225. /**
  226. * Perform privacy checking for every unit
  227. * @param $b - boolan value to enable or disable privacy checking
  228. */
  229. public function setProcessPrivateContent ($b) {
  230. $this->bProcessPrivateContent = $b;
  231. }
  232. /**
  233. * Get html box of search results (usually used in grlobal search)
  234. * @return html code
  235. */
  236. function processing () {
  237. $sCode = $this->displayResultBlock();
  238. if ($this->aCurrent['paginate']['num'] > 0) {
  239. $sPaginate = $this->showPagination();
  240. $sCode = $this->displaySearchBox($sCode, $sPaginate);
  241. }
  242. else {
  243. $sCode = $this->bDisplayEmptyMsg ? $this->displaySearchBox(MsgBox(_t($this->sDisplayEmptyMsgKey ? $this->sDisplayEmptyMsgKey : '_Empty'))) : '';
  244. }
  245. return $sCode;
  246. }
  247. /**
  248. * Get html output of search result
  249. * @return html code
  250. */
  251. function displayResultBlock () {
  252. $aData = $this->getSearchData();
  253. if (count($aData) > 0) {
  254. $sCode .= $this->addCustomParts();
  255. foreach ($aData as $iKey => $aValue) {
  256. $sCode .= $this->displaySearchUnit($aValue);
  257. }
  258. }
  259. return $sCode;
  260. }
  261. /**
  262. * Add different code before html output (usually redeclared)
  263. * no return result
  264. */
  265. function addCustomParts () {
  266. }
  267. /**
  268. * Get array for rss output
  269. */
  270. function rss () {
  271. if (!isset($this->aCurrent['rss']['fields']) || !isset($this->aCurrent['rss']['link']))
  272. return '';
  273. $aData = $this->getSearchData();
  274. $f = &$this->aCurrent['rss']['fields'];
  275. if ($aData) {
  276. foreach ($aData as $k => $a) {
  277. $aData[$k][$f['Link']] = $this->getRssUnitLink ($a);
  278. }
  279. }
  280. bx_import('BxDolRssFactory');
  281. $oRss = new BxDolRssFactory ();
  282. return $oRss->GenRssByCustomData(
  283. $aData,
  284. isset($this->aCurrent['rss']['title']) && $this->aCurrent['rss']['title'] ? $this->aCurrent['rss']['title'] : $this->aCurrent['title'],
  285. $this->aCurrent['rss']['link'],
  286. $this->aCurrent['rss']['fields'],
  287. isset($this->aCurrent['rss']['image']) ? $this->aCurrent['rss']['image'] : '',
  288. isset($this->aCurrent['rss']['profile']) ? $this->aCurrent['rss']['profile'] : 0
  289. );
  290. }
  291. /**
  292. * Return rss unit link (redeclared)
  293. */
  294. function getRssUnitLink (&$a) {
  295. // override this functions to return permalink to rss unit
  296. }
  297. /**
  298. * Naming fields in query's body
  299. * @param string $sFieldName name of field
  300. * @param string $sTableName name of field's table
  301. * $param string $sOperator of field's calculation (like MAX)
  302. * @param boolean $bRenameMode indicator for renaming and unsetting fields from field of class $this->aPseud
  303. * return $sqlUnit sql code and unsetting elements from aPseud field
  304. */
  305. function setFieldUnit ($sFieldName, $sTableName, $sOperator = '', $bRenameMode = true) {
  306. if (strlen($sOperator) > 0) {
  307. $sqlUnit = "$sOperator(`$sTableName`.`$sFieldName`)";
  308. }
  309. else
  310. $sqlUnit = "`$sTableName`.`$sFieldName`";
  311. if (isset($this->aPseud) && $bRenameMode !== false) {
  312. $sKey = array_search($sFieldName, $this->aPseud);
  313. if ($sKey !== false) {
  314. $sqlUnit .= " as `$sKey`";
  315. unset($this->aPseud[$sKey]);
  316. }
  317. }
  318. return $sqlUnit . ', ';
  319. }
  320. /**
  321. * Get html code of of every search unit
  322. * @param array $aData array of every search unit
  323. * return html code
  324. */
  325. function displaySearchUnit ($aData) {
  326. }
  327. /**
  328. * Get html code of search box with search results
  329. * @param string $sCode html code of search results
  330. * $param $sPaginate html code of paginate
  331. * return html code
  332. */
  333. function displaySearchBox ($sCode, $sPaginate = '') {
  334. }
  335. /**
  336. * Get html code of pagination
  337. */
  338. function showPagination ($bAdmin = false, $bChangePage = true, $bPageReload = true) {
  339. }
  340. /**
  341. * Get array of data with search results
  342. * return array with data
  343. */
  344. function getSearchData () {
  345. $this->aPseud = $this->_getPseud();
  346. $this->setConditionParams();
  347. if ($this->aCurrent['paginate']['num'] > 0) {
  348. $aData = $this->getSearchDataByParams();
  349. return $aData;
  350. }
  351. }
  352. /**
  353. * Get array with code for sql elements
  354. * @param boolean #bREnameMode indicator of renmaing fields
  355. * return array with joinFields, ownFields, groupBy and join elements
  356. */
  357. function getJoins ($bRenameMode = true) {
  358. $aSql = array();
  359. // joinFields & join
  360. if (isset($this->aCurrent['join']) && is_array($this->aCurrent['join'])) {
  361. $aSql = array('join'=>'', 'ownFields'=>'', 'joinFields'=>'', 'groupBy'=>'');
  362. foreach ($this->aCurrent['join'] as $sKey => $aValue) {
  363. if (is_array($aValue['joinFields'])) {
  364. foreach ($aValue['joinFields'] as $sValue) {
  365. $aSql['joinFields'] .= $this->setFieldUnit($sValue, $aValue['table'], isset($aValue['operator']) ? $aValue['operator'] : null, $bRenameMode);
  366. }
  367. }
  368. // group by
  369. if (isset($aValue['groupTable']))
  370. $aSql['groupBy'] = "GROUP BY `{$aValue['groupTable']}`.`{$aValue['groupField']}`, ";
  371. $sOn = isset($aValue['mainTable']) ? $aValue['mainTable'] : $this->aCurrent['table'];
  372. $aSql['join'] .= " {$aValue['type']} JOIN `{$aValue['table']}` ON `{$aValue['table']}`.`{$aValue['onField']}`=`$sOn`.`{$aValue['mainField']}`";
  373. $aSql['ownFields'] .= $this->setFieldUnit($aValue['mainField'], $sOn, '', $bRenameMode);
  374. }
  375. $aSql['joinFields'] = trim($aSql['joinFields'], ', ');
  376. $aSql['groupBy'] = trim($aSql['groupBy'], ', ');
  377. }
  378. return $aSql;
  379. }
  380. /**
  381. * Concat sql parts of query, run it and return result array
  382. * @param $aParams addon param
  383. * return $aData multivariate array
  384. */
  385. function getSearchDataByParams ($aParams = '') {
  386. $aSql = array('ownFields'=>'', 'joinFields'=>'', 'order'=>'');
  387. // searchFields
  388. foreach ($this->aCurrent['ownFields'] as $sValue) {
  389. $aSql['ownFields'] .= $this->setFieldUnit($sValue, $this->aCurrent['table']);
  390. }
  391. // joinFields & join
  392. $aJoins = $this->getJoins();
  393. if (!empty($aJoins)) {
  394. $aSql['ownFields'] .= $aJoins['ownFields'];
  395. $aSql['joinFields'] .= $aJoins['joinFields'];
  396. $aSql['join'] = $aJoins['join'];
  397. $aSql['groupBy'] = $aJoins['groupBy'];
  398. }
  399. else
  400. $aSql['ownFields'] = trim($aSql['ownFields'], ', ');
  401. // from
  402. $aSql['from'] = " FROM `{$this->aCurrent['table']}`";
  403. // where
  404. $aSql['where'] = $this->getRestriction();
  405. // limit
  406. $aSql['limit'] = $this->getLimit();
  407. // sorting
  408. $this->setSorting();
  409. $aSort = $this->getSorting($this->aCurrent['sorting']);
  410. foreach ($aSort as $sKey => $sValue)
  411. $aSql[$sKey] .= $sValue;
  412. // rate part
  413. $aRate = $this->getRatePart();
  414. if (is_array($aRate)) {
  415. foreach ($aRate as $sKey => $sValue)
  416. $aSql[$sKey] .= $sValue;
  417. }
  418. // execution
  419. $sqlQuery = "SELECT " . $aSql['ownFields'];
  420. if (isset($aSql['joinFields']))
  421. $sqlQuery .= ' ' . $aSql['joinFields'];
  422. $sqlQuery .= ' ' . $aSql['from'];
  423. if (isset($aSql['join']))
  424. $sqlQuery .= ' ' . $aSql['join'];
  425. if (isset($aSql['where']))
  426. $sqlQuery .= ' ' . $aSql['where'];
  427. if (isset($aSql['groupBy']))
  428. $sqlQuery .= ' ' . $aSql['groupBy'];
  429. if (isset($aSql['order']))
  430. $sqlQuery .= ' ' . $aSql['order'];
  431. if (isset($aSql['limit']))
  432. $sqlQuery .= ' ' . $aSql['limit'];
  433. // echoDbg($sqlQuery);
  434. $aRes = BxDolDb::getInstance()->getAll($sqlQuery);
  435. return $aRes;
  436. }
  437. /**
  438. * Set class fields condition params and paginate array
  439. */
  440. function setConditionParams() {
  441. $aWhere = array();
  442. $aWhere[] = '1';
  443. $sKeyword = bx_process_input(bx_get('keyword'));
  444. if ($sKeyword !== false)
  445. $this->aCurrent['restriction']['keyword'] = array(
  446. 'value' => $sKeyword,
  447. 'field' => '',
  448. 'operator' => 'against'
  449. );
  450. if (isset($_GET['ownerName'])) {
  451. $sName = bx_process_input($_GET['ownerName']);
  452. $iUser = (int)BxDolProfileQuery::getInstance()->getIdByNickname($sName);
  453. BxDolMenu::getInstance()->setCurrentProfileID($iUser);
  454. }
  455. elseif (isset($_GET['userID']))
  456. $iUser = bx_process_input($_GET['userID'], BX_DATA_INT);
  457. if (!empty($iUser))
  458. $this->aCurrent['restriction']['owner']['value'] = $iUser;
  459. $this->setPaginate();
  460. $iNum = $this->getNum();
  461. if ($iNum > 0) {
  462. $this->aCurrent['paginate']['num'] = $iNum;
  463. }
  464. else {
  465. $this->aCurrent['paginate']['num'] = 0;
  466. }
  467. }
  468. /**
  469. * Check number of records on current page
  470. * return number of records on current page + 1
  471. */
  472. function getNum () {
  473. $aJoins = $this->getJoins(false);
  474. $sqlQuery = "SELECT * FROM `{$this->aCurrent['table']}` " . (isset($aJoins['join']) ? $aJoins['join'] : '' ). $this->getRestriction() . (isset($aJoins['groupBy']) ? $aJoins['groupBy'] : '') . ' ' . $this->getLimit(true);
  475. return count(BxDolDb::getInstance()->getAll($sqlQuery));
  476. }
  477. /**
  478. * Check restriction params and make condition part of query
  479. * return $sqlWhere sql code of query for WHERE part
  480. */
  481. function getRestriction () {
  482. $oDb = BxDolDb::getInstance();
  483. $sqlWhere = '';
  484. if (isset($this->aCurrent['restriction'])) {
  485. $aWhere[] = '1';
  486. foreach ($this->aCurrent['restriction'] as $sKey => $aValue) {
  487. $sqlCondition = '';
  488. if (isset($aValue['operator']) && isset($aValue['value']) && $aValue['value'] !== '' && $aValue['value'] !== false && $aValue['value'] !== null) {
  489. $sFieldTable = isset($aValue['table']) ? $aValue['table'] : $this->aCurrent['table'];
  490. $sqlCondition = "`{$sFieldTable}`.`{$aValue['field']}` ";
  491. switch ($aValue['operator']) {
  492. case 'against':
  493. $aCond = isset($aValue['field']) && strlen($aValue['field']) > 0 ? $aValue['field'] : $this->aCurrent['searchFields'];
  494. $sqlCondition = !empty($aCond) ? $this->getSearchFieldsCond($aCond, $aValue['value']) : "";
  495. break;
  496. case 'like':
  497. $sqlCondition .= "LIKE '%" . $oDb->escape($aValue['value']) . "%'";
  498. break;
  499. case 'in':
  500. case 'not in':
  501. $sValuesString = $this->getMultiValues($aValue['value']);
  502. $sqlCondition .= strtoupper($aValue['operator']) . '('.$sValuesString.')';
  503. break;
  504. default:
  505. $sqlCondition .= $aValue['operator'] . (isset($aValue['no_quote_value']) && $aValue['no_quote_value'] ? $aValue['value'] : "'" . $oDb->escape($aValue['value']) . "'");
  506. break;
  507. }
  508. }
  509. if (strlen($sqlCondition) > 0)
  510. $aWhere[] = $sqlCondition;
  511. }
  512. $sqlWhere .= "WHERE ". implode(' AND ', $aWhere);
  513. }
  514. return $sqlWhere;
  515. }
  516. /**
  517. * Get limit part of query
  518. * return $sqlFrom code for limit part pf query
  519. */
  520. function getLimit ($isAddPlusOne = false) {
  521. if (!isset($this->aCurrent['paginate']))
  522. return;
  523. $sqlFrom = (int)$this->aCurrent['paginate']['start'] > 0 ? (int)$this->aCurrent['paginate']['start'] : 0;
  524. $sqlTo = $this->aCurrent['paginate']['perPage'] + ($isAddPlusOne ? 1 : 0);
  525. if ($sqlTo > 0)
  526. return 'LIMIT ' . $sqlFrom . ', ' . $sqlTo;
  527. }
  528. /**
  529. * Set sorting field of class
  530. */
  531. function setSorting () {
  532. $this->aCurrent['sorting'] = isset($_GET[$this->aCurrent['name'] . '_mode']) ? $_GET[$this->aCurrent['name'] . '_mode'] : $this->aCurrent['sorting'];
  533. }
  534. /**
  535. * Get sorting part of query according current sorting mode
  536. * @param string $sSortType sorting type
  537. * return array with sql elements order and ownFields
  538. */
  539. function getSorting ($sSortType = 'last') {
  540. $aOverride = $this->getAlterOrder();
  541. if (is_array($aOverride) && !empty($aOverride))
  542. return $aOverride;
  543. $aSql = array();
  544. switch ($sSortType) {
  545. case 'rand':
  546. $aSql['order'] = "ORDER BY RAND()";
  547. break;
  548. case 'top':
  549. $sHow = "DESC";
  550. $aSql['order'] = "ORDER BY `Rate` $sHow, `RateCount` $sHow, `date` $sHow";
  551. break;
  552. case 'score':
  553. if (is_array($this->aCurrent['restriction']['keyword'])) {
  554. $aSql['order'] = "ORDER BY `score` DESC";
  555. $aSql['ownFields'] .= $this->getSearchFieldsCond($this->aCurrent['searchFields'], $this->aCurrent['restriction']['keyword']['value'], 'score'). ', ';
  556. }
  557. break;
  558. case 'voteTime':
  559. $aSql['order'] = "ORDER BY `voteTime` DESC";
  560. break;
  561. case 'none':
  562. $aSql['order'] = "";
  563. break;
  564. default:
  565. $aSql['order'] = "ORDER BY `date` DESC";
  566. }
  567. return $aSql;
  568. }
  569. /**
  570. * Return own varaint for sorting (redeclare if necessary)
  571. * return array of sql elements
  572. */
  573. function getAlterOrder () {
  574. return array();
  575. }
  576. /**
  577. * Set paginate fields of class according GET params 'start' and 'per_page'
  578. * forcePage is need for setting most important number of current page
  579. */
  580. function setPaginate () {
  581. $this->aCurrent['paginate']['perPage'] = (isset($_GET['per_page']) && (int)$_GET['per_page'] != 0) ? (int)$_GET['per_page'] : $this->aCurrent['paginate']['perPage'];
  582. if (empty($this->aCurrent['paginate']['perPage']))
  583. $this->aCurrent['paginate']['perPage'] = BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT;
  584. $this->aCurrent['paginate']['start'] = isset($this->aCurrent['paginate']['forceStart'])
  585. ? (int)$this->aCurrent['paginate']['forceStart']
  586. : (empty($_GET['start']) ? 0 : (int)$_GET['start']);
  587. if ($this->aCurrent['paginate']['start'] < 0)
  588. $this->aCurrent['paginate']['start'] = 0;
  589. }
  590. /**
  591. * Get sql where condition for search fields
  592. * @param array of search fields
  593. * @param string $sKeyword keyword value for search
  594. * @param string sPseud for setting new name for generated set of fields in query
  595. * return sql code of WHERE part in query
  596. */
  597. function getSearchFieldsCond ($aFields, $sKeyword, $sPseud = '') {
  598. if (!$sKeyword)
  599. return;
  600. $oDb = BxDolDb::getInstance();
  601. $bLike = getParam('useLikeOperator');
  602. $sKeyword = $oDb->escape($sKeyword);
  603. $aSql = array(
  604. 'function' => 'MATCH',
  605. 'operator' => 'AGAINST',
  606. 'word' => $sKeyword
  607. );
  608. if ($bLike == 'on') {
  609. $aSql = array(
  610. 'function' => 'CONCAT', // TODO: get rid of CONCAT !!!
  611. 'operator' => 'LIKE',
  612. 'word' => '%' . preg_replace('/\s+/', '%', $sKeyword) . '%'
  613. );
  614. }
  615. $sqlWhere = " {$aSql['function']}(";
  616. if (is_array($aFields)) {
  617. foreach ($aFields as $sValue)
  618. $sqlWhere .= "`{$this->aCurrent['table']}`.`$sValue`, ";
  619. } else {
  620. $sqlWhere .= "`{$this->aCurrent['table']}`.`$aFields`";
  621. }
  622. $sqlWhere = trim($sqlWhere, ', ') . ") {$aSql['operator']} ('{$aSql['word']}')";
  623. if (strlen($sPseud) > 0)
  624. $sqlWhere .= " as `$sPseud`";
  625. return $sqlWhere;
  626. }
  627. /**
  628. * Get set from several values for 'in' and 'not in' operators
  629. * @param $aValues array of values
  630. * return sql code for field with operator IN (NOT IN)
  631. */
  632. function getMultiValues ($aValues) {
  633. $oDb = BxDolDb::getInstance();
  634. return $oDb->implode_escape($aValues);
  635. }
  636. /**
  637. * Generate rate object if it wasn't created yet
  638. */
  639. function getRatePart () {
  640. if ($this->iRate == 1) {
  641. bx_import('BxTemplVote');
  642. $this->oRate = new BxTemplVote($this->aCurrent['name'], 0, 0);
  643. }
  644. }
  645. /**
  646. * System method for filling aPseud array.
  647. * Fill field aPseud for current class (if you will use own getSearchUnit methods then not necessary to redeclare).
  648. */
  649. function _getPseud () {
  650. }
  651. /**
  652. * Add replace markers. Markers are replaced in titles and browse urls
  653. * @param $a array of markers as key => value
  654. * @return true on success or false on error
  655. */
  656. public function addMarkers ($a) {
  657. if (empty($a) || !is_array($a))
  658. return false;
  659. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  660. return true;
  661. }
  662. /**
  663. * Replace provided markers in a string
  664. * @param $mixed string or array to replace markers in
  665. * @return string where all occured markers are replaced
  666. */
  667. protected function _replaceMarkers ($mixed) {
  668. return bx_replace_markers($mixed, $this->_aMarkers);
  669. }
  670. }