Advertisement
vanchelo

getViewed

Feb 5th, 2013
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. <?php
  2. if (!isset($_SESSION['viewed'])) $_SESSION['viewed'] = array();
  3. // ID текущего ресурса
  4. $id = $modx->resource->id;
  5. // Шаблоны ресуры которых мы отслеживаем
  6. $tpls = array(1,2,3,4);
  7. $action = $modx->getOption('action',$scriptProperties, '');
  8. $limit = $modx->getOption('limit',$scriptProperties, 10);
  9. // Чанк для вывода просмотренных ресурсов
  10. $tpl = $modx->getOption('tpl',$scriptProperties, 'chunk.tpl');
  11.  
  12. // Вносим ID просмотренных ресурсов
  13. if (in_array($modx->resource->template, $tpls)) {
  14.     if (!isset($_SESSION['viewed'])) {
  15.         $_SESSION['viewed'] = array($id);
  16.     }
  17.     else {
  18.         if (in_array($id, $_SESSION['viewed'])) {
  19.             $key = array_search($id, $_SESSION['viewed']);
  20.             unset($_SESSION['viewed'][$key]);
  21.         }
  22.         // Храним только 30 последних ресурсов
  23.         if (count($_SESSION['viewed']) > 30) {
  24.             array_shift($_SESSION['viewed']);
  25.         }
  26.         $_SESSION['viewed'][] = $id;
  27.     }
  28. }
  29.  
  30. // Если указано действие returnViewed - выводим просмотренные товары
  31. if ($action == 'returnViewed') {
  32.     $ids = array_reverse($_SESSION['viewed']);
  33.     $limit = $modx->getOption('limit',$scriptProperties,10);
  34.     $offset = $modx->getOption('offset',$scriptProperties,0);
  35.     $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
  36.  
  37.     if (empty($ids)) return 'Вы еще не просмотрели ни одного ресурса.';
  38.  
  39.     $c = $modx->newQuery('modResource');
  40.     $c->where(array('id:IN' => $ids, 'deleted' => false, 'published' => true));
  41.  
  42.     $total = $modx->getCount('modResource',$c);
  43.  
  44.     $c->limit($limit,$offset);
  45.     $collection = $modx->getCollection('modResource',$c);
  46.  
  47.     $modx->setPlaceholder($totalVar,$total);
  48.  
  49.     $output = '';
  50.     foreach ($collection as $resource) {
  51.         $tvs = $resource->getMany('TemplateVars');
  52.     $tvsArr = array();
  53.         foreach($tvs as $tv) {
  54.         $tvsArr[$tv->get('name')] = $tv->renderOutput($resource->get('id'));   
  55.     }
  56.         $tmp = array_merge($resource->toArray(),$tvsArr);
  57.         $output .= $modx->getChunk($tpl,$tmp);
  58.     }
  59.  
  60.     return $output;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement