<?php
/**
 * @package linea21.modules
 * @subpackage publication
 * @author linea21 <info@linea21.com>
 * @version $id SVN
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

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

if(isset($_POST['id']))	$id=$_POST['id'];
if(isset($_GET['id']))	$id=$_GET['id'];

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


$req_det= SQL_getoneCompletePublication($id);
$result=$sql_object->DBSelect($req_det);
if(count($result)>1) exit();

$publi_title=formatText($result[0]['publi_title'], '2HTML');
$form_title=formatText($result[0]['publi_title'], '2FIELD');
$form_resume=formatText($result[0]['publi_resume'], '2FIELD');
if(empty($result[0]['publi_comment'])) $form_comment='';
else $form_comment=formatText($result[0]['publi_comment'], '2FIELD');
$form_theme=$result[0]['publi_theme'];
$form_scale=$result[0]['publi_scale'];
$form_level=$result[0]['publi_level'];
$form_author=formatText($result[0]['user_login'], '2FIELD');
$form_statut=$result[0]['publi_statut'];
$current_statut=$result[0]['publi_statut'];
$publi_owner=$result[0]['publi_posted_by'];

//////////// Check Droits utilisateur ////////////
if (!HaveRight('publication')) ReloadIndex('admin');
if (GetSessionElement('id')!=$publi_owner && !IsSuperAdmin()) ReloadIndex('admin');
////////////
?>

<div id="chemin">
	<ul>
		<li>
			<a href="index.php"><? echo $GLOBALS['lang']['way']['home']; ?></a>
		</li>
		<li>
			<a href="<?php echo $rub_link; ?>"><? echo $GLOBALS['lang']['menu']['publication']; ?></a>
		</li>
		<li>
			<span><? echo formatNavTitle($GLOBALS['lang']['publication']['mod']. ' : '. $publi_title); ?></span>
		</li>
	</ul>
</div>
<!--end barre haute -->
<?php
$publication_object= new publication;

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

if(isset($_POST['valider'])) {
	$table[0]=$_POST['publi_title'];
	$table[1]=$_POST['publi_resume'];
	$table[2]=$_POST['publi_theme'];
	$table[3]=$_POST['publi_scale'];
	$table[4]=$_POST['publi_level'];
	$table[5]=$_POST['publi_author'];
	$table[6]=$_POST['publi_comment'];
	$table[7]=$_POST['publi_statut'];
	$table[8]=$current_statut; // statut courant

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

	if(is_string($integrity)) {
		$text_format='2FIELD';
	} else {
		$text_format='2HTML';
		$table[5]=$integrity['user_id'];
		}
	$form_title=formatText($_POST['publi_title'], $text_format);
	$form_resume=formatText($_POST['publi_resume'], $text_format);
	$form_theme=$_POST['publi_theme'];
	$form_scale=$_POST['publi_scale'];
	$form_level=$_POST['publi_level'];
	$form_author=formatText($_POST['publi_author'], $text_format);
	$form_comment=formatText($_POST['publi_comment'], $text_format);

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

// 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"';
$select_statut="<select name=\"publi_statut\" id=\"publi_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";


if(!isset($integrity) || is_string($integrity)) {
include_once("../lib/input_helpers.php");

// select-box de thèmes
$data=$sql_object->DBSelect($req_list_theme);
$select_theme=ThemeSelectBox($data, "publi_theme", $form_theme);


// select-box des échelles
$data=$sql_object->DBSelect($req_list_scale);
$select_scale=ScaleSelectBox($data, "publi_scale", $form_scale);

// select-box des niveaux
$data=$sql_object->DBSelect($req_list_level);
$select_level=LevelSelectBox($data, "publi_level", $form_level);

}

include_once("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($publi_title); ?></h2>
			<form id="modpublication" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
				<p>
				<label for="publi_title"><? echo $GLOBALS['lang']['publication']['title']; ?> : *</label>
				<input name="publi_title" type="text" class="textfield" id="publi_title" maxlength="150" value="<?php echo $form_title; ?>" />
				</p>
				<p>
				<label for="publi_resume"><? echo $GLOBALS['lang']['publication']['resume']; ?> : *</label>
				<textarea id="publi_resume" name="publi_resume" class="largetextfield" <?php echo AREA_SETTINGS; ?>><?php echo $form_resume; ?></textarea>
				</p>
				<p>
				<label for="publi_theme" ><? echo $GLOBALS['lang']['publication']['theme']; ?> : *</label>
				<?php echo $select_theme; ?>
				</p>
				<p>
				<label for="publi_scale" ><? echo $GLOBALS['lang']['publication']['scale']; ?> : *</label>
				<?php echo $select_scale; ?>
				</p>
				<p>
				<label for="publi_level" ><? echo $GLOBALS['lang']['publication']['level']; ?> : *</label>
				<?php echo $select_level; ?>
				</p>
				<p>
				<label for="publi_author"><? echo $GLOBALS['lang']['publication']['author']; ?> : *</label>
				<input name="publi_author" type="text" class="textfield" id="publi_author" maxlength="150" value="<?php echo $form_author; ?>" />
				</p>
				<p>
				<label for="publi_statut"><? echo $GLOBALS['lang']['publication']['statut']; ?> : *</label>
				<?php echo $select_statut; ?>
				<br />
				</p>
				<p>
				<label for="publi_comment"><? echo $GLOBALS['lang']['publication']['comment']; ?> : </label>
				<textarea id="publi_comment" name="publi_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('javascript:history.go(-1);'); ?>
				<input name="valider" type="submit" value="<? echo $GLOBALS['lang']['btn']['valid'] ?>" class="button" id="valider" />
				</div>
			</form>
		</div>
<?php include_once($dir."/help.php"); ?>
</div>
<?php
// Récapitulatif
} else	{

$requete=SQL_getonethemeName($form_theme);
$result=$sql_object->DBSelect($requete);
if($result==0 || count($result)!=1) exit;
else $theme_denomination=formatText($result[0]['theme_name']);

$requete=SQL_getonescaleDenomination($form_scale);
$result=$sql_object->DBSelect($requete);
if($result==0 || count($result)!=1) exit;
else $scale_denomination=formatText($result[0]['scale_denomination']);

$requete=SQL_getonelevelName($form_level);
$result=$sql_object->DBSelect($requete);
if($result==0 || count($result)!=1) exit;
else $level_denomination=formatText($result[0]['level_name']);

$display_form_resume=empty_nc($form_resume);
$display_form_comment=empty_nc($form_comment);

?>
<div class="contentcontainer">
<?php
include_once($dir."/menurub.php");
?>
<div id="content">
			<h2><? echo formatTitleh2($GLOBALS['lang']['recap']['title']); ?></h2>
			<?php
			echo "<dl class=\"dl3\">\n";
			echo "<dt>".$GLOBALS['lang']['publication']['title']."</dt>\n";
			echo "<dd>". $form_title."</dd>\n";
			echo "<dt>". $GLOBALS['lang']['publication']['resume']. "</dt>\n";
			echo "<dd>".$display_form_resume."</dd>\n";
			echo "<dt>".$GLOBALS['lang']['publication']['theme']." :</dt>\n";
			echo "<dd>".$theme_denomination."</dd>\n";
			echo "<dt>".$GLOBALS['lang']['publication']['scale']." :</dt>\n";
			echo "<dd>".$scale_denomination."</dd>\n";
			echo "<dt>".$GLOBALS['lang']['publication']['level']." :</dt>\n";
			echo "<dd>".$level_denomination."</dd>\n";
			echo "<dt>".$GLOBALS['lang']['publication']['author']." :</dt>\n";
			echo "<dd>".$form_author."</dd>\n";
			echo "<dt>".$GLOBALS['lang']['divers']['statut'] ."</dt>\n";
			echo "<dd>". display_statut($_POST['publi_statut']). "</dd>\n";
			echo "<dt>".$GLOBALS['lang']['publication']['comment'] ."</dt>\n";
			echo "<dd>". $display_form_comment. "</dd>\n";
			echo "</dl>\n";
			?>
			<form id="modpublication" 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('?rub=publication&amp;todo=list'); ?>
				<input name="retour" type="button" value="<? echo $GLOBALS['lang']['btn']['preview'] ?>" class="button" id="retour" onclick="history.go(-1);" />
				<input name="enregistrer" type="submit" value="<? echo $GLOBALS['lang']['btn']['save'] ?>" class="button" id="valider" />
				</div>
			</form>
		</div>
	<?php include_once($dir."/help.php"); ?>
</div>
<?php
}
?>
