Created
September 22, 2022 03:27
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 | |
/** | |
* Return array with filters | |
* | |
* @param array|string $ids | |
* @param boolean $build | |
* | |
* @return array|boolean | |
*/ | |
public function getFilters($ids, $build = true) { | |
// prepare ids | |
if (!is_array($ids)) { | |
$ids = array_map('trim', explode(',', $ids)); | |
} | |
if (empty($ids)) { | |
return false; | |
} | |
$cacheOptions = ['cache_key'=>'msearch2']; | |
// Return results from cache | |
if ($build && $cache = $this->modx->cacheManager->get('prep_' . md5(implode(',', $ids) . $this->config['filters']),$cacheOptions)) { | |
$this->methods = $cache['methods']; | |
$this->aliases = $cache['aliases']; | |
$this->filters = $cache['filters']; | |
//$this->modx->log(1,"prep_"); | |
return $cache['filters']; | |
} | |
// elseif (!$build && !empty($this->filters)) { | |
// return $this->filters; | |
// } | |
elseif (!$build && $cache0 = $this->modx->cacheManager->get('fltr_' . md5(implode(',', $ids) . $this->config['filters']),$cacheOptions ) | |
) { | |
$this->methods = $cache0['methods']; | |
$this->aliases = $cache0['aliases']; | |
$this->filters = $cache0['filters']; | |
//$this->modx->log(1,"fltr_"); | |
//$this->pdoTools->addTime("filters0 ".'fltr_' . md5(implode(',', $ids) . $this->config['filters']).print_r($cache0,1)); | |
return $this->filters; | |
} | |
//$this->modx->log(1,"no filter"); | |
if (!is_object($this->filtersHandler)) { | |
$this->loadHandler(); | |
} | |
if (empty($this->filters) || empty($this->methods)) { | |
// Preparing filters | |
$filters = $built = $duplicates = array(); | |
$tmp_filters = array_map('trim', explode(',', $this->config['filters'])); | |
foreach ($tmp_filters as $v) { | |
$v = strtolower($v); | |
if (empty($v)) { | |
continue; | |
} | |
elseif (strpos($v, $this->config['filter_delimeter']) !== false) { | |
@list($table, $filter) = explode($this->config['filter_delimeter'], $v); | |
} | |
else { | |
$table = 'resource'; | |
$filter = $v; | |
} | |
$tmp = explode($this->config['method_delimeter'], $filter); | |
$name = $tmp[0]; | |
$filter = !empty($tmp[1]) | |
? $tmp[1] | |
: 'default'; | |
// Duplicates | |
if (isset($filters[$table][$name])) { | |
$old_filter = $built[$table . $this->config['filter_delimeter'] . $name]; | |
$new_name = $name . '-' . $old_filter; | |
$built[$table . $this->config['filter_delimeter'] . $new_name] = $old_filter; | |
$filters[$table][$new_name] = $filters[$table][$name]; | |
$duplicates[$table][$new_name] = $name; | |
$new_name = $name . '-' . $filter; | |
$duplicates[$table][$new_name] = $name; | |
$name = $new_name; | |
} | |
$filters[$table][$name] = array(); | |
$built[$table . $this->config['filter_delimeter'] . $name] = $filter; | |
} | |
// Retrieving filters | |
foreach ($filters as $table => &$fields) { | |
$method = 'get' . ucfirst($table) . 'Values'; | |
$keys = !empty($duplicates[$table]) | |
? array_diff(array_keys($fields), array_keys($duplicates[$table])) | |
: array_keys($fields); | |
if (method_exists($this->filtersHandler, $method)) { | |
$fields = call_user_func_array(array($this->filtersHandler, $method), array($keys, $ids)); | |
if (!empty($duplicates[$table])) { | |
foreach ($duplicates[$table] as $key => $field) { | |
$fields[$key] = $fields[$field]; | |
} | |
} | |
} | |
else { | |
$this->modx->log(modX::LOG_LEVEL_ERROR, '[mSearch2] Method "' . $method . '" not exists in class "' . get_class($this->filtersHandler) . '". Could not retrieve filters from "' . $table . '"'); | |
} | |
} | |
unset($fields); | |
// Remove duplicates | |
foreach ($duplicates as $table => $fields) { | |
$tmp = array_unique($fields); | |
foreach ($tmp as $tmp2) { | |
unset($filters[$table][$tmp2]); | |
unset($built[$table . $this->config['filter_delimeter'] . $tmp2]); | |
} | |
} | |
// Prepare aliases | |
$aliases = array(); | |
if (!empty($this->config['aliases'])) { | |
$tmp = array_map('trim', explode(',', $this->config['aliases'])); | |
foreach ($tmp as $v) { | |
if (strpos($v, '==') !== false) { | |
$tmp2 = array_map('trim', explode('==', $v)); | |
$aliases[str_replace('.', '_', $tmp2[0])] = $tmp2[1]; | |
} | |
} | |
$this->aliases = $aliases; | |
} | |
$this->filters = $filters; | |
$this->methods = $built; | |
} | |
// The replacement dots to underscores in filters names | |
foreach ($this->filters as $table => $fields) { | |
foreach ($fields as $key => $values) { | |
if (strpos($key, '.') !== false) { | |
$this->filters[$table][str_replace('.', '_', $key)] = $values; | |
unset($this->filters[$table][$key]); | |
} | |
} | |
} | |
$cache1 = array( | |
'filters' => $this->filters, | |
'methods' => $this->methods, | |
'aliases' => $this->aliases, | |
); | |
// Set cache | |
if (!empty($this->config['cacheTime'])) { | |
$this->modx->cacheManager->set('fltr_' . md5(implode(',', $ids) . $this->config['filters']), $cache1, $this->config['cacheTime'],$cacheOptions); | |
} | |
if (!$build) { | |
return $this->filters; | |
} | |
$built = $this->methods; | |
$prepared = array(); | |
foreach ($this->filters as $table => $filters) { | |
foreach ($filters as $key => $values) { | |
$new_key = $table . $this->config['filter_delimeter'] . $key; | |
$filter = !empty($built[$new_key]) | |
? $built[$new_key] | |
: 'default'; | |
$method = 'build' . ucfirst($filter) . 'Filter'; | |
if ($filter == 'default') { | |
switch ($table) { | |
case 'tv': | |
$method = 'buildTVsFilter'; | |
break; | |
case 'msoption': | |
$method = 'buildOptionsFilter'; | |
break; | |
} | |
} | |
if (method_exists($this->filtersHandler, $method)) { | |
$prepared[$new_key] = call_user_func_array(array($this->filtersHandler, $method), array($values, $key)); | |
} | |
elseif (method_exists($this->filtersHandler, 'buildDefaultFilter')) { | |
$prepared[$new_key] = call_user_func_array(array($this->filtersHandler, 'buildDefaultFilter'), array($values, $key)); | |
} | |
else { | |
$this->modx->log(modX::LOG_LEVEL_ERROR, '[mSearch2] Method "' . $method . '" not exists in class "' . get_class($this->filtersHandler) . '". Could not build filter for "' . $new_key . '"'); | |
$prepared[$new_key] = $values; | |
} | |
} | |
} | |
// Sort filters | |
foreach ($built as $key => &$values) { | |
if (isset($prepared[$key])) { | |
$values = $prepared[$key]; | |
unset($prepared[$key]); | |
} | |
} | |
// Add new generated filters to the end of list | |
$built = array_merge($built, $prepared); | |
$cache = array( | |
'filters' => $built, | |
'methods' => $this->methods, | |
'aliases' => $this->aliases, | |
); | |
// Set cache | |
if (!empty($this->config['cacheTime'])) { | |
$this->modx->cacheManager->set('prep_' . md5(implode(',', $ids) . $this->config['filters']), $cache, $this->config['cacheTime'],$cacheOptions); | |
} | |
return $built; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment