Женим mFilter2 и Gallery

Выводим картики Gallery, фильтруем с помощью mFilter2.
Будет доступно два фильтра по tag и description



Необходимые компоненты
Устанавливаем необходимые компоненты:
Gallery
mSearch2


Метод фильтрации
Создаем кастомный метод фильтрации customfilter.class.php
в папке
core/components/msearch2/custom/filters/
вставляем следующий код
<?php

class CustomFilter extends mse2FiltersHandler
{
    /* @var mSearch2 $mse2 */
    public $mse2;
    /* @var modX $modx */
    public $modx;
    public $config = array();
   
    public function __construct(mSearch2 &$mse2, array $config = array())
    {
        parent::__construct($mse2, $config);
        $config = &$this->config;
        if (isset($config['galleryConfig']) AND !is_array($config['galleryConfig'])) {
            $config['galleryConfig'] = json_decode($config['galleryConfig'], 1);
        }
        if (!is_array($config['galleryConfig'])) {
            $config['galleryConfig'] = array();
        }
    }
    public function getGalTagValues(array $fields, array $ids)
    {
        $filters = array();
        $galleryConfig = $this->config['galleryConfig'];
        $album = isset($galleryConfig['album']) ? $galleryConfig['album'] : 0;
        $album = array_map('trim', explode(',', $album));
        $c = $this->modx->newQuery('galTag');
        $c->setClassAlias('Tags');
        $c->innerJoin('galItem', 'Item');
        $c->innerJoin('galAlbumItem', 'Album', 'Album.item = Item.id');
        $c->where(array(
            'Album.album:IN' => $album,
        ));
        $c->select('' . $this->modx->getSelectColumns('galTag', 'Tags', '', $fields));
        $c->select('' . $this->modx->getSelectColumns('galItem', 'Item', '', array('id', /*'name'*/)));
        $c->limit(0);
        $tstart = microtime(true);
        if ($c->prepare() && $c->stmt->execute()) {
            $this->modx->queryTime += microtime(true) - $tstart;
            $this->modx->executedQueries++;
            while ($row = $c->stmt->fetch(PDO::FETCH_ASSOC)) {
                foreach ($row as $k => $v) {
                    $v = str_replace('"', '"', trim($v));
                    if ($k == 'id') {
                        continue;
                    } else if (isset($filters[$k][$v])) {
                        $filters[$k][$v][$row['id']] = $row['id'];
                    } else {
                        $filters[$k][$v] = array($row['id'] => $row['id']);
                    }
                }
            }
        } else {
            $this->modx->log(modX::LOG_LEVEL_ERROR, "[mSearch2] Error on get filter params.\nQuery: " . $c->toSQL() .
                "\nResponse: " . print_r($c->stmt->errorInfo(), 1)
            );
        }
        return $filters;
    }
    public function buildGalItemFilter(array $values, $name = '')
    {
        if (count($values) < 2 && empty($this->config['showEmptyFilters'])) {
            return array();
        }
        $results = array();
        foreach ($values as $value => $ids) {
            if ($value !== '') {
                $results[$value] = array(
                    'title'     => $value,
                    'value'     => $value,
                    'type'      => 'default',
                    'resources' => $ids,
                );
            }
        }
        ksort($results);
        return $results;
    }
    public function filterGalItem(array $requested, array $values, array $ids)
    {
        $matched = array();
        $tmp = array_flip($ids);
        foreach ($requested as $value) {
            $value = str_replace('"', '"', $value);
            if (isset($values[$value])) {
                $resources = $values[$value];
                foreach ($resources as $id) {
                    if (isset($tmp[$id])) {
                        $matched[] = $id;
                    }
                }
            }
        }
        return $matched;
    }
    public function getGalItemArrValues(array $fields, array $ids)
    {
        $filters = array();
        $galleryConfig = $this->config['galleryConfig'];
        $album = isset($galleryConfig['album']) ? $galleryConfig['album'] : 0;
        $album = array_map('trim', explode(',', $album));
        $c = $this->modx->newQuery('galItem');
        $c->setClassAlias('Item');
        $c->innerJoin('galAlbumItem', 'Album', 'Album.item = Item.id');
        $c->where(array(
            'Album.album:IN' => $album,
        ));
        $c->select('' . $this->modx->getSelectColumns('galItem', 'Item', '', array_merge(array('id'), $fields)));
        $c->limit(0);
        $tstart = microtime(true);
        if ($c->prepare() && $c->stmt->execute()) {
            $this->modx->queryTime += microtime(true) - $tstart;
            $this->modx->executedQueries++;
            while ($row = $c->stmt->fetch(PDO::FETCH_ASSOC)) {
                foreach ($row as $k => $tmp) {
                    if ($k == 'id') {
                        continue;
                    }
                    $tmp = str_replace('"', '"', trim($tmp));
                    $tmp = explode(',', $tmp);
                    foreach ($tmp as $v) {
                        if ($v == '') {
                            continue;
                        }
                        if (isset($filters[$k][$v])) {
                            $filters[$k][$v][$row['id']] = $row['id'];
                        } else {
                            $filters[$k][$v] = array($row['id'] => $row['id']);
                        }
                    }
                }
            }
        } else {
            $this->modx->log(modX::LOG_LEVEL_ERROR, "[mSearch2] Error on get filter params.\nQuery: " . $c->toSQL() .
                "\nResponse: " . print_r($c->stmt->errorInfo(), 1)
            );
        }
        return $filters;
    }
}
не забываем прописать настройку mse2_filters_handler_class = CustomFilter

