PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/search.php

https://code.google.com/
PHP | 442 lines | 335 code | 56 blank | 51 comment | 82 complexity | 8121a7b3f83fad0999ffadfe0863ede4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
  2. /**
  3. * OSClass รข&#x20AC;&#x201C; software for creating and publishing online classified advertising platforms
  4. *
  5. * Copyright (C) 2010 OSCLASS
  6. *
  7. * This program is free software: you can redistribute it and/or modify it under the terms
  8. * of the GNU Affero General Public License as published by the Free Software Foundation,
  9. * either version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public
  16. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class CWebSearch extends BaseModel
  19. {
  20. var $mSearch;
  21. var $nice_url;
  22. var $uri;
  23. function __construct()
  24. {
  25. parent::__construct() ;
  26. $this->mSearch = Search::newInstance();
  27. $this->uri = preg_replace('|^' . REL_WEB_URL . '|', '', $_SERVER['REQUEST_URI']);
  28. $this->nice_url = false;
  29. if( !stripos($_SERVER['REQUEST_URI'], 'search') && osc_rewrite_enabled() ) {
  30. $this->nice_url = true;
  31. }
  32. if( $this->nice_url ) {
  33. // redirect if it ends with a slash
  34. if( preg_match('|/$|', $this->uri) ) {
  35. $redirectURL = osc_base_url() . $this->uri;
  36. $redirectURL = preg_replace('|/$|', '', $redirectURL);
  37. $this->redirectTo($redirectURL);
  38. }
  39. $search_uri = preg_replace('|/[0-9]+$|', '', $this->uri);
  40. $this->_exportVariableToView('search_uri', $search_uri);
  41. // remove seo_url_search_prefix
  42. if( osc_get_preference('seo_url_search_prefix') != '' ) {
  43. $this->uri = str_replace( osc_get_preference('seo_url_search_prefix') . '/', '', $this->uri);
  44. }
  45. // get page if it's set in the url
  46. $iPage = preg_replace('|.*/([0-9]+)$|', '$01', $this->uri);
  47. if( $iPage > 0 ) {
  48. Params::setParam('iPage', $iPage);
  49. // redirect without number of pages
  50. if( $iPage == 1 ) {
  51. $this->redirectTo(osc_base_url() . $search_uri);
  52. }
  53. }
  54. if( Params::getParam('iPage') > 1 ) {
  55. $this->_exportVariableToView('canonical', osc_base_url() . $search_uri);
  56. }
  57. $params = preg_split('|_|', preg_replace('|.*?/|', '', $search_uri));
  58. if( preg_match('|r([0-9]+)$|', $params[0], $r) ) {
  59. $region = Region::newInstance()->findByPrimaryKey($r[1]);
  60. Params::setParam('sRegion', $region['pk_i_id']);
  61. } else if( preg_match('|c([0-9]+)$|', $params[0], $c) ) {
  62. $city = City::newInstance()->findByPrimaryKey($c[1]);
  63. Params::setParam('sCity', $city['pk_i_id']);
  64. } else {
  65. Params::setParam('sCategory', $search_uri);
  66. }
  67. if( count($params) == 2 ) {
  68. $location = $params[1];
  69. if( preg_match('|r([0-9]+)$|', $location, $r) ) {
  70. $region = Region::newInstance()->findByPrimaryKey($r[1]);
  71. Params::setParam('sRegion', $region['pk_i_id']);
  72. }
  73. if( preg_match('|c([0-9]+)$|', $location, $c) ) {
  74. $city = City::newInstance()->findByPrimaryKey($c[1]);
  75. Params::setParam('sCity', $city['pk_i_id']);
  76. }
  77. }
  78. }
  79. }
  80. //Business Layer...
  81. function doModel()
  82. {
  83. osc_run_hook('before_search');
  84. $mCategories = Category::newInstance() ;
  85. if(osc_rewrite_enabled()) {
  86. // IF rewrite is not enabled, skip this part, preg_match is always time&resources consuming task
  87. $p_sParams = "/".Params::getParam('sParams', false, false);
  88. if(preg_match_all('|\/([^,]+),([^\/]*)|', $p_sParams, $m)) {
  89. $l = count($m[0]);
  90. for($k = 0;$k<$l;$k++) {
  91. switch($m[1][$k]) {
  92. case osc_get_preference('rewrite_search_country'):
  93. $m[1][$k] = 'sCountry';
  94. break;
  95. case osc_get_preference('rewrite_search_region'):
  96. $m[1][$k] = 'sRegion';
  97. break;
  98. case osc_get_preference('rewrite_search_city'):
  99. $m[1][$k] = 'sCity';
  100. break;
  101. case osc_get_preference('rewrite_search_city_area'):
  102. $m[1][$k] = 'sCityArea';
  103. break;
  104. case osc_get_preference('rewrite_search_category'):
  105. $m[1][$k] = 'sCategory';
  106. break;
  107. case osc_get_preference('rewrite_search_user'):
  108. $m[1][$k] = 'sUser';
  109. break;
  110. case osc_get_preference('rewrite_search_pattern'):
  111. $m[1][$k] = 'sPattern';
  112. break;
  113. default :
  114. break;
  115. }
  116. $_REQUEST[$m[1][$k]] = $m[2][$k];
  117. $_GET[$m[1][$k]] = $m[2][$k];
  118. unset($_REQUEST['sParams']);
  119. unset($_GET['sParams']);
  120. unset($_POST['sParams']);
  121. }
  122. }
  123. }
  124. ////////////////////////////////
  125. //GETTING AND FIXING SENT DATA//
  126. ////////////////////////////////
  127. $p_sCategory = Params::getParam('sCategory');
  128. if(!is_array($p_sCategory)) {
  129. if($p_sCategory == '') {
  130. $p_sCategory = array() ;
  131. } else {
  132. $p_sCategory = explode(",",$p_sCategory);
  133. }
  134. }
  135. $p_sCityArea = Params::getParam('sCityArea');
  136. if(!is_array($p_sCityArea)) {
  137. if($p_sCityArea == '') {
  138. $p_sCityArea = array() ;
  139. } else {
  140. $p_sCityArea = explode(",", $p_sCityArea);
  141. }
  142. }
  143. $p_sCity = Params::getParam('sCity');
  144. if(!is_array($p_sCity)) {
  145. if($p_sCity == '') {
  146. $p_sCity = array() ;
  147. } else {
  148. $p_sCity = explode(",", $p_sCity);
  149. }
  150. }
  151. $p_sRegion = Params::getParam('sRegion');
  152. if(!is_array($p_sRegion)) {
  153. if($p_sRegion == '') {
  154. $p_sRegion = array() ;
  155. } else {
  156. $p_sRegion = explode(",", $p_sRegion);
  157. }
  158. }
  159. $p_sCountry = Params::getParam('sCountry');
  160. if(!is_array($p_sCountry)) {
  161. if($p_sCountry == '') {
  162. $p_sCountry = array() ;
  163. } else {
  164. $p_sCountry = explode(",", $p_sCountry);
  165. }
  166. }
  167. $p_sUser = Params::getParam('sUser');
  168. if(!is_array($p_sUser)) {
  169. if($p_sUser == '') {
  170. $p_sUser = '';
  171. } else {
  172. $p_sUser = explode(",", $p_sUser);
  173. }
  174. }
  175. $p_sPattern = strip_tags(Params::getParam('sPattern'));
  176. // ADD TO THE LIST OF LAST SEARCHES
  177. if(osc_save_latest_searches()) {
  178. if(trim($p_sPattern)!='') {
  179. LatestSearches::newInstance()->insert(array( 's_search' => trim($p_sPattern), 'd_date' => date('Y-m-d H:i:s')));
  180. }
  181. }
  182. $p_bPic = Params::getParam('bPic');
  183. ($p_bPic == 1) ? $p_bPic = 1 : $p_bPic = 0 ;
  184. $p_sPriceMin = Params::getParam('sPriceMin');
  185. $p_sPriceMax = Params::getParam('sPriceMax');
  186. //WE CAN ONLY USE THE FIELDS RETURNED BY Search::getAllowedColumnsForSorting()
  187. $p_sOrder = Params::getParam('sOrder');
  188. if(!in_array($p_sOrder, Search::getAllowedColumnsForSorting())) {
  189. $p_sOrder = osc_default_order_field_at_search() ;
  190. }
  191. $old_order = $p_sOrder;
  192. //ONLY 0 ( => 'asc' ), 1 ( => 'desc' ) AS ALLOWED VALUES
  193. $p_iOrderType = Params::getParam('iOrderType');
  194. $allowedTypesForSorting = Search::getAllowedTypesForSorting() ;
  195. $orderType = osc_default_order_type_at_search();
  196. foreach($allowedTypesForSorting as $k => $v) {
  197. if($p_iOrderType==$v) {
  198. $orderType = $k;
  199. break;
  200. }
  201. }
  202. $p_iOrderType = $orderType;
  203. $p_sFeed = Params::getParam('sFeed');
  204. $p_iPage = 0;
  205. if( is_numeric(Params::getParam('iPage')) && Params::getParam('iPage') > 0 ) {
  206. $p_iPage = intval(Params::getParam('iPage')) - 1;
  207. }
  208. if($p_sFeed != '') {
  209. $p_sPageSize = 1000;
  210. }
  211. $p_sShowAs = Params::getParam('sShowAs');
  212. $aValidShowAsValues = array('list', 'gallery');
  213. if (!in_array($p_sShowAs, $aValidShowAsValues)) {
  214. $p_sShowAs = osc_default_show_as_at_search() ;
  215. }
  216. // search results: it's blocked with the maxResultsPerPage@search defined in t_preferences
  217. $p_iPageSize = intval(Params::getParam('iPagesize')) ;
  218. if($p_iPageSize > 0) {
  219. if($p_iPageSize > osc_max_results_per_page_at_search()) $p_iPageSize = osc_max_results_per_page_at_search() ;
  220. } else {
  221. $p_iPageSize = osc_default_results_per_page_at_search() ;
  222. }
  223. //FILTERING CATEGORY
  224. $bAllCategoriesChecked = false ;
  225. if(count($p_sCategory) > 0) {
  226. foreach($p_sCategory as $category) {
  227. $this->mSearch->addCategory($category);
  228. }
  229. } else {
  230. $bAllCategoriesChecked = true ;
  231. }
  232. //FILTERING CITY_AREA
  233. foreach($p_sCityArea as $city_area) {
  234. $this->mSearch->addCityArea($city_area);
  235. }
  236. $p_sCityArea = implode(", ", $p_sCityArea);
  237. //FILTERING CITY
  238. foreach($p_sCity as $city) {
  239. $this->mSearch->addCity($city);
  240. }
  241. $p_sCity = implode(", ", $p_sCity);
  242. //FILTERING REGION
  243. foreach($p_sRegion as $region) {
  244. $this->mSearch->addRegion($region);
  245. }
  246. $p_sRegion = implode(", ", $p_sRegion);
  247. //FILTERING COUNTRY
  248. foreach($p_sCountry as $country) {
  249. $this->mSearch->addCountry($country);
  250. }
  251. $p_sCountry = implode(", ", $p_sCountry);
  252. // FILTERING PATTERN
  253. if($p_sPattern != '') {
  254. $this->mSearch->addPattern($p_sPattern);
  255. $osc_request['sPattern'] = $p_sPattern;
  256. } else {
  257. // hardcoded - if there isn't a search pattern, order by dt_pub_date desc
  258. if($p_sOrder == 'relevance') {
  259. $p_sOrder = 'dt_pub_date';
  260. foreach($allowedTypesForSorting as $k => $v) {
  261. if($p_iOrderType=='desc') {
  262. $orderType = $k;
  263. break;
  264. }
  265. }
  266. $p_iOrderType = $orderType;
  267. }
  268. }
  269. // FILTERING USER
  270. if($p_sUser != '') {
  271. $this->mSearch->fromUser($p_sUser);
  272. }
  273. // FILTERING IF WE ONLY WANT ITEMS WITH PICS
  274. if($p_bPic) {
  275. $this->mSearch->withPicture(true) ;
  276. }
  277. //FILTERING BY RANGE PRICE
  278. $this->mSearch->priceRange($p_sPriceMin, $p_sPriceMax);
  279. //ORDERING THE SEARCH RESULTS
  280. $this->mSearch->order( $p_sOrder, $allowedTypesForSorting[$p_iOrderType]) ;
  281. //SET PAGE
  282. $this->mSearch->page($p_iPage, $p_iPageSize);
  283. osc_run_hook('search_conditions', Params::getParamsAsArray());
  284. if(!Params::existParam('sFeed')) {
  285. // RETRIEVE ITEMS AND TOTAL
  286. $aItems = $this->mSearch->doSearch();
  287. $iTotalItems = $this->mSearch->count();
  288. $iStart = $p_iPage * $p_iPageSize ;
  289. $iEnd = min(($p_iPage+1) * $p_iPageSize, $iTotalItems) ;
  290. $iNumPages = ceil($iTotalItems / $p_iPageSize) ;
  291. osc_run_hook('search', $this->mSearch) ;
  292. //preparing variables...
  293. $regionName = $p_sRegion;
  294. if( is_numeric($p_sRegion) ) {
  295. $r = Region::newInstance()->findByPrimaryKey($p_sRegion);
  296. if( $r ) {
  297. $regionName = $r['s_name'];
  298. }
  299. }
  300. $cityName = $p_sCity;
  301. if( is_numeric($p_sCity) ) {
  302. $c = City::newInstance()->findByPrimaryKey($p_sCity);
  303. if( $c ) {
  304. $cityName = $c['s_name'];
  305. }
  306. }
  307. //$this->_exportVariableToView('non_empty_categories', $aCategories) ;
  308. $this->_exportVariableToView('search_start', $iStart) ;
  309. $this->_exportVariableToView('search_end', $iEnd) ;
  310. $this->_exportVariableToView('search_category', $p_sCategory) ;
  311. // hardcoded - non pattern and order by relevance
  312. $p_sOrder = $old_order;
  313. $this->_exportVariableToView('search_order_type', $p_iOrderType) ;
  314. $this->_exportVariableToView('search_order', $p_sOrder) ;
  315. $this->_exportVariableToView('search_pattern', $p_sPattern) ;
  316. $this->_exportVariableToView('search_from_user', $p_sUser) ;
  317. $this->_exportVariableToView('search_total_pages', $iNumPages) ;
  318. $this->_exportVariableToView('search_page', $p_iPage) ;
  319. $this->_exportVariableToView('search_has_pic', $p_bPic) ;
  320. $this->_exportVariableToView('search_region', $regionName) ;
  321. $this->_exportVariableToView('search_city', $cityName) ;
  322. $this->_exportVariableToView('search_price_min', $p_sPriceMin) ;
  323. $this->_exportVariableToView('search_price_max', $p_sPriceMax) ;
  324. $this->_exportVariableToView('search_total_items', $iTotalItems) ;
  325. $this->_exportVariableToView('items', $aItems) ;
  326. $this->_exportVariableToView('search_show_as', $p_sShowAs) ;
  327. $this->_exportVariableToView('search', $this->mSearch) ;
  328. // json
  329. $json = $this->mSearch->toJson();
  330. $this->_exportVariableToView('search_alert', base64_encode($json)) ;
  331. //calling the view...
  332. $this->doView('search.php') ;
  333. } else {
  334. $this->mSearch->page(0, osc_num_rss_items());
  335. // RETRIEVE ITEMS AND TOTAL
  336. $iTotalItems = $this->mSearch->count();
  337. $aItems = $this->mSearch->doSearch();
  338. $this->_exportVariableToView('items', $aItems) ;
  339. if($p_sFeed=='' || $p_sFeed=='rss') {
  340. // FEED REQUESTED!
  341. header('Content-type: text/xml; charset=utf-8');
  342. $feed = new RSSFeed;
  343. $feed->setTitle(__('Latest listings added') . ' - ' . osc_page_title());
  344. $feed->setLink(osc_base_url());
  345. $feed->setDescription(__('Latest listings added in') . ' ' . osc_page_title());
  346. if(osc_count_items()>0) {
  347. while(osc_has_items()) {
  348. if(osc_count_item_resources() > 0){
  349. osc_has_item_resources();
  350. $feed->addItem(array(
  351. 'title' => osc_item_title(),
  352. 'link' => htmlentities( osc_item_url(), ENT_COMPAT, "UTF-8" ),
  353. 'description' => osc_item_description(),
  354. 'dt_pub_date' => osc_item_pub_date(),
  355. 'image' => array( 'url' => htmlentities(osc_resource_thumbnail_url(), ENT_COMPAT, "UTF-8"),
  356. 'title' => osc_item_title(),
  357. 'link' => htmlentities( osc_item_url() , ENT_COMPAT, "UTF-8") )
  358. ));
  359. } else {
  360. $feed->addItem(array(
  361. 'title' => osc_item_title(),
  362. 'link' => htmlentities( osc_item_url() , ENT_COMPAT, "UTF-8"),
  363. 'description' => osc_item_description(),
  364. 'dt_pub_date' => osc_item_pub_date()
  365. ));
  366. }
  367. }
  368. }
  369. osc_run_hook('feed', $feed);
  370. $feed->dumpXML();
  371. } else {
  372. osc_run_hook('feed_' . $p_sFeed, $aItems) ;
  373. }
  374. }
  375. }
  376. //hopefully generic...
  377. function doView($file)
  378. {
  379. osc_run_hook("before_html");
  380. osc_current_web_theme_path($file) ;
  381. Session::newInstance()->_clearVariables();
  382. osc_run_hook("after_html");
  383. }
  384. }
  385. /* file end: ./search.php */
  386. ?>