<?
/**
 * 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;
  
  $alert=getAlert();
  //================ POST AND GET ================
  //Delete group
  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delGroup($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("The group has been deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("The group hasn't been deleted")."</div>";
    goToMainPage($alert,"./");
  }
  //================ PAGE LOGIC ================
  //Get group list
  $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/icon_group.gif\" alt=\"client\" />"; $sectionName=T_("Extensions"); }
    if ($type==GROUP_OF_OFFICE)     {$prot="<img src=\"$relBasePath/public/img/icon_offices.gif\" alt=\"department\" />"; $sectionName=T_("Office"); }
    if ($type==GROUP_OF_RESOURCE)   {$prot="<img src=\"$relBasePath/public/img/icon_resurces.gif\" alt=\"resource\" />"; $sectionName=T_("Provider"); }
    if ($type==GROUP_OF_PHONENUMBER){$prot="<img src=\"$relBasePath/public/img/icon_numbers.gif\" alt=\"Phone Number\" />"; $sectionName=T_("Number"); }
    if ($type==GROUP_OF_APPLICATION){$prot="<img src=\"$relBasePath/public/img/icon_appls.gif\" alt=\"Application\" />"; $sectionName=T_("Application"); }
    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><?=T_("Groups")?></h1>
    <h3>
      <a href="edit/" class="admin" ><?=T_("New group")?></a> 
    </h3>
  </div>
  <table class="todolist">
    <tr>
      <?=$html?>
    </tr>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=T_("Informations")?></h1>
          <p><?=T_("This page shows the groups configured on the system, sorted by type of grouped entities. To create a new group follow the link under the section title, otherwise click on an existing group to edit it or on the basket on the right of the name to delete it.")?></p>
        </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');
?>

