you can try this snippet:
<?php
//[[filterResources? &tvNames=`cb1,cb2`]]
//$tvNames = 'cb1,cb2';
//$debug = 1;
$tmplVarTbl = $modx->getTableName('modTemplateVar');
$tmplVarResourceTbl = $modx->getTableName('modTemplateVarResource');
$tvNames = explode(',', $tvNames);
$c = $modx->newQuery('modResource');
$columns = $modx->getSelectColumns('modResource', 'modResource', 'res_', array('id'));
$c->select($columns);
foreach ($tvNames as $cGroup => $tvName) {
$tvValue = $_REQUEST[$tvName];
$tvName = $modx->quote($tvName);
if (is_array($tvValue)){
$conjunction= xPDOQuery::SQL_AND;
foreach ($tvValue as $value){
$c->where("EXISTS (SELECT 1 FROM {$tmplVarResourceTbl} tvr JOIN {$tmplVarTbl} tv ON CONCAT('||',tvr.value,'||') LIKE '%||{$value}||%' AND tv.name = {$tvName} AND tv.id = tvr.tmplvarid WHERE tvr.contentid = modResource.id)",$conjunction, null, $cGroup);
$conjunction= xPDOQuery::SQL_OR;
}
}
else{
$c->where("EXISTS (SELECT 1 FROM {$tmplVarResourceTbl} tvr JOIN {$tmplVarTbl} tv ON CONCAT('||',tvr.value,'||') LIKE '%||{$tvValue}||%' AND tv.name = {$tvName} AND tv.id = tvr.tmplvarid WHERE tvr.contentid = modResource.id)",xPDOQuery::SQL_AND, null, $cGroup);
}
}
$c->prepare();
if ($debug) {
echo $c->toSql().'<br />';
}
$sql = $c->toSql();
/* hmm.. funktioniert irgendwie nicht
if ($collection = $modx->getCollection($c)){
foreach ($collection as $object){
print_r($object->toArray());
$ids[] = $object->get('res_id');
}
}
*/
//dann eben so:
$stmt = $modx->query($sql);
if ($stmt) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$ids[] = $row['res_id'];
}
}
$scriptProperties['resources'] = implode(',', $ids);
if ($debug) {
echo 'IDs: '. $scriptProperties['resources'].'<br />';
}
if (!empty($ids)){
return $modx->runSnippet('getResources', $scriptProperties);
}
The snippet looks a bit complicated, because it handles also multiselects or checkboxes and uses the OR-conjunction between there arrays. This part isn't relevant for your situation.
Give your select-boxes the same names as your TVs and call it this way:
[[!filterResources? &tvNames=`Location,Type,Cost`]]
You can add all getResources-properties to this snippet-call like:
&tpl=`yourGetResourcesTpl` and all that.
If you want to have your select-boxes autocreated from your TV-options you can try this snippet:
<?php
//[[getoptions? &tpl=`mySelectBoxChunk` &tvname=`Location` &name=`Location`]]
$tv = $modx->getObject('modTemplateVar',array('name'=>$tvname));
$options = explode('||',$tv->get('elements'));
foreach ($options as $option){
$opt = explode ('==',$option);
$ph['value'] = count($opt)>1 ? $opt[1]: $opt[0];
$ph['text'] = $opt[0];
$ph['name'] = !empty($asArray) ? $name.'[]' : $name;
$ph['checked'] = '';
$ph['selected'] = '';
if ((is_array($_REQUEST[$name]) && in_array($ph['value'],$_REQUEST[$name])) || $_REQUEST[$name] == $ph['value']){
$ph['checked'] = 'checked="checked"';
$ph['selected'] = 'selected="selected"';
}
$output .= $modx->getChunk($tpl,$ph);
}
return $output;
[ed. note: Bruno17 last edited this post 14 years, 10 months ago.]