<?php
/**
 * Directory module
 * 
 * Modify given directory entry from back-office
 * 
 * @package linea21\modules\yellowpages
 * @author $Author$ - linea21 <info@linea21.com>
 * @version $Id$ 
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

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

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

include_once("../lib/input_helpers.php");
if (isset($_GET['id']))	$yp_id=$_GET['id'];
if (isset($_POST['id']))	$yp_id=$_POST['id'];

$requete=SQL_getdetyp($yp_id);
$data_yp=$sql_object->DBSelect($requete, 'OBJECT');

// Do record exists?
if(!isset($data_yp[0]->yellowp_name)) redirect_to('@module_default');

$display_title=formatTitleh2($data_yp[0]->yellowp_name);
$navtitle=_t('yp','mod')." : ".$data_yp[0]->yellowp_name;

?>
<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','yellowpages');?></a></li>
	<li><span><?php echo formatnavTitle($navtitle); ?></span></li>
</ul>
</div>
<!--end barre haute -->
<?php
if(isset($_POST['enregistrer'])) {
  $data_table=unserialize(urldecode($_POST['data_table']));
  $yp_object= new yellowpages;
  $result=$yp_object->ModifyYellowPages($yp_id, $data_table, $sql_object);
  $link_confirm="confirm.php?rub=".$rub."&todo=".$todo."&id=".$yp_id;

  if($result) {
      header("Location: ".safe_redirect($link_confirm));
      exit;
  }
  else  system_error();
}

$form_name=formatText($data_yp[0]->yellowp_name, '2FIELD');
$form_activity=formatText($data_yp[0]->yellowp_activity, '2FIELD');
$form_level=$data_yp[0]->yellowp_level;
$form_street=formatText($data_yp[0]->yellowp_street, '2FIELD');
$form_postal_code=formatText($data_yp[0]->yellowp_postal_code, '2FIELD');
$form_city=formatText($data_yp[0]->yellowp_city, '2FIELD');
$form_phone=$data_yp[0]->yellowp_phone;
$form_fax=$data_yp[0]->yellowp_fax;
$form_email=formatText($data_yp[0]->yellowp_email, '2FIELD');
$form_website=formatText($data_yp[0]->yellowp_website, '2FIELD');
$form_statut=$data_yp[0]->yellowp_statut;
if($form_statut=='AA') $mask_statut_D="checked=\"checked\""; else $mask_statut_D='';
if($form_statut=='P') $mask_statut_P="checked=\"checked\""; else $mask_statut_P='';


if(isset($_POST['valider'])) {
  $table[0]=$_POST['yp_name'];
  $table[1]=$_POST['yp_activity'];
  $table[2]=$_POST['yp_level'];
  $table[3]=$_POST['yp_street'];
  $table[4]=$_POST['yp_postal_code'];
  $table[5]=$_POST['yp_city'];
  $table[6]='';
  $table[7]=str_replace(' ', '', $_POST['yp_phone']);
  $table[8]=str_replace(' ', '', $_POST['yp_fax']);
  $table[9]=$_POST['yp_email'];
  $table[10]=$_POST['yp_website'];
  $table[11]=$_POST['yp_statut'];
  $yp_object= new yellowpages;
  $integrity=$yp_object->CheckDataIntegrity($table);

  $form_name=formatText($_POST['yp_name'], '2FIELD');
  $form_activity=formatText($_POST['yp_activity'], '2FIELD');
  $form_level=$_POST['yp_level'];
  $form_street=formatText($_POST['yp_street'], '2FIELD');
  $form_postal_code=formatText($_POST['yp_postal_code'], '2FIELD');
  $form_city=formatText($_POST['yp_city'], '2FIELD');
  $form_phone=str_replace(' ', '', $_POST['yp_phone']);
  $form_fax=str_replace(' ', '', $_POST['yp_fax']);
  $form_email=formatText($_POST['yp_email'], '2FIELD');
  $form_website=formatText($_POST['yp_website'], '2FIELD');
  $form_statut=$_POST['yp_statut'];
  if($form_statut=='AA') $mask_statut_D="checked=\"checked\""; else $mask_statut_D='';
  if($form_statut=='P') $mask_statut_P="checked=\"checked\""; else $mask_statut_P='';

  $table=urlencode(serialize($table));
}

include_once("../lib/input_helpers.php");
include_once(override('../level/mysql.inc.php', THEME_ADMIN_PATH));
// select-box des niveaux
$data=$sql_object->DBSelect(SQL_getLevelsList());
$select_level=LevelSelectBox($data, "yp_level", $form_level);

?>

<?php
if(!isset($integrity) || is_string($integrity)) {
  ?>
<div class="contentcontainer">
<?php
include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));
?>
<div id="content">
<?php
$link_det_state=$rub_link."&todo=det&state=".$form_statut."&amp;id=";
if ($form_statut=='D') {  ?>
  <div class="mod_link">
  	<a href="<?php echo $link_det_state.$yp_id ;?>"><?php echo _t('yp','confirmation');?></a>
  </div>
  <br class="brendstep" />
<?php } ?>
<?php
if(isset($integrity) && is_string($integrity)) display_errors($integrity);
?>
<h2><?php echo $display_title; ?></h2>

<form id="addyp" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<p>
	<label for="yp_name"><?php echo mb_ucfirst(_t('yp','name2')); ?>&nbsp;:&nbsp;*</label>
	<input name="yp_name" type="text" class="textfield" id="yp_name" maxlength="150" value="<?php echo $form_name; ?>" required />
</p>
<p>
	<label for="yp_activity"><?php echo mb_ucfirst(_t('yp','activity')); ?>&nbsp;:&nbsp;*</label>
	<input name="yp_activity" type="text" class="textfield" id="yp_activity" maxlength="255" value="<?php echo $form_activity; ?>" required />
</p>
<p>
	<label for="yp_level"><?php echo mb_ucfirst(_t('yp','level')); ?> : </label>
	<?php echo $select_level; ?>
</p>
<?php if($form_statut=='P' || $form_statut=='AA'){?>
	<div class="label"><?php echo mb_ucfirst(_t('divers','statut')); ?>&nbsp;:&nbsp;*</div>
	<div class="rightpanel">
    	<input name="yp_statut" type="radio" class="radio" id="statut_P" value="P" <?php echo $mask_statut_P; ?> required />
    	<label for="statut_P"><?php echo mb_ucfirst(_t('statut','public')); ?></label>
    	<input name="yp_statut" type="radio" class="radio" id="statut_D" value="AA" <?php echo $mask_statut_D; ?> />
    	<label for="statut_D"><?php echo mb_ucfirst(_t('statut','AA')); ?></label>
	</div>
<?php } else {
  echo "<input name=\"yp_statut\" type=\"hidden\" value=\"D\" id=\"yp_statut\" />" ;
}
?>
<h3 class="part"><?php echo mb_ucfirst(_t('yp','infos_adresse')); ?></h3>

<p>
	<label for="yp_street"><?php echo mb_ucfirst(_t('yp','street')); ?>&nbsp;:&nbsp;*</label>
	<input name="yp_street" type="text" class="textfield" id="yp_street" maxlength="200" value="<?php echo $form_street; ?>" required />
</p>
<p>
	<label for="yp_postal_code"><?php echo mb_ucfirst(_t('yp','postal_code')); ?>&nbsp;:&nbsp;*</label>
	<input name="yp_postal_code" type="text" class="textfield" id="yp_postal_code" maxlength="6" value="<?php echo $form_postal_code; ?>" required />
</p>
<p>
	<label for="yp_city"><?php echo mb_ucfirst(_t('yp','city')); ?>&nbsp;:&nbsp;*</label>
	<input name="yp_city" type="text" class="textfield" id="yp_city" maxlength="100" value="<?php echo $form_city; ?>" required />
</p>
<p>
	<label for="yp_phone"><?php echo mb_ucfirst(_t('yp','phone')); ?> :</label>
	<input name="yp_phone" type="text" class="textfield phone-format" id="yp_phone" maxlength="14" value="<?php echo $form_phone; ?>" />
</p>
<p>
	<label for="yp_fax"><?php echo mb_ucfirst(_t('yp','fax')); ?> :</label>
	<input name="yp_fax" type="text" class="textfield phone-format" id="yp_fax" maxlength="14" value="<?php echo $form_fax; ?>" />
</p>
<p>
	<label for="yp_email"><?php echo mb_ucfirst(_t('yp','email')); ?>&nbsp;:&nbsp;*</label>
	<input name="yp_email" type="text" class="textfield" id="yp_email" maxlength="150" value="<?php echo $form_email; ?>" required />
</p>
<p>
	<label for="yp_website"><?php echo mb_ucfirst(_t('yp','website')); ?> :</label>
	<input	name="yp_website" type="text" class="textfield" id="yp_website" maxlength="150" value="<?php echo $form_website; ?>" placeholder="http(s)://" />
</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=det&amp;id=".$yp_id); ?>
	<input name="id" type="hidden" value="<?php echo $yp_id; ?>" id="id" />
	<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	{
  if($form_level==0) $display_form_level=empty_nc('');
  else {
    $record=$sql_object->DBSelect(SQL_getoneLevel($form_level));
    $display_form_level=formatText($record[0]['level_name'], '2HTML');
  }
  $display_form_name=formatText(empty_nc($form_name), '2HTML');
  $display_form_activity=formatText(empty_nc($form_activity), '2HTML');
  $display_form_street=formatText(empty_nc($form_street), '2HTML');
  $display_form_postal_code=formatText(empty_nc($form_postal_code), '2HTML');
  $display_form_city=formatText(empty_nc($form_city), '2HTML');
  $display_form_phone=formatText(empty_nc($form_phone), '2HTML');
  $display_form_fax=formatText(empty_nc($form_fax), '2HTML');
  $display_form_email=formatText(empty_nc($form_email), '2HTML');
  $display_form_website=formatText(empty_nc($form_website), '2HTML');
  if ($form_statut=='D') $display_form_statut=_t('yp','draft');
  else $display_form_statut=display_statut($form_statut);
  ?>
<div class="contentcontainer">
<?php
include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));
?>
<div id="content">
<h2><?php echo _t('recap','title'); ?></h2>
<dl class="summary">
	<dt><?php echo mb_ucfirst(_t('yp','name2')); ?> :</dt>
	<dd><?php echo $display_form_name; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','activity')) ;?> :</dt>
	<dd><?php echo $display_form_activity; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','level')); ?> :</dt>
	<dd><?php echo $display_form_level; ?></dd>
	<dt><?php echo mb_ucfirst(_t('divers','statut')); ?> :</dt>
	<dd><?php echo $display_form_statut; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','street')); ?> :</dt>
	<dd><?php echo $display_form_street; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','postal_code')); ?> :</dt>
	<dd><?php echo $display_form_postal_code; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','city')); ?> :</dt>
	<dd><?php echo $display_form_city; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','phone')); ?> :</dt>
	<dd class="phone-format"><?php echo $display_form_phone; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','fax')); ?> :</dt>
	<dd class="phone-format"><?php echo $display_form_fax; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','email')); ?> :</dt>
	<dd><?php echo $display_form_email; ?></dd>
	<dt><?php echo mb_ucfirst(_t('yp','website')); ?> :</dt>
	<dd><?php echo $display_form_website; ?></dd>
</dl>
<form id="addscale" 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" />
  	<input name="id" type="hidden" value="<?php echo $yp_id; ?>" id="id" />
  	<?php echo cancel_button($rub_link."&amp;todo=det&amp;id=".$yp_id); ?>
  	<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> <!-- closing #content -->
</div> <!-- closing .contentcontainer -->
<?php } ?>
<?php 
if(U_L == 'fr') {
	footerAddJS('../lib/js/jquery-mask-plugin/dist/jquery.mask.min.js');
	footerAddInlineJS("
 		$( '.phone-format').each(function( index ) {
	  if($(this).text() != '".empty_nc('')."') $(this).mask('00 00 00 00 00');
	});");
}
?>