Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tvWhere parameter to allow filtering by template variable #62

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 111 additions & 2 deletions core/components/wayfinder/wayfinder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,110 @@ public function getData() {
if (!empty($ids)) {
$c = $this->modx->newQuery('modResource');
$c->leftJoin('modResourceGroupResource','ResourceGroupResources');
if (!empty($this->_config['tvWhere'])) {
//$tvFilters = $this->modx->fromJSON($this->_config['tvWhere']);
$tvFilters = explode(',', $this->_config['tvWhere']);

$tmplVarTbl = $this->modx->getTableName('modTemplateVar');
$tmplVarResourceTbl = $this->modx->getTableName('modTemplateVarResource');
$conditions = array();
$operators = array(
'<=>' => '<=>',
'===' => '=',
'!==' => '!=',
'<>' => '<>',
'==' => 'LIKE',
'!=' => 'NOT LIKE',
'<<' => '<',
'<=' => '<=',
'=<' => '=<',
'>>' => '>',
'>=' => '>=',
'=>' => '=>'
);
foreach ($tvFilters as $fGroup => $tvFilter) {
$filterGroup = array();
$filters = explode(',', $tvFilter);
$multiple = count($filters) > 0;
foreach ($filters as $filter) {
$operator = '==';
$sqlOperator = 'LIKE';
foreach ($operators as $op => $opSymbol) {
if (strpos($filter, $op, 1) !== false) {
$operator = $op;
$sqlOperator = $opSymbol;
break;
}
}
$tvValueField = 'tvr.value';
$tvDefaultField = 'tv.default_text';
$f = explode($operator, $filter);
if (count($f) >= 2) {
if (count($f) > 2) {
$k = array_shift($f);
$b = join($operator, $f);
$f = array($k, $b);
}
$tvName = $this->modx->quote($f[0]);
if (is_numeric($f[1]) && !in_array($sqlOperator, array('LIKE', 'NOT LIKE'))) {
$tvValue = $f[1];
if ($f[1] == (integer)$f[1]) {
$tvValueField = "CAST({$tvValueField} AS SIGNED INTEGER)";
$tvDefaultField = "CAST({$tvDefaultField} AS SIGNED INTEGER)";
} else {
$tvValueField = "CAST({$tvValueField} AS DECIMAL)";
$tvDefaultField = "CAST({$tvDefaultField} AS DECIMAL)";
}
} else {
$tvValue = $this->modx->quote($f[1]);
}
if ($multiple) {
$filterGroup[] =
"(EXISTS (SELECT 1 FROM {$tmplVarResourceTbl} tvr JOIN {$tmplVarTbl} tv ON {$tvValueField} {$sqlOperator} {$tvValue} AND tv.name = {$tvName} AND tv.id = tvr.tmplvarid WHERE tvr.contentid = modResource.id) " .
"OR EXISTS (SELECT 1 FROM {$tmplVarTbl} tv WHERE tv.name = {$tvName} AND {$tvDefaultField} {$sqlOperator} {$tvValue} AND tv.id NOT IN (SELECT tmplvarid FROM {$tmplVarResourceTbl} WHERE contentid = modResource.id)) " .
")";
} else {
$filterGroup =
"(EXISTS (SELECT 1 FROM {$tmplVarResourceTbl} tvr JOIN {$tmplVarTbl} tv ON {$tvValueField} {$sqlOperator} {$tvValue} AND tv.name = {$tvName} AND tv.id = tvr.tmplvarid WHERE tvr.contentid = modResource.id) " .
"OR EXISTS (SELECT 1 FROM {$tmplVarTbl} tv WHERE tv.name = {$tvName} AND {$tvDefaultField} {$sqlOperator} {$tvValue} AND tv.id NOT IN (SELECT tmplvarid FROM {$tmplVarResourceTbl} WHERE contentid = modResource.id)) " .
")";
}
} elseif (count($f) == 1) {
$tvValue = $this->modx->quote($f[0]);
if ($multiple) {
$filterGroup[] = "EXISTS (SELECT 1 FROM {$tmplVarResourceTbl} tvr JOIN {$tmplVarTbl} tv ON {$tvValueField} {$sqlOperator} {$tvValue} AND tv.id = tvr.tmplvarid WHERE tvr.contentid = modResource.id)";
} else {
$filterGroup = "EXISTS (SELECT 1 FROM {$tmplVarResourceTbl} tvr JOIN {$tmplVarTbl} tv ON {$tvValueField} {$sqlOperator} {$tvValue} AND tv.id = tvr.tmplvarid WHERE tvr.contentid = modResource.id)";
}
}
}
$conditions[] = $filterGroup;
}
if (!empty($conditions)) {
$firstGroup = true;
foreach ($conditions as $cGroup => $co) {
if (is_array($co)) {
$first = true;
foreach ($co as $cond) {
if ($first && !$firstGroup) {
$c->condition($c->query['where'][0][1], $cond, xPDOQuery::SQL_OR, null, $cGroup);
} else {
$c->condition($c->query['where'][0][1], $cond, xPDOQuery::SQL_AND, null, $cGroup);
}
/*
$c->prepare();
die("sql:\n" . $c->toSQL());
die('<pre>' . print_r($criteria,true) . '</pre>');
*/
$first = false;
}
} else {
$c->condition($c->query['where'][0][1], $co, $firstGroup ? xPDOQuery::SQL_AND : xPDOQuery::SQL_OR, null, $cGroup);
}
$firstGroup = false;
}
}
} // end tvWhere
$c->query['distinct'] = 'DISTINCT';

/* add the ignore hidden option to the where clause */
Expand Down Expand Up @@ -592,6 +696,11 @@ public function getData() {
$c->select(array(
'protected' => 'ResourceGroupResources.document_group',
));

/*
$c->prepare();
die($c->toSQL());
*/

$result = $this->modx->getCollection('modResource', $c);

Expand Down Expand Up @@ -639,7 +748,7 @@ public function getData() {
$resultIds[] = $tempDocInfo['id'];
$tempDocInfo['content'] = $tempDocInfo['class_key'] == 'modWebLink' ? $tempDocInfo['content'] : '';
/* create the link */
$linkScheme = $this->_config['fullLink'] ? 'full' : $this->modx->getOption('link_tag_scheme',null,'');
$linkScheme = $this->_config['fullLink'] ? 'full' : '';
if (!empty($this->_config['scheme'])) $linkScheme = $this->_config['scheme'];

if ($this->_config['useWeblinkUrl'] !== 'false' && $tempDocInfo['class_key'] == 'modWebLink') {
Expand Down Expand Up @@ -930,4 +1039,4 @@ public function modxPrep($value) {
$value = str_replace("}","&#125;",$value);
return $value;
}
}
}