<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=market.main
 * [END_COT_EXT]
 */

defined('COT_CODE') or die('Wrong URL.');

if($usr['id'] > 0) {
  $vizitedmarket = $usr['profile']['user_vizitedmarket'];
  $vizitedmarket = explode(',', $vizitedmarket);

  $vizitedmarket_c = cot_import('vizitedmarket', 'C', 'TXT');
  $vizitedmarket_c = explode(',', $vizitedmarket_c);
  foreach($vizitedmarket_c as $vm) {
    if($vm > 0 && !in_array($vm, $vizitedmarket)) $vizitedmarket[] = $vm;
  }

  if(!in_array($item['item_id'], $vizitedmarket)) {
    array_push($vizitedmarket, $item['item_id']);
  }
  $vizitedmarket = array_diff($vizitedmarket, array(''));
  $jj=0;
  while(count($vizitedmarket) > 300) {
    unset($vizitedmarket[$jj]);
    $jj++;
  }
  $usr['vizitedmarket'] = $vizitedmarket;
  $vizitedmarket = implode(',', $vizitedmarket);

  $db->update($db_users, array('user_vizitedmarket' => $vizitedmarket), 'user_id='.$usr['id']);
  cot_setcookie('vizitedmarket', '', time()-3600, $cfg['cookiepath'], $cfg['cookiedomain'], $sys['secure'], true);
} else {
  $vizitedmarket = cot_import('vizitedmarket', 'C', 'TXT');
  $vizitedmarket = explode(',', $vizitedmarket);
  if(!in_array($item['item_id'], $vizitedmarket)) {
    array_push($vizitedmarket, $item['item_id']);
    $vizitedmarket = array_diff($vizitedmarket, array(''));
    $jj=0;
    while(count($vizitedmarket) > 100) {
      unset($vizitedmarket[$jj]);
      $jj++;
    }
    $usr['vizitedmarket'] = $vizitedmarket;
    $vizitedmarket = implode(',', $vizitedmarket);

    cot_setcookie('vizitedmarket', $vizitedmarket, time()+14*24*3600, $cfg['cookiepath'], $cfg['cookiedomain'], $sys['secure'], true);
  }
}

if($usr['id'] == 0 || $usr['id'] != $item['item_userid']) cot_setcookie('lastvizitedmarket', $item['item_id'], time()+14*24*3600, $cfg['cookiepath'], $cfg['cookiedomain'], $sys['secure'], true);