© Сделано на LiveStreet
Дизайн - ©2009 MODx RED Group
<?php
/*[[SqlFilter?&Docfield=`id`&filterTV =`filter_1_parameter,перкаль|filter_2_sostav,хлопок|DescriptionName1,белый`&mode=`OR`]]
* &tvReturnName=`image` //какое ТВ возвращать
* &filterTV =`filter_1_parameter,перкаль|filter_2_sostav,хлопок|DescriptionName1,белый` //имя ТВ
* &mode=`OR` // можно через AND, можно через OR
* &parentId=`2`// ID родителя, задается опционально
* &limit=`50`; // ограничение
* &Docfield=`id` // опционально одновременно можно вызывать либо tvReturnName либо Docfield
* @EVAL $pt=$modx->getDocument($docid,'pagetitle'); return $modx->evalSnippets("[[SqlFilter?&Docfield=`id`&filterTV =`filter_1_parameter,{$pt['pagetitle']}|filter_2_sostav,{$pt['pagetitle']}`&mode=`OR`]]");
*/
if(!isset($filterTV)) $filterTV = 'filter_1_parameter,перкаль|filter_2_sostav,хлопок'; // - имя 1 тв по которой фильтровать, значение филтра
if(!isset($mode))$mode="OR";
$limit = isset($limit) ? $limit=" LIMIT ".$limit.";" : ';';
$parentId =isset($parentId) ? $parentId: false;
$request="";
$where="";
$select_fields = "SELECT ";
$content_base = $modx->getFullTableName('site_content');
$site_tv_vals = $modx->getFullTableName('site_tmplvar_contentvalues');
$site_tv=$modx->getFullTableName('site_tmplvars');
$query=" FROM $content_base as sc ";
$FilterArr=explode("|", $filterTV);
$num=0;
foreach($FilterArr as $param) {
$Filter=explode(",", $param);
$inputTVname=$Filter[0];
$f_val = $Filter[1];
$query.="LEFT JOIN ($site_tv as tv{$num}, $site_tv_vals as tv{$num}val) ".
"ON tv{$num}.id=tv{$num}val.tmplvarid and tv{$num}val.contentid=sc.id and tv{$num}.name='$inputTVname' ";
if ($num>0){$where.=" $mode ";}
$where.="tv{$num}val.value='{$f_val}'";
$num++;
}
if ($parentId){
$where="(".$where.")";
$children = $modx->getChildIds($parentId, 10);
$where .= ' AND sc.id IN ('.implode(',', $children) . ') ';
}
$where="WHERE ".$where." GROUP BY sc.id";
if(isset($tvReturnName)) {
$query.="INNER JOIN ($site_tv as tvreturn, $site_tv_vals as tvreturnval) ".
"ON tvreturn.id=tvreturnval.tmplvarid and tvreturnval.contentid=sc.id and tvreturn.name='$tvReturnName' ";
$select_fields.="tvreturnval.value";
}
elseif (isset($Docfield)) {$select_fields.="sc.{$Docfield}";}
else return'';
$request = $modx->db->query($select_fields.$query.$where.$limit);
//$result = $modx->db->makeArray($request);
$result = array();
while($v = $modx->db->getValue($request)){
$result[] = $v;
}
return implode(',', $result);
?>