Файл: sources/search.php
Строк: 230
<?php
if (!defined('VAPHP'))
{
die("Этот файл недоступен.");
}
class search extends base
{
function search()
{
global $CONF, $FORM, $LNG, $TMPL;
$TMPL['header'] = $LNG['search_header'];
if (isset($FORM['q']) and $FORM['q'])
{
$this->process();
}
}
function process()
{
global $CONF, $DB, $FORM, $LNG, $TMPL;
$TMPL['query'] = strip_tags($FORM['q']);
$FORM['q'] = trim($FORM['q']);
if(strlen($FORM['q'])>2)
{
$words = explode(' ', $DB->escape($FORM['q']));
// Отфильтровуем слова из 1 или 2 букв
$filtered_words = array_filter($words, create_function('$word', 'return strlen($word) > 2;'));
if (count($filtered_words) > 0)
{
$words = $filtered_words;
}
$query = "SELECT id, URL, description, name, banner_url, num_views FROM {$CONF['sql_prefix']}_sites WHERE visib = '1' AND add_admin = '0' AND (";
$i = 0;
foreach ($words as $word)
{
if ($i > 0)
{
$query .= " OR ";
}
$query .= "description LIKE '%{$word}%' OR name LIKE '%{$word}%'";
$i++;
}
$query .= ") order by {$CONF['site_sorting_field']} {$CONF['site_sorting']}";
$result = $DB->query($query, __FILE__, __LINE__);
$num_results = $DB->num_rows($result);
//адрес этой страницы
$template_href="{$CONF['site_url']}/search";
//закодировать поисковый запрос для формирования ссылок
$code_query = htmlentities(urlencode($FORM['q']));
if ($FORM['pp'])
{
$pp = intval($FORM['pp']);
if ($pp < $CONF['sites_per_page'][0]) {$pp = $CONF['sites_per_page'][0];}
}
else {$pp = $CONF['sites_per_page'][0];}
foreach($CONF['sites_per_page'] as $value)
{
if ($pp == $value) {$per_page_menu.= "<b>$value</b> ";}
else{
$per_page_menu.=<<<EndHTML
<a href="{$template_href}/$value/{$code_query}">{$value}</a>
EndHTML;
}
}
if (isset($FORM['p']))
{
$p = intval ($FORM['p']);
if ($p > ceil($num_results/$pp) || $p < 1) {$p=1;} //если номер страницы больше максимально возможного - присваиваем ему значение 1
$start = intval($p - 1);
if ($start < 0) $start=0;
$start = $start * $pp;
}
else
{
$p = 1;
$start = 0;
}
if($num_results > $pp) //проверка необходимости в постраничном выводе
{
$TMPL['page_menu'] = '';
$num_page = ceil($num_results/$pp); //количество страниц
$PrevPage = $p-1;
$NextPage = $p+1;
//определение переменных для счетчика цикла
if($num_page > $CONF['lenght_pp_block']){
$left_pages = floor(($CONF['lenght_pp_block']-1)/2);
$number_start_page = $p-$left_pages;
if($number_start_page <= 0) {$number_start_page = 1;}
$number_end_page = $number_start_page + $CONF['lenght_pp_block']-1;
if($number_end_page > $num_page) {$number_end_page = $num_page; $number_start_page = $number_end_page-$CONF['lenght_pp_block']+1;}
}else
{
$number_start_page = 1;
$number_end_page = $num_page;
}
//само построение кода постраничного вывода
if($number_start_page > 1) {
$prev_page = $number_start_page-1;
$TMPL['page_menu'] .= "<a href="{$template_href}/1/$pp/{$code_query}">1</a> "; //ссылка на самую первую страницу
$TMPL['page_menu'] .= "<a href="{$template_href}/{$prev_page}/$pp/{$code_query}">...</a> "; //
}
for($i=$number_start_page; $i<=$number_end_page; $i++)
{
if($i == $p)
$TMPL['page_menu'] .= "<b>$i</b> ";
else
$TMPL['page_menu'] .= "<a href="{$template_href}/{$i}/{$pp}/{$code_query}">$i</a> ";
}
if($number_end_page < $num_page) {
$next_page = $number_end_page+1;
$TMPL['page_menu'] .= "<a href="{$template_href}/{$next_page}/$pp/{$code_query}">...</a> ";
$TMPL['page_menu'] .= "<a href="{$template_href}/{$num_page}/$pp/{$code_query}">$num_page</a> ";
}
if($p <= 1) {$TMPL['AtStart'] = "<span class=AtStart><IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" src="{$CONF['skins_image']}/button_prev.gif"></span>";}
else {$TMPL['AtStart'] = "<a href="{$template_href}/{$PrevPage}/{$pp}/{$code_query}" class=AtStart><IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" src="{$CONF['skins_image']}/button_prev_act.gif"></a>";}
if($p >= $num_page) {$TMPL['AtEnd']="<a class=AtEnd><IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" src="{$CONF['skins_image']}/button_next.gif"></a>";}
else {$TMPL['AtEnd']="<a href="{$template_href}/{$NextPage}/{$pp}/{$code_query}" class=AtEnd><IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" src="{$CONF['skins_image']}/button_next_act.gif"></a>";}
$TMPL['page_menu'] = $TMPL['AtStart'].$TMPL['page_menu'].$TMPL['AtEnd'];
$TMPL['page_menu']="<div class="PagingStrip">".$TMPL['page_menu']."</div>";
//конец построения кода постраничного вывода
}
$num_first_site = $start + 1;
$num_final_site = $num_first_site + $pp - 1;
if ($num_final_site > $num_results) {$num_final_site = $num_results;}
$LNG['g_number_of_all'] = sprintf($LNG['g_number_of_all'], $num_first_site, $num_final_site, $num_results);
if ($num_results != 0) $TMPL['admin_content'] .= <<<EndHTML
<table width="100%">
<tr><td colspan="2">
<table width="100%" border=0>
<tr>
<td width="33%"></td>
<td width="33%" align="center"><span class="GreyTextNormal">{$LNG['g_number_of_all']}</span></td>
<td width="33%" align="right"><span class="GreyTextSmall">{$LNG['g_sites_per_page']} <img src="{$CONF['skins_image']}/arrow_right.gif"> $per_page_menu</span></td>
</tr>
</table>
</td></tr>
<tr><td colspan="2"><hr width=100% size="1" class="SeparatorHr2"></td></tr>
<tr><td colspan="2" align=center>{$TMPL['page_menu']}</td></tr>
<tr><td colspan="2"><hr width=100% size="1" class="SeparatorHr"></td></tr>
</table>
EndHTML;
}
if (!$num_results)
{
$this->error($LNG['search_no_sites']);
}
else
{
$TMPL['list_sites'] .= <<<EndHTML
<tr><td colspan="2">
<table width="100%" border=0>
<tr>
<td width="33%"></td>
<td width="33%" align="center"><span class="GreyTextNormal">{$LNG['g_number_of_all']}</span></td>
<td width="33%" align="right"><span class="GreyTextSmall">{$LNG['g_sites_per_page']} <img src="{$CONF['skins_image']}/arrow_right.gif"> $per_page_menu</span></td>
</tr>
</table>
</td></tr>
<tr><td colspan="2"><hr width=100% size="1" class="SeparatorHr2"></td></tr>
<tr><td colspan="2" align=center>{$TMPL['page_menu']}</td></tr>
<tr><td colspan="2"><hr width=100% size="1" class="SeparatorHr"></td></tr>
EndHTML;
}
$result = $DB->select_limit($query, $pp, $start, __FILE__, __LINE__);
while (list($id, $URL, $description, $name, $banner_url, $num_views) = $DB->fetch_array($result))
{
foreach ($words as $word)
{
$word = preg_quote($word);
$description = preg_replace_callback("/({$word})/i", create_function('$matches', 'return "<b>{$matches[1]}</b>";'), $description);
$name = preg_replace_callback("/({$word})/i", create_function('$matches', 'return "<b>{$matches[1]}</b>";'), $name);
}
if(strlen($description) > $CONF['cut_descr'] and $CONF['cut_descr']!=0)
{
$description = substr($description, 0, $CONF['cut_descr']);
$description .= "...";
$html_cod="<a href="{$CONF['site_url']}/site/{$id}"><b>{$name}</b></a><br>{$description}";
}else
{
$html_cod="<a href="{$CONF['site_url']}/site/{$id}"><b>{$name}</b></a><br>{$description}";
}
$href_button = "{$CONF['site_url']}/site/{$id}";
$TMPL['list_sites'] .= <<<EndHTML
<tr>
<td><a href="{$href_button}" target=blank><img src="{$banner_url}" width="{$CONF['max_banner_width']}" height="{$CONF['max_banner_height']}" border="0" title="{$name}"></a></td>
<td width=100%>
<table width=100%>
<tr><td colspan="2">$html_cod</td></tr>
<tr>
<td width=50%>{$LNG['g_views']}: {$num_views}</td>
<td><a href="{$CONF['site_url']}/site/{$id}">{$LNG['g_detail']} >></a></td>
</tr>
</table>
</td>
</tr>
<tr><td colspan="2"><hr width=100% size="1" class="SeparatorHr"></td></tr>
EndHTML;
}
$TMPL['content'] = $this->do_skin('list_sites');
}
}
?>