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

    $entry=Array(
      "id" => $_POST["id"],
      "name" => $_POST["name"],
      "label" => $_POST["label"],
      "description" => $_POST["description"], 
      "extension" => $_POST["extension"], 
      "code" => $_POST["code"],  
      "groups" => $_POST["group"],  
      "macro" => $_POST["macro"]  
    ); 
    
    //merge the parameter
    if (is_array($_POST["macro_param"]))
    foreach ($_POST["macro_param"] as $k=>$v) $p.="$k=$v|";
    $p=substr($p,0,-1);
    $entry["parameter"]=$p;
    
    //save the group informations
    $ret=$asterisk->putApplication($entry);
    if ($ret>0) $alert="<div class=\"AlertGood\">L'applicazione è stata correttamente modificata </div>";
    else $alert="<div class=\"AlertBad\">L'applicazione NON è stata modificata</div>";
    goToMainPage($alert); 
  }
  //--------------------------------------------------------------------------
  
  $pageTitle="New Application";
  $ret=$asterisk->getApplication(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach($ret as $a) {

    //echo "<pre>".array2xml($a)."</pre>";

    $appList.="<li><a href=\".?id=".$a["id"]."\">".$a["label"]."</a></li>";

    //if modify, read the group informations
    if (isset($_GET["id"]) and $a["id"]==$_GET["id"]) {
      $id=$a["id"];
      $name=$a["name"];
      $label=$a["label"];
      $description=$a["description"];
      $extension=$a["extension"];
      $code=$a["code"];
      $macro=$a["macro"];
      $parameter=$a["parameter"];
      $groups=$a["groups"];
      
      $pageTitle="Edit application $label";
    }  
  }
  
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>

  <?$sel="application";include("../../navigation.php");?>
  
  <div class="SectionHeader">
    <h1><?=$pageTitle?></h1>
    <h3><a href="../" class="admin" ><?=$cwlang["exten"]["return"]?></a></h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?>" method="post">
      <p class="blockintro">Edit your the settings of this macro</p>
      <div class="block">
        <dl>
          <dt class="required"><?=$cwlang["exten"][""]?>Name:</dt>
          <dd><input type="textfield" id="name" name="name" value="<?=$name?>" /><span class="help"> Write i.e. <strong>callforward</strong></span></dd>
          <dt class="required"><?=$cwlang["label"][""]?>Label:</dt>
          <dd><input type="textfield" id="label" name="label" value="<?=$label?>" /><span class="help"> More detailed name, i.e. <strong>Call Forwarding</strong></span></dd>
          <dt class="required"><?=$cwlang["exten"][""]?>Description:</dt>
          <dd><textarea id="description" name="description" style="width: 500px"><?=$description?></textarea></dd>
          <dt class="required"><?=$cwlang["label"][""]?>Extension:</dt>
          <dd><input type="textfield" id="extension" name="extension" value="<?=$extension?>" /><span class="help"> Write the extension catch, i.e. <strong>*72</strong></span></dd>
          <dt class="required"><?=$cwlang["exten"][""]?>Code:</dt>
          <dd><textarea id="code" name="code" style="width: 500px; height: 50px"><?=$code?></textarea></dd>
          <dt class="required"></dt>
          <dd><span class="help">You can write lines like: <strong>GOTO DefaultRule|999999|1</strong></span></dd>
          
        </dl>
        <input type="hidden" name="id" value="<?=$id?>" />
      </div>

      <?echo listMacro($js, $macro, $parameter);?>      
      
      <?echo listGroup(GROUP_OF_APPLICATION, $js, $groups);?>      

      <div class="action">
        <?if ($_GET["id"]!="") {?><input type="hidden" name="id" value="<?=$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["isdngrp"][""]?>Macro List</h1>
          <p><?=$cwlang["isdngrp"][""]?>Choose the macro you want to edit
            <ul>
              <?=$macroList?>
            </ul>
          </p>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
 	sajax_handle_client_request();

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

