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

    //errArr($_POST["code"]);
  
    $entry=Array(
      "id" => $_POST["id"],
      "name" => $_POST["name"],
      "label" => $_POST["label"],
      "description" => $_POST["description"], 
      "parameter" => $_POST["parameter"], 
      "code" => $_POST["code"]  
    ); 
    
    //save the group informations
    $ret=$asterisk->putMacro($entry);
    if ($ret>0) $alert="<div class=\"AlertGood\">".T_("The macro has been saved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("The macro has been saved")."</div>";
    goToMainPage($alert);
  }
  //--------------------------------------------------------------------------
  
  $pageTitle="New Macro";
  $in_use=0;
  $ret=$asterisk->getMacro(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach($ret as $m) {
    if($m['in_use']==1) $lock="<img width=\"12\" src=\"$relBasePath/public/img/lock.gif\" alt=\"Locked\" />"; else $lock="";
    $macroList.="<li><a href=\".?id=".$m["id"]."\">".$m["label"]."</a> $lock </li>";

    //if modify, read the group informations
    if (isset($_GET["id"]) and $m["id"]==$_GET["id"]) {
      $id=$m["id"];
      $name=$m["name"];
      $label=$m["label"];
      $description=$m["description"];
      $parameter=$m["parameter"];
      $code=$m["code"];
      $in_use=$m['in_use'];
      $pageTitle="Edit macro $label";
    }  
  }
  $locked="<img width=\"12\" style=\"vertical-align:top;\" src=\"$relBasePath/public/img/lock.gif\" alt=\"Locked\" />";
  //================ MAIN CONTENTS ================
  ob_start();
  ?>

  <?$sel="macro";include("../../navigation.php");?>
  
  <div class="SectionHeader">
    <h1><?=$pageTitle?></h1>
    <h3><a href="../" class="admin" ><?=T_("Return without save")?></a></h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?>" method="post">
      <p class="blockintro"><?=T_("Edit your the settings of this macro")?></p>
      <div class="block">
        <dl>
          <dt class="required"><?=T_("Name")?>:</dt>
          <dd><input type="textfield" id="name" name="name" value="<?=$name?>" <? if($in_use==1) echo "readonly=\"readonly\"";?>/><? if($in_use==1) echo $locked;  ?></dd>
          <dt class="required"><?=T_("Label")?>:</dt>
          <dd><input type="textfield" id="label" name="label" value="<?=$label?>" <? if($in_use==1) echo "readonly=\"readonly\"";?>/><? if($in_use==1) echo $locked;  ?></dd>
          <dt class="required"><?=T_("Description")?>:</dt>
          <dd><textarea id="description" name="description" style="width: 500px"><?=$description?></textarea></dd>
          <dt class="required"><?=T_("Parameter")?> :</dt>
          <dd>
            <textarea id="parameter" name="parameter"  wrap="off" style="width: 500px; height: 100px" <? if($in_use==1) echo "readonly=\"readonly\"";?>><?=$parameter?></textarea>
            <? if($in_use==1) echo $locked;  ?>
          </dd>
          <dt class="required"><?=T_("Code")?>:</dt>
          <dd><textarea id="code" name="code" wrap="off" style="width: 500px; height: 150px"><?=$code?></textarea></dd>
        </dl>
        <div class="help">
          <br/>
          <?=T_("Note: in parameters, you can specify what input variable can accept this macro.<br/>Use this syntax:")?>
          <?=T_("<blockquote>&lt;param name=\"XXX\" type=\"YYY\" label=\"ZZZ\" description=\"KKK\" /&gt;</blockquote>
          where<br/><strong>name</strong> is the name of parameter,  <br/>
          <strong>label</strong> is the label of parameter, <br/>
          <strong>description</strong> is the text description of parameter, <br/>
          <strong>type</strong> is one of these values: QUEUE, GROUP, EXTENSION, 
          IVR, TEXTFIELD, EMAIL_EXTENSION, OUTGOING_RULE, INCOMING_RULE, SOUNDFILE, TRUNK, CONFERENCE <br/>
          Look at this example:
          
            <blockquote>&lt;param name=\"dest\" type=\"IVR\" label=\"Play this IVR\" description=\"bla bla\" /&gt;</blockquote>")?>
            
        </div>
        <input type="hidden" name="id" value="<?=$id?>" />
      </div>
      <div class="action">
        <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><?=T_("Informations")?></h1>
          <p><?=T_("This page allows you to create a new macro or to modify an existing one. To create a new macro you have to define the name, the parameters, the source code and to press save button on the end of the page. If macro is included in incoming rules or applications it will not be possible to change name, label and parameters.")?></p>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

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

