<?php
/**
 * @package linea21.modules
 * @subpackage workshoprep
 * @author linea21 <info@linea21.com>
 * @version $id SVN
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 * 'Workshoprep' (as Workshop Report) module is used by 'Workshop' modules
 */

//////////// Check Inclusion de pages ////////////
if (!function_exists('AuthenthificationProcess')) {
    include_once("../lib/lib_common.php");
    ReloadIndex('admin');
} 
////////////


if(isset($_GET['work_id'])) $work_id=$_GET['work_id'];
if(isset($_POST['work_id'])) $work_id=$_POST['work_id'];
$cancel_action_link="?rub=workshop&amp;todo=det&amp;id=".$work_id;


$requete=SQL_getWorkshopDenomination($work_id);
$result=$sql_object->DBSelect($requete);
if(count($result)>1) exit();
$workrep_workshop_name=$result[0]['workshop_denomination'];

//////////// Check Droits utilisateur ////////////
if (!HaveRight('workshop')) ReloadIndex('admin');
if(!AuthUser4Workshop($work_id, $sql_object) && !IsSuperAdmin()) ReloadIndex('admin');
////////////
$rub_link="index.php?rub=workshop";
?>

<div id="chemin">
	<ul>
		<li>
			<a href="index.php" class="chemin_home"><? echo _t('way','home'); ?></a>
		</li>
		<li>
			   <a href="<?php echo $rub_link; ?>" id="chemin_<? echo $rub; ?>" class="chemin_rub"><? echo _t('menu','workshop'); ?></a>
		</li>
		<li>
			<span><? echo formatNavTitle(_t('workshoprep','add'). " (" .$workrep_workshop_name.")"); ?></span>
		</li>
	</ul>
</div>
<!--end barre haute -->
<?php
$form_title='';
$form_resume='';
$form_comment='';
$mask_statut_P='';
$mask_statut_D="checked=\"checked\"";


$workshop_report_object= new workshop_report;

if(isset($_POST['enregistrer'])) {
	$data_table=unserialize(urldecode($_POST['data_table']));
	$result=$workshop_report_object->AddWorkshopReport($data_table, $sql_object);
	$link_confirm="confirm.php?rub=".$rub."&todo=".$todo."&id=".$result;
	if(is_numeric($result)) header("Location: ".$link_confirm);
	else  system_error();
}

if(isset($_POST['valider'])) {
	$table[0]=$_POST['workrep_title'];
	$table[1]=$_POST['workrep_resume'];
	$table[2]=$_POST['workrep_comment'];
	$table[3]=$_POST['workrep_statut'];
	$table[4]=$_POST['work_id'];

	$integrity=$workshop_report_object->CheckDataIntegrity($table, $sql_object);

	if(is_string($integrity)) {
		$text_format='2FIELD';
	} else {
		$text_format='2HTML';
		}
	$form_title=formatText($_POST['workrep_title'], $text_format);
	$form_resume=formatText($_POST['workrep_resume'], $text_format);

	$form_comment=formatText($_POST['workrep_comment'], $text_format);

	if($_POST['workrep_statut']=='D') $mask_statut_D="checked=\"checked\""; else $mask_statut_D='';
	if($_POST['workrep_statut']=='P') $mask_statut_P="checked=\"checked\""; else $mask_statut_P='';
	$table=urlencode(serialize($table));
}

include_once(THEME_ADMIN_PATH."quickicons.php");

?>

<?php
if(!isset($integrity) || is_string($integrity)) {
?>
<div class="contentcontainer">
		<?php
		include_once($dir."/menurub.php");
		?>
		<div id="content">
			<?php
			if(isset($integrity) && is_string($integrity)) display_errors($integrity);
			?>
			<h2><? echo formatTitleh2(_t('workshoprep','add_title'). " (" .$workrep_workshop_name.")"); ?></h2>
			<form id="addworkrep" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
				<p>
				<label for="workrep_title"><? echo _t('workshoprep','title'); ?> : *</label>
				<input name="workrep_title" type="text" class="textfield" id="workrep_title" maxlength="200" value="<?php echo $form_title; ?>" />
				</p>
				<p>
				<label for="workrep_resume"><? echo _t('workshoprep','resume'); ?> : *</label>
				<textarea id="workrep_resume" name="workrep_resume" class="largetextfield" <?php echo AREA_SETTINGS; ?>><?php echo $form_resume; ?></textarea>
				</p>
				<p>
				<label for="statut_D"><? echo _t('workshoprep','statut'); ?> : *</label>
				<input name="workrep_statut" type="radio" id="statut_P" value="P" <?php echo $mask_statut_P; ?> /><span class="radio"><?php echo _t('statut','public'); ?></span>
				<input name="workrep_statut" type="radio" id="statut_D" value="D" <?php echo $mask_statut_D; ?> /><span class="radio"><?php echo _t('statut','draft'); ?></span>
				<br />
				</p>
				<p>
				<label for="workrep_comment"><? echo _t('workshoprep','comment'); ?> : </label>
				<textarea id="workrep_comment" name="workrep_comment" class="largetextfield" <?php echo AREA_SETTINGS; ?>><?php echo $form_comment; ?></textarea>
				</p>
				<div>
				<input name="work_id" type="hidden" value="<?php echo $work_id; ?>" id="work_id" />
				<input name="rub" type="hidden" value="<?php echo $rub; ?>" id="rub" />
				<input name="todo" type="hidden" value="<?php echo $todo; ?>" id="todo" />
				<?php echo cancel_button($cancel_action_link); ?>
				<input name="valider" type="submit" value="<? echo _t('btn','valid') ?>" class="button" id="valider" />
				</div>
			</form>
		</div>
<?php include_once($dir."/help.php"); ?>
</div>
<?php
// Récapitulatif
} else	{

if(empty($form_comment)) $display_form_comment=_t('divers','none');
else $display_form_comment=$form_comment;

?>
<div class="contentcontainer">
<?php
include_once($dir."/menurub.php");
?>
<div id="content">
			<h2><? echo _t('recap','title') ?></h2>
			<?php
			echo "<dl class=\"dl3\">\n";
			echo "<dt>"._t('workshoprep','title')."</dt>\n";
			echo "<dd>". $form_title."</dd>\n";
			echo "<dt>". _t('workshoprep','resume'). "</dt>\n";
			echo "<dd>". $form_resume."</dd>\n";
			echo "<dt>". _t('divers','statut') ."</dt>\n";
			echo "<dd>". display_statut($_POST['workrep_statut']). "</dd>\n";
			echo "<dt>"._t('workshoprep','comment') ."</dt>\n";
			echo "<dd>". $display_form_comment. "</dd>\n";
			echo "</dl>\n";
			?>
			<form id="addworkrep" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
				<div>
				<input name="work_id" type="hidden" value="<?php echo $work_id; ?>" id="work_id" />
				<input name="rub" type="hidden" value="<?php echo $rub; ?>" id="rub" />
				<input name="todo" type="hidden" value="<?php echo $todo; ?>" id="todo" />
				<input name="data_table" type="hidden" value="<?php echo $table; ?>" id="data_table" />
				<input name="retour" type="button" value="<? echo _t('btn','preview') ?>" class="button" id="retour" onclick="history.go(-1);" />
				<input name="enregistrer" type="submit" value="<? echo _t('btn','save') ?>" class="button" id="valider" />
				</div>
			</form>
		</div>
	<?php include_once($dir."/help.php"); ?>
</div>
<?php
}
?>
