Skip to content

Instantly share code, notes, and snippets.

@sheronov
Last active March 18, 2023 00:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sheronov/a64692ced49a4f120ae87fd9795a0299 to your computer and use it in GitHub Desktop.
Save sheronov/a64692ced49a4f120ae87fd9795a0299 to your computer and use it in GitHub Desktop.
Интеграция компонента Tagger с фильтрами в mFilter2 (компонент mSearch2) - Расширение класса фильтрации через системную настройку mse2_filters_handler_class
<?php
class taggerCustomFilter extends mse2FiltersHandler {
/**
* Retrieves values from Tagger table
*
* @param array $fields
* @param array $ids
*
* @return array
*/
public function getTaggerValues(array $fields, array $ids) {
$filters = array();
if(!$this->modx->addPackage('tagger',$this->modx->getOption('tagger.core_path',null,$this->modx->getOption('core_path').'components/tagger/').'model/')) {
return $filters;
}
$q = $this->modx->newQuery('TaggerTagResource');
$q->innerJoin('TaggerTag','TaggerTag','TaggerTag.id = TaggerTagResource.tag');
$q->where(array('TaggerTagResource.resource:IN' => $ids,'TaggerTag.group:IN'=>$fields));
$q->select(array('TaggerTagResource.resource,TaggerTag.tag,TaggerTag.group'));
$tstart = microtime(true);
if ($q->prepare() && $q->stmt->execute()) {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
foreach ($row as $k => $v) {
$v = str_replace('"', '&quot;', trim($v));
if($k == 'tag') {
$k = $row['group'];
}
if ($k == 'resource' || $k == 'group') {
continue;
}
elseif (isset($filters[$k][$v])) {
$filters[$k][$v][$row['resource']] = $row['resource'];
}
else {
$filters[$k][$v] = array($row['resource'] => $row['resource']);
}
}
}
}
else {
$this->modx->log(modX::LOG_LEVEL_ERROR, "[mSearch2] Error on get filter params.\nQuery: " . $q->toSQL() . "\nResponse: " . print_r($q->stmt->errorInfo(), 1));
}
return $filters;
}
/**
* Prepares values for filter
* Retrieves labels of tagger tags and replaces ids in array keys by it
*
* @param array $values
* @param string $name
*
* @return array Prepared values
*/
public function buildTgroupFilter(array $values,$name = '') {
if(!$this->modx->addPackage('tagger',$this->modx->getOption('tagger.core_path',null,$this->modx->getOption('core_path').'components/tagger/').'model/')) {
return array();
}
$tags = array_keys($values);
if (empty($tags) || (count($tags) < 2 && empty($this->config['showEmptyFilters']))) {
return array();
}
$results = array();
$q = $this->modx->newQuery('TaggerTag', array('tag:IN' => $tags));
$q->select('tag,label');
$tstart = microtime(true);
if ($q->prepare() && $q->stmt->execute()) {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
$tags = array();
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
$tags[$row['tag']] = $row['label'];
}
foreach ($values as $tag => $ids) {
$title = !isset($tags[$tag])
? $this->modx->lexicon('mse2_filter_boolean_no')
: $tags[$tag];
$results[$title] = array(
'title' => $title,
'value' => $tag,
'type' => 'tagger',
'resources' => $ids
);
}
}
ksort($results);
return $results;
}
}
@sheronov
Copy link
Author

sheronov commented May 23, 2018

  1. Положить файл в директорию core/components/msearch2/custom/filters/
  2. Прописать в системной настройке mse2_filters_handler_class: taggerCustomFilter
  3. В вызове mFilter2 в параметре filters и aliases указывать значения так: (где 1,b3 - id групп в Tagger)
&filters=`
        tagger|3:tgroup,
        tagger|1:tgroup
    `
&aliases=`
      tagger|3==cars,
      tagger|1==colors
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment