<?
/**
 * VoiceOne Settings/Groups/edit
 *
 * Frontend to provide access to the groups system in voiceone.
 * @package voiceone
 * @subpackage  settings_groups
 */
 
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage,$relBasePath;    
 	sajax_init();

  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $data=$_POST["group"];
    $entry=Array(
            "id" =>   $data["id"],
            "type" => $data["type"],
            "name" => $data["name"]
        );
    if (is_array($_POST["entity"]))
    foreach($_POST["entity"] as $e) $entry["entity"][]=$e;
    //echo "<pre>";print_r($entry);phpinfo();die;
    $ret=$asterisk->putGroup($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".$cwlang["group"]["alertDelGood"]."</div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".$cwlang["group"]["alertDelBad"]."</div>";
    
    goToMainPage($alert);
  }
  
  $imgClient="<img src=\"$relBasePath/public/img/client.gif\" alt=\"client\" />";
  $imgDep="<img src=\"$relBasePath/public/img/dep.gif\" alt=\"department\" />";
  $imgRes="<img src=\"$relBasePath/public/img/res.gif\" alt=\"resource\" />";
  $imgNum="<img src=\"$relBasePath/public/img/number.gif\" alt=\"phone number\" />";

  if ($_GET["id"]!="") {
    //retrieve the group informations
    $grp=$asterisk->getGroup($_GET["id"],0);
    $grptype=$grp[0]["type"];
    $grpname=$grp[0]["name"];
    $grpentity=$grp[0]["entity"];
    $grpright=$grp[0]["right"];
    $title=$cwlang["group"]["modGroup"]." $grpname";
  } else {
    $title=$cwlang["group"]["newGroup"];
    $grptype=GROUP_OF_EXTENSION; //set the default group type
  }
  
  //retrieve the extensions information
  $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"];
    $callerid=$v["name"]." ".$v["surname"]." &lt;".$v["extension"]."&gt;";
    $technology=$v["technology"];
    $prot=getIcon($technology);
    if (@in_array($id, $grpentity)) $chk="checked='checked'"; else $chk=""; 
    $html_client.="<td> <input type=\"checkbox\" name=\"entity[]\" value=\"$id\" $chk/> $prot $callerid </td>\n";
    if ((++$count)%2==0) $html_client.="</tr><tr>\n";
  }

