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

<script type="text/javascript">
    $(document).ready( function()
    {
    	// Autocomplete initialization
		var t = new $.TextboxList('#workshop_organizer', {unique: true, plugins: {autocomplete: {onlyFromValues: true, placeholder:"<?php echo _t('textbox_js', 'placeholder')?>"}}});
					
		$.ajax({url: '../workshop/_ajax_user_autocomplete.php?type=O', dataType: 'json', success: function(r){
			t.plugins['autocomplete'].setValues(r);
		}});
    });
</script>

<?php
include_once("../lib/input_helpers.php");
$form_denomination='';
$form_resume='';
$form_comment='';
$form_organizer='';
$form_theme='';
$form_level='';
$mask_statut_P='';
$mask_statut_D="checked=\"checked\"";

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

//////////// Check Droits utilisateur ////////////
if (!$l21auth->isSuperAdmin())
ReloadIndex('admin');
////////////

$workshop_object= new workshop;

if(isset($_POST['enregistrer'])) {
  $data_table=unserialize(urldecode($_POST['data_table']));
  $result=$workshop_object->AddWorkshop($data_table, $sql_object);
  $data_table[6]=str_replace(" ","", $data_table[6]);
  $table_organizer=@explode(',',$data_table[6]);
  for ($i=0;$i<count($table_organizer);$i++){
    $result2=$workshop_object->AddUserWorkshop($result, trim($table_organizer[$i]), "O" , $sql_object);
  }
  $link_confirm="confirm.php?rub=".$rub."&todo=".$todo;
  if(is_numeric($result)) header("Location: ".$link_confirm);
  else  system_error();
}

if(isset($_POST['valider'])) {

  $table[0]=$_POST['workshop_denomination'];
  $table[1]=$_POST['workshop_resume'];
  $table[2]=$_POST['workshop_theme'];
  $table[3]=$_POST['workshop_comment'];
  $table[4]=$_POST['workshop_level'];
  $table[5]=$_POST['workshop_statut'];
  $table[6]=$_POST['workshop_organizer'];


  $workshop_object= new workshop;
  $integrity=$workshop_object->CheckDataIntegrity($table, $sql_object);
  if(is_string($integrity)) {
    $text_format='2FIELD';
  } else {
    $text_format='2HTML';
  }
  $form_denomination=formatText($_POST['workshop_denomination'], $text_format);
  $form_resume=formatText($_POST['workshop_resume'], $text_format);
  $form_theme=$_POST['workshop_theme'];
  $form_level=$_POST['workshop_level'];
  $form_organizer=formatText(str_replace(",",", ", $_POST['workshop_organizer']), $text_format);
  $form_comment=formatText($_POST['workshop_comment'], $text_format);

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


if(!isset($integrity) || is_string($integrity)) {

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

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

include_once(THEME_ADMIN_PATH."quickicons.php");

?>
<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('workshop','add')); ?></span></li>
</ul>
</div>
<!--end barre haute -->
<?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 _t('workshop','add_title') ?></h2>

<form id="addworkshop" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<p>
	<label for="workshop_denomination"><?php echo mb_ucfirst(_t('workshop','title')); ?> : *</label>
	<input name="workshop_denomination" type="text" class="textfield" id="workshop_denomination" maxlength="255" value="<?php echo $form_denomination; ?>" />
</p>
<p>
	<label for="workshop_resume"><?php echo mb_ucfirst(_t('workshop','resume')); ?> : *</label>
	<textarea id="workshop_resume" name="workshop_resume" <?php echo AREA_SETTINGS; ?> class="largetextfield"><?php echo $form_resume; ?></textarea>
</p>
<p>
	<label for="workshop_theme"><?php echo mb_ucfirst(_t('workshop','theme')); ?> : *</label>
	<?php echo $select_theme; ?>
</p>
<p>
	<label for="workshop_level"><?php echo mb_ucfirst(_t('workshop','level')); ?> : </label>
    <?php echo $select_level; ?>
</p>
<p>
	<label for="workshop_organizer"><?php echo mb_ucfirst(_t('workshop','organizer')); ?> :&nbsp;*</label>
	<input name="workshop_organizer" type="text" class="textfield" id="workshop_organizer" maxlength="150" value="<?php echo $form_organizer; ?>" />
</p>

	<div class="label"><?php echo mb_ucfirst(_t('workshop','statut')); ?> : *</div>
	<div class="rightpanel">
    	<input name="workshop_statut" type="radio" id="statut_P" value="P" <?php echo $mask_statut_P; ?> />
    	<label for="statut_P"><?php echo mb_ucfirst(_t('statut','public')); ?></label>
    	<input name="workshop_statut" type="radio" id="statut_D" value="D" <?php echo $mask_statut_D; ?> />
    	<label for="statut_D"><?php echo mb_ucfirst(_t('statut','draft')); ?></label>
	</div>

<p>
	<label for="workshop_comment"><?php echo mb_ucfirst(_t('workshop','comment')); ?> : </label>
	<textarea id="workshop_comment" name="workshop_comment" <?php echo AREA_SETTINGS; ?> class="largetextfield"><?php echo $form_comment; ?></textarea>
</p>
  <div>
    <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($rub_link."&amp;todo=list"); ?>
    <input name="valider" type="submit" value="<?php echo _t('btn','valid') ?>" class="button" id="valider" />
  </div>
</form>
</div>
<?php include_once(override($dir.'/help.php', THEME_ADMIN_PATH)); ?></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'], '2HTML');

  if($form_level <> -1) {
    $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'], '2HTML');
  } else $level_denomination=_t('divers','none');


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

  ?>
<div class="contentcontainer">
<?php
include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));
?>
<div id="content">
<h2><?php echo _t('recap','title') ?></h2>
<?php
echo "<dl class=\"dl3\">\n";
echo "<dt>".mb_ucfirst(_t('workshop','title'))." :</dt>\n";
echo "<dd>". $form_denomination."</dd>\n";
echo "<dt>".mb_ucfirst(_t('workshop','resume')). " :</dt>\n";
echo "<dd>".$display_form_resume."</dd>\n";
echo "<dt>".mb_ucfirst(_t('workshop','theme'))." :</dt>\n";
echo "<dd>".$theme_denomination."</dd>\n";
echo "<dt>".mb_ucfirst(_t('workshop','level'))." :</dt>\n";
echo "<dd>".$level_denomination."</dd>\n";
echo "<dt>".mb_ucfirst(_t('workshop','organizer'))." :</dt>\n";
echo "<dd>".$form_organizer."</dd>\n";
echo "<dt>".mb_ucfirst(_t('divers','statut')) ." :</dt>\n";
echo "<dd>". display_statut($_POST['workshop_statut']). "</dd>\n";
echo "<dt>".mb_ucfirst(_t('workshop','comment')) ." :</dt>\n";
echo "<dd>". $display_form_comment. "</dd>\n";
echo "</dl>\n";
?>
<form id="addworkshop" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
  <div>
  	<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($rub_link."&amp;todo=list"); ?>
  	<input name="data_table" type="hidden" value="<?php echo $table; ?>" id="data_table" />
  	<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>
<?php include_once(override($dir.'/help.php', THEME_ADMIN_PATH)); ?></div>
<?php
}
?>