caching = false; $what = $error = false; $config->factory("Listing"); $msg = ''; /** gets current page and defines start position **/ $page = 0; if(isset($_GET['page'])) { $page = (int)$_GET['page']; } elseif(isset($_POST['_settings']['page'])) { $page = (int)$_POST['_settings']['page']; } $page = ($page < 1) ? 1 : $page; $start = ($page - 1) * $config->get('num_index_listings'); $esynSmarty->assign("page", $page); $esynSmarty->assign("POST_json", "[]"); /** get link fields for display **/ $fields = $esynListing->getFieldsForSearch(); $temp = array(); foreach($fields as $f) { $temp[$f['name']] = $f; } $fields = $temp; unset($temp); $what = ''; $listings = array(); if(isset($_GET['what']) && !isset($_GET['adv'])) { $what = trim($_GET['what']); esynLoadUTF8Core(); esynLoadUTF8Util("validation"); if(!utf8_is_valid($what)) { trigger_error("Bad characters in 'query' variable in searching", E_USER_NOTICE); esynLoadUTF8Util("bad"); $what = utf8_bad_replace($what); } $len = utf8_strlen($what); if($len < 2) { $error = true; } if($len > 50) { $error = true; } if(!$error) { // escape wild characters $what = str_replace(array("%","_"),array("\%","\_"), $what); $what = esynSanitize::sql($what); if(!$searchType) { $searchType = 1; } /** search listings box formation **/ $nm = $config->get('num_index_listings'); $cause = $esynListing->getSearchCriterias($what, $searchType); $replaceQuery = false; $config->startHook("injectSearchClause"); if(!$replaceQuery) { $listings = $esynListing->getByCriteria($start, $nm, $cause, true); } $config->startHook("afterGetSearchResult"); $total_listings = 0; if(!empty($listings)) { $total_listings = $esynListing->foundRows(); } $esynSmarty->assign('total_listings', $total_listings); $c = count($listings); for($i=0; $i < $c; $i++) { if(!empty($listings[$i]['path'])) { $listings[$i]['path'] = $listings[$i]['path'].'/'; } } $esynSmarty->assign('listings', $listings); $url = "search.php?what=".urlencode($what)."&type=".$searchType; $esynSmarty->assign('url', $url); } } if(isset($_POST['searchquery']) || (isset($_GET['adv']) && isset($_GET['paging']))) { // user paging if(!isset($_POST['searchquery']) && isset($_GET['adv']) && !empty($_SESSION['lastSearchState'])) { $_POST = $_SESSION['lastSearchState']; } // from 0 to 1 $criteriasRate = 0; $what = trim($_POST['searchquery']); $textFields = array(); // $_POST['queryFilter']; esynLoadUTF8Core(); esynLoadUTF8Util("validation"); if(!utf8_is_valid($what)) { trigger_error("Bad characters in 'query' variable in searching", E_USER_NOTICE); esynLoadUTF8Util("bad"); $what = utf8_bad_replace($what); } $textQueryProvided = false; $list = array(); $fulltext = array(); foreach($fields as $f) { if($f['searchable'] == 2) { $fulltext[] = $f['name']; } $list[] = $f['name']; } $like = array(); $sqlQuery = array(); $len = utf8_strlen($what); if($len > 3 && $len < 50) { $criteriasRate += 0.2; // escape wild characters $what = esynSanitize::sql($what); $match = ' MATCH('; $temp = array(); // textfields where to search if(is_array($_POST['queryFilter']) && !empty($_POST['queryFilter'])) { foreach($_POST['queryFilter'] as $qf) { /*if(in_array($qf, $fulltext)) { $temp[] = $qf; }*/ if(in_array($qf, $list)) { $temp[] = $qf; } } } if(empty($temp)) { $match .= "`t1`.`title`, `t1`.`description`"; } else { foreach($temp as $t) { $match .= "`t1`.`".$t."`,"; } $match = rtrim($match, ","); } unset($temp); if(!isset($_POST['match'])) { $_POST['match'] = 'any'; } $against = ''; switch($_POST['match']) { case "any": $words = explode(" ", $what); // remove duplicated words $words = array_flip(array_flip($words)); $against = implode(" ", $words); $mode = "IN BOOLEAN MODE"; break; case "all": $words = explode(" ", $what); // remove duplicated words $words = array_flip(array_flip($words)); foreach($words as $k => $i) { $i = trim($i); $words[$k] = "+".$i; if(strlen($i) < 3) { unset($words[$k]); } $against = implode(" ", $words); $mode = "IN BOOLEAN MODE"; } break; case "exact": default: $against = "\"".$what."\""; $mode = "IN BOOLEAN MODE"; break; } $match .= ") AGAINST('".$against."' ".$mode.") "; $sqlQuery[] = $match; $textQueryProvided = true; } else { // escape wild characters $what = esynSanitize::sql($what); } $nm = $config->get('num_index_listings'); $s = ''; /* * t1 - is a 'listings' table (there are JOINs) */ foreach($_POST as $k => $f) { if(in_array($k, $list, true)) { if(($fields[$k]['type'] == 'checkbox' && is_array($f) && !empty($f)) || (($fields[$k]['type'] == 'combo' || $fields[$k]['type'] == 'radio') && $fields[$k]['show_as'] == 'checkbox')) { $s = "("; foreach($f as $x) { $s .= " FIND_IN_SET('".(int)$x."', t1.`".$k."`) OR "; } // remove last "OR " $s = substr($s, 0, -3); $s .= ")"; $sqlQuery[] = $s; // lower rate as of 'OR' operator may return too match result set $criteriasRate += 0.075; } elseif(($fields[$k]['show_as'] == 'combo') && is_scalar($f) && ($f !== '_doesnt_selected_')) { $criteriasRate += 0.1; // system stores numeric value and the title stored to the `language` $sqlQuery[] = "t1.`".$k."`='".(int)$f."'"; } elseif($fields[$k]['show_as'] == 'radio' && is_scalar($f)) { $criteriasRate += 0.1; // system stores numeric value and the title stored to the `language` $sqlQuery[] = "t1.`".$k."`='".(int)$f."'"; } elseif(($fields[$k]['type'] == 'storage' || $fields[$k]['type'] == 'image') && is_array($f)) { if($f['has'] == 'y') { $sqlQuery[] = "t1.`".$k."` <> ''"; } else { $sqlQuery[] = "t1.`".$k."` = ''"; } $criteriasRate += 0.075; } } } if(isset($_POST['_from']) && isset($_POST['_to'])) { foreach($_POST['_from'] as $k => $v) { if($fields[$k]['type'] == 'number' && in_array($k, $list, true)) { if((float)$v > 0) { // if both are set if(isset($_POST['_to'][$k]) && strlen($_POST['_to'][$k]) > 0) { $from = min((float)$v, (float)$_POST['_to'][$k]); $to = max((float)$v, (float)$_POST['_to'][$k]); if($to == $from) { $s = "`".$k."` = ".$to; } else { $s = "`".$k."` BETWEEN '".$from."' AND '".$to."' "; } $criteriasRate += 0.1; } else // if only from is set { $s = "`".$k."` > '".(float)$v."'"; $criteriasRate += 0.1; } } else { // if only "to" is set if(isset($_POST['_to'][$k]) && (float)$_POST['_to'][$k] > 0) { $to = (float)$_POST['_to'][$k]; $s = "`".$k."` < '".(float)$to."'"; $criteriasRate += 0.1; } else // none of them is set { $s = false; } } } if($s) { $sqlQuery[] = "(".$s.")"; } } } $cause = implode(" AND ", $sqlQuery); $sortBy = 't1.`date`'; if(!isset($_POST['_settings']['sort']) || $_POST['_settings']['sort'] == 'relevance') { $_POST['_settings']['sort'] = 'relevance'; if($textQueryProvided) { $sortBy = 'search_score'; } } if($criteriasRate < 0.2) { $error = true; $msg = $esynI18N['not_enough_criterias']; } $total_listings = 0; // not enough search criterias if(!$error) { $replaceQuery = true; $config->startHook("injectAdvancedSearchClause"); if($replaceQuery) { $listings = $esynListing->getAdvSearchListings((!empty($match) ? $match." as search_score, " : ''), $cause, $sortBy, $start, $nm); } $config->startHook("afterGetAdvSearchResult"); if(!empty($listings)) { $total_listings = $esynListing->foundRows(); } } $esynSmarty->assign_by_ref('total_listings', $total_listings); $c = count($listings); for($i=0; $i < $c; $i++) { if(!empty($listings[$i]['path'])) { $listings[$i]['path'] = $listings[$i]['path'].'/'; } } $esynSmarty->assign_by_ref('listings', $listings); $what = str_replace(' ', '+', $what); $url = "search.php?paging&adv"; $esynSmarty->assign_by_ref('url', $url); $_SESSION['lastSearchState'] = $_POST; // there are no found listings if(!$c) { if(!isset($json) || !is_object($json)) { esynLoadClass("JSON"); $json = new Services_JSON; } $esynSmarty->assign("POST_json", $json->encode($_SESSION['lastSearchState'])); $showForm = true; } else { $showForm = false; } } elseif($_SESSION['lastSearchState']) { if(!isset($json) || !is_object($json)) { esynLoadClass("JSON"); $json = new Services_JSON; } $esynSmarty->assign("POST_json", $json->encode($_SESSION['lastSearchState'])); } else { $esynSmarty->assign("POST_json", '[]'); } include_once(ESYN_TEMPLATES.$config->get('tmpl').ESYN_DS.'Layout.php'); $gDirLayout = & new esynLayout; /** defines page title **/ $esynSmarty->assign('title', $esynI18N['search']); /** breadcrumb formation **/ if(isset($listings) && !empty($listings) && isset($_GET['adv'])) { $bc['details']['url'] = "search.php?adv"; $bc['details']['caption'] = $esynI18N['search_criterias']; } $bc['search']['url'] = ''; $bc['search']['caption'] = isset($_GET['adv']) ? $esynI18N['advanced_search'] : $esynI18N['search']; $breadcrumb = $gDirLayout->printBreadcrumb(0, $bc, 1); $esynSmarty->assign_by_ref('breadcrumb', $breadcrumb); if(isset($_GET['adv'])) { $esynDb->setTable("search_sections"); $sections = $esynDb->all("*", "`lang`='".ESYN_LANGUAGE."' ORDER by `order` ASC"); if($sections) { $buffer = array(); foreach($sections as $s) { $buffer[$s['key']] = $s; } $sections = $buffer; unset($buffer); } $esynDb->resetTable(); $temp = array(); foreach ($fields as $key => $value) { if (!in_array($value['type'], array('text', 'textarea', 'number'), true)) { if($value['type'] == 'image') { $temp = $value; $temp['file_types'] = explode(',', $value['file_types']); } else if($value['type'] == "storage") { $temp = $value; $temp['file_types'] = explode(',', $value['file_types']); } else { if ($value['type'] == 'checkbox') { $fields[$key]['default'] = explode(',', $value['default']); $temp = $value; } elseif ($value['type'] == 'combo') { $fields[$key]['default'] = explode(',', $value['default']); $temp = $value; } elseif ($value['type'] == 'radio') { $fields[$key]['default'] = explode(',', $value['default']); $temp = $value; } $values = explode(',', $value['values']); $temp['values'] = array(); foreach($values as $v) { $k = 'field_'.$value['name'].'_'.$v; $temp['values'][$v] = $esynI18N[$k]; } } } else { if($value['type'] == 'number') { $esynDb->setTable("language"); $ranges = $esynDb->keyvalue("`key`,`value`", "`key` LIKE 'field\_".$value['name']."\_range\_%'"); $esynDb->resetTable(); $value['ranges'] = array(); if(!empty($ranges)) { foreach($ranges as $k2=>$v2) { $k2 = array_pop(explode("_", $k2)); $value['ranges'][$k2] = $v2; } } ksort($value['ranges']); } $temp = $value; if($value['type'] != 'number') { $textFields[$key] = $temp; } } if($value['type'] != 'text' && $value['type'] != 'textarea') { if(empty($temp['section_key']) || empty($sections[$temp['section_key']])) { $temp['section_key'] = "___empty___"; } $sections[$temp['section_key']]['fields'][] = $temp; } } $esynSmarty->assign('textFields', $textFields); } $esynSmarty->assign('adv', isset($_GET['adv'])); $esynSmarty->assign('sections', $sections); $esynSmarty->assign('showForm', $showForm); $esynSmarty->assign('msg', $msg); $esynSmarty->display('search.tpl');