<?
/**
 * VoiceOne Settings/Queues/edit
 *
 * Frontend to provide access to the queue system in voiceone.
 * @package voiceone
 * @subpackage  settings_queues
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $data=$_POST["queue"];
    $entry=Array();
        
    //create the entry array with the posted data  
    if (is_array($data))
    foreach ($data as $k=>$v) $entry[$k]=$v;
        
    if (is_array($_POST["member"]))
    foreach($_POST["member"] as $g) $entry["member"][]=$g;
    //echo "<pre>";print_r($entry);phpinfo();die;
    
    $ret=$asterisk->putQueue($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".$cwlang["queue"]["alertModGood"]."</div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".$cwlang["queue"]["alertModBad"]."</div>";

    goToMainPage($alert);
  }

  
  //retrieve the queue informations
  if ($_GET["id"]!="") {
    $queue=$asterisk->getQueue($_GET["id"]);
    $queueName=$queue[0]["name"];
    $strategy=$queue[0]["strategy"];
    $music=$queue[0]["music"];
    $announceFrequency=$queue[0]["announce-frequency"];
    $announceHoldtime=$queue[0]["announce-holdtime"];
    $joinempty=$queue[0]["joinempty"];
    $queueMember=$queue[0]["member"];
    $title=$cwlang["queue"]["modifyQueue"].$queueName;
  } else {
    $strategy="ringall";
    $music="default";
    $announceFrequency="90";
    $announceHoldtime="yes";
    $joinempty="yes";
    $title=$cwlang["queue"]["newQueue"];
  }
  
  //retrieve the list of client
  $ret=$asterisk->getClient(0,0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if (is_array($ret))
  foreach($ret as $v) {
    $id=$v["id"];
    $name=$v["name"]." ".$v["surname"]." &lt;".$v["extension"]."&gt;";
    $technology=$v["technology"];
    $prot=getIcon($technology);
    if (@in_array($id, $queueMember)) $chk="checked='checked'"; else $chk=""; 
    $html.="<td> <input type=\"checkbox\" name=\"member[]\" value=\"$id\" $chk/> $prot $name </td>\n";
    if ((++$count)%2==0) $html.="</tr><tr>\n";
  }

  
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$title?></h1>
    <h3>
      <a href="../" class="admin" ><?=$cwlang["exten"]["return"]?></a> 
    </h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?><? if ($_GET["id"]!="") echo "?id=".$_GET["id"]; ?>" method="post">
      
      <div id="client_form" >
        <h3><?=$cwlang["queue"]["titleForm"]?></h3>
        <p class="blockintro"><?=$cwlang["queue"]["textForm"]?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="queue[name]"><?=$cwlang["queue"]["name"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="queue[name]" name="queue[name]" value="<?=$queueName?>" /></dd>
            <?=$cwlang["queue"]["nameExample"]?>
          </dl>
          <dl>
            <dt class="required"><label for="queue[strategy]"><?=$cwlang["queue"]["strategy"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="queue[strategy]" name="queue[strategy]" value="<?=$strategy?>" /></dd>
            <?=$cwlang["queue"]["strategyExample"]?>
          </dl>
          <dl>
            <dt class="required"><label for="queue[music]"><?=$cwlang["queue"]["melody"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="queue[music]" name="queue[music]" value="<?=$music?>" /></dd>
            <?=$cwlang["queue"]["melodyExample"]?>
          </dl>
          <dl>
            <dt class="required"><label for="queue[announce-frequency]"><?=$cwlang["queue"]["frequency"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="queue[announce-frequency]" name="queue[announce-frequency]" value="<?=$announceFrequency?>" /></dd>
            <?=$cwlang["queue"]["frequencyExample"]?>
          </dl>
          <dl>
            <dt class="required"><label for="queue[announce-holdtime]"><?=$cwlang["queue"]["holdtime"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="queue[announce-holdtime]" name="queue[announce-holdtime]" value="<?=$announceHoldtime?>" /></dd>
            <?=$cwlang["queue"]["holdtimeExample"]?>
          </dl>
          <dl>
            <dt class="required"><label for="queue[joinempty]"><?=$cwlang["queue"]["joinEmpty"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="queue[joinempty]" name="queue[joinempty]" value="<?=$joinempty?>" /></dd>
            <?=$cwlang["queue"]["joinEmptyExample"]?>
          </dl>

          <p class="blockintro"><?=$cwlang["queue"]["extensionInfo"]?></p>
          <table class="todolist" >
            <tr><?=$html?></tr>
          </table>
        </div>
      </div>      
      
      <div class="action">
        <?if ($_GET["id"]!="") {?><input type="hidden" name="queue[id]" value="<?=$_GET["id"]?>" /><?}?>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Annulla" onclick="location.href='../'; return false;"/>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Salva" />
      </div>
    </form>
  </div>  
  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["queue"]["sidebarEditTitle"]?></h1>
          <?=$cwlang["queue"]["sidebarEditText"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

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

