<?
/**
 * VoiceOne Settings/Rules/timesegement/edit
 *
 * Frontend to provide access to the rules system in voiceone.
 * @package voiceone
 * @subpackage  settings_rules
 */
  include ("../../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {

    $id=$_POST["id"];
    $name=$_POST["name"];
    $date_from=implode("|",$_POST["date_from"]);
    $date_to=implode("|",$_POST["date_to"]);
    $hour_from=implode("|",$_POST["hour_from"]);
    $hour_to=implode("|",$_POST["hour_to"]);
    $week_day=@implode("|",$_POST["week_day"]);
    $day=@implode("|",$_POST["day"]);
    $month=@implode("|",$_POST["month"]);
    
    $entry=Array(
      "id" => $id,
      "name" => $name,
      "date_from" => $date_from,
      "date_to" => $date_to,
      "hour_from" => $hour_from,
      "hour_to" => $hour_to,
      "week_day" => $week_day,
      "day" => $day, 
      "month" => $month 
    );

    //save the group informations
    //echo "<pre>"; print_r($entry); echo "</pre>";
    $ret=$asterisk->putTimeSegment($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Time segment saved</div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Time segment error</div>";
    
    goToMainPage($alert);
  }
  //--------------------------------------------------------------------------
  
  $date_from=array();
  $date_to=array();
  $hour_from=array();
  $hour_to=array();
  $week_day=array();
  $day=array();
  $month=array();

  //if modify, read the group informations
  if (isset($_REQUEST["id"])) {
    $ret=$asterisk->getTimeSegment($_REQUEST["id"]);
    if ($err=$asterisk->getError()) die("Error: $err");
    $ret=$ret[0];
    $id=$ret["id"];
    $name=$ret["name"];
    $date_from=split("\|",$ret["date_from"]);
    $date_to=split("\|",$ret["date_to"]);
    $hour_from=split("\|",$ret["hour_from"]);
    $hour_to=split("\|",$ret["hour_to"]);
    $week_day=split("\|",$ret["week_day"]);
    $day=split("\|",$ret["day"]);
    $month=split("\|",$ret["month"]);
  }  


  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>

  <?$sel="timesegment";include("../../navigation.php");?>
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["isdngrp"][""]?>New/Edit Time Segment</h1>
    <h3><a href="../" class="admin" ><?=$cwlang["isdngrp"]["return"]?></a></h3>
  </div>
  
  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if (isset($_GET["id"])) echo "?id=".$_GET["id"];?>" method="post" onSubmit="selectAll(getElementById('listRight')); return selectAll(getElementById('phonenumber'))">
      
    	<h2>Time segment Definition</h2>
      <p class="blockintro">You can choose a time segment that adapt to your needs</p>
      <div class="block">
        <table >
          <tr><td></td><td colspan="3"><h3>Time segment information</h3></td></tr>
          <tr>
            <td class="required"><label for="name">name:</label></td>
            <td colspan="3"><input type="text" size="25" maxlength="30" id="name" name="name" value="<?=$name?>" /></td>
          </tr>
          <tr><td></td><td class="help" colspan="3"><hr/></td></tr>

          <tr><td></td><td colspan="3"><h3>Date segment</h3></td></tr>
          <tr>
            <td class="required"><label for="date_from[]">From:</label></td>
            <td><input type="text" size="25" maxlength="30" id="date_from[]" name="date_from[]" value="<?=$date_from[0]?>" /></td>
            <td class="required"><label for="date_to[]">To:</label></td>
            <td><input type="text" size="25" maxlength="30" id="date_to[]" name="date_to[]" value="<?=$date_to[0]?>" /></td>
          </tr>
          <tr><td></td><td class="help" colspan="3">Write the date between which this time segment is available. Leave blank for always.<br />Write i.e. 24-12-2006 14:00<hr/></td></tr>
          
          <tr><td></td><td colspan="3"><h3>Hour Settings</h3></td></tr>
          <tr>
            <td class="required"><label for="hour_from[]">From:</label></td>
            <td><input type="text" size="25" maxlength="30" id="hour_from[]" name="hour_from[]" value="<?=$hour_from[0]?>" /></td>
            <td class="required"><label for="hour_to[]">To:</label></td>
            <td><input type="text" size="25" maxlength="30" id="hour_to[]" name="hour_to[]" value="<?=$hour_to[0]?>" /></td>
          </tr>
          <tr>
            <td class="required"><label for="hour_from[]">From:</label></td>
            <td><input type="text" size="25" maxlength="30" id="hour_from[]" name="hour_from[]" value="<?=$hour_from[1]?>" /></td>
            <td class="required"><label for="hour_to[]">To:</label></td>
            <td><input type="text" size="25" maxlength="30" id="hour_to[]" name="hour_to[]" value="<?=$hour_to[1]?>" /></td>
          </tr>
          <tr><td></td><td class="help" colspan="3">Write the time, i.e. 9:00, or 23:52:30<hr/></td></tr>
          
          <tr><td></td><td colspan="3"><h3>Week days</h3></td></tr>
          <tr >
            <td class="required"><label for="week_day[]">Days:</label></td>
            <td colspan="3">
              <table class="timesegment">
              <tr>
                <td>Su <input type="checkbox" id="week_day[]" name="week_day[]" value="1" <?if (in_array("1",$week_day)) echo "checked=\"checked\""; ?>/></td>
                <td>Mo <input type="checkbox" id="week_day[]" name="week_day[]" value="2" <?if (in_array("2",$week_day)) echo "checked=\"checked\""; ?>/></td>
                <td>Tu <input type="checkbox" id="week_day[]" name="week_day[]" value="3" <?if (in_array("3",$week_day)) echo "checked=\"checked\""; ?>/></td>
                <td>We <input type="checkbox" id="week_day[]" name="week_day[]" value="4" <?if (in_array("4",$week_day)) echo "checked=\"checked\""; ?>/></td>
                <td>Th <input type="checkbox" id="week_day[]" name="week_day[]" value="5" <?if (in_array("5",$week_day)) echo "checked=\"checked\""; ?>/></td>
                <td>Fr <input type="checkbox" id="week_day[]" name="week_day[]" value="6" <?if (in_array("6",$week_day)) echo "checked=\"checked\""; ?>/></td>
                <td>Sa <input type="checkbox" id="week_day[]" name="week_day[]" value="7" <?if (in_array("7",$week_day)) echo "checked=\"checked\""; ?>/></td>
              </tr>
              </table>
            </td>
          </tr>
          <tr><td></td><td class="help" colspan="3">Leave unchecked to select all days<hr/></td></tr>

          <tr><td></td><td colspan="3"><h3>Month days</h3></td></tr>
          <tr>
            <td class="required"><label for="day[]">Days:</label></td>
            <td colspan="3">
              <table class="timesegment">
              <tr >
                <td>1 <input type="checkbox" id="day[]" name="day[]" value="1" <?if (in_array("1",$day)) echo "checked=\"checked\""; ?>/></td> 
                <td>2 <input type="checkbox" id="day[]" name="day[]" value="2" <?if (in_array("2",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>3 <input type="checkbox" id="day[]" name="day[]" value="3" <?if (in_array("3",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>4 <input type="checkbox" id="day[]" name="day[]" value="4" <?if (in_array("4",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>5 <input type="checkbox" id="day[]" name="day[]" value="5" <?if (in_array("5",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>6 <input type="checkbox" id="day[]" name="day[]" value="6" <?if (in_array("6",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>7 <input type="checkbox" id="day[]" name="day[]" value="7" <?if (in_array("7",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>8 <input type="checkbox" id="day[]" name="day[]" value="8" <?if (in_array("8",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>9 <input type="checkbox" id="day[]" name="day[]" value="9" <?if (in_array("9",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>10 <input type="checkbox" id="day[]" name="day[]" value="10" <?if (in_array("10",$day)) echo "checked=\"checked\""; ?>/></td>
              </tr>
              <tr>
                <td>11 <input type="checkbox" id="day[]" name="day[]" value="11" <?if (in_array("11",$day)) echo "checked=\"checked\""; ?>/></td> 
                <td>12 <input type="checkbox" id="day[]" name="day[]" value="12" <?if (in_array("12",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>13 <input type="checkbox" id="day[]" name="day[]" value="13" <?if (in_array("13",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>14 <input type="checkbox" id="day[]" name="day[]" value="14" <?if (in_array("14",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>15 <input type="checkbox" id="day[]" name="day[]" value="15" <?if (in_array("15",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>16 <input type="checkbox" id="day[]" name="day[]" value="16" <?if (in_array("16",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>17 <input type="checkbox" id="day[]" name="day[]" value="17" <?if (in_array("17",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>18 <input type="checkbox" id="day[]" name="day[]" value="18" <?if (in_array("18",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>19 <input type="checkbox" id="day[]" name="day[]" value="19" <?if (in_array("19",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>20 <input type="checkbox" id="day[]" name="day[]" value="20" <?if (in_array("20",$day)) echo "checked=\"checked\""; ?>/></td>
              </tr>
              <tr>
                <td>21 <input type="checkbox" id="day[]" name="day[]" value="21" <?if (in_array("21",$day)) echo "checked=\"checked\""; ?>/></td> 
                <td>22 <input type="checkbox" id="day[]" name="day[]" value="22" <?if (in_array("22",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>23 <input type="checkbox" id="day[]" name="day[]" value="23" <?if (in_array("23",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>24 <input type="checkbox" id="day[]" name="day[]" value="24" <?if (in_array("24",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>25 <input type="checkbox" id="day[]" name="day[]" value="25" <?if (in_array("25",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>26 <input type="checkbox" id="day[]" name="day[]" value="26" <?if (in_array("26",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>27 <input type="checkbox" id="day[]" name="day[]" value="27" <?if (in_array("27",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>28 <input type="checkbox" id="day[]" name="day[]" value="28" <?if (in_array("28",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>29 <input type="checkbox" id="day[]" name="day[]" value="29" <?if (in_array("29",$day)) echo "checked=\"checked\""; ?>/></td>
                <td>30 <input type="checkbox" id="day[]" name="day[]" value="30" <?if (in_array("30",$day)) echo "checked=\"checked\""; ?>/></td>
              </tr>
              <tr>
                <td>31 <input type="checkbox" id="day[]" name="day[]" value="31" <?if (in_array("31",$day)) echo "checked=\"checked\""; ?>/></td> 
              </tr>
              </table>
            </td>
          </tr>
          <tr><td></td><td class="help" colspan="3">Leave unchecked to select all days<hr/></td></tr>

          <tr><td></td><td colspan="3"><h3>Months</h3></td></tr>
          <tr>
            <td class="required"><label for="month[]">Months:</label></td>
            <td colspan="3">
              <table class="timesegment">
              <tr >
                <td>Gennuary <input type="checkbox" id="month[]" name="month[]" value="1" <?if (in_array("1",$month)) echo "checked=\"checked\""; ?>/></td> 
                <td>February <input type="checkbox" id="month[]" name="month[]" value="2" <?if (in_array("2",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>March <input type="checkbox" id="month[]" name="month[]" value="3" <?if (in_array("3",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>April <input type="checkbox" id="month[]" name="month[]" value="4" <?if (in_array("4",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>May <input type="checkbox" id="month[]" name="month[]" value="5" <?if (in_array("5",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>June <input type="checkbox" id="month[]" name="month[]" value="6" <?if (in_array("6",$month)) echo "checked=\"checked\""; ?>/></td>
              </tr>
              <tr>
                <td>July <input type="checkbox" id="month[]" name="month[]" value="7" <?if (in_array("7",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>August <input type="checkbox" id="month[]" name="month[]" value="8" <?if (in_array("8",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>September <input type="checkbox" id="month[]" name="month[]" value="9" <?if (in_array("9",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>October <input type="checkbox" id="month[]" name="month[]" value="10" <?if (in_array("10",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>November <input type="checkbox" id="month[]" name="month[]" value="11" <?if (in_array("11",$month)) echo "checked=\"checked\""; ?>/></td>
                <td>Dicember <input type="checkbox" id="month[]" name="month[]" value="12" <?if (in_array("12",$month)) echo "checked=\"checked\""; ?>/></td>
              </tr>
              </table>
            </td>
          </tr>
          <tr><td></td><td class="help" colspan="3">Leave unchecked to select all months</td></tr>

        </table>
      </div>

    <div class="action">
      <?if (isset($_GET["id"])) {?><input type="hidden" name="id" value="<?=$id?>" /><?}?>
      <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../groups.php'; return false;"/>
      <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
    </div>
  </form>
 </div>  
  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["isdngrp"]["sidebarTitle"]?></h1>
          <p><?=$cwlang["isdngrp"]["sidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "rules");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

