<?
/**
 * VoiceOne Settings/IVR/edit
 *
 * Frontend to provide access to the ivr system in voiceone.
 * @package voiceone
 * @subpackage  settings_ivr
 * @todo Put edit.php in a subdirectory "edit" to follow the convention as for all other setup items
 */
 
  include ("../../admin/config/cfg.php");
  global $relBasePath, $asterisk, $ivrPath, $cwlang, $cwLanguage;
 	sajax_init();

  //================ PAGE LOGIC ================

  if (count($_POST)>0) {
/*
    //move audio files
    if ($_FILES['node']['name']['audio']!="") {
      $ext = strrchr($_FILES['node']['name']['audio'], '.'); 
      $audio= "node_".$data["id"].$ext;
      if (move_uploaded_file($_FILES['node']['tmp_name']['audio'], $ivrPath.$audio)) {
        $entry["audio"]=$audio; 
        exec("$ivrMoveFile $ivrPath$audio");
      } else 
        die("Error: audio files can't be moved in the correct location ($ivrPath$audio) . Check the write permissions.");
    }
    if ($_FILES['node']['name']['taudio']!="") {
      $ext = strrchr($_FILES['node']['name']['taudio'], '.'); 
      $taudio= "tnode_".$data["id"].$ext;
      if (move_uploaded_file($_FILES['node']['tmp_name']['taudio'], $ivrPath.$taudio)) { 
        $entry["taudio"]=$taudio; 
        exec("$ivrMoveFile $ivrPath$taudio");
      } else 
        die("Error: taudio files can't be moved in the correct location. Check the write permissions.");
    }
    if ($_FILES['node']['name']['iaudio']!="") {
      $ext = strrchr($_FILES['node']['name']['iaudio'], '.'); 
      $iaudio= "inode_".$data["id"].$ext;
      if (move_uploaded_file($_FILES['node']['tmp_name']['iaudio'], $ivrPath.$iaudio)) { 
        $entry["iaudio"]=$iaudio; 
        exec("$ivrMoveFile $ivrPath$iaudio");
      } else  
        die("Error: iaudio files can't be moved in the correct location. Check the write permissions.");
    }
*/

    $data=$_POST["node"];
    if ($data["irepeat"]=="") $data["irepeat"]=0; 
    if ($data["trepeat"]=="") $data["trepeat"]=0; 
    $entry=Array(
      "id"          => $data["id"], 
      "name"        => $data["name"], 
      "parent"      => $data["parent"],
      "order"       => $data["order"],
      "key"         => $data["key"], 
      "audio"       => $_POST["audio"], 
      "macro"       => $_POST["macro"], 
      "timesegment" => $data["timesegment"], 
      "taudio"      => $_POST["taudio"], 
      "trepeat"     => $data["trepeat"], 
      "iaudio"      => $_POST["iaudio"],
      "irepeat"     => $data["irepeat"]
    );

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

    $ret=$asterisk->putIvrNode($entry);
    if ($ret>0) $alert="<div id=\"alert\" class=\"AlertGood\">".$cwlang["ivr"]["alertModGood"]."</div>";
    else $alert="<div id=\"alert\" class=\"AlertBad\">".$cwlang["ivr"]["alertModBad"]."</div>";
    goToMainPage($alert,"index.php?nodeId=$ret&choosedIvr=".$_REQUEST["choosedIvr"]);
  }

  //retrieve the parent node id
  if ($_GET["parentId"]!="") $parent=$_GET["parentId"];

  if ($_REQUEST["choosedIvr"]!="") {
    $nodeArray=$asterisk->getIvrNode(0,$_REQUEST["choosedIvr"]);
    foreach ($nodeArray as $k=>$v) if ($v["id"]==$nodeId) $node=$v;
  }  
  
  if ($_GET["nodeId"]!="") {
    $nodeId=$node["id"];
    $name=$node["name"];
    $parent=$node["parent"];
    $key=$node["key"];
    $audio=$node["audio"];
    $macro=$node["macro"];
    $parameter=$node["parameter"];
    $timesegment=$node["timesegment"];
    $taudio=$node["taudio"];
    $trepeat=$node["trepeat"];
    $iaudio=$node["iaudio"];
    $irepeat=$node["irepeat"];
    
    //check if it is a folder
    $folder=false;
    foreach ($nodeArray as $k=>$v)  if ($v["parent"]==$nodeId) $folder=true;
  }  

  //get the time segment available
  $tsHtml.="<option value=\"0\">[any time]</option>\n";
  $ret=$asterisk->getTimeSegment(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach ($ret as $ts) {
    $tsid=$ts["id"];
    $tsname=$ts["name"];
    if ($tsid==$timesegment) $selected=" selected=\"selected\" ";
    else $selected="";
    $tsHtml.="<option value=\"$tsid\" $selected>$tsname</option>\n";
  }
  
  //retrieve the IVR nodes for changing node position
  foreach ($nodeArray as $k=>$v) {
    if ($v["parent"]==$parent)
      if ($v["id"]!=$nodeId) 
        $html_nodes.="<option value=\"".$v["order"]."\">".$cwlang["ivr"]["replace"]." ".$v["name"]."</option>\n";
      else
        $html_nodes.="<option value=\"".$v["order"]."\" selected=\"selected\">".$cwlang["ivr"]["currentPosition"]." </option>\n";       
  }
  if (isset($_GET["parentId"])) $html_nodes.="<option value=\"0\" selected=\"selected\">".$cwlang["ivr"][""]."At the last position </option>\n";
  
  //set the page title
  if (isset($_GET["parentId"])) $title="Create new node";
  else if ($folder===true) $title="Edit menu ".$name; 
  else if ($folder===false) $title="Edit node ".$name;

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

    <div id="basic_form">
      <form action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">
      <h1>General information</h1>
      <p class="blockintro"><?=$cwlang["ivr"]["nodeDesc"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required"><label for="node[name]"><?=$cwlang["ivr"]["name"]?>:</label></td>
            <td><input type="text" size="25" maxlength="30" id="node[name]" name="node[name]" value="<?=$name?>" /></td>
          </tr>
          <tr>
            <td class="required"><label for="node[key]"><?=$cwlang["ivr"]["key"]?>:</label></td>
            <td><input type="text" size="25" maxlength="50" id="node[key]" name="node[key]" value="<?=$key?>" /><?=$cwlang["ivr"]["keyHelp"]?></td>
          </tr>
          <tr>
            <td class="required"><label for="node[audio]"><?=$cwlang["ivr"]["audio"]?>:</label></td>
            <td>
              <? echo getAudioPanel("audio",$audio,$js); ?>
            </td>
          </tr>
  <?        if ($parent!=0) { ?>
          <tr>
            <td class="required"><label for="node[order]"><?=$cwlang["ivr"]["position"]?>:</label></td>
            <td><select id="node[order]" name="node[order]"><?=$html_nodes?></select></td>
          </tr>
  <?        } ?>          
        </table>
      </div>
<!--
      <h2><?=$cwlang["ivr"][""]?>Time Segment</h2>
      <p class="blockintro"><?=$cwlang["ivr"][""]?>Choose when this node is active</p>
      <div class="block">
        <dl>
          <dt class="required"><label for="node[timesegment]"><?=$cwlang["ivr"][""]?>Time Segment:</label></dt>
          <dd><select id="node[timesegment]" name="node[timesegment]"><?=$tsHtml?></select></dd>
        </dl>
      </div>   
-->  
<?    if ($folder!==true) { echo listMacro($js, $macro, $parameter);?>      
<?    } else { ?>

      <h2><?=$cwlang["ivr"]["timeout"]?></h2>
      <p class="blockintro"><?=$cwlang["ivr"]["timeoutDesc"]?></p>
      <div class="block">
        <table cellpadding="0" cellspacing="0">
          <tr>
            <td class="required"><label for="node[taudio]"><?=$cwlang["ivr"]["audio"]?>:</label></td>
            <td><? echo getAudioPanel("taudio",$taudio,$js); ?></td>
          </tr>
          <tr>
            <td class="required"><label for="node[trepeat]"><?=$cwlang["ivr"]["repeat"]?>:</label></td>
            <td><input type="checkbox" id="node[trepeat]" name="node[trepeat]" value="1" <?if ($trepeat==1) echo "checked=\"checked\" "?>/>
              <?=$cwlang["ivr"]["repeatHelp"]?> <?=$name?>
            </td>
          </tr>
        </table>
      </div>      
  
  
      <h2><?=$cwlang["ivr"]["invalid"]?></h2>
      <p class="blockintro"><?=$cwlang["ivr"]["invalidHelp"]?></p>
      <div class="block">
        <table cellpadding="0" cellspacing="0">
          <tr>
            <td class="required"><label for="node[iaudio]"><?=$cwlang["ivr"]["audio"]?>:</label></td>
            <td><?echo getAudioPanel("iaudio",$iaudio,$js); ?></td>
          </tr>
          <tr>
            <td class="required"><label for="node[irepeat]"><?=$cwlang["ivr"]["repeat"]?>:</label></td>
            <td><input type="checkbox" id="node[irepeat]" name="node[irepeat]" value="1" <?if ($irepeat==1) echo "checked=\"checked\" "?> />
              <?=$cwlang["ivr"]["repeatHelp"]?> <?=$name?>
            </td>
          </tr>
        </table>
      </div>      
  
  <?      }  ?>        
          

      <div class="action">
        <input type="hidden" name="node[id]" value="<?=$nodeId?>" />
        <input type="hidden" name="node[parent]" value="<?=$parent?>" />
        <input type="hidden" name="choosedIvr" value="<?=$_REQUEST["choosedIvr"]?>" />
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='index.php?choosedIvr=<?=$_REQUEST["choosedIvr"]?>'; return false;"/>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
      </div>
    </form>
   </div>  

  </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>
  <?  
  $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');
?>
