<?php defined('COT_CODE') or die('Wrong URL');
/* ====================
[BEGIN_COT_EXT]
Hooks=index.tags
Order=11
[END_COT_EXT]
==================== */

if(!$onlypopularmarket) {
  $vmlimit = 10;

  $vizitedmarket = $usr['vizitedmarket'];
  if(!is_array($vizitedmarket)) {
    if($usr['id'] > 0) {
      $vizitedmarket = $usr['profile']['user_vizitedmarket'];
    } else {
      $vizitedmarket = cot_import('vizitedmarket', 'C', 'TXT');
    }
    $vizitedmarket = explode(',', $vizitedmarket);
  }

  if(count($vizitedmarket) > 0 && (count($vizitedmarket) > 1 || $vizitedmarket[0] > 0)) {
    /*
    if(count($vizitedmarket) > $vmlimit) {
      $vizitedmarket = array_slice($vizitedmarket, -$vmlimit);
    }
    */
    $vmitems = $db->query("SELECT * FROM $db_market WHERE item_state=0 AND (item_id=".implode(' OR item_id=', $vizitedmarket).") ORDER BY item_date LIMIT ".$vmlimit)->fetchAll();
    foreach($vmitems as $item) {
      $t->assign(cot_generate_usertags($item['item_userid'], 'VPRD_OWNER_'));
      $t->assign(cot_generate_markettags($item, 'VPRD_', $cfg['market']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));
      $t->parse('MAIN.VIZITED_MARKET.VM_ITEMS');
    }
    $t->parse('MAIN.VIZITED_MARKET');

    $popularparse = array();
    $populardefault = array(
      'logo', 'smm', 'links', 'email-marketing'
    );

    $popularcats = $db->query("SELECT item_cat, COUNT(*) as cnt FROM $db_market WHERE item_id=".implode(' OR item_id=', $vizitedmarket)." GROUP BY item_cat ORDER BY cnt DESC LIMIT 4")->fetchAll(PDO::FETCH_COLUMN);
    foreach($popularcats as $cat) {
      $popularparse[] = $cat;
    }
    foreach($populardefault as $cat) {
      $popularparse[] = $cat;
    }
    for($i=1;$i<=4;$i++) {
      $cat = $structure['market'][$popularparse[$i]];
      $cat['code'] = $popularparse[$i];
      $t->assign('PCAT_ROW', $cat);
      $t->parse('MAIN.POPULAR_CATS');
      //$t->parse('MAIN.POPULAR_CATS_SLIDER');
    }
    /*
    for($i=5;$i<=8;$i++) {
      if(!empty($popularparse[$i])) {
        $cat = $structure['market'][$popularparse[$i]];
        $cat['code'] = $popularparse[$i];
        $t->assign('PCAT_ROW', $cat);
        $t->parse('MAIN.POPULAR_CATS_SLIDER');
      }
    }
    */
  }

  $lastvizitedmarket = cot_import('lastvizitedmarket', 'C', 'INT');
  if($lastvizitedmarket > 0) {
    $vmitem = $db->query("SELECT * FROM $db_market WHERE item_state=0 AND item_id=".$lastvizitedmarket." LIMIT 1")->fetch();
    if($vmitem['item_id'] > 0) {
      $t->assign(cot_generate_markettags($vmitem, 'LVPRD_', $cfg['market']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));
      $t->parse('MAIN.LAST_VIZITED_MARKET');
    }
  }
}