<?php

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

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

$id = cot_import('id', 'G', 'INT');
$type = cot_import('type', 'G', 'INT');
$r = cot_import('r', 'G', 'ALP');

$c = cot_import('c', 'G', 'TXT');
if (!empty($c) && !isset($structure['market'][$c]))
{
	$c = '';
}

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

if ($a == 'add')
{
  cot_shield_protect();

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

	$ritem = cot_projects_import('POST', array(), $usr);
	
	/* === Hook === */
	foreach (cot_getextplugins('projects.add.add.import') as $pl)
	{
		include $pl;
	}
	/* ===== */

	cot_projects_validate($ritem);

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

  $result = array(
    'result' => 'error',
    'redirectUrl' => '',
    'errors' => array()
  );

	if (!cot_error_found())
	{
    $result['result'] = 'success';
		$id = cot_projects_add($ritem, $usr);
		
		switch ($ritem['item_state'])
		{
			case 0:
				if (!$usr['isadmin'])
				{
					$rbody = cot_rc($L['project_added_mail_body'], array(
						'user_name' => $usr['profile']['user_name'],
						'prj_name' => $ritem['item_title'],
						'sitename' => $cfg['maintitle'],
						'link' => COT_ABSOLUTE_URL . cot_url('projects', 'id=' . $id, '', true)
					));
					cot_mail($usr['profile']['user_email'], $L['project_added_mail_subj'], $rbody);
				}

        $result['redirectUrl'] = cot_url('projects', 'm=myprojects', '', true);
				break;
			case 1:
        $result['redirectUrl'] = cot_url('projects', 'm=myprojects', '', true);
				break;
			case 2:
				if (!$usr['isadmin'])
				{
					$rbody = cot_rc($L['project_senttovalidation_mail_body'], array( 
						'user_name' => $usr['profile']['user_name'],
						'prj_name' => $ritem['item_title'],
						'sitename' => $cfg['maintitle'],
						'link' => COT_ABSOLUTE_URL . $r_url
					));
					cot_mail($usr['profile']['user_email'], $L['project_senttovalidation_mail_subj'], $rbody);
				}

        $result['redirectUrl'] = cot_url('projects', 'm=myprojects', '', true);
				break;
		}

    $_SESSION['project_add'] = '';
	}
  else
	{
    $result['errors'] = array();
  	$errors = cot_get_messages('', 'error');
  	if (count($errors) > 0)
  	{
  		foreach ($errors as $msg)
  		{
  			$result['errors'][$msg['src']] = isset($L[$msg['text']]) ? $L[$msg['text']] : $msg['text'];
  		}
  	}
  	cot_clear_messages();

    $_SESSION['project_add'] = $ritem;
	}

  echo json_encode($result);
  exit;
}

if (empty($ritem['item_cat']) && !empty($c))
{
	$ritem['item_cat'] = $c;
	$usr['isadmin'] = cot_auth('projects', $ritem['item_cat'], 'A');
}

$out['subtitle'] = $L['projects_add_project_title'];
$out['head'] .= $R['code_noindex'];
$sys['sublocation'] = $structure['market'][$c]['title'];

$mskin = cot_tplfile(array('projects', 'add', $structure['market'][$ritem['item_cat']]['tpl']));

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

$t = new XTemplate($mskin);

// Error and message handling
cot_display_messages($t);

// Extra fields
foreach($cot_extrafields[$db_projects] as $exfld)
{
	$uname = strtoupper($exfld['field_name']);
	$exfld_val = cot_build_extrafields('ritem'.$exfld['field_name'], $exfld, $ritem['item_'.$exfld['field_name']]);
	$exfld_title = isset($L['projects_'.$exfld['field_name'].'_title']) ?  $L['projects_'.$exfld['field_name'].'_title'] : $exfld['field_description'];
	$t->assign(array(
		'PRJADD_FORM_'.$uname => $exfld_val,
		'PRJADD_FORM_'.$uname.'_TITLE' => $exfld_title,
		'PRJADD_FORM_EXTRAFLD' => $exfld_val,
		'PRJADD_FORM_EXTRAFLD_TITLE' => $exfld_title
		));
	$t->parse('MAIN.EXTRAFLD');
}

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

    $t->assign(array(
      'CAT_ROW_ID' => $cat['id'],
      'CAT_ROW_TITLE' => $cat['title'],
      'CAT_ROW_CODE' => $code,
    ));

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

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

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