Приведем пример, как в несколько шагов можно создать простой поиск по названию объекта системы. В поле поиска можно вводить выражения AND, OR или + и скобки. Этот учебный пример можно легко настроить под свои потребности.
Первый шаг. Создайте чанк с кодом (Id) chunks_finder (подробнее о том, как создавать чанки, описано в п.Пример создания чанка). На вкладке Код Чанка вставьте следующее (уберите лишние теги возле пробелов):
< form name="quick_find" action="" method="post" >
< input type="text" name="searchkeywords" id="idsearchkeywords" class="input" maxlength="50" style="width:75%" / >
< input type="hidden" name="search_in_description" value="" / >
< img src="main/styles/img/find.png" alt="" title="" style="height:16px;" / >
< /form >
Второй шаг. Создайте сниппет c кодом (Id) snippets_finder (подробнее о том, как создавать сниппеты, описано в Создание сниппета). На вкладке php-Скрипт карточки сниппета вставьте следующий код:
// Create new instance of the wAjaxGrid class
include_once(SYS_BASE_PATH.'main/classes/wajaxgrid.class.php');
$fn = __FILE__;
$action = (isset($action)) ? $action : $parser->getPageId();
$searchkeywords = (isset($_REQUEST['searchkeywords'])) ? $_REQUEST['searchkeywords'] : '';
$search_in_description = (isset($search_in_description)) ? $search_in_description : 1;
$quick_find_text = (isset($quick_find_text)) ? $quick_find_text : 'quicksearch';
$ext_find_text = (isset($ext_find_text)) ? $ext_find_text : 'advsearch';
$searchTpl = isset($searchTpl) ? $searchTpl: 'chunks_finder';
$title = (isset($title)) ? $title : 'quicksearch';
if(!$searchTpl) return;
$output = str_replace("", $parser->gt($title, $fn), $parser->getChunk($searchTpl));
$output = str_replace("", $parser->gt($quick_find_text, $fn), $output);
$output = str_replace("", $parser->gt($ext_find_text, $fn), $output);
$output = str_replace("", $parser->makeUrl($action), $output);
$output = str_replace("", $search_in_description, $output);
print($output);
if($searchkeywords)
{
$grid = new wAjaxGrid();
$grid->container = 'grid_arrsimple';
$grid->pagelen = 10;
// Set column names
$grid->colnames = $parser->gt('id', $fn).','.$parser->gt('name', $fn);
$grid->addHidden('searchkeywords', $searchkeywords);
$grid->q->select('o.objid');
$grid->q->lselect('o.name');
$grid->q->from('sys_objects o');
$str = trim(str_replace('(', '( ', $searchkeywords));
$str = str_replace(')', ' )', $str);
$str = str_replace('+', ' OR ', $str);
$tok = preg_split('/[[:space:]]+/', $str);
$output = '';
$brc = 0;
$pre = '';
for($i = 0; $i < sizeof($tok); $i++)
{
$word = strtolower($tok[$i]);
switch($word)
{
case '(': $brc++;
if($output and ($pre !='and') and ($pre != 'or') and ($pre != '(')) $output .= ' AND';
$pre = '(';
$output .= '('; continue 2;
break;
case ')': $brc--;
$pre = ')';
$output .= ')'; continue 2;
break;
case 'and':
case 'or':
$pre = $word;
$output .= ' '.strtoupper($word); continue 2;
break;
default:
if($output and ($pre !='and') and ($pre != 'or') and ($pre != '(')) $output .= ' AND';
$pre = $word;
$output .= " (o.name_".$parser->lang_suff." LIKE '%".$tok[$i]."%') ";
if($search_in_description) $output .= " OR (o.description LIKE '%".$tok[$i]."%')";
}
}
if($output) $grid->q->where($output);
$grid->q->orderby('o.objid');
$grid->Show();
}
Третий шаг. Создайте страницу, на которой будет происходить вызов сниппета (подробнее о том, как создавать страницы в MVC-2, описано в п.Создание страницы).
При использовании MVC-1 в модели или контроллере применяется вызов сниппета:
$this->parser->runSnippet('snippets_finder');
При использовании MVC-2 в содержании страницы, шаблоне вызывается сниппет (уберите лишние пробелы возле тегов):
{ { snippets_finder } }
Зайдите на созданную страницу, где происходит вызов сниппета, и введите в поле поиска слово Контакты (для поиска страниц, в названии которых встречается это ключевое слово). Если вы все сделали правильно, под полем для поиска появятся результаты поиска.