Last active
May 3, 2017 20:40
-
-
Save eonarik/9d9caffa0e60d5cb5308fa33107d7390 to your computer and use it in GitHub Desktop.
modx processor getfilter
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 | |
// v.1.0.1 | |
class modWebResourcesGetfilterProcessor extends modProcessor { | |
public function process() { | |
$response = array('total' => 0, 'success' => 0); | |
$tvs = $this->getProperty('tvs') ? $this->getProperty('tvs') : array(); | |
$resource_fields = array_keys($this->modx->newObject('modResource')->toArray()); | |
$select = $excl = $tvs_name = []; | |
if($tvs){ | |
$response['success'] = 1; | |
$c = $this->modx->newQuery('modResource'); | |
$alias = $c->getAlias(); | |
if(!is_array($tvs)){ | |
$tvs = array_map('trim',explode(',',$tvs)); | |
} | |
foreach($tvs as &$tv){ | |
if(!is_numeric($tv)){ | |
if( | |
!in_array($tv,$resource_fields) | |
AND $tvobject = $this->modx->getObject('modTemplateVar', ['name' => $tv]) | |
) { | |
$tv = $tvobject->id; | |
$tvs_name[] = $tvobject->name; | |
} else { | |
$excl[] = $tv; | |
unset($tv); | |
} | |
} else { | |
$tvs_name[] = $tv; | |
} | |
} | |
if(!empty($tvs)){ | |
$c->innerJoin('modTemplateVarResource','tv_res' | |
,"tv_res.contentid = {$alias}.id and tv_res.tmplvarid in (". implode(',',$tvs) .")"); | |
$c->innerJoin('modTemplateVar','tv',"tv.id = tv_res.tmplvarid"); | |
$select = array_merge($select,array( | |
"{$alias}.id as id", | |
"tv.id as tv_id", | |
"tv.name as tv_name", | |
"tv.caption as tv_caption", | |
"tv_res.value as tv_value", | |
"count(*) as count" | |
)); | |
} | |
$c->select($select); | |
$c->groupby("tv_res.tmplvarid"); | |
$c->groupby("tv_res.value"); | |
if( | |
$sort = $this->getProperty('sort') | |
){ | |
$c->sortby($sort, $this->getProperty('dir') ? $this->getProperty('dir') : 'asc'); | |
} else { | |
$c->sortby('FIELD(tv_name, "'. implode('","',$tvs_name) .'")'); | |
} | |
if($where = $this->getProperty('where')) | |
$c->where($where); | |
// Обработаем параметр tvFilter | |
if($filter = $this->getProperty('tvFilter')){ | |
foreach($filter as $key=>$value){ | |
// ключ может быть такого вида 'tv.value:>' либо 'or:tv.value:like' | |
$ev = explode(':',$key); | |
if(count($ev) > 2){ | |
$key = str_replace('.value','',$ev[1]); | |
} else { | |
$key = str_replace('.value','',$ev[0]); | |
} | |
// проверим указан ли ключ тв как строка ли число (ид) | |
if(is_numeric($key)){ | |
$tv = $this->modx->getObject('modTemplateVar', $key); | |
} else { | |
$tv = $this->modx->getObject('modTemplateVar', array('name' => $key)); | |
} | |
// приджойним все это дело | |
if($tv){ | |
$c->innerJoin('modTemplateVarResource', "{$tv->name}", "{$alias}.id = {$tv->name}.contentid AND {$tv->name}.tmplvarid = {$tv->id}"); | |
$c->select(array("{$tv->name}.value")); | |
} | |
} | |
$c->where($filter); | |
} | |
if(!$c->prepare()) $response['success'] = 0; | |
// echo $c->toSQL(); | |
if(!$c->stmt->execute()) $response['success'] = 0; | |
if($response['success']) { | |
$response['object'] = array(); | |
foreach($c->stmt->fetchAll(2) as $l){ | |
$response['total'] += $l['count']; | |
$object =& $response['object'][$l['tv_id']]; | |
$object['name'] = $l['tv_name']; | |
$object['caption'] = $l['tv_caption']; | |
$l['tv_value_desc'] = $l['tv_value']; | |
if( | |
$diapason_tvs = $this->getProperty('diapason') | |
AND in_array($l['tv_name'], $diapason_tvs) | |
){ | |
if(empty($object['min_value'])){ | |
$object['min_value'] = $l['tv_value']; | |
} | |
if(empty($object['max_value'])){ | |
$object['max_value'] = $l['tv_value']; | |
} | |
if($object['min_value'] > (int)$l['tv_value']){ | |
$object['min_value'] = (int)$l['tv_value']; | |
}else if($object['max_value'] < (int)$l['tv_value']){ | |
$object['max_value'] = (int)$l['tv_value']; | |
} | |
} else { | |
$object['values'][] = $l; | |
} | |
} | |
} | |
} | |
return $response; | |
} | |
} | |
return 'modWebResourcesGetfilterProcessor'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment