<?
/**
 * VoiceOne Settings/Extensions
 *
 * Frontend to provide access to the extensions system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 */
 
  include ("../../admin/config/cfg.php");
  global $cwlang,$cwUserId,$cwUserInfo;
  
  //================ POST & GET =================
  $alert=getAlert();
  if((isset($_GET["del"]))&&($_GET["del"]!="")){
    $ret=$asterisk->delContactGroup($_GET["del"],$cwUserId);
    //set return
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Group of Contacts successfully deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Delete failed")."</div>";
    goToMainPage($alert,"./");
  }
  //================ PAGE LOGIC ================
  //Get group list
  $ret=$asterisk->getContactGroup(0,$cwUserId);
  if ($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if(is_array($ret)){
    $html.="<tr>";
    foreach($ret as $v) {
      $id=$v["id"];
      $name=$v["name"];
      $entityArr=$v["entity"];
      if(is_array($entityArr)) $entityCount=count($entityArr); else $entityCount=0;
      $icon="<img src=\"$relBasePath/public/img/icon_group.gif\" alt=\"Contact Groups\" />"; 
      $erasable=$asterisk->isContactGroupErasable($id,$cwUserId);
      if($erasable>0) $del="<a href=\"$PHP_SELF?del=$id\" onclick=\"if(confirm('".sprintf(T_("Are you sure to remove the group: %s ?"),$name )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a>";
      else $del="<img src=\"$relBasePath/public/img/lock.gif\" alt=\"".T_("Group includes one or more contacts not modifiable")."\"/>";
      $html.="<td>$icon <a href=\"edit/?id=$id\">$name</a> ($entityCount) $del</td>\n";
      if ((++$count)%2==0) $html.="</tr><tr>\n";
    }
    if(($count!=0)&&(($count%2)!=0)) $html.="<td colspan=\"".($count%2)."\">&nbsp;</td>\n";
    $html.="</tr>";
  }
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Groups of Contacts")?></h1>
    <h3>
      <a href="edit/" class="admin" ><?=T_("New Groups")?></a> 
    </h3>
  </div>
  <table class="todolist">
    <?=$html?>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="groups";include("../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
      <p><?=T_("This page shows the contacts groups configured on the system. 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 style="text-align: center"><img src="<?=$relBasePath?>public/img/book.jpg" alt="Rubrica" align="center"/></div>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();
  
  //================ SETTINGS ================
  $smarty->assign("section", "contacts");
  $smarty->assign("subsection", "");
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('main.tpl');
?>

