<?php
/* **
 * @application PhpMyShopping
 * @part        Admin boutique
 * @file        admin.php
 * @path        ./admin/
 * @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
 */

if ((!isset($_SESSION['LoginOk']) or $_SESSION['LoginOk'] != 'IdentOk')) {
    header('Location: ./login.php');
}

switch ($C3) {
/** -------------------------------------------------------------------------------------
 * Liste des admins
 */
case 'list':
    echo $admin->sous_menu(array(2));
    echo $admin->listAdmin();
    break;

/** -------------------------------------------------------------------------------------
 * Formulaire d'ajout d'admin
 */
case 'insert':
    echo $admin->sous_menu(array(1));
    echo $admin->formAdmin();
    break;

/** -------------------------------------------------------------------------------------
 * Enregistrement de l'admin dans la table ShopAdmin
 */
case 'insert_ok':
    echo $admin->sous_menu(array(2));
    $field['FirstName'] = strtoupper($admin->remplace_car($_POST['FirstName']));
    $field['LastName']  = ucwords($admin->remplace_car($_POST['LastName']));
    $field['UserName']  = strtolower($admin->remplace_car($_POST['UserName']));
    $field['ID_Gender'] = $_POST['ID_Gender'];
    $field['Status']    = $_POST['Status'];

    if ((!$_POST['Password']) or (!$_POST['Password_Confirm']) or ($_POST['Password'] != $_POST['Password_Confirm'])) {
        $data_back = array('FirstName' => $_POST['FirstName'],
                           'LastName'  => $_POST['LastName'],
                           'UserName'  => $_POST['UserName'],
                           'Status'    => $_POST['Status'],
                           'ID_Gender' => $_POST['ID_Gender'],
                           'message'   => '<b class="red">Veuillez resaisir votre mot de passe et le confirmer.</b>');
        echo $admin->formAdmin($_SESSION['ID_Shop'], 0, $data_back);
    } else {
        $field['Password'] = $_POST['Password'];
        $admin->insert_sql('ShopAdmin', $field, 'SHA1');
        echo $admin->listAdmin($_SESSION['ID_Shop']);
    }

    break;

/** -------------------------------------------------------------------------------------
 * Affichage des infos admin en mode edition
 */
case 'edit':
    echo $admin->sous_menu(array(1));

    if (!isset($_GET['ID_ShopAdmin']) || !$_GET['ID_ShopAdmin']) {
        echo $admin->listAdmin();
    } else {
        echo $admin->formAdmin($_GET['ID_ShopAdmin']);
    }

    break;

/** -------------------------------------------------------------------------------------
 * Enregistrement des modifications apportees a l'admin
 */
case 'update':
    echo $admin->sous_menu(array(2));
    $field['FirstName'] = strtoupper($admin->remplace_car($_POST['FirstName']));
    $field['LastName']  = ucwords($admin->remplace_car($_POST['LastName']));
    $field['UserName']  = strtolower($admin->remplace_car($_POST['UserName']));
    $field['ID_Gender'] = $_POST['ID_Gender'];
    $field['Status']    = $_POST['Status'];

    $newField = array('FirstName' => $field['FirstName'],
                      'LastName'  => $field['LastName'],
                      'UserName'  => $field['UserName'],
                      'Status'    => $field['Status'],
                      'ID_Gender' => $field['ID_Gender']);

    if ((!$_POST['Password']) && (!$_POST['Password_Confirm'])) {
        /**
         * Pas de changement au niveau du password
         */
        $admin->update_sql('ShopAdmin', $newField, $_POST['ID_ShopAdmin'], 'SHA1');
        echo $admin->listAdmin();
    } elseif (($_POST['Password']) and ($_POST['Password_Confirm'])) {
        /**
         * Modification du password
         */
        if ($_POST['Password'] != $_POST['Password_Confirm']) {
            /**
             * Les deux passwords ne sont pas identiques
             */
            $newField['message'] = '<b class="red">Vos mots de passe sont diff&eacute;rents.</b>';
            echo $admin->formAdmin($_POST['ID_ShopAdmin'], $newField);
        } else {
            $field['Password'] = $_POST['Password'];
            $admin->update_sql('ShopAdmin', $field, $_POST['ID_ShopAdmin'], 'SHA1');
            echo $admin->listAdmin();
        }
    } else {
        $newField['message'] = '<b class="red">Vos mots de passe sont diff&eacute;rents.</b>';
        echo $admin->formAdmin($_POST['ID_ShopAdmin'], $newField);
    }

    break;

/** -------------------------------------------------------------------------------------
 * Demande de confirmation avant suppression de l'admin
 */
case 'delete':
    echo $admin->sous_menu(array(1));
    echo '
    <p class="message_confirm">Souhaitez-vous vraiment effacer cet admin ?<br /><br />
      [<a class="lien_admin" href="?C1=AD&C2=AD&C3=delete_ok&ID_ShopAdmin='.$_GET['ID_ShopAdmin'].'">Oui</a>]
      <b>::</b>
      [<a class="lien_admin" href="?C1=AD&C2=AD&C3=list">Non</a>]
    </p>';
    break;

/** -------------------------------------------------------------------------------------
 * Suppression de l'admin
 */
case 'delete_ok':
    echo $admin->sous_menu(array(2));
    $admin->delete_tuple('ShopAdmin', 'ID_ShopAdmin', $_GET['ID_ShopAdmin']);
    echo $admin->listAdmin();

    break;

/** -------------------------------------------------------------------------------------
 * Affichage par defaut de la liste des admins
 */
default:
    echo $admin->sous_menu(array(2));
    echo $admin->listAdmin();
    break;
}
?>
