<?
/**
 * VoiceOne Settings/IVR/
 *
 * Frontend to provide access to the ivr system in voiceone.
 * @package voiceone
 * @subpackage  settings_ivr
 */
 
  include ("../../admin/config/cfg.php");
  global $cwlang;
 	sajax_init();

  //================ PAGE LOGIC ================
  $alert=getAlert();

  if ($_GET["cmd"]=="delNode" and count($_POST)==0) {
    $ret=$asterisk->delIvrNode($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["ivr"]["alertDelGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["ivr"]["alertDelBad"]."</div>";
    goToMainPage($alert,"index.php?choosedIvr=".$_REQUEST["choosedIvr"]);
  }

  
  if ($_GET["cmd"]=="del" and count($_POST)==0) {
    $ret=$asterisk->delIvr($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["ivr"]["alertDelGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["ivr"]["alertDelBad"]."</div>";
    goToMainPage($alert,"index.php");
  }

  if ($_GET["cmd"]=="new" and count($_POST)==0) {
    $ret=$asterisk->putIvr(array("name"=>$_GET["name"]));
    if ($ret!==false) $alert="<div class=\"AlertGood\">".$cwlang["ivr"]["alertNewGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["ivr"]["alertNewBad"]."</div>";
    goToMainPage($alert,"index.php?choosedIvr=$ret");
  }

  
  //get the IVR list
  $ivr=$asterisk->getIvr(0);
  if (is_array($ivr))
  foreach($ivr as $i) {
    $id=$i["id"];
    $name=$i["name"];
    if (!isset($_REQUEST["choosedIvr"])) $_REQUEST["choosedIvr"]=$id;  
    if ($_REQUEST["choosedIvr"]==$id) $style=" style=\"font-weight: bold\" "; else $style="";
    $html.="<li><a href=\"?choosedIvr=$id\" $style>$name</a>  <a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('Are you sure to delete the $name IVR?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></li>";
  }
  if (count($ivr)>0) $html="<h1>".$cwlang["ivr"]["chooseIvr"]."</h1>\n<ul>$html</ul>\n";


  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["ivr"]["title"]?></h1>
  </div>
  <div id="editPane">
    <?=$cwlang["ivr"]["titleDesc"]?>
        
  </div>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <?=manageNode($js);?>
          <br/>
          <?=$html?>
          <br/>
          <h1><?=$cwlang["ivr"]["newIvr"]?></h1>
            <form action="<?=$PHP_SELF?>" method="get">
              <table>
                <tr><td><span class="help"><?=$cwlang["ivr"]["name"]?>:</span></td><td><input type="text" name="name" /></span></td></tr>
                <tr><td></td><td><input type="submit" value="<?=$cwlang["ivr"]["newIvrButton"]?>" /></td></tr>
              </table>
              <input type="hidden" name="cmd" value="new" />
            </form>

        </div>
        <?=$loadPane?>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

  $smarty->assign("section", "ivr");
  $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');
?>

