<?php

/**
 * projects module
 *
 * @package projects
 * @version 2.5.2
 * @author CMSWorks Team
 * @copyright Copyright (c) CMSWorks.ru, littledev.ru
 * @license BSD
 */

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

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin'], $usr['auth_offers']) = cot_auth('projects', 'any', 'RWA1');
cot_block($usr['auth_offers']);

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

if($cfg['projects']['offersperpage'] > 0)
{
	list($pn, $d, $d_url) = cot_import_pagenav('d', $cfg['projects']['offersperpage']);
}
/* === Hook === */
foreach (cot_getextplugins('projects.myprojects.first') as $pl)
{
	include $pl;
}
/* ===== */

$t = new XTemplate(cot_tplfile(array('projects', 'myprojects')));

$out['subtitle'] = 'Запросы на услуги';

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

$where['userid'] = "item_userid=".$usr['id'];
$order['date'] = 'item_date DESC';

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

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

$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)->fetchAll();

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

$jj=0;
foreach($sqllist 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']));

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

	$t->parse("MAIN.PRJ_ROWS");
}

if($jj > 0) {
  $t->parse("MAIN.LAST_PRJ_BUTTON");
} else {
  $t->parse("MAIN.PRJS_EMPTY");
}
/* === Hook === */
foreach (cot_getextplugins('projects.myprojects.tags') as $pl)
{
	include $pl;
}
/* ===== */

// Error handling
cot_display_messages($t);

$t->parse("MAIN");
$module_body = $t->text('MAIN');