Wallogit.com
2017 © Pedro Peláez
If you want to find some materials for a given key - the fastest way is to use class CMSSearch, (*1)
In CMSSearch constructor you must define at least 2 first parameters, (*2)
For example if you want to find some materials with key 'first' and create search in fields with identifiers 5, 2 and 7, you can use follow code, (*3)
PHP code:, (*4)
// Define fields array
$fields = array(5, 2, 7);
// Create search object
$search = new samsonos\cms\search\CMSSearch('first', $fields, array('key'=>$search));
// Create view of founded objects
$materialHTML = '';
foreach ($search->searchMaterials() as $product) {
$materialHTML .= m()->view('search/item')->product($product)->output();
}
$searchBlock = m()->view('search/items')
->set($search->pager)
->searchItems($materialHTML)
->output();
Thanks to CMSSearch module you can create a simple preview using your own async controller that must be defined in attribute 'preview-action' of search input. For using that you need add class 'samson_CMS_searchInput' to your search input. If you want to show loader while you are waiting for ajax response, you can add block with class samson_CMS_searchLoader to your html code., (*5)
HTML code:, (*6)
LESS code:, (*7)
.samson_CMS_searchLoader {
top: 0;
width: 100%;
height: 100%;
border-radius: 5px;
text-align: center;
background-color: rgba(0,0,0,0.45);
position: absolute;
}
.samson_CMS_searchPreview {
position: absolute;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
width: 100%;
top: 40px;
border-radius: 5px;
height: 300px;
background: #ffffff;
z-index: 90;
.samson_CMS_searchPreviewItems {
margin: 15px;
}
}
PHP code:, (*8)
/**
* Async controller for rendering search preview
*/
function search_async_preview()
{
$search = new \samsonos\cms\search\CMSSearch($_GET['key'], array(85, 49), array(), 5, 'search', 'searchExternalHandler');
$response = array('status' => 1);
$itemView = '';
foreach ($search->searchMaterials() as $item) {
$itemView .= m()->view('search/preview_item')->item($item)->output();
}
$response['html'] = $itemView;
return $response;
}
/**
* Search external handler for modifying query
*/
function searchExternalHandler(& $query)
{
$query = $query[0];
$query->cond('material.MyField', 1);
}