-
-
Save Sentinel-7/8a899d6ad040c7a0b914555b437e5d0c to your computer and use it in GitHub Desktop.
Интеграция компонента Tagger с фильтрами в mFilter2 (компонент mSearch2) - Расширение класса фильтрации через системную настройку mse2_filters_handler_class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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('"', '"', 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment