Closed mittus closed 5 years ago
Hi,
I took down the I18n-Search support a while ago - it caused a lot of bloated stuff.
If your search only affects the items, you can easily create your own search function. For example, by loading all SimpleItems into memory (all at once or sequentially), and then using getSimpleItems(selector)
to search for a term. As selector you can use these:
search% (Like pattern starting with)
%search (Like pattern ending with)
%search% (Containing the pattern)
However, if you want the search to work through the entire page, you won't be able to avoid writing yourself an indexing function for the I18N plugin: http://mvlcek.bplaced.net/get-simple/i18nsearch/i18n-search-custom-indexing
An example, if you have 3 items:
Array (
[1] => SimpleItem Object
(
...
[description] => We have big bananas on our farm.
)
[2] => SimpleItem Object
(
...
[description] => Bananas are high in potassium and contain good levels of protein and dietary fiber.
)
[3] => SimpleItem Object
(
...
[description] => We love to eat apples and bananas
)
)
Your selector:
$result = $mapper->getSimpleItems('description=banan%');
Your result would be:
Array
(
[2] => SimpleItem Object
(
...
[description] => Bananas are high in potassium and contain good levels of protein and dietary fiber.
)
)
Your selector:
$result = $mapper->getSimpleItems('description=%banan');
Your result would be empty
Your selector:
$result = $mapper->getSimpleItems('description=%banan%');
Search result:
Array
(
[1] => SimpleItem Object
(
...
[description] => We have big bananas on our farm.
)
[2] => SimpleItem Object
(
...
[description] => Bananas are high in potassium and contain good levels of protein and dietary fiber.
)
[3] => SimpleItem Object
(
...
[description] => We love to eat apples and bananas
)
)
Here is another example of a search through multiple categories:
// The categories to search.
$searchCategories = [ 24, 25, 26 ];
// Your search term can also be user input $_POST/$_GET
$search = 'bananas';
// Search
$result = im_search([
'categories' => $searchCategories,
'search' => $search
]);
// Output search result
if($result) {
echo "<ul>$result</ul>";
}
function im_search(array $options)
{
$default = '<li>The search failed.</li>';
if(isset($options['categories']) && isset($options['search'])) {
if(!is_array($options['categories'])) return $default;
$buff = '';
foreach($options['categories'] as $catid) {
$buff .= im_search_for((int) $catid,
imanager('sanitizer')->text($options['search'])
);
}
return ($buff) ? $buff : $default;
}
}
function im_search_for($id, $searsh)
{
$imapper = imanager()->getItemMapper();
$cmapper = imanager()->getCategoryMapper();
$imapper->alloc($id);
$res = $imapper->getSimpleItems("description=%$searsh%");
if($res) {
return im_render_search_result($res, $cmapper->categories[$id]);
}
}
function im_render_search_result(array $result, Category $category)
{
$output = '';
foreach($result as $item) {
$item_slug = imanager('sanitizer')->pageName($item->name);
$category_slug = imanager('sanitizer')->pageName($category->name);
$url = "./$category_slug/$item_slug/";
ob_start(); ?>
<li><a href="<?php echo $url; ?>"><?php echo $item->name; ?></a></li>
<?php $output .= ob_get_clean();
}
return $output;
}
Hello!
Do you have example with using I18N Search plugin with items? I need global searching from all categories and items. And I can't configure it