<?php
/* **
 * @application PhpMyShopping
 * @part        Admin Boutique (AB)
 * @file        class.admin.php
 * @path        ./admin/include/
 * @author      Stephane DINE <stephane@phpmyshopping.org>
 * @site        http://www.phpmyshopping.org
 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
 *
 * PHP version 5
 *
 * Classe Admin
 */

require_once 'class.lib.php';

class Admin extends Lib
{


    /** *****************************************************************************************************************************
     * Constructeur
     *
     * @access public
     */
    public function __construct()
    {
    }




    /** *****************************************************************************************************************************
     * Methode listAdmin
     *
     * @desc   Liste des administrateurs
     * @return srting
     * @access public
     */
    public function listAdmin()
    {
        $list = '
        <div class="heading">Liste des admins</div>
        <div id="list">
        <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="60%">
          <tr bgcolor="#F1F3F5">
            <th width="63%">Pr&eacute;nom - nom</th>
            <th width="25%">Identifiant</th>
            <th width="1%">Status</th>
            <th width="5%" align="center">Modifier</th>
            <th width="5%" align="center">Effacer</th>
          </tr>';

        /**
         * Recuperation des donnees pour la creation de la liste
         */
        $sql   = 'SELECT * FROM ShopAdmin ORDER BY Username ASC';
        $query = $this->query($sql);
        $nb    = mysql_num_rows($query);

        while ($result = mysql_fetch_array($query)) {
            if ($result['Status'] == 'L') {
                $status = '<b class="violet">Bloqu&eacute;</b>';
            } elseif ($result['Status'] == 'D') {
                $status = '<b class="red">Effac&eacute;</b>';
            } else {
                $status = '<b class="green">Admin</b>';
            }

            if ($result['ID_ShopAdmin'] > 1) {
                $deleteLink = '<a href="?C1=AD&C2=AD&C3=delete&ID_ShopAdmin='.$result['ID_ShopAdmin'].'"><img src="./images/delete.png" alt="Del" title="Efacer cet admin" /></a>';
            } else {
                $deleteLink = '&nbsp;';
            }

            $list .= '
            <tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
              <td align="left">&nbsp;'.$result['FirstName'].' '.$result['LastName'].'&nbsp;</td>
              <td align="left">&nbsp;'.$result['UserName'].'&nbsp;</td>
              <td align="center">'.$status.'</td>
              <td align="center"><a href="?C1=AD&C2=AD&C3=edit&ID_ShopAdmin='.$result['ID_ShopAdmin'].'"><img src="./images/update.png" alt="Edit" title="Editer cet admin" /></a></td>
              <td align="center">'.$deleteLink.'</td>
            </tr>';
        }

        $list .= '
        </table>
        </div>';

        return $list;
    }




    /** ****************************************************************************************************
     * Methode formAdmin
     *
     * @desc   Formulaire de saisie/modification d'un admin
     * @param  int   $ID_ShopAdmin Identifiant de l'admin de boutique
     * @param  array $data_back    Donnees de retour en cas d'eeruer dans la saisie
     * @return string
     * @access public
     */
    public function formAdmin($ID_ShopAdmin = 0, $data_back = '')
    {
        if ($ID_ShopAdmin > 0) {
            /**
             * Modification d'un admin - recuperation des donnees
             */
            $sqlNbUser       = 'SELECT COUNT(ID_ShopAdmin) AS NbUser FROM ShopAdmin';
            $queryNbUser     = $this->query($sqlNbUser);
            $resultNbUser    = mysql_fetch_array($queryNbUser);
            $nbUser          = $resultNbUser['NbUser'];

            $sql             = 'SELECT * FROM ShopAdmin WHERE ID_ShopAdmin = '.$ID_ShopAdmin;
            $query           = $this->query($sql);
            $result          = mysql_fetch_array($query);

            $selected        = 'selected_'.$result['Status'];
            $$selected       = ' selected="selected"';
            $C3              = 'update';
            $hidden_action   = '<input type="hidden" name="ID_ShopAdmin" value="'.$result['ID_ShopAdmin'].'">';
            $error_password  = 'Confirmation du mot de passe :';
            $text_empty_pass = '&nbsp;(Laisser vide si pas de modification)';
            $title           = 'Modification donn&eacute;es admin';
            $statusFields    = '<option value="D"'.$selected_D.'>Compte effac&eacute;</option>';

            if ($data_back) {
                $result['FirstName'] = $data_back['FirstName'];
                $result['LastName']  = $data_back['LastName'];
                $result['UserName']  = $data_back['UserName'];
                $result['Status']    = $data_back['Status'];
                $class_error_password = 'class="input_error"';
                $error_password = '<b class="red">Les deux mots de passe sont diff&eacute;rent</b>';
            }
        } else {
            /**
             * Ajout d'un admin
             */
            $C3              = 'insert_ok';
            $hidden_action   = '';
            $selected_A      = ' selected="selected"';
            $error_password  = 'Confirmez le mot de passe :';
            $text_empty_pass = '';
            $title           = 'Cr&eacute;ation d\'un admin';
            $statusFields    = '';
            $nbUser          = 2;

            if ($data_back) {
                $result['FirstName'] = $data_back['FirstName'];
                $result['LastName']  = $data_back['LastName'];
                $result['UserName']  = $data_back['UserName'];
                $result['Status']    = $data_back['Status'];
                $message             = $data_back['message'];
            }
        }

        $form = '
        <div class="heading">'.$title.'</div>
        <div id="list">

        <form action="" method="POST" id="formAdmin">
          <input type="hidden" name="C1" value="AD" />
          <input type="hidden" name="C2" value="AD" />
          <input type="hidden" name="C3" value="'.$C3.'" />
          '.$hidden_action.'

          <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="700">
            <tr bgcolor="#F1F3F5">
              <td colspan="2" class="h1" align="left">&nbsp;</td>
            </tr>
            <tr bgcolor="#FFFFFF">
              <td align="right">Civilit&eacute; :</td>
              <td align="left">'.$this->liste_select('ID_Gender', array('ID_Gender', 'GenderName'), 'Gender', 'GenderName', $result['ID_Gender']).'</td>
            </tr>';
            $form .= $this->create_field('Nom :', array('R','T','L','M','FirstName'), 'input', 'text', 'FirstName', 30, 20, $result['FirstName']);
            $form .= $this->create_field('Pr&eacute;nom :', array('R','T','L','M','LastName'), 'input', 'text', 'LastName', 30, 20, $result['LastName']);
            $form .= $this->create_field('Nom d\'utilisateur :', array('R','T','L','M','UserName'), 'input', 'text', 'UserName', 30, 20, $result['UserName']);
            $form .= $this->create_field('Mot de passe :', array('R','T','L','M','Password'), 'input', 'password', 'Password', 30, 30, '', 0, 0, $message);
            $form .= '
            <tr bgcolor="#FFFFFF">
              <td align="right">'.$error_password.'</td>
              <td><input '.$class_error_password.' type="password" name="Password_Confirm" id="Password_Confirm" size="30" maxlength="30">'.$text_empty_pass.'</td>
            </tr>';

        /**
         * Verification si un seul compte user : pas possible de bloquer ou de supprimer le compte
         */
        if ($nbUser > 1) {
            $form .= '
            <tr bgcolor="#FFFFFF">
              <td align="right">Status :</td>
              <td align="left">
                <select name="Status">
                  <option value="A"'.$selected_A.'>Administrateur</option>
                  <option value="L"'.$selected_L.'>Compte bloqu&eacute;</option>
                  '.$statusFields.'
                </select>
              </td>
            </tr>';
        } else {
            $form .= '<input type="hidden" name="Status" value="'.$result['Status'].'" />';
        }

        $form .= '
            <tr bgcolor="#F1F3F5">
              <td colspan="2" align="center">
                <a href="#" onClick="checkForm(\'formAdmin\')" class="formButton">Enregistrer</a>
                <b>::</b>
                <a href="?C1=AD&C2=ADO&C3=list" class="formButton">Annuler</a>
              </td>
            </tr>
          </table>

        </form>
        </div>';

        return $form;
    }

}
?>