Сниппет изображений
Создаем сниппет gallery.items

вставляем следующий код
<?php
/** @var modX $modx */
/** @var array $scriptProperties */
$galleryConfig = isset($scriptProperties['galleryConfig']) ? $scriptProperties['galleryConfig'] : '';
if (!empty($galleryConfig) AND !is_array($galleryConfig)) {
    $galleryConfig = json_decode($galleryConfig, true);
    $scriptProperties = array_merge($scriptProperties, $galleryConfig);
}
// thumbnails
$thumbnails = isset($scriptProperties['thumbnails']) ? $scriptProperties['thumbnails'] : '';
if (!empty($thumbnails) AND !is_array($thumbnails)) {
    $thumbnails = json_decode($thumbnails, true);
}
if (is_array($thumbnails)) {
    foreach ($thumbnails as $name => $thumbnail) {
        if (!is_array($thumbnail)) {
            $thumbnail = array();
        }
        $thumbnail = array_merge(array(
            'w'   => 100,
            'h'   => 100,
            'zc'  => 0,
            'far' => 'C',
            'q'   => 90,
        ), $thumbnail);
        $thumbnails[$name] = $thumbnail;
    }
}
$album = $modx->getOption('album', $scriptProperties, 0);
$resources = $modx->getOption('resources', $scriptProperties);
if (!empty($resources)) {
    $id = $resources;
}
/** @var pdoFetch $pdoFetch */
if (!$modx->loadClass('pdofetch', MODX_CORE_PATH . 'components/pdotools/model/pdotools/', false, true)) {
    return false;
}
$pdoFetch = new pdoFetch($modx, $scriptProperties);
$pdoFetch->addTime('pdoTools loaded.');
$class = 'galItem';
// Start build "where" expression
$where = array(
    'AlbumItems.album' => $album,
);
if (empty($showInactive)) {
    $where[$class . '.active'] = 1;
}
if (!empty($id)) {
    $id = array_map('trim', explode(',', $id));
    $where[$class . '.id:IN'] = $id;
}
// Add grouping
$groupby = array(
    'galItem.id',
);
// Join tables
$leftJoin = array(
    'Tags' => array('class' => 'galTag'),
);
$select = array(
    'AlbumItems' => $modx->getSelectColumns('galAlbumItem', 'AlbumItems', '', array('album', 'rank'), false),
    'galItem'    => $modx->getSelectColumns('galItem', 'galItem'),
    'Tags'       => "GROUP_CONCAT(`Tags`.`tag` SEPARATOR ',') as `tags`",
);
// AlbumItems
$innerJoin = array(
    'AlbumItems' => array('class' => 'galAlbumItem', 'galItem.id = AlbumItems.item AND AlbumItems.album = ' . $album)
);
// Add user parameters
foreach (array('where', 'leftJoin', 'innerJoin', 'select', 'groupby') as $v) {
    if (!empty($scriptProperties[$v])) {
        $tmp = $scriptProperties[$v];
        if (!is_array($tmp)) {
            $tmp = json_decode($tmp, true);
        }
        if (is_array($tmp)) {
            $$v = array_merge($$v, $tmp);
        }
    }
    unset($scriptProperties[$v]);
}
$pdoFetch->addTime('Conditions prepared');
$default = array(
    'class'             => 'galItem',
    'where'             => $where,
    'leftJoin'          => $leftJoin,
    'innerJoin'         => $innerJoin,
    'select'            => $select,
    'groupby'           => $class . '.id',
    'sortby'            => 'AlbumItems.rank',
    'sortdir'           => 'ASC',
    'return'            => !empty($returnIds)
        ? 'ids'
        : 'data',
    'nestedChunkPrefix' => 'minishop2_',
);
// Merge all properties and run!
$pdoFetch->setConfig(array_merge($default, $scriptProperties), false);
$rows = $pdoFetch->run();
// Process rows
$output = array();
if (!empty($rows) AND is_array($rows)) {
    foreach ($rows as $k => $row) {
        $item = $modx->getObject('galItem', $row['id']);
        if (empty($item)) continue;
        $add = $item->toArray();
        $add['filename'] = basename($item->get('filename'));
        $add['filesize'] = $item->get('filesize');
        if (!empty($thumbnails)) {
            foreach ($thumbnails as $name => $thumbnail) {
                $add[$name] = str_replace('&', '&', $item->get('thumbnail', $thumbnail));
            }
        }
        $row['idx'] = $pdoFetch->idx++;
        $row = array_merge($row, $add);
        $tpl = $pdoFetch->defineChunk($row);
        $output[] = $pdoFetch->getChunk($tpl, $row);
    }
}
$log = '';
if ($modx->user->hasSessionContext('mgr') AND !empty($showLog)) {
    $log .= '<pre class="galItemsLog">' . print_r($pdoFetch->getTime(), 1) . '</pre>';
}
// Return output
if (!empty($returnIds) AND is_string($rows)) {
    $modx->setPlaceholder('galItems.log', $log);
    if (!empty($toPlaceholder)) {
        $modx->setPlaceholder($toPlaceholder, $rows);
    } else {
        return $rows;
    }
} else if (!empty($toSeparatePlaceholders)) {
    $output['log'] = $log;
    $modx->setPlaceholders($output, $toSeparatePlaceholders);
} else {
    if (empty($outputSeparator)) {
        $outputSeparator = "\n";
    }
    $output['log'] = $log;
    $output = implode($outputSeparator, $output);
    if (!empty($tplWrapper) AND (!empty($wrapIfEmpty) || !empty($output))) {
        $output = $pdoFetch->getChunk($tplWrapper, array(
            'output' => $output,
        ));
    }
    if (!empty($toPlaceholder)) {
        $modx->setPlaceholder($toPlaceholder, $output);
    } else {
        return $output;
    }
}
Вызов mFilter2
[[!mFilter2?
     	    &class=`galItem`
            &element=`gallery.items`
            &limit=`16`
           
            &filters=`
            galTag|tag:galitem,
            galItemArr|description
            ` 
            &galleryConfig=`{ "album":2,"thumbnails": { "thumb":{ "w":400,"h":300,"zc":1,"q":90 },"main":{ "w":700,"h":500,"zc":0,"q":90 } } }`
            ]]
в параметре galleryConfig указываем конфиг галереи. В данном случае у нас галерея с идентификатором 2 и два вида превью

Чанк оформления элемента
Пример чанка
<div class="cell-sm-6 cell-md-4 ms2_product text-center">
    <div class="product product-grid">
        <div class="product-img-wrap">
            <a class="img-thumbnail-variant-3"
               href="{$main}" title="{$name}" data-fancybox="gallery" data-caption="{$name}">
                <figure>
                    <img src="{$thumb}" alt="{$name}" style="width:100%;" class="img-responsive">
                </figure>
                <div class="caption"><span class="icon hover-top-element linear-icon-picture"></span>
                    <p class="heading-5 hover-top-element">{$name}</p>
                    <div class="divider"></div>
                    <p class="small hover-bottom-element"></p><span class="icon arrow-right linear-icon-plus"></span>
                </div>
            </a>
        </div>
        <div class="product-caption">
            <h6 class="product-title"><a href="">{$name}</a></h6>
        </div>
    </div>
</div>
ps. Всем спасибо за внимание.
Володя
12 февраля 2018, 14:19
modx.pro
17
2 483
+16
Поблагодарить автора Отправить деньги

Комментарии: 0

Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
0