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

$onlypopularmarket = false;

$curindexcategory = array();
$indexcategory = cot_import('indexcategory', 'G', 'TXT');
if(!empty($indexcategory) && COT_AJAX) $curindexcategory = $structure['market'][$indexcategory];
$curindexcategory['code'] = $indexcategory;
$t->assign('CUR_INDEXCATEGORY', $curindexcategory);

if($curindexcategory['id'] > 0) $onlypopularmarket = true;

$ilimit = 10;

if(!$onlypopularmarket) {
  $indexitems = $db->query("SELECT * FROM $db_market WHERE item_state=0 AND item_showonindex=1 ORDER BY item_date DESC LIMIT ".$ilimit)->fetchAll();
  foreach($indexitems as $item) {
    $t->assign(cot_generate_usertags($item['item_userid'], 'IPRD_OWNER_'));
    $t->assign(cot_generate_markettags($item, 'IPRD_', $cfg['market']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));
    $t->parse('MAIN.POPULAR_MARKET');
  }
}

$sdisplay = cot_import('sdisplay', 'G', 'TXT');
if(!$sdisplay) {
  $sdisplay = cot_import('sdisplay', 'C', 'TXT');
  if(!$sdisplay) $sdisplay = 'table';
} elseif(!empty($sdisplay)) {
  cot_setcookie('sdisplay', $sdisplay, time()+(86400*7), $cfg['cookiepath'], $cfg['cookiedomain'], $sys['secure'], true);
}

$index_presearch = cot_import('index_presearch', 'G', 'TXT');

if(!empty($index_presearch)) {
  cot_setcookie('index_presearch', $index_presearch, time()+86400*7, $cfg['cookiepath'], $cfg['cookiedomain'], $sys['secure'], true);
} else {
  $index_presearch = cot_import('index_presearch', 'C', 'TXT');
}

if(empty($index_presearch) || !in_array($index_presearch, array('groups', 'new', 'favs', 'projects'))) {
  $index_presearch = 'popular';
}
if($usr['id'] == 0) {
  $index_presearch = 'new';
  $sdisplay = 'table';
}

$where = array();
$where['item_state'] = 'item_state=0';
$orderby = 'item_date DESC';

if($curindexcategory['id'] > 0) {
  $where['item_cat'] = "item_cat='".$curindexcategory['code']."'";
  $index_presearch = 'popular';
}

if($index_presearch == 'popular') {
  $where['item_showonindex'] = 'item_showonindex=1';
  $orderby = 'item_showonindex_lastshow ASC, item_mk_popular DESC, item_mk_rating DESC';
} elseif($index_presearch == 'groups') {
  $orderby = 'item_mk_popular DESC, item_mk_rating DESC';
}

if($index_presearch == 'popular' && !$onlypopularmarket) {
  $popularcats = $db->query("SELECT item_cat, COUNT(*) as cnt FROM $db_market WHERE ".implode(' AND ', $where)." GROUP BY item_cat ORDER BY cnt DESC")->fetchAll();
  foreach($popularcats as $catcode) {
    $cat = $structure['market'][$catcode['item_cat']];
    $cat['code'] = $catcode['item_cat'];
    $t->assign('PCAT_ROW', $cat);
    $t->parse('MAIN.POPULAR_CATS_SLIDER');
  }
}

/* === Hook === */
foreach (cot_getextplugins('market.index.presearch.query') as $pl)
{
	include $pl;
}
/* ===== */

$extp1 = cot_getextplugins('market.index.presearch.loop');

if($index_presearch != 'projects') {
  $ilimit = ($usr['id'] == 0 ? 18 : 25);
  $jj = 0;
  $indexitems = $db->query("SELECT * FROM $db_market WHERE ".implode(' AND ', $where)." ORDER BY ".$orderby." LIMIT ".$ilimit)->fetchAll();
  foreach($indexitems as $item) {
    $jj++;
    $t->assign(cot_generate_usertags($item['item_userid'], 'IPRD_OWNER_'));
    $t->assign(cot_generate_markettags($item, 'IPRD_', $cfg['market']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));

    /* === Hook === */
    foreach ($extp1 as $pl)
    {
    	include $pl;
    }
    /* ===== */

    $db->update($db_market, array('item_showonindex_lastshow' => $sys['now']), 'item_id='.$item['item_id']);

    $t->parse("MAIN.MARKET_PRESEARCH_PC".($sdisplay == 'list' ? '_LIST' : ''));
  }
  if($jj == 0) $t->parse("MAIN.MARKET_PRESEARCH_EMPTY");
}

/* === Hook === */
foreach (cot_getextplugins('market.index.presearch.tags') as $pl)
{
	include $pl;
}
/* ===== */

$sortlinks = array();
foreach(array('popular', 'new', 'groups', 'favs') as $val) {
  $sortlinks[$val] = cot_url('index', 'index_presearch='.$val);
}

$displaylinks = array();
$tmpurl = $list_url_path;
foreach(array('table', 'list') as $val) {
  $displaylinks[$val] = cot_url('index', 'sdisplay='.$val);
}

$t->assign(array(
  "SEARCH_SORTLINKS_URL" => $sortlinks,
  "SEARCH_DISPLINKS_URL" => $displaylinks,
  "SEARCH_INDEX_PRESEARCH" => $index_presearch
));