Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!isset($_SESSION['viewed'])) $_SESSION['viewed'] = array();
- // ID текущего ресурса
- $id = $modx->resource->id;
- // Шаблоны ресуры которых мы отслеживаем
- $tpls = array(1,2,3,4);
- $action = $modx->getOption('action',$scriptProperties, '');
- $limit = $modx->getOption('limit',$scriptProperties, 10);
- // Чанк для вывода просмотренных ресурсов
- $tpl = $modx->getOption('tpl',$scriptProperties, 'chunk.tpl');
- // Вносим ID просмотренных ресурсов
- if (in_array($modx->resource->template, $tpls)) {
- if (!isset($_SESSION['viewed'])) {
- $_SESSION['viewed'] = array($id);
- }
- else {
- if (in_array($id, $_SESSION['viewed'])) {
- $key = array_search($id, $_SESSION['viewed']);
- unset($_SESSION['viewed'][$key]);
- }
- // Храним только 30 последних ресурсов
- if (count($_SESSION['viewed']) > 30) {
- array_shift($_SESSION['viewed']);
- }
- $_SESSION['viewed'][] = $id;
- }
- }
- // Если указано действие returnViewed - выводим просмотренные товары
- if ($action == 'returnViewed') {
- $ids = array_reverse($_SESSION['viewed']);
- $limit = $modx->getOption('limit',$scriptProperties,10);
- $offset = $modx->getOption('offset',$scriptProperties,0);
- $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
- if (empty($ids)) return 'Вы еще не просмотрели ни одного ресурса.';
- $c = $modx->newQuery('modResource');
- $c->where(array('id:IN' => $ids, 'deleted' => false, 'published' => true));
- $total = $modx->getCount('modResource',$c);
- $c->limit($limit,$offset);
- $collection = $modx->getCollection('modResource',$c);
- $modx->setPlaceholder($totalVar,$total);
- $output = '';
- foreach ($collection as $resource) {
- $tvs = $resource->getMany('TemplateVars');
- $tvsArr = array();
- foreach($tvs as $tv) {
- $tvsArr[$tv->get('name')] = $tv->renderOutput($resource->get('id'));
- }
- $tmp = array_merge($resource->toArray(),$tvsArr);
- $output .= $modx->getChunk($tpl,$tmp);
- }
- return $output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement