Skip to content

Instantly share code, notes, and snippets.

@inogo
Last active March 15, 2016 08:56
Show Gist options
  • Save inogo/10ffbb15a845cdb1057e to your computer and use it in GitHub Desktop.
Save inogo/10ffbb15a845cdb1057e to your computer and use it in GitHub Desktop.
<!-- chunk: tpl.catalogFilter -->
<div class="filter clearfix">
[[!initProductsFilter]]
<form action="" method="get" class="filter-form[[+filter.active:notempty=` filter-active`]]">
<div class="filter-buttons">
<button type="submit" class="btn">Фильтр</button>
<button type="reset" class="btn filter-reset">Сбросить</button>
</div>
<div class="filter-rows">
<div class="filter-row">
<select name="order" class="filter-input">
<option value="">Параметры</option>
<option value="Data.price:ASC"[[+filter.order:is=`Data.price:ASC`:then=` selected`]]>Сначала дешевые</option>
<option value="Data.price:DESC"[[+filter.order:is=`Data.price:DESC`:then=` selected`]]>Сначала дорогие</option>
<option value="Data.new:DESC"[[+filter.order:is=`Data.new:DESC`:then=` selected`]]>Новинка</option>
<option value="Data.favorite:DESC"[[+filter.order:is=`Data.favorite:DESC`:then=` selected`]]>Акция</option>
<option value="Data.popular:DESC"[[+filter.order:is=`Data.popular:DESC`:then=` selected`]]>Популярные</option>
</select>
<input type="number" min="0" step="500" name="price[min]" class="filter-input filter-price" value="[[+filter.price.min]]" placeholder="Цена от"> &ndash;
<input type="number" min="0" step="500" name="price[max]" class="filter-input filter-price" value="[[+filter.price.max]]" placeholder="Цена до">
<input type="text" name="s" value="[[+filter.search:htmlent]]" class="filter-input search" placeholder="Артикул или название">
</div>
<div class="filter-row filter-options clearfix">
[[!filterOptionList? &option=`size` &title=`Размер`]]
[[!filterOptionList? &option=`color` &title=`Цвет`]]
[[!filterOptionList? &option=`tags` &title=`Особенности`]]
</div>
</div>
</form>
</div>
<?php
/* snippet: filterOptionList */
$option = $modx->getOption('option', $scriptProperties, '');
$name = $modx->getOption('name', $scriptProperties, $option);
$title = $modx->getOption('title', $scriptProperties, '');
$category = $modx->getOption('category', $scriptProperties, $modx->resource->id);
//$selected = $modx->getOption('selected', $scriptProperties, $modx->getPlaceholder('filter.option.' . $name));
$default = $modx->getOption('default', $scriptProperties, '');
if (empty($option)) return '';
$selected = isset($_REQUEST['option'][$option]) && is_array($_REQUEST['option'][$option]) ? $_REQUEST['option'][$option] : array();
$q = $modx->newQuery('msProductOption');
$q->innerJoin('msProduct', 'msProduct', 'msProduct.id=msProductOption.product_id');
$q->sortby('msProductOption.value','ASC');
$q->select('DISTINCT(msProductOption.value)');
$q->where(array('msProductOption.key' => $option));
if (!empty($category)) {
$ids = $modx->getChildIds($category);
$ids[] = $category;
$q->innerJoin('msCategory', 'msCategory', 'msCategory.id=msProduct.parent');
$q->where(array('msCategory.id:IN' => $ids));
}
$result = "<select name=\"option[$option][]\" class=\"filter-input chosen-select\" data-placeholder=\"$title\" multiple>\n";
// $result .= "<option value=\"$default\">$title</option\n";
if ($q->prepare() && $q->stmt->execute()) {
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
$sel = in_array($row['value'], $selected) ? ' selected' : '';
$esc_val = htmlspecialchars($row['value']);
$result .= "<option value=\"{$esc_val}\"$sel>{$row[value]}</option>\n";
}
}
$result .= '</select>';
return $result;
/* snippet: initProductsFilter */
$prefix = $modx->getOption('prefix', $scriptProperties, 'filter');
$ph = array();
$price = isset($_REQUEST['price']) ? $_REQUEST['price'] : array();
if (isset($price['min']) && $price['min'] > 0)
$ph['price.min'] = (int)$price['min'];
if (isset($price['min']) && $price['max'] > 0)
$ph['price.max'] = (int)$price['max'];
$order = isset($_REQUEST['order']) ? trim(strip_tags($_REQUEST['order'])) : '';
if (strlen($order))
$ph['order'] = $order;
$search = isset($_REQUEST['s']) ? trim(strip_tags($_REQUEST['s'])) : '';
if (strlen($search))
$ph['search'] = $search;
$ph['active'] = count($ph) > 0 ? '1' : '';
$modx->toPlaceholders($ph, $prefix);
$modx->regClientCSS('assets/templates/js/vendor/chosen/chosen.min.css');
$modx->regClientScript('assets/templates/js/vendor/chosen/chosen.jquery.min.js');
/* snippet: filterProducts */
$allowedOptions = array_map('trim', explode(',', $modx->getOption('allowedOptions', $scriptProperties, 'size,tags,color')));
$options = isset($_REQUEST['option']) ? $_REQUEST['option'] : array();
$options = array_intersect_key($options, array_flip($allowedOptions));
$price = isset($_REQUEST['price']) ? $_REQUEST['price'] : array();
$order = isset($_REQUEST['order']) ? $_REQUEST['order'] : '';
$order = array_map('strip_tags', explode(':', $order));
$search = isset($_REQUEST['s']) ? trim(strip_tags($_REQUEST['s'])) : '';
$where = array();
if (strlen($search))
$where[] = array('pagetitle:LIKE' => "%$search%", 'OR:Data.article:LIKE' => "%$search%");
if (isset($price['min']) && $price['min'] > 0)
$where['Data.price:>'] = (int)$price['min'];
if (isset($price['max']) && $price['max'] > 0)
$where['Data.price:<'] = (int)$price['max'];
$props = array(
'sortby' => !empty($order[0]) ? $order[0] : 'menuindex',
'sortdir' => count($order) == 2 && strcasecmp($order[1], 'DESC') == 0 ? 'DESC' : 'ASC',
);
if (count($where))
$props['where'] = $modx->toJSON($where);
if (count($options)) {
$optionFilters = array();
foreach ($options as $key => $values) {
$filtered_values = array();
foreach ($values as $val) {
$val = trim(strip_tags($val));
if (strlen($val)) {
$filtered_values[] = $val;
}
}
$optionFilters[$key . ':IN'] = $filtered_values;
}
$props['optionFilters'] = $modx->toJSON($optionFilters);
}
$props = array_merge($scriptProperties, $props);
if ($modx->getOption('debug', $scriptProperties, false)) {
// $props['showLog'] = 1;
$result .= '<!--' . print_r($props, true) . '-->' . PHP_EOL;
}
$result .= $modx->runSnippet('vwMsProducts', $props);
return $result;
/* snippet patch (line 98): msProducts */
if (is_string($value) || is_array($value)) {
$conj = !empty($conj) ? $conj.':' : '';
$opt_where[] = array("{$conj}`{$key}`.`value`:{$operator}" => $value);
} else {
if (!empty($conj)) {
$last_where = end($opt_where);
if (is_array($last_where)) {
$conj = !empty($conj) ? $conj.':' : '';
$opt_where[] = array("{$conj}`{$key}`.`value`:{$operator}" => $value);
} else {
array_splice($opt_where, -1, 1, $last_where . " {$conj} `{$key}`.`value`{$operator}{$value}");
}
} else {
$opt_where[] = "`{$key}`.`value`{$operator}{$value}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment