<?php
/**
 * User module
 * 
 * Display users list in back-office
 * 
 * @package linea21\core\user
 * @author $Author$ - linea21 <info@linea21.com>
 * @version $Id$ 
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

//////////// Check Inclusion de pages ////////////
if (!class_exists('auth')) {
  include_once("../lib/lib_common.php");
  ReloadIndex('admin');
}
////////////

//////////// Check Droits utilisateur ////////////
if (!$l21auth->isSuperAdmin()) ReloadIndex('admin');
////////////


if(!isset($_GET['groupe']) || $_GET['groupe']==-1) $groupe=-1;
elseif($_GET['groupe']!=-1) {
  $groupe=$_GET['groupe'];
  $alphabeta=-1;
}

if(!isset($_GET['alphabeta']) || $_GET['alphabeta']==-1) $alphabeta=-1;
elseif($_GET['alphabeta']!=-1) {
  $alphabeta=$_GET['alphabeta'];
  $groupe=-1;
}

if(!isset($_GET['debut'])) $debut=0;
else $debut=$_GET['debut'];

if($groupe!=-1 || $alphabeta!=-1) {
  $link_back="<div class=\"filter\"><a href=\"".$rub_link."&amp;todo=list\">"._t('user','all')."</a></div>";
  if($groupe!=-1) {
    $requete_count=SQLCount_getlistuser('GROUP', $groupe);
    $req_user_list=SQL_getlistuser($debut, SELECT_LIMIT, 'GROUP', $groupe);
  }
  if($alphabeta!=-1) {
    $requete_count=SQLCount_getlistuser('ALPHA', $alphabeta);
    $req_user_list=SQL_getlistuser($debut, SELECT_LIMIT, 'ALPHA', $alphabeta);
  }
}
else {
  $link_back='';
  // Liste des utilisateurs par défaut
  $requete_count=SQLCount_getlistuser();
  $req_user_list=SQL_getlistuser($debut);
}
$result_user_list=$sql_object->DBSelect($req_user_list);

$result_nb_req=$sql_object->DBSelect($requete_count);
$count_request=$result_nb_req[0]['num_rows'];



$link_sup=$rub_link."&amp;todo=sup&amp;id=";
$link_mod=$rub_link."&amp;todo=mod&amp;id=";
$link_det=$rub_link."&amp;todo=det&amp;id=";
$link_self=$rub_link."&amp;todo=list&amp;groupe=".$groupe."&amp;alphabeta=".$alphabeta;
$link_list_group=$rub_link."&amp;todo=list&amp;groupe=";
$link_list_alphabetical=$rub_link."&amp;todo=list&amp;alphabeta=";


// Liste des groupes d'utilisateurs
$result_group=$sql_object->DBSelect($req_list_group);
$select_group="<select name=\"group_filter\" id=\"group_filter\">\n";
$select_group.="<option value=\"-1\">"._t('user','selectgroup')."</option>\n";
for($i=0; $i<count($result_group); $i++)  {
  if($groupe==$result_group[$i]['catus_id']) $mask=" selected=\"selected\""; else $mask='';
  $select_group.="\t<option value=\"".$link_list_group.$result_group[$i]['catus_id']."\"".$mask.">".formatText($result_group[$i]['catus_name'])."</option>\n";
}
$select_group.="</select>\n";

$js = "	$('#group_filter').change(function() {
					if($(this).val() != -1) window.location = $(this).val();
				});";

footerAddInlineJS($js);

// Liste alphabetique
$result_alphabetical=$sql_object->DBSelect($req_alphabetical);
$select_alphabetical="<select name=\"alhpabetical_filter\" id=\"alhpabetical_filter\">\n";
$select_alphabetical.="<option value=\"-1\">"._t('user','alphabetical')."</option>\n";
for($i=0; $i<count($result_alphabetical); $i++)  {
  if($alphabeta==$result_alphabetical[$i]['first_letter']) $mask=" selected=\"selected\""; else $mask='';
  $select_alphabetical.="\t<option value=\"".$link_list_alphabetical.$result_alphabetical[$i]['first_letter']."\"".$mask.">".formatText($result_alphabetical[$i]['first_letter'])."</option>\n";
}
$select_alphabetical.="</select>\n";

$js = "	$('#alhpabetical_filter').change(function() {
				if($(this).val() != -1) window.location = $(this).val();
				});";

footerAddInlineJS($js);

//nombre total d'utilisateurs
$result_group=$sql_object->DBSelect($req_nb_inscrit);
$nb_inscrits=$result_group[0]['nb_inscrit'];
$nb_inscrits_display = $nb_inscrits." "._t('user','nb_total')."<br />\n";
if($count_request<=1) $count_request_display = _t('divers','query_result')." : ".$count_request." "._t('divers','record')."<br />\n";
else $count_request_display = _t('divers','query_result')." : ".$count_request." "._t('divers','records')."<br />\n";
?>
<div id="chemin">
<ul>
	<li><a href="index.php" class="chemin_home"><?php echo _t('way','home'); ?></a></li>
	<li><a href="<?php echo $rub_link; ?>" id="chemin_<?php echo $rub; ?>" class="chemin_rub"><?php echo _t('menu','user'); ?></a></li>
	<li><span><?php echo formatNavTitle(_t('user','list')); ?></span></li>
</ul>
</div>
<!--end barre haute -->

<div class="contentcontainer"><?php	include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));	?>
<div id="content"><?php
echo $link_back;
?>
<div class="filterselect"><?php
echo $select_group;
echo $select_alphabetical;

?></div>
<p><?php
echo $nb_inscrits_display;
echo $count_request_display;
?></p>
<?php
echo linkin_page($link_self, $count_request, $debut);

if($result_user_list!=0) {
  echo "<table class=\"multisort\">\n";
  echo "<thead>\n";
  echo "<tr>\n";
  echo "<td>".mb_ucfirst(_t('user','login'))."</td>\n";
  echo "<td>".mb_ucfirst(_t('user','first_name'))."</td>\n";
  echo "<td>".ucfirst(_t('user','last_name'))."</td>\n";
  echo "<td>".mb_ucfirst(_t('user','groupe'))."</td>\n";
  echo "<td>".mb_ucfirst(_t('user','date_record'))."</td>\n";
  echo "<td class=\"sorter-false\">&nbsp;<span class=\"visually-hidden\">".mb_ucfirst(_t('user','actions'))."</span></td>\n";
  echo "</tr>\n";
  echo "</thead>\n";

  echo "<tbody>\n";
  for($i=0; $i<count($result_user_list); $i++) {

  	if($result_user_list[$i]['user_password'] == '@ldap_forward') {
  		$suffix= ' <i class="icon-universal-access"></i>';
  	} else {
  		$suffix= '';
  	}
    echo "<tr>\n";
    echo "<td><a href=\"".$link_det.$result_user_list[$i]['user_id']."\" title=\"".formatText(_t('user','det'))."\">".formatText($result_user_list[$i]['user_login']) . $suffix . "</a></td>\n";
    echo "<td>".formatText($result_user_list[$i]['profile_firstname']). "</td>\n";
    echo "<td>".formatText($result_user_list[$i]['profile_lastname'])."</td>\n";
    echo "<td>".formatText($result_user_list[$i]['catus_name'])."</td>\n";
    echo "<td>".$result_user_list[$i]['user_date_crea_display']."</td>\n";
    echo "<td>\n";
    echo "<div class=\"iconetab\">";
    echo "\t<a href=\"".$link_sup.$result_user_list[$i]['user_id']."\" class=\"ico_sup\" title=\""._t('user','sup')."\"><i class=\"icon-trash\"></i><span>" . _t('user','sup') . "</span></a>\n";
    echo "\t<a href=\"".$link_mod.$result_user_list[$i]['user_id']."\" class=\"ico_mod\" title=\""._t('user','mod')."\"><i class=\"icon-pencil\"></i><span>" . _t('user','mod') . "</span></a>\n";
    echo "</div>";
    echo "</td>\n";
    echo "</tr>\n";
  }
  echo "</tbody>";
  echo "</table>";
} else {
  echo "<div class=\"info\">"._t('user','noresult')."</div>";
}

echo linkin_page($link_self, $count_request, $debut);

// Ability to sort table
addDynamicCSS('../lib/js/jquery.tablesorter/theme.default.css');
footerAddJS('../lib/js/jquery.tablesorter/jquery.tablesorter.min.js');
$str = '$("table.multisort").tablesorter({	});';
footerAddInlineJS($str);
?>
</div> <!-- closing #content -->
</div> <!-- closing .contentcontainer -->