<?php

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

cot_block($usr['id'] > 0);

$a = cot_import('a', 'G', 'INT');
$ajax = cot_import('ajax', 'G', 'INT');
if(!COT_AJAX) $ajax = 0;

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

$maxrowsperpage = 20;
list($pn, $d, $d_url) = cot_import_pagenav('d', $maxrowsperpage);
	
/* === Hook === */
foreach (cot_getextplugins('projects.list.first') as $pl)
{
	include $pl;
}
/* ===== */

if (!empty($c))
{
	$out['subtitle'] = (!empty($cfg['projects']['cat_' . $c]['metatitle'])) ? $cfg['projects']['cat_' . $c]['metatitle'] : $cfg['projects']['cat___default']['metatitle'];
	$out['subtitle'] = (!empty($out['subtitle'])) ? $out['subtitle'] : $L['projects'];
	$out['desc'] = (!empty($cfg['projects']['cat_' . $c]['metadesc'])) ? $cfg['projects']['cat_' . $c]['metadesc'] : $cfg['projects']['cat___default']['metadesc'];
	$out['keywords'] = (!empty($cfg['projects']['cat_' . $c]['keywords'])) ? $cfg['projects']['cat_' . $c]['keywords'] : $cfg['projects']['cat___default']['keywords'];
}
else
{
	$out['subtitle'] = (!empty($cfg['projects']['cat___default']['metatitle'])) ? $cfg['projects']['cat___default']['metatitle'] : $L['projects'];
	$out['desc'] = $cfg['projects']['cat___default']['metadesc'];
	$out['keywords'] = $cfg['projects']['cat___default']['keywords'];
}

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

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

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

if($usr['id'] > 0) {
  $notidsprj = $db->query("SELECT offer_pid FROM $db_projects_offers WHERE offer_userid=".$usr['id'])->fetchAll(PDO::FETCH_COLUMN);
  if(count($notidsprj) > 0) $where['item_ids'] = "item_id NOT IN ('" . implode("','", $notidsprj) . "')";

  $where['item_userid'] = "item_userid!=".$usr['id'];
}

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

$list_url_path = array('c' => $c);

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

$mskin = cot_tplfile(array('projects', 'list', $structure['projects'][$c]['tpl']));

/* === Hook === */
foreach (cot_getextplugins('projects.list.query') as $pl)
{
	include $pl;
}
/* ===== */

$t = new XTemplate($mskin);

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

$db->update($db_projects, array('item_state' => 1), 'item_state=0 AND ((item_update>0 AND item_update<'.($sys['now'] - 86400).') OR ((item_update=0 AND item_date<'.($sys['now'] - 86400).')))');

$totalitems = $db->query("SELECT COUNT(*) FROM $db_projects AS p $join_condition
	" . $where . "")->fetchColumn();

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

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

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

$sqllist_rowset = $sqllist->fetchAll();

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

$jj=0;
foreach($sqllist_rowset as $item)
{
	$jj++;
	$t->assign(cot_generate_usertags($item, 'PRJ_ROW_OWNER_'));
	$t->assign(cot_generate_projecttags($item, 'PRJ_ROW_', $cfg['projects']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));

  $t->assign(array(
    'PRJ_ROW_OFFEREXITST' => (($usr['id'] > 0 && $db->query("SELECT COUNT(*) FROM $db_projects_offers WHERE offer_pid=".$item['item_id'].' AND offer_userid='.$usr['id'])->fetchColumn() > 0) ? 1 : 0)
  ));

	/* === Hook - Part2 : Include === */
	foreach ($extp as $pl)
	{
		include $pl;
	}
	/* ===== */

	$t->parse("MAIN.PRJ_ROWS");
}
if($jj == 0) $t->parse("MAIN.PRJ_ROWS_EMPTY");

if(!$ajax) {
  $jjcat=0;
  foreach($structure['market'] as $code => $cat) {
    if (mb_substr_count($cat['path'], ".") == 0) {
      $cat['subcatselected'] = 0;
      $cat['subcnt'] = 0;
      foreach($structure['market'] as $subcode => $subcat) {
        if($subcat['parent'] == $code && $subcat['parent'] != $subcode) {
          $cat['subcnt']++;
          if($c == $subcode) $cat['subcatselected'] = 1;
          $t->assign(array(
            'SUBCAT_ROW_ID' => $subcat['id'],
            'SUBCAT_ROW_TITLE' => $subcat['title'],
            'SUBCAT_ROW_CODE' => $subcode,
            'SUBCAT_ROW_SELECTED' => ($c == $subcode ? ' selected="selected"' : '')
          ));
          $t->parse('MAIN.CATSUBCATS_ROW.SUBCATS_ROW');
        }
      }

      $t->assign(array(
        'CAT_ROW_ID' => $cat['id'],
        'CAT_ROW_TITLE' => $cat['title'],
        'CAT_ROW_CODE' => $code,
        'CAT_ROW_SELECTED' => (($cat['subcatselected'] || $c == $code)? ' selected="selected"' : '')
      ));

      $t->parse('MAIN.CATS_ROW');
      if($cat['subcnt'] > 0) $t->parse('MAIN.CATSUBCATS_ROW');
    }
  }
}

/* === Hook === */
foreach (cot_getextplugins('projects.list.tags') as $pl)
{
	include $pl;
}
/* ===== */
$t->parse('MAIN');
$module_body = $t->text('MAIN');