<?
/**
 * VoiceOne Settings/Groups
 *
 * Frontend to provide access to the groups system in voiceone.
 * @package voiceone
 * @subpackage  settings_groups
 */
 
  include ("../../admin/config/cfg.php");
  global $cwlang;
  
  //================ PAGE LOGIC ================
  $alert=getAlert();
  
  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delGroup($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["group"]["alertDelGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["group"]["alertDelBad"]."</div>";
  }
  
  $ret=$asterisk->getGroup(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"];
    $type=$v["type"];
    $metagroup=$v["metagroup"];
    if ($metagroup==1) continue;
    $entityCount=count($v["entity"]);
    if ($type==GROUP_OF_EXTENSION)     {$prot="<img src=\"$relBasePath/public/img/client.gif\" alt=\"client\" />"; $sectionName=$cwlang["group"]["extension"]; }
    if ($type==GROUP_OF_OFFICE) {$prot="<img src=\"$relBasePath/public/img/dep.gif\" alt=\"department\" />"; $sectionName=$cwlang["group"]["department"]; }
    if ($type==GROUP_OF_RESOURCE)   {$prot="<img src=\"$relBasePath/public/img/res.gif\" alt=\"resource\" />"; $sectionName=$cwlang["group"]["provider"]; }
    if ($type==GROUP_OF_PHONENUMBER){$prot="<img src=\"$relBasePath/public/img/number.gif\" alt=\"Phone Number\" />"; $sectionName=$cwlang["group"]["phonenumber"]; }
    if ($type!=$oldType) {
      if (($count)%2==1) {$html.="<td></td>\n"; $count++;}
      $html.="</tr><tr><td colspan=\"2\" class=\"todolisttitle\">$sectionName</td></tr><tr>\n";
    }
    $html.="<td>$prot <a href=\"edit/?id=$id\">$name</a> ($entityCount) <a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('Are you sure to remove the $name group?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td>\n";
    if ((++$count)%2==0) $html.="</tr><tr>\n";
    $oldType=$type;
  }
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["group"]["title"]?></h1>
    <h3>
      <a href="edit/" class="admin" ><?=$cwlang["group"]["newGroup"]?></a> 
    </h3>
  </div>
  <table class="todolist">
    <tr>
      <?=$html?>
    </tr>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

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

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

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