/*
  //retrieve the rights information
  $ret=$asterisk->getRight(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if (is_array($ret))
  foreach($ret as $v) {
    $id=$v["id"];
    $name=$v["name"];
    if (@in_array($id, $grpright)) $chk="checked='checked'"; else $chk=""; 
    $html_right.="<td> <input type=\"checkbox\" name=\"right[]\" value=\"$id\" $chk/> $name </td>\n";
    if ((++$count)%2==0) $html_client.="</tr><tr>\n";
  }
*/
  
  //retrieve the department information
  $ret=$asterisk->getOffice(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if (is_array($ret))
  foreach($ret as $v) {
    $id=$v["id"];
    $name=$v["name"];
    $technology=$v["technology"];
    $prot=getIcon($technology);
    if (@in_array($id, $grpentity)) $chk="checked='checked'"; else $chk=""; 
    $html_dep.="<td> <input type=\"checkbox\" name=\"entity[]\" value=\"$id\" $chk/> $prot $name</td>\n";
    if ((++$count)%2==0) $html_dep.="</tr><tr>\n";
  }

  //retrieve the resource information
  $ret=$asterisk->getProvider(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"];
    $technology=$v["technology"];
    $prot=getIcon($technology);
    if (@in_array($id, $grpentity)) $chk="checked='checked'"; else $chk=""; 
    $html_res.="<td> <input type=\"checkbox\" name=\"entity[]\" value=\"$id\" $chk/> $prot $name </td>\n";
    if ((++$count)%2==0) $html_res.="</tr><tr>\n";
  }

  $ret=$asterisk->getPhoneNumber(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if (is_array($ret))
  foreach($ret as $v) {
    $id=$v["id"];
    $name=$v["name"];
    if (@in_array($id, $grpentity)) $chk="checked='checked'"; else $chk=""; 
    $html_num.="<td> <input type=\"checkbox\" name=\"entity[]\" value=\"$id\" $chk/> $imgNum $name </td>\n";
    if ((++$count)%2==0) $html_num.="</tr><tr>\n";
  }
  
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$title?></h1>
    <h3>
      <a href="../" class="admin" ><?=$cwlang["group"]["return"]?></a> 
    </h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?><? if ($_GET["id"]!="") echo "?id=".$_GET["id"]; ?>" method="post">
    	<h2><?=$cwlang["group"]["type"]?></h2>
      <p class="blockintro"><?=$cwlang["group"]["typeDesc"]?></p>
      <table>
        <tr>
          <td width="25%">
            <div id="client_container" class="block button <?if ($grptype==GROUP_OF_EXTENSION) echo 'blockover';?>" onmouseover="groupSwitch('client')" onmouseout="groupRestore()" onclick="groupCheck('client')">
              <input type="radio" name="type" value="<?=GROUP_OF_EXTENSION?>" id="client" <?if ($grptype==GROUP_OF_EXTENSION) echo "checked='checked'";?>/> 
              <label for="type"><?=$imgClient?> <?=$cwlang["group"]["extension"]?></label>
            </div>
          </td>
          <td width="25%">
            <div id="dep_container" class="block button <?if ($grptype==GROUP_OF_OFFICE) echo 'blockover';?>" onmouseover="groupSwitch('dep')" onmouseout="groupRestore()" onclick="groupCheck('dep')">
              <input type="radio" id="dep" name="type" value="<?=GROUP_OF_OFFICE?>" <?if ($grptype==GROUP_OF_OFFICE) echo "checked='checked'";?>/> 
              <label for="type"><?=$imgDep?> <?=$cwlang["group"]["department"]?></label>
            </div>
          </td>
          <td width="25%">
            <div id="res_container" class="block button <?if ($grptype==GROUP_OF_RESOURCE) echo 'blockover';?>" onmouseover="groupSwitch('res')" onmouseout="groupRestore()" onclick="groupCheck('res')">
              <input type="radio" id="res" name="type" value="<?=GROUP_OF_RESOURCE?>" <?if ($grptype==GROUP_OF_RESOURCE) echo "checked='checked'";?>/> 
              <label for="type"><?=$imgRes?> <?=$cwlang["group"]["provider"]?></label>
            </div>
          </td>
          <td width="25%">
            <div id="num_container" class="block button <?if ($grptype==GROUP_OF_PHONENUMBER) echo 'blockover';?>" onmouseover="groupSwitch('num')" onmouseout="groupRestore()" onclick="groupCheck('num')">
              <input type="radio" id="num" name="type" value="<?=GROUP_OF_PHONENUMBER?>" <?if ($grptype==GROUP_OF_PHONENUMBER) echo "checked='checked'";?>/> 
              <label for="type"><?=$imgNum?> <?=$cwlang["group"]["phonenumber"]?></label>
            </div>
          </td>
        </tr>
      </table>

      
      <div id="client_form" style="display: <?if ($grptype==GROUP_OF_EXTENSION) echo 'block'; else echo 'none';?>" >
        <h2><?=$cwlang["group"]["extensionGroup"]?></h2>
        <p class="blockintro"><?=$cwlang["group"]["checkExtension"]?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="group[name]"><?=$cwlang["group"]["name"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="group[name]" name="group[name]" value="<?=$grpname?>" /></dd>
            <?=$cwlang["group"]["extensionNameHelp"]?>
          </dl>
          <p class="blockintro"><?=$cwlang["group"]["checkExtension2"]?></p>
          <table class="todolist" >
            <tr><?=$html_client?></tr>
          </table>
          <input type="hidden" name="group[type]" value="<?=GROUP_OF_EXTENSION?>" />
        </div>
      </div>      

      <div id="dep_form" style="display: <?if ($grptype==GROUP_OF_OFFICE) echo 'block'; else echo 'none';?>" >
        <h2><?=$cwlang["group"]["deptGroup"]?></h2>
        <p class="blockintro"><?=$cwlang["group"]["checkDept"]?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="group[name]"><?=$cwlang["group"]["name"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="group[name]" name="group[name]" value="<?=$grpname?>" /></dd>
            <?=$cwlang["group"]["deptNameHelp"]?>
          </dl>
          <p class="blockintro"><?=$cwlang["group"]["checkDept2"]?></p>
          <table class="todolist" >
            <tr><?=$html_dep?></tr>
          </table>
          <input type="hidden" name="group[type]" value="<?=GROUP_OF_OFFICE?>" />
        </div>
      </div>      

      <div id="res_form"  style="display: <?if ($grptype==GROUP_OF_RESOURCE) echo 'block'; else echo 'none';?>" >
        <h2><?=$cwlang["group"]["providerGroup"]?></h2>
        <p class="blockintro"><?=$cwlang["group"]["checkProvider"]?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="group[name]"><?=$cwlang["group"]["name"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="group[name]" name="group[name]" value="<?=$grpname?>" /></dd>
            <?=$cwlang["group"]["providerNameHelp"]?>
          </dl>
          <p class="blockintro"><?=$cwlang["group"]["checkProvider2"]?></p>
          <table class="todolist" >
            <tr><?=$html_res?></tr>
          </table>
          <input type="hidden" name="group[type]" value="<?=GROUP_OF_RESOURCE?>" />
        </div>
      </div>      
      
      <div id="num_form"  style="display: <?if ($grptype==GROUP_OF_PHONENUMBER) echo 'block'; else echo 'none';?>" >
        <h2><?=$cwlang["group"]["phoneNumberGroup"]?></h2>
        <p class="blockintro"><?=$cwlang["group"]["checkPhoneNumber"]?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="group[name]"><?=$cwlang["group"]["name"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="group[name]" name="group[name]" value="<?=$grpname?>" /></dd>
            <?=$cwlang["group"]["phoneNumberNameHelp"]?>
          </dl>
          <p class="blockintro"><?=$cwlang["group"]["checkPhoneNumber2"]?></p>
          <table class="todolist" >
            <tr><?=$html_num?></tr>
          </table>
          <input type="hidden" name="group[type]" value="<?=GROUP_OF_PHONENUMBER?>" />
        </div>
      </div>      
      
      <div class="action">
        <? if ($_GET["id"]!="") {?><input type="hidden" name="group[id]" value="<?=$_GET["id"]?>" /><?}?>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
      </div>
    </form>
  </div>  
  <script>
        window.onload=function() {
          switchEnable(document.getElementById('client_form'), true, 1);
          switchEnable(document.getElementById('dep_form'), true, 1);
          switchEnable(document.getElementById('res_form'), true, 1);
          switchEnable(document.getElementById('num_form'), true, 1);
          <?
            if ($grptype==GROUP_OF_EXTENSION) $txt="client";
            else if ($grptype==GROUP_OF_OFFICE) $txt="dep";
            else if ($grptype==GROUP_OF_RESOURCE) $txt="res";
            else if ($grptype==GROUP_OF_PHONENUMBER) $txt="num";
            if ($txt!="") echo "switchEnable(document.getElementById('".$txt."_form'), false, 1);\n";
          ?>
        }
  </script>
  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["group"]["editSidebar"]?></h1>
          <?=$cwlang["group"]["editSidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

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

