Last active
April 12, 2017 08:49
-
-
Save AlexanderKomkov/a62adb2c032b6dbeb33f to your computer and use it in GitHub Desktop.
myCustomFilter
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 myCustomFilter extends mse2FiltersHandler { | |
/** | |
* Retrieves values from Resource table | |
* | |
* @param array $fields Name of resource fields. | |
* @param array $ids Ids of needed resources | |
* | |
* @return array Array with resource fields as keys and resources ids as values | |
*/ | |
public function getResourceValues(array $fields, array $ids) { | |
$filters = array(); | |
$catalog = false; | |
$no_id = false; | |
if (!in_array('id', $fields)) { | |
$fields[] = 'id'; | |
$no_id = true; | |
} | |
if (in_array('parent', $fields) && $this->mse2->checkMS2()) { | |
$parent = ($this->mse2->config['parents'] !== '') ? $this->mse2->config['parents'] : $this->modx->resource->get('id'); | |
$q = $this->modx->newQuery('msCategory'); | |
$q->select('id'); | |
$q->where(array('parent:=' => $parent)); | |
$tcstart = microtime(true); | |
if ($q->prepare() && $q->stmt->execute()) { | |
$this->modx->queryTime += microtime(true) - $tcstart; | |
$this->modx->executedQueries++; | |
$catalog = $q->stmt->fetchAll(PDO::FETCH_COLUMN); | |
} | |
} | |
$q = $this->modx->newQuery('modResource'); | |
$q->select(implode(',', $fields)); | |
$q->where(array('modResource.id:IN' => $ids)); | |
if (in_array('parent', $fields) && $this->mse2->checkMS2()) { | |
$q->leftJoin('msCategoryMember','Member', '`Member`.`product_id` = `modResource`.`id`'); | |
$q->orCondition(array('Member.product_id:IN' => $ids)); | |
$q->select('category_id'); | |
} | |
$tstart = microtime(true); | |
if ($q->prepare() && $q->stmt->execute()) { | |
$this->modx->queryTime += microtime(true) - $tstart; | |
$this->modx->executedQueries++; | |
$i = 0; | |
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) { | |
foreach ($row as $k => $v) { | |
$v = trim($v); | |
if ($k == 'category_id') { | |
if ($row['parent'] == $v) {continue;} | |
else {$k = 'parent';} | |
} | |
if ($v == '' || ($k == 'id' && $no_id)) { | |
continue; | |
} | |
elseif (isset($filters[$k][$v])) { | |
if (!in_array($row['id'], $filters[$k][$v])) { | |
if ($k == 'parent') { | |
if (in_array($v, $catalog)) $filters[$k][$v][] = $row['id']; | |
} | |
else { | |
$filters[$k][$v][] = $row['id']; | |
} | |
} | |
} | |
else { | |
if ($k == 'parent') { | |
if (in_array($v, $catalog)) $filters[$k][$v] = array($row['id']); | |
} | |
else { | |
$filters[$k][$v] = array($row['id']); | |
} | |
} | |
$i++; | |
} | |
} | |
} | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment