Skip to content

Instantly share code, notes, and snippets.

@Kookabura
Last active October 25, 2017 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kookabura/e9ef4c0f94faa8deaf1fe6e02c41d1d7 to your computer and use it in GitHub Desktop.
Save Kookabura/e9ef4c0f94faa8deaf1fe6e02c41d1d7 to your computer and use it in GitHub Desktop.
Исправленная версия sfMenu при правилах по нескольким полям
<?php
public function countChildren($links = array()) {
$tree = array();
$time = microtime(true);
foreach ($links as $link) {
$total = 0;
$addTVs = $fields_where = $innerJoin = array();
foreach ($link['words'] as $field) {
switch ($field['class']) {
case 'msProductData':
case 'modResource':
case 'msProductOption':
$fw = $field['class'] . '.' . $field['key'];
if ($field['class'] == 'msProductData') {
$innerJoin['msProductData'] = array('class' => 'msProductData', 'on' => 'msProductData.id = modResource.id');
}
if ($field['class'] == 'msProductOption') {
$innerJoin['msProductOption'] = array('class' => 'msProductOption', 'on' => 'msProductOption.product_id = modResource.id');
$fields_where[$field['class'] . '.key:IN'][] = $field['key'];
$fw = $field['class'] . '.value';
}
if ($field['slider']) {
$slider = explode(',', $field['word_input']);
$fields_where[$fw . ':>='] = $slider[0];
if ($slider[1]) {
$fields_where[$fw . ':<='] = $slider[1];
}
} else {
$values = explode(',', $field['word_input']);
$tmp = count($fields_where[$fw . ':IN']) ? $fields_where[$fw . ':IN'] : array();
$fields_where[$fw . ':IN'] = array_merge($tmp, $values);
$groupby = 'msProductOption.product_id HAVING COUNT(DISTINCT msProductOption.value) = ' . count($fields_where[$fw . ':IN']);
}
break;
case 'modTemplateVar':
$addTVs[] = $field['key'];
if ($field['exact']) {
$fields_where['TV' . $field['key'] . '.value'] = $field['word_input'];
} else {
$fields_where['TV' . $field['key'] . '.value:LIKE'] = '%' . $field['word_input'] . '%';
}
break;
case 'msVendor':
$innerJoin['msProductData'] = array('class' => 'msProductData', 'on' => 'msProductData.id = modResource.id');
$innerJoin['msVendor'] = array('class' => 'msVendor', 'on' => 'msVendor.id = msProductData.vendor');
$fields_where[$field['class'] . '.id'] = $field['word_input'];
break;
default:
break;
}
}
$addTVs = implode(',', $addTVs);
if ($link_where = $this->modx->fromJSON($link['count_where'])) {
$fields_where = array_merge($fields_where,$link_where);
}
if ($where = $this->modx->fromJSON($link['count_where'])) {
$fields_where = array_merge($fields_where,$where);
}
if($link['count_parents']) {
$parents = $link['count_parents'];
} elseif($link['page_id']) {
$parents = $link['page_id'];
} else {
$parents = $this->config['parents'];
}
$config = array(
'showLog' => 0,
'class' => 'modResource',
'parents' => $parents,
'includeTVs' => $addTVs,
'innerJoin' => $innerJoin,
'where' => $fields_where,
'return' => 'data',
'select' => array(
'modResource' => 'COUNT(modResource.id) as count'
),
);
if ($groupby && $field['class'] == 'msProductOption') $config['groupby'] = $groupby;
$this->pdoTools->setConfig($config);
$run = $this->pdoTools->run();
if (count($run)) {
$total = $run[0]['count'];
}
$link['total'] = $total;
if ($mincount = (int)$this->config['mincount']) {
if ($link['total'] >= $mincount) {
$tree[$link['id']] = $link;
}
} else {
$tree[$link['id']] = $link;
}
}
if((int)$this->config['sortcount']) {
uasort($tree, function ($a, $b) {
if ($a['total'] == $b['total']) {return 0;}
if($this->config['sortdir'] == 'DESC') {
return ($a['total'] > $b['total']) ? -1 : 1;
} else {
return ($a['total'] < $b['total']) ? -1 : 1;
}
});
}
$this->pdoTools->addTime('Count Children complete', microtime(true) - $time);
return $tree;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment