<?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');

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

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

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

$out['subtitle'] = $L['offers_useroffers'];

$where['userid'] = "o.offer_userid=" . $usr['id'];

$order['date'] = 'o.offer_date DESC';

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

$where = ($where) ? 'WHERE ' . implode(' AND ', $where) : '';
$order = ($order) ? 'ORDER BY ' . implode(', ', $order) : '';
$query_limit = ($maxrowperpage > 0) ? "LIMIT $d, ".$maxrowperpage : '';

$totalitems = $db->query("SELECT COUNT(*) FROM $db_projects_offers AS o 
	LEFT JOIN $db_projects AS p ON o.offer_pid=p.item_id
	" . $where . "")->fetchColumn();

$sql = $db->query("SELECT o.* FROM $db_projects_offers AS o
	LEFT JOIN $db_projects AS p ON o.offer_pid=p.item_id
	" . $where . "
	" . $order . "
	" . $query_limit . "");

if($maxrowperpage > 0)
{
	$pagenav = cot_pagenav('projects', 'm=useroffers&choise=' . $choise, $d, $totalitems, $maxrowperpage);
	
	$t->assign(array(
		"PAGENAV_PAGES" => $pagenav['main'],
		"PAGENAV_PREV" => $pagenav['prev'],
		"PAGENAV_NEXT" => $pagenav['next'],
	));
}

$catpatharray[] = array(cot_url('projects'), $L['projects']);
$catpatharray[] = array('', $L['offers_useroffers']);
$catpath = cot_breadcrumbs($catpatharray, $cfg['homebreadcrumb'], true);

$t->assign(array(
	"BREADCRUMBS" => $catpath,
));

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

while ($offer = $sql->fetch())
{
  $mdata = (!empty($offer['offer_json']) ? json_decode($offer['offer_json'],1) : array());
  if(!is_array($mdata)) $mdata = array();

  $prjitem = $db->query("SELECT * FROM $db_projects WHERE item_id=".$offer['offer_pid'].' LIMIT 1')->fetch();

  //$t->assign(cot_generate_usertags($offer['offer_userid'], 'OFFER_ROW_OWNER_'));

  $t->assign(cot_generate_usertags($prjitem['item_userid'], 'OFFER_ROW_PROJECT_OWNER_'));
	$t->assign(cot_generate_projecttags($prjitem, 'OFFER_ROW_PROJECT_'));

  $t->assign(array(
    "OFFER_ROW_ID" => $offer['offer_id'],
  	"OFFER_ROW_DATE" => cot_date('d M, H:i', $offer['offer_date']),
  	"OFFER_ROW_DATE_STAMP" => $offer['offer_date'],
    "OFFER_ROW_CHOISE" => $offer['offer_choise'],
    "OFFER_ROW_ORDER_ID" => $mdata['orderId'],
    "OFFER_ROW_VIEW" => $offer['offer_view'],
  	"OFFER_ROW_TEXT" => cot_parse($offer['offer_text']),
    "OFFER_ROW_MDATA" => $mdata,
    "OFFER_ROW_COST" => number_format(($mdata['price'] > 0 ? $mdata['price'] : 500), 2, '.', ' '),
    "OFFER_ROW_DURATION" => cot_declension(($mdata['duration'] > 0 ? $mdata['duration'] : 1), 'день,дня,дней')
  ));
	
	/* === Hook - Part2 : Include === */
	foreach ($extp as $pl)
	{
		include $pl;
	}
	/* ===== */
	
	$t->parse("MAIN.OFFER_ROWS");
}

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

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