<?php

/* ====================
  [BEGIN_COT_EXT]
  Hooks=standalone
  [END_COT_EXT]
  ==================== */

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

$c = cot_import('c', 'G', 'ALP');

if($usr['id'] == 0) cot_redirect(cot_url('index', '', true));

require_once cot_incfile('marketfav', 'plug');
$maxrowsperpage = 500;

list($pn, $d, $d_url) = cot_import_pagenav('d', $maxrowsperpage);

$where = array();
$order = array();

$where['state'] = "item_state=0";

$order['date'] = 'item_cat DESC, item_date DESC';

if (!empty($c))
{
	//$catsub = cot_structure_children('market', $c);
	//$where['cat'] = "item_cat IN ('" . implode("','", $catsub) . "')";
  //$order['date'] = 'item_date DESC';
}

$favsids = $db->query("SELECT fav_pid FROM $db_marketfav
			WHERE fav_uid=".$usr['id'])->fetchAll(PDO::FETCH_COLUMN);

$where['favsids'] = "item_id=0";
if(count($favsids) > 0) {
  $where['favsids'] = '(item_id='.implode(' OR item_id=', $favsids).')';
}

$list_url_path = array('e' => 'marketfav', 'c' => $c);

// Building the canonical URL
$out['canonical_uri'] = cot_url('plug', $list_url_path);

$where = ($where) ? 'WHERE ' . implode(' AND ', $where) : '';
$order = ($order) ? 'ORDER BY ' . implode(', ', $order) : '';

$totalitems = $db->query("SELECT COUNT(*) FROM $db_market AS m $join_condition
	LEFT JOIN $db_users AS u ON u.user_id=m.item_userid
	" . $where . "")->fetchColumn();

$sqllist = $db->query("SELECT m.*, u.* $join_columns
	FROM $db_market AS m $join_condition
	LEFT JOIN $db_users AS u ON u.user_id=m.item_userid
	" . $where . "
	" . $order . "
	LIMIT $d, " . $maxrowsperpage);

$pagenav = cot_pagenav('plug', $list_url_path, $d, $totalitems, $maxrowsperpage);

$t->assign(array(
	"PAGENAV_PAGES" => $pagenav['main'],
	"PAGENAV_PREV" => $pagenav['prev'],
	"PAGENAV_NEXT" => $pagenav['next'],
	"PAGENAV_COUNT" => $totalitems,
));

$sqllist_rowset = $sqllist->fetchAll();

/* === Hook === */
$extp = cot_getextplugins('market.list.loop');
/* ===== */

$catscnt = 0;
$lastcat = '';

$jj=0;
$sqllist_rowset[] = array('lastparse' => 1, 'item_cat' => 'lastparse_debug');
foreach ($sqllist_rowset as $item)
{
	$jj++;
  if(empty($lastcat) || $lastcat != $item['item_cat']) {
    $t->assign(array(
      'FAVBLOCK_CAT_TITLE' => $structure['market'][$lastcat]['title'],
      'FAVBLOCK_CAT_CNT' => $catscnt,
      'FAVBLOCK_CAT_URL' => cot_url('plug', 'e=marketfav&c='.$lastcat)
    ));
    if(!empty($lastcat)) {
      $t->parse("MAIN.FAV_EXISTS.FAVCAT_ROW");
      if(empty($c) || $c == $lastcat) $t->parse("MAIN.FAV_EXISTS.PRD_BLOCK");
      $catscnt = 0;
    }
    $lastcat = $item['item_cat'];
  }
  $catscnt++;

  if(!$item['lastparse'] && (empty($c) || $c == $lastcat)) {
  	$t->assign(cot_generate_usertags($item, 'PRD_ROW_OWNER_'));
  	$t->assign(cot_generate_markettags($item, 'PRD_ROW_', $cfg['market']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));
  	$t->assign(array(
  		"PRD_ROW_ODDEVEN" => cot_build_oddeven($jj),
  	));

  	$t->parse("MAIN.FAV_EXISTS.PRD_BLOCK.PRD_ROWS");
  }
}

if($jj > 0) {
  $t->assign('FAVBLOCK_CAT_ALLCNT', ($jj-1));
  $t->parse("MAIN.FAV_EXISTS");
} else {
  $t->parse("MAIN.FAV_EMPTY");
}

echo print_r($catsarr,1);



