<?php
/**
 * Workshop report module
 * 
 * Modify given workshop report from back-office
 * 
 * @package linea21\modules\workshoprep
 * @author $Author$ - linea21 <info@linea21.com>
 * @version $Id$ 
 * @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 (!class_exists('auth')) {
  include_once("../lib/lib_common.php");
  ReloadIndex('admin');
}
////////////

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


//////////// Check Droits utilisateur ////////////
if (!$l21auth->hasRight('workshop')) ReloadIndex('admin');
if(!$l21auth->isWorkgroupOrganiser($l21auth->GetSessionElement('id'), $sql_object, $id) && !$l21auth->isSuperAdmin()) ReloadIndex('admin');
////////////


$mask_statut_P='';
$mask_statut_D='';
$mask_statut_AA='';
$mask_statut_PA='';
$mask_visible_Y='';
$mask_visible_N='';

$req_det= SQL_getoneCompleteWorkshopRep($id);
$result=$sql_object->DBSelect($req_det);

// Do record exists?
if(!isset($result[0]['workrep_title'])) redirect_to('@module_default');

$form_title=formatText($result[0]['workrep_title'], '2FIELD');
$form_resume=formatText($result[0]['workrep_resume'], '2FIELD');
$form_comment=formatText($result[0]['workrep_comment'], '2FIELD');
$form_visible=$result[0]['workrep_visible'];
$form_statut=$result[0]['workrep_statut'];
$current_statut=$result[0]['workrep_statut'];

$workshopreport_title=formatText($result[0]['workrep_title'], '2HTML');
$workrep_workshop_name=formatText($result[0]['workshop_denomination'], '2HTML');
$workshop_title=$workrep_workshop_name;
$current_title=$workshopreport_title;
$work_id=$result[0]['workrep_workshop_id'];
$rub_link="index.php?rub=workshop";


?>

<div id="chemin">
<ul>
	<li><a href="index.php" class="chemin_home"><?php echo _t('way','home'); ?></a></li>
	<li><a href="<?php echo $rub_link; ?>" id="chemin_<?php echo $rub; ?>" class="chemin_rub"><?php echo _t('menu','workshop'); ?></a></li>
	<li><span><?php echo formatnavTitle(_t('workshoprep','mod').' : '.$current_title); ?></span></li>
</ul>
</div>
<!--end barre haute -->
<?php


$workshop_report_object= new workshop_report;

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

if(isset($_POST['valider'])) {
    
  $table = array();
  
  $table[0]=$_POST['workrep_title'];
  $table[1]=$_POST['workrep_resume'];
  $table[2]=$_POST['workrep_comment'];
  $table[3]=$_POST['workrep_statut'];
  $table[4]=$current_statut; // statut courant
  $table['visible']=$_POST['workrep_visible'];

  $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);
  $form_visible=$_POST['workrep_visible'];
  $form_statut=$_POST['workrep_statut'];
  $table=urlencode(serialize($table));
  
  if($_POST['workrep_visible'] == 1) {
      $display_form_visible = _t('divers', 'yes');
  } else {
      $display_form_visible = _t('divers', 'no');
  }
}


// select-box statut
if($form_statut=='P') $mask_statut_P=' selected="selected"';
if($form_statut=='D') $mask_statut_D=' selected="selected"';
if($form_statut=='AA') $mask_statut_AA=' selected="selected"';
if($form_statut=='PA') $mask_statut_PA=' selected="selected"';

if($form_visible==0) $mask_visible_N="checked=\"checked\""; else $mask_visible_N='';
if($form_visible==1) $mask_visible_Y="checked=\"checked\""; else $mask_visible_Y='';

$select_statut="<select name=\"workrep_statut\" id=\"workrep_statut\">\n";
$select_statut.="<option value=\"P\"".$mask_statut_P.">".display_statut('P')."</option>\n";
$select_statut.="<option value=\"D\"".$mask_statut_D.">".display_statut('D')."</option>\n";
$select_statut.="<option value=\"AA\"".$mask_statut_AA.">".display_statut('AA')."</option>\n";
$select_statut.="<option value=\"PA\"".$mask_statut_PA.">".display_statut('PA')."</option>\n";
$select_statut.="</select>\n";

?>

<?php
if(!isset($integrity) || is_string($integrity)) {
  ?>
<div class="contentcontainer"><?php
include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));
?>
<div id="content">
<?php
if(isset($integrity) && is_string($integrity)) display_errors($integrity);
?>
<h2><?php echo formatTitleh2($current_title); ?></h2>
<?php
echo '<div class="subtitle" id="workshop-subtitle">'.$workshop_title.'</div>';
?>
<form id="modworkrep" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
	<p>
		<label for="workrep_title"><?php echo mb_ucfirst(_t('workshoprep','title')); ?>&nbsp;:&nbsp;*</label>
		<input name="workrep_title" type="text" class="textfield" id="workrep_title" maxlength="200" value="<?php echo $form_title; ?>" required />
	</p>
	<p>
		<label for="workrep_resume"><?php echo mb_ucfirst(_t('workshoprep','resume')); ?>&nbsp;:&nbsp;*</label>
		<textarea id="workrep_resume" name="workrep_resume" class="largetextfield" <?php echo AREA_SETTINGS; ?> required><?php echo $form_resume; ?></textarea>
	</p>
	<div class="label"><?php echo mb_ucfirst(_t('workshoprep','visible')); ?> : </div>
    <div class="rightpanel">
        	<input name="workrep_visible" type="radio" class="radio" id="visible_N" value="0" <?php echo $mask_visible_N; ?> required />
        	<label for="visible_N"><?php echo mb_ucfirst(_t('divers','no')); ?></label>
        	<input name="workrep_visible" type="radio" class="radio" id="visible_Y" value="1" <?php echo $mask_visible_Y; ?> />
        	<label for="visible_Y"><?php echo mb_ucfirst(_t('divers','yes')); ?></label>
    </div><br />
	<p>
	<label for="workrep_statut"><?php echo mb_ucfirst(_t('workshoprep','statut')); ?> : </label>
	<?php echo $select_statut; ?>
	</p>
	<p>
		<label for="workrep_comment"><?php echo mb_ucfirst(_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="id" type="hidden" value="<?php echo $id; ?>" id="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="<?php echo _t('btn','valid') ?>" class="button" id="valider" />
	</div>
</form>
</div> <!-- closing #content -->
</div> <!-- closing .contentcontainer -->
<?php
// Récapitulatif
} else	{
  $display_form_comment=empty_none($form_comment);

  ?>
<div class="contentcontainer"><?php
include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));
?>
<div id="content">
<h2><?php echo formatTitleh2(_t('recap','title')); ?></h2>
<?php
echo "<dl class=\"summary\">\n";
echo "<dt>".mb_ucfirst(_t('workshoprep','title')) ." :</dt>\n";
echo "<dd>". $form_title."</dd>\n";
echo "<dt>". mb_ucfirst(_t('workshoprep','resume')). " :</dt>\n";
echo "<dd>". $form_resume."</dd>\n";
echo "<dt>". mb_ucfirst(_t('workshoprep','visible')) ." :</dt>\n";
echo "<dd>". $display_form_visible. "</dd>\n";
echo "<dt>". mb_ucfirst(_t('divers','statut')) ." :</dt>\n";
echo "<dd>". display_statut($_POST['workrep_statut']). "</dd>\n";
echo "<dt>". mb_ucfirst(_t('workshoprep','comment')) ." :</dt>\n";
echo "<dd>". $display_form_comment. "</dd>\n";
echo "</dl>\n";
?>
<form id="modworkrep" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>"
	method="post">
<div><input name="id" type="hidden" value="<?php echo $id; ?>" id="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" /> <?php echo cancel_button($cancel_action_link); ?> <input
	name="retour" type="button" value="<?php echo _t('btn','preview') ?>"
	class="button" id="retour" onclick="history.go(-1);" /> <input
	name="enregistrer" type="submit" value="<?php echo _t('btn','save') ?>"
	class="button" id="valider" /></div>
</form>
</div> <!-- closing #content -->
</div> <!-- closing .contentcontainer -->
<?php } ?>