<?php
/**
 * Directory module
 * 
 * Add a directory entry page
 * 
 * @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($_POST['enregistrer'])) {
	
	include_once(override('../mail/mail_actions.php'));
	include_once(override('../mail/send.php'));
	
  $data_table=unserialize(urldecode($_POST['data_table']));
  $yp_object= new yellowpages;
  $result=$yp_object->AddYellowPages($data_table, $sql_object);
  $link_confirm="confirm.php?rub=".$rub."&todo=".$todo;
  if(is_numeric($result)) {
  	
  	// we prepare and send email
  	$a = array();
  	$a['action'] = 'add_entry';
  	$link = HrefMaker(array('rub' => $GLOBALS['links'][U_L]['directory-detail']['linkvalue'], 'id' => $result));
  	$a['link_entry'] = $link;
  	$a['recipient'] = $data_table[9];
  	
  	$emailContainer = prepare_email($a);
  	$r = send_email($emailContainer);

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

$form_name='';
$form_activity='';
$form_level='';
$form_street='';
$form_postal_code='';
$form_city='';
$form_phone='';
$form_fax='';
$form_email='';
$form_website='';
$form_statut='';
$mask_statut_P="checked=\"checked\"";
$mask_statut_D='';

if(isset($_POST['valider'])) {
    
  $table = array();
  $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'];
  ($_POST['yp_website'] != 'http://') ? $table[10]=$_POST['yp_website'] : $table[10]='';
  $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=$_POST['yp_postal_code'];
  $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);

?>
<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(_t('yp','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('yp','add_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>

<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>

<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=list"); ?>
    <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');
  if($form_website=='http://') $form_website='';
  $display_form_website=formatText(empty_nc($form_website), '2HTML');
  $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" />
  	<?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> <!-- 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');
	});");
}
?>