Skip to content
Browse files

bugfix and speed improvement

  • Loading branch information...
1 parent d614de3 commit cc9d438dcb02440e051db0a6e1338cd6af806990 Sammyboy committed Jan 9, 2012
Showing with 38 additions and 53 deletions.
  1. +1 −34 xdbfilter.class.inc.php
  2. +37 −19 xdbfilter.php
View
35 xdbfilter.class.inc.php
@@ -41,7 +41,7 @@ function filterrows($rows, $filters_arr, $multiselectTvs_arr = array()) {
$pusharray = 1;
} else {
foreach ($filterBys as $filterBy) {
- $filterRowVal = isset($row[$filterBy]['value']) ? $row[$filterBy]['value'] : $row[$filterBy];
+ $filterRowVal = $row[$filterBy];
$pusharray = 0;
$filterValues = $filters[$filterBy];
if ($filterValues == '')
@@ -82,39 +82,6 @@ function filterrows($rows, $filters_arr, $multiselectTvs_arr = array()) {
return $outputrows;
}
- // -----------------------------
- // function to get template vars
- // -----------------------------
-
- function getTemplateVars($idnames = array (), $fields = "*", $docid = "", $published = 1) {
- global $modx;
- if (($idnames != '*' && !is_array($idnames)) || count($idnames) == 0) {
- return false;
- } else {
- $result = array();
-
- // get user defined template variables
- $fields = ($fields == "") ? "tv.*" : 'tv.'.implode(',tv.', preg_replace("/^\s/i", "", explode(',', $fields)));
- $sort = ($sort == "") ? "" : 'tv.'.implode(',tv.', preg_replace("/^\s/i", "", explode(',', $sort)));
- if ($idnames == "*")
- $query = "tv.id<>0";
- else
- $query = (is_numeric($idnames[0]) ? "tv.id" : "tv.name")." IN ('".implode("','", $idnames)."')";
-
- $sql = "SELECT $fields, IF(tvc.value!='',tvc.value,tv.default_text) as value ";
- $sql .= "FROM ".$modx->getFullTableName('site_tmplvars')." tv ";
- $sql .= "INNER JOIN ".$modx->getFullTableName('site_tmplvar_templates')." tvtpl ON tvtpl.tmplvarid = tv.id ";
- $sql .= "LEFT JOIN ".$modx->getFullTableName('site_tmplvar_contentvalues')." tvc ON tvc.tmplvarid=tv.id AND tvc.contentid = '".$docid."' ";
- $sql .= "WHERE ".$query;
-
- $rs = $modx->dbQuery($sql);
- for ($i = 0; $i < @$modx->recordCount($rs); $i++) {
- array_push($result, @$modx->fetchRow($rs));
- }
- return $result;
- }
- }
-
// ---------------------------------
// function to get all template vars
// ---------------------------------
View
56 xdbfilter.php
@@ -106,6 +106,8 @@
$xdbconfig['filters'] = $filter;
$xdbconfig['filters_arr'] = (trim($xdbconfig['filters']) !== '') ? explode('||', $xdbconfig['filters']) : array();
$link .= '&filters='.$filter;
+} elseif (isset($_REQUEST[$xdbconfig['id_'].'xdbfilterclear'])) {
+ $xdbconfig['clear'] = 1;
} else {
$link .= '&filters='.$xdbconfig['filters'];
}
@@ -169,19 +171,29 @@
// set field names
$docfields = $xdb->xdbconfig['outputFields'];
- $tvnames = array();
+ if (($count = count($docfields)) > 0) {
+ for ($i = 0; $i < $count; ++$i) {
+ if (strpos(ltrim($docfields[$i]), "tv") === 0)
+ unset($docfields[$i]);
+ else {
+ $field = explode(":", $docfields[$i]);
+ $docfields[$i] = $field[0];
+ }
+ }
+ }
+ $tvnames = $tvElements = array();
foreach ($xdb->filterFields as $field) {
if (strpos($field, "tv") === 0)
$tvnames[] = substr($field, 2);
else
array_push($docfields, $field);
}
-
+
// remove double entries
$docfields = array_unique($docfields);
$tvnames = array_unique($tvnames);
-
+
// get a list of all documents and their tv values from the database
$allrows = $xdb->getAllVars($docfields, $tvnames, "name,elements", $where, $xdb->xdbconfig['orderby'], $xdb->xdbconfig['limit'], $xdb->xdbconfig['offset']);
@@ -194,7 +206,10 @@
$value = eval(ltrim(substr($val, 5), " :"));
}
$value = str_replace(array('{{', '}}'), '', $value);
- $allrows[$pos]['tv'.$var['tvName']] = array('value' => $value, 'elements' => $var['tvElements']);
+ $tvName = 'tv'.$var['tvName'];
+ if (isset($var['tvElements']) && !isset($tvElements[$tvName]))
+ $tvElements[$tvName] = $var['tvElements'];
+ $allrows[$pos][$tvName] = $value;
}
}
} else
@@ -215,26 +230,30 @@
file_put_contents(MODX_BASE_PATH.XDBFILTER_PATH.'output.txt', var_export($allrows,true));
}
+if (isset($xdb->xdbconfig['clear'])) {
+ $preselectRows = $rows = $allrows;
+} else {
+ // first filter all rows which are in preselect parameter
+ $preselectRows = $xdb->filterrows($allrows, $xdbconfig['preselect_arr'], $xdbconfig['multiselectTvs_arr']);
-// first filter all rows which are in preselect parameter
-$preselectRows = $xdb->filterrows($allrows, $xdbconfig['preselect_arr'], $xdbconfig['multiselectTvs_arr']);
-
-// make outputFields placeholder
-$rows = $xdb->filterrows($preselectRows, $xdbconfig['filters_arr'], $xdbconfig['multiselectTvs_arr']);
+ // make outputFields placeholder
+ $rows = $xdb->filterrows($preselectRows, $xdbconfig['filters_arr'], $xdbconfig['multiselectTvs_arr']);
+}
if ($xdb->xdbconfig['debug']) {
foreach ($rows[0] as $key => $rowfield) {
echo $key.'<br/>';
}
}
+
foreach ($xdbconfig['outputFields'] as $field) {
$listid = array();
$fieldarr = explode(':', $field);
$field = $fieldarr[0];
$delimiter = count($fieldarr[1]) > 0 ? $fieldarr[1] : ',';
foreach ($rows as $row) {
- $listid[] = is_array($row[$field]) ? $row[$field]['value'] : $row[$field];
+ $listid[] = $row[$field];
}
$listid = implode($delimiter, array_unique($listid));
$listid = preg_replace('/'.$delimiter.'$/', '', $listid);
@@ -258,7 +277,7 @@
$multiselectTvValues = array();
foreach ($filterRows as $row) {
- $filterRowVal = is_array($row[$filterField]) ? $row[$filterField]['value'] : $row[$filterField];
+ $filterRowVal = $row[$filterField];
if (in_array($filterField, $xdbconfig['multiselectTvs_arr']) && !empty($filterRowVal)) {
array_push($multiselectTvValues, $filterRowVal);
}
@@ -289,12 +308,11 @@
}
}
-
+
if (strpos($filterField, "tv") === 0) {
- $i = count($filterRows);
- while (!isset($filterRows[$i][$filterField]) && (--$i > -1));
- if (isset($filterRows[$i][$filterField]['elements'])) {
- $elements = $filterRows[$i][$filterField]['elements'];
+ // get tv list elements
+ if (isset($tvElements[$filterField])) {
+ $elements = $tvElements[$filterField];
if (stripos($val = trim($elements), '@eval') === 0) {
$elements = eval(ltrim(substr($val, 5), " :"));
}
@@ -304,7 +322,7 @@
$elements[$i] = isset($optionValue) ? $optionValue : $optionName;
$optionNames[$i] = $optionName;
}
-
+
// sort option list
$new = array();
foreach ($filterFieldValues as $val) {
@@ -313,7 +331,7 @@
$new[$pos] = array('value' => $val, 'name' => $optionNames[$pos]);
}
ksort($new);
-
+
$filterFieldValues = $new;
}
}
@@ -322,7 +340,7 @@
$value = isset($field['value']) ? $field['value'] : $field;
$filterValues = strtolower($filters[$filterField]);
$values = explode('|', $filterValues);
- if (in_array(strtolower($value), $values)) {
+ if (!isset($xdb->xdbconfig['clear']) && in_array(strtolower($value), $values)) {
$filterItemTplData['filteritemchecked'] = '1';
} else {
$filterItemTplData['filteritemchecked'] = '0';

0 comments on commit cc9d438

Please sign in to comment.
Something went wrong with that request. Please try again.