<?php
/* **
 * @application PhpMyShopping
 * @part        Admin Boutique (AB)
 * @file        class.catalog.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 Catalog
 *
 * Gestion des articles, categories, sous-categories et marques de produit
 */

require_once 'class.lib.php';

class Catalog extends Lib
{
	/**
	 * Largeur de l'image affichee dans le formulaire de modification d'une categorie
	 * @access public
	 */
	public $ImgCatWA;

	/**
	 * Hauteur de l'image affichee dans le formulaire de modification d'une categorie
	 * @access public
	 */
	public $ImgCatHA;

	/**
	 * Largeur de l'image affichee dans la liste des categorie
	 * @access public
	 */
	public $ImgCatWM;

	/**
	 * Hauteur de l'image affichee dans la liste des categorie
	 * @access public
	 */
	public $ImgCatHM;

	/**
	 * Largeur maximale de la petite image
	 * @access public
	 */
	public $ImgProWA;

	/**
	 * Hauteur maximale de la petite image
	 * @access public
	 */
	public $ImgProHA;

	/**
	 * Largeur maximale de l'image intermediaire
	 * @access public
	 */
	public $ImgProWN;

	/**
	 * Hauteur maximale de l'image intermediaire
	 * @access public
	 */
	public $ImgProHN;

	/**
	 * Largeur maximale de la grande image
	 * @access public
	 */
	public $ImgProWB;

	/**
	 * Hauteur maximale de la grande image
	 * @access public
	 */
	public $ImgProHB;

	/**
	 * Identifiant de la boutique
	 * @access public
	 */
	public $id_shop;




	/** ****************************************************************************************************
	 * Constructeur
	 */
	public function __construct($nbmax = 0, $urlRewriting = false)
	{
		$this->ImgCatWM = 20;
		$this->ImgCatHM = 20;
		$this->ImgCatWA = 100;
		$this->ImgCatHA = 100;

		$this->ImgProWA = 50;
		$this->ImgProHA = 50;
		$this->ImgProWN = 150; // 160
		$this->ImgProHN = 135; // 135
		$this->ImgProWI = 280;
		$this->ImgProHI = 420;
		$this->ImgProWB = 590; // 550
		$this->ImgProHB = 490; // 450

		$this->nbmax        = $nbmax;
		$this->urlRewriting = $urlRewriting;
		$this->id_shop      = $id_shop;

		include_once '../ident.php';
	}




	/** ****************************************************************************************************
	 * Methode formCategory
	 *
	 * @desc  Methode permettant l'enregistrement et la modification d'une categorie de produit.
	 *        Cette methode prend en parametre l'ID_Category quand il s'agit d'une modification.
	 * @param int $ID_Category Identifiant de la categorie
	 * @return string
	 * @access public
	 */
	public function formCategory($ID_Category = 0)
	{
		if ($ID_Category > 0) {
			/**
			 * Modification d'une categorie
			 */
			$sql           = 'SELECT * FROM Category WHERE ID_Category = '.$ID_Category;
			$query         = $this->query($sql);
			$result        = mysql_fetch_array($query);
			$hidden_action = '<input type="hidden" name="C3" value="update"><input type="hidden" name="champ[ID_Category]" value="'.$result['ID_Category'].'">';
			$selected      = 'selected_'.$result['Status'];
			$$selected     = ' selected="selected"';

			$title = 'Modification d\'une c&eacute;tagorie';
		} else {
			/**
			 * Ajout d'une categorie
			 */
			$hidden_action = '<input type="hidden" name="C3" value="insert_ok">';
			$selected_E    = ' selected="selected"';
			$title         = 'Cr&eacute;ation d\'une cat&eacute;gorie';
		}

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

		<form action="" method="POST" id="formCategory">
		  <input type="hidden" name="C1" value="CA">
		  <input type="hidden" name="C2" value="CA">'.
		  $hidden_action.'

		  <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="400">
			<tr bgcolor="#F1F3F5">
			  <td colspan="2">&nbsp;</td>
			</tr>'.
			$this->create_field('Cat&eacute;gorie :', array('R','T','L','M','CategoryName'), 'input', 'text', 'champ[CategoryName]', 40, 50, $result['CategoryName']);
			$form .= '
			<tr bgcolor="#FFFFFF">
			  <td class="right">Status :</td>
			  <td class="left">
				<select name="champ[Status]">
				  <option value="E"'.$selected_E.'>Cat&eacute;gorie active</option>
				  <option value="H"'.$selected_H.'>Cat&eacute;gorie cach&eacute;e</option>
				</select>
			  </td>
			</tr>
			<tr bgcolor="#F1F3F5">
			  <td colspan="2" align="center">
				<a href="#" onClick="checkForm(\'formCategory\')" class="formButton">Enregistrer</a>
				<b>::</b>
				<a href="?C1=CA&C2=CA&C3=list" class="formButton">Annuler</a>
			  </td>
			</tr>
		  </table>

		</form>
		</div>';

		return $form;
	}




	/** ****************************************************************************************************
	 * Methode listCategory
	 *
	 * @desc   Methode affichant la liste des categorie pour une boutique donnee
	 * @return string
	 * @access public
	 */
	public function listCategory()
	{
		$list = '<div class="heading">Liste des cat&eacute;gories</div>';

		/**
		 * Recuperation des donnees pour la creation de la liste
		 */
		$sql = 'SELECT ID_Category, CategoryName, Status
				FROM Category
				WHERE Status != "D"
				ORDER BY CategoryName ASC';
		$query = $this->query($sql);

		if (mysql_num_rows($query) > 0) {
			$list .= '
			<div id="list">
			<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="500">
			  <tr bgcolor="#F1F3F5">
				<th width="3%" class="center">Status</th>
				<th width="87%" class="left">Cat&eacute;gorie</th>
				<th width="5%" class="center">Modifier</th>
				<th width="5%" class="center">Effacer</th>
			  </tr>';

			while ($result = mysql_fetch_array($query)) {
				/**
				 * Status de la cattegorie
				 */
				if ($result['Status'] == 'E') {
					$status = '<img src="./images/1.png" alt="Active" title="Active" />';
				} elseif ($result['Status'] == 'H') {
					$status = '<img src="./images/0.png" alt="Non active" title="Non active" />';
				}

				/**
				 * Affichage des donnees
				 */
				$list .= '
				<tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
				  <td class="center" nowrap="nowrap">'.$status.'</td>
				  <td class="left">&nbsp;'.$result['CategoryName'].'&nbsp;</td>
				  <td class="center"><a href="?C1=CA&C2=CA&C3=edit&ID_Category='.$result['ID_Category'].'"><img src="./images/update.png" alt="Modifier" title="Modifier" /></a></td>
				  <td class="center"><a href="?C1=CA&C2=CA&C3=delete_confirm&ID_Category='.$result['ID_Category'].'"><img src="./images/delete.png" alt="Effacer" title="Effacer" /></a></td>
				</tr>';
			}

			$list .= '
			</table>
			</div>';
		} else {
			$list .= '<div class="description"><br />Vous n\'avez aucune cat&eacute;gories de cr&eacute;&eacute;es.</div>';
		}
		return $list;
	}




	/** ****************************************************************************************************
	 * Methode verrifProductCategory
	 *
	 * @desc   Verrification des produits ayant pour categorie celle qui sera effacee
	 * @param  int $ID_Category Identifiant de la categorie
	 * @return int/bool
	 * @access public
	 */
	public function verrifProductCategory($ID_Category)
	{
		$sql = 'SELECT COUNT(Product.ID_Product) AS NbProducts
				FROM Product, Category
				WHERE Product.ID_Category = Category.ID_Category
				AND Product.Status != "D"
				AND Category.ID_Category = '.$ID_Category;

		$nbRows = mysql_fetch_array($this->query($sql));

		if ($nbRows['NbProducts'] > 0) {
			/**
			 * Il y a des produits dont il faut modifier l'ID_Category
			 */
			return $nbRows['NbProducts'];
		} else {
			/**
			 * Aucuns produits trouves avec l'ID_Category qui sera efface
			 */
			return false;
		}
	}




	/** ****************************************************************************************************
	 * Methode formSubCategory
	 *
	 * @desc   Methode permettant l'enregistrement et la modification d'une sous-categorie de produit.
	 *         Cette methode prend en parametre l'ID_SubCategory quand il s'agit d'une modification.
	 * @param  int $ID_SubCategory Identifiant de la categorie
	 * @return string
	 * @access public
	 */
	public function formSubCategory($ID_SubCategory = 0)
	{
		if ($ID_SubCategory > 0) {
			/**
			 * Modification d'une sous-categorie
			 *
			 * Recuperation des donnees a modifier
			 */
			$sql = 'SELECT * FROM SubCategory WHERE ID_SubCategory = '.$ID_SubCategory;
			$query = $this->query($sql);
			$result = mysql_fetch_array($query);

			$hidden_action = '<input type="hidden" name="C3" value="update"><input type="hidden" name="champ[ID_SubCategory]" value="'.$result['ID_SubCategory'].'">';
			$selected      = 'selected_'.$result['Status'];
			$$selected     = ' selected="selected"';
			$title         = 'Modification d\'une sous-c&eacute;tagorie';
		} else {
			/**
			 * Ajout d'une sous-categorie
			 */
			$hidden_action = '<input type="hidden" name="C3" value="insert_ok">';
			$selected_E    = ' selected="selected"';
			$title         = 'Cr&eacute;ation d\'une sous-cat&eacute;gorie';
		}

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

		<form action="" method="POST" id="formSubCategory">
		  <input type="hidden" name="C1" value="CA">
		  <input type="hidden" name="C2" value="SC">
		  '.$hidden_action.'

		  <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="400">
			<tr bgcolor="#F1F3F5">
			  <td colspan="2">&nbsp;</td>
			</tr>'.
			$this->create_field('Sous-at&eacute;gorie :', array('R','T','L','M','SubCategoryName'), 'input', 'text', 'champ[SubCategoryName]', 40, 50, $result['SubCategoryName']);
			$form .= '
			<tr bgcolor="#FFFFFF">
			  <td align="right">Status :</td>
			  <td align="left">
				<select name="champ[Status]">
				  <option value="E"'.$selected_E.'>Sous-cat&eacute;gorie active</option>
				  <option value="H"'.$selected_H.'>Sous-c&eacute;gorie cach&eacute;e</option>
				</select>
			  </td>
			</tr>
			<tr bgcolor="#F1F3F5">
			  <td colspan="2" align="center">
				<a href="#" onClick="checkForm(\'formSubCategory\')" class="formButton">Enregistrer</a>
				<b>::</b>
				<a href="?C1=CA&C2=SC&C3=list" class="formButton">Annuler</a>
			  </td>
			</tr>
		  </table>

		</form>

		</div>';

		return $form;
	}




	/** ****************************************************************************************************
	 * Methode listSubCategory
	 *
	 * @desc   Methode affichant la liste des sous-categorie pour une boutique donnee
	 * @return string
	 * @access public
	 */
	public function listSubCategory()
	{
		$list = '<div class="heading">Liste des sous-cat&eacute;gories</div>';

		/**
		 * Recuperation des donnees pour la creation de la liste
		 */
		$sql = 'SELECT ID_SubCategory, SubCategoryName, Status
				FROM SubCategory
				WHERE Status != "D"
				ORDER BY SubCategoryName ASC';
		$query = $this->query($sql);
		$nb = mysql_num_rows($query);

		if ($nb > 0) {
			$list .= '
			<div id="list">
			<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="500">
			  <tr bgcolor="#F1F3F5">
				<th width="3%" class="center">Status</th>
				<th width="87%" class="left">Sous-cat&eacute;gorie</th>
				<th width="5%" class="center">Modifier</th>
				<th width="5%" class="center">Effacer</th>
			  </tr>';

			while ($result = mysql_fetch_array($query)) {
				/**
				 * Status de la sous-cattegorie
				 */
				if ($result['Status'] == 'E') {
					$status = '<img src="./images/1.png" alt="Active" title="Active" />';
				} elseif ($result['Status'] == 'H') {
					$status = '<img src="./images/0.png" alt="Non active" title="Non active" />';
				}

				/**
				 * Affichage des donnees
				 */
				$list .= '
				<tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
				  <td class="center" nowrap="nowrap">'.$status.'</td>
				  <td class="left">&nbsp;'.$result['SubCategoryName'].'&nbsp;</td>
				  <td class="center"><a href="?C1=CA&C2=SC&C3=edit&ID_SubCategory='.$result['ID_SubCategory'].'"><img src="./images/update.png" alt="Modifier" title="Modifier" /></a></td>
				  <td class="center"><a href="?C1=CA&C2=SC&C3=delete_confirm&ID_SubCategory='.$result['ID_SubCategory'].'"><img src="./images/delete.png" alt="Effacer" title="Effacer" /></a></td>
				</tr>';
			}

			$list .= '
			</table>
			</div>';
		} else {
			$list .= '<div class="description"><br />Vous n\'avez aucune sous-cat&eacute;gories de cr&eacute;&eacute;es.</div>';
		}

		return $list;
	}




	/** ****************************************************************************************************
	 * Methode verrifProductSubCategory
	 *
	 * @desc   Verrification des produits ayant pour sous-categorie celle qui sera effacee
	 * @param  int $ID_SubCategory Identifiant de la sous-categorie
	 * @return int/bool
	 * @access public
	 */
	public function verrifProductSubCategory($ID_SubCategory)
	{
		$sql = 'SELECT COUNT(Product.ID_Product) AS NbProducts
				FROM Product, SubCategory
				WHERE Product.ID_SubCategory = SubCategory.ID_SubCategory
				AND Product.Status != "D"
				AND SubCategory.ID_SubCategory = '.$ID_SubCategory;
		$nbRows = mysql_fetch_array($this->query($sql));
		if ($nbRows['NbProducts'] > 0) {
			/**
			 * Il y a des produits dont il faut modifier l'ID_SubCategory
			 */
			return $nbRows['NbProducts'];
		} else {
			/**
			 * Aucuns produits trouves avec l'ID_SubCategory qui sera efface
			 */
			return false;
		}
	}




	/** ****************************************************************************************************
	 * Methode listProduct
	 *
	 * @desc   Liste des produits
	 * @param  int $debut       Nombre servant a la barre de navigation flechee
	 * @param  int $ID_Category Identifiant de la categorie
	 * @return string
	 * @access public
	 */
	public function listProduct($data, $ID_Category = 'X')
	{
		$list = '<div class="heading">Liste des articles</div>';

		/**
		 * Liste des categories pour une boutique donnees
		 */
		$newSql = 'SELECT cat.ID_Category, CategoryName
				   FROM Category AS cat, Product AS pro
				   WHERE cat.ID_Category = pro.ID_Category
				   AND cat.Status != "D"
				   GROUP BY ID_Category
				   ORDER BY CategoryName ASC';
		$queryNumCat = $this->query($newSql);

		if (mysql_num_rows($queryNumCat) > 1) {
			$listCategory = '<br />
				<form action="" method="GET" name="categoryList">
				  &nbsp;&nbsp;&nbsp;
				  <input type="submit" value="Voir les articles pour la cat&eacute;gorie ->">
				  <input type="hidden" name="C1" value="CA">
				  <input type="hidden" name="C2" value="AR">
				  <input type="hidden" name="C3" value="list">
				  <select name="ID_Category" size="1" onChange="submit()" id="categoryList">
				  '.$this->liste_select('ID_Category', array('ID_Category', 'CategoryName'), 'Category', 'CategoryName', $ID_Category, true, array('--[ Toutes ]--' => 'X'), $newSql, false).'
				</form>';
		} else {
			$listCategory = '';
		}

		/*if ($ID_Category != 'X') {
			$categoryName = ' Cat&eacute;gorie "'.$this->getField('Category', 'CategoryName', 'ID_Category', $ID_Category).'"';
		}*/

		/**
		 * Recuperation des donnees pour la creation de la liste
		 */
		if ($ID_Category == 'X') {
			$ID_Category = '%';
			$ID_CatBarNav = 'X';
		} else {
			$ID_CatBarNav = $ID_Category;
		}


		$sql = 'SELECT ID_Product, Reference, ProductName,
					   pro.Status, ProductPrice, ProductStock,
					   ManageStock, NbImage, TaxValue,
					   SalePriceType, SalePrice
				FROM Product AS pro
				INNER JOIN Category AS cat ON cat.ID_Category = pro.ID_Category
				LEFT JOIN Tax ON Tax.ID_Tax = pro.ID_Tax
				WHERE pro.ID_Category LIKE "'.$ID_Category.'"
				AND cat.Status != "D"
				AND pro.Status != "D"
				ORDER BY ProductName ASC
				LIMIT '.$data['debut'].', '.$this->nbmax;

		$query = $this->query($sql);
		$nb = mysql_num_rows($query);

		if ($nb > 0) {
			$list .= $listCategory.'
			<div id="list">
			<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="100%">
			  <tr bgcolor="#F1F3F5">
				<th width="1%" class="center">Status</th>
				<th width="1%" align="center">Image</th>
				<th width="15%" align="left">R&eacute;f&eacute;rence</th>
				<th width="*%" align="left">Produit</th>
				<th width="5%" align="center" nowrap="nowrap">Prix HT</th>
				<th width="5%" align="center" nowrap="nowrap">TVA</th>
				<th width="5%" align="center" nowrap="nowrap">Remise</th>
				<th width="5%" align="center">Stock</th>
				<th width="1%" class="center">Modifier</th>
				<th width="1%" class="center">Effacer</th>
			  </tr>';

			while ($result = mysql_fetch_array($query)) {
				/**
				 * Status du produit
				 */
				if ($result['Status'] == 'E') {
					$status = '<img src="./images/1.png" alt="Active" title="Actif" />';
				} elseif ($result['Status'] == 'H') {
					$status = '<img src="./images/0.png" alt="Non active" title="Non actif" />';
				}

				/**
				 * Preparation de l'affichage de l'image
				 */
				if ($result['NbImage'] > 0) {
					$imgName    = $this->printImage('../images/product_mini/', $result['ID_Product'].'_'.substr($result['NbImage'], 0, 1));
					$imgProduct = '<img src="../images/product_mini/'.$imgName['name'].'" class="category" width="30" height="30" />';
				} else {
					$imgProduct = '&nbsp;';
				}

				/**
				 * Afficahage du stock si gere
				 */
				if ($result['ManageStock'] == '1') {
					if ($result['ProductStock'] < 1) {
						$productStock = '<b class="red">'.$result['ProductStock'].'</b>';
					} else {
						$productStock = '<b class="green">'.$result['ProductStock'].'</b>';
					}
				} else {
					$productStock = '<b class="red">---</b>';
				}

				/**
				 * Formatage de la remise
				 */
				switch($result['SalePriceType']) {
				case '0':
					$remise = '---';
					break;

				case '1':
					$remise = $result['SalePrice'].' %';
					break;

				case '2':
					$remise = $result['SalePrice'].' &euro;';
					break;
				}


				/**
				 * Affichage des donnees
				 */
				$list .= '
				<tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
				  <td class="center">'.$status.'</td>
				  <td class="center">'.$imgProduct.'</td>
				  <td class="left" nowrap="nowrap">&nbsp;'.$result['Reference'].'&nbsp;</td>
				  <td class="left">&nbsp;'.$result['ProductName'].'&nbsp;</td>
				  <td class="right" nowrap="nowrap">&nbsp;'.$result['ProductPrice'].'&nbsp;&euro;&nbsp;</td>
				  <td class="right" nowrap="nowrap">&nbsp;'.$result['TaxValue'].'&nbsp;%&nbsp;</td>
				  <td class="right" nowrap="nowrap">&nbsp;<b class="green">'.$remise.'</b>&nbsp;</td>
				  <td class="center" nowrap="nowrap">&nbsp;'.$productStock.'&nbsp;</td>
				  <td class="center" width="1%"><a href="?C1=CA&C2=AR&C3=edit&ID_Product='.$result['ID_Product'].'&debut='.$data['debut'].'"><img src="./images/update.png" alt="Modifier" title="Modifier" /></a></td>
				  <td class="center" width="1%"><a href="?C1=CA&C2=AR&C3=delete_confirm&ID_Product='.$result['ID_Product'].'"><img src="./images/delete.png" alt="Effacer" title="Effacer" /></a></td>
				</tr>';
			}

			/**
			 * Barre de navigation numerique
			 */
			$criteres  = '&C1=CA&C2=AR&C3=list&ID_Category='.$ID_CatBarNav;
			$css_i     = ' style="font-size: 14px; color: green; font-style: normal;"';
			$css_a     = ' style="font-size: 14px; color: red; font-style: normal;"';
			$nbProduct = 'SELECT COUNT(ID_Product)
						  FROM Product AS pro
						  INNER JOIN Category AS cat ON cat.ID_Category = pro.ID_Category
						  WHERE pro.ID_Category LIKE "'.$ID_Category.'"
						  AND cat.Status != "D"
						  AND pro.Status != "D";';

			$list .= '
			  <tr bgcolor="#F1F3F5">
				<td colspan="10" class="center">&nbsp;'.$this->barreNav('', 'index.php', $criteres, $data['debut'], 20, $css_i, false, $nbProduct, './images', '', false, $css_a).'</td>
			  </tr>
			</table>
			</div>';
		} else {
			$list .= '<div class="description"><br />Vous n\'avez aucun articles de cr&eacute;&eacute;s.</div>';
		}

		return $list;
	}




	/** ****************************************************************************************************
	 * Methode formAddProduct
	 *
	 * @desc   Affichage du formulaire de saisie de produit.
	 * @return string
	 * @access public
	 */
	public function formAddProduct()
	{
		/**
		 * Liste des categories pour une boutique donnees
		 */
		$sqlListCategory = 'SELECT ID_Category, CategoryName
							FROM Category
							WHERE Status != "D"
							GROUP BY ID_Category
							ORDER BY CategoryName ASC';
		$queryListCategory = $this->query($sqlListCategory);

		if (mysql_num_rows($queryListCategory) > 0) {
			$listCategory = $this->liste_select('champ[ID_Category]', array('ID_Category', 'CategoryName'), 'Category', 'CategoryName', '', false, '', $sqlListCategory);
		}

		/**
		 * Liste des sous-categories
		 */
		$sqlListSubCategory = 'SELECT ID_SubCategory, SubCategoryName
							   FROM SubCategory
							   WHERE Status != "D"
							   GROUP BY ID_SubCategory
							   ORDER BY SubCategoryName ASC';
		$queryListSubCategory = $this->query($sqlListSubCategory);

		if (mysql_num_rows($queryListSubCategory) > 0) {
			$listSubCategory = $this->liste_select('champ[ID_SubCategory]', array('ID_SubCategory', 'SubCategoryName'), 'SubCategory', 'SubCategoryName', '', false, '', $sqlListSubCategory);
		}

		/**
		 * Liste des taxes pour une boutique donnees
		 */
		$sqlTaxListSelect = 'SELECT ID_Tax, TaxName, TaxValue FROM Tax ORDER BY TaxName ASC';
		$queryTax = $this->query($sqlTaxListSelect);

		while ($resultTax = @mysql_fetch_array($queryTax)) {
			if ($resultTax['ID_Tax'] == $result['ID_Tax']) {
				$taxListSelect .= '<option value="'.$resultTax['ID_Tax'].'" selected="selected">'.$resultTax['TaxName'].' - '.$resultTax['TaxValue'].' %</option>'."\r\n";
			} else {
				$taxListSelect .= '<option value="'.$resultTax['ID_Tax'].'">'.$resultTax['TaxName'].' - '.$resultTax['TaxValue'].' %</option>'."\r\n";
			}
		}

		$form = '
		<div class="heading">Ajout d\'un article</div>

		<form action="" method="POST" id="addProduct" name="addProduct" enctype="multipart/form-data">
		  <input type="hidden" name="C1" value="CA">
		  <input type="hidden" name="C2" value="AR">
		  <input type="hidden" name="C3" value="insert_ok">

		  <div class="tabber">';

		/** ----------------------------------------------------------------------------------------------------
		 * Premier onglet - Details du produit
		 */
		$form .= '
		  <div class="tabbertab">
			<h2>Fiche article</h2>
			<div class="tab_1">

		<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">
		  <tr bgcolor="#F1F3F5">
			<td colspan="2">&nbsp;</td>
		  </tr>

		  <tr bgcolor="#FFFFFF">
			<td align="right">Cat&eacute;gorie :</td>
			<td align="left">

			  <div id="list1">
				<div id="listSelect">'.$listCategory.'</div>
				<div id="ID_Cat">&nbsp;<a href="javascript:addData(\'ID_Cat\')" class="formButton">Cr&eacute;er une cat&eacute;gorie</a></div>
			  </div>

			</td>
		  </tr>
		  <tr bgcolor="#FFFFFF">
			<td align="right">Sous-cat&eacute;gorie :</td>
			<td align="left">

			  <div id="list2">
				<div id="listSelect2">'.$listSubCategory.'</div>
				<div id="ID_SCat">&nbsp;<a href="javascript:addData(\'ID_SCat\')" class="formButton">Cr&eacute;er une sous-cat&eacute;gorie</a></div>
			  </div>

			</td>
		  </tr>'.
		  $this->create_field('Article :', array('R','T','L','M','ProductName'), 'input', 'text', 'champ[ProductName]', 70, 120).
		  $this->create_field('R&eacute;f&eacute;rence :', array('R','T','L','M','Reference'), 'input', 'text', 'champ[Reference]', 30, 40).
		  $this->create_field('Prix :', array('R','T','L','M','ProductPrice'), 'input', 'text', 'champ[ProductPrice]', 10, 10, '', 0, 0, '<b>&euro;</b> <b class="green">Hors taxes</b>');

		  /**
		   * Champ pour la saisie du montant de la remise
		   */
		  $form .= '
		  <tr bgcolor="#FFFFFF">
			<td align="right">Remise :</td>
			<td align="left">
			  <select name="champ[SalePriceType]" size="1">
				<option value="0">Non d&eacute;finie</option>
				<option value="1">En % sur le prix HT</option>
				<option value="2">En &euro; sur le prix HT</option>
			  </select>&nbsp;
			  <input type="number" name="champ[SalePrice]" size="7" maxlength="12" />
			  <b class="green">Laissez vide ce champ si pas de remise</b>
			</td>
		  </tr>';

		  $form .= '
		  <tr bgcolor="#FFFFFF">
			<td align="right">Taxe :</td>
			<td align="left"><select name="champ[ID_Tax]" size="1">'.$taxListSelect.'</select></td>
		  </tr>'.
		  $this->create_field('Descriptif :', array('R','T','L','M'), 'textarea', '', 'champ[ProductDescription]', 0, 0, '', 90, 12);


		  /**
		   * Gestion et saisie du stock
		   */
		  $form .= '
		  <tr bgcolor="#FFFFFF">
			<td align="right">G&eacute;rer le stock pour ce produit ? :</td>
			<td align="left">
			  <input type="radio" name="champ[ManageStock]" value="1" /> Oui
			  <input type="radio" name="champ[ManageStock]" value="0" checked="checked" /> Non&nbsp;&nbsp;&nbsp;
			  <b class="green">Si vous ne g&eacute;rez pas de stock pour ce produit, laissez le champ Stock &agrave; z&eacute;ro.
			</td>
		  </tr>
		  <tr bgcolor="#FFFFFF">
			<td align="right">Vente hors stock ? :</td>
			<td align="left">
			  <input type="radio" name="champ[EmptyStockSale]" value="1" checked="checked" /> Oui
			  <input type="radio" name="champ[EmptyStockSale]" value="0" /> Non&nbsp;&nbsp;&nbsp;
			  <b class="green">Autoriser la vente si le stock est &eacute;puis&eacute; ?
			</td>
		  </tr>';
		  $form .= $this->create_field('Stock :', array('R','T','L','M'), 'input', 'number', 'champ[ProductStock]', 5, 5, 0);

		  /**
		   * Champ de saisie pour le poids avec choix des grammes ou kilos
		   */
		  $form .= '
		  <tr bgcolor="#FFFFFF">
			<td align="right">Poids du produit :</td>
			<td align="left">
			  <input type="number" name="champ[Weight]" value="'.$Weight.'" size="9" maxlength="5" />
			  <input type="radio" name="champ[WeightType]" value="G" checked="checked" /> grammes
			  <input type="radio" name="champ[WeightType]" value="K" /> kilos
			</td>
		  </tr>

		  <tr bgcolor="#FFFFFF">
			<td align="right">Status :</td>
			<td align="left">
			  <select name="champ[Status]">
				<option value="E" selected="selected">Produit actif</option>
				<option value="H">Produit cach&eacute;</option>
			  </select>
			</td>
		  </tr>';

		  $textDateEnd = 'Format : AAAA-MM-JJ<br />
		  <b class="red">Si vous ne  souhaitez pas utiliser cette fonctionnalit&eacute;, laissez vide ce champ<br />
		  ou assurez-vous qu\'il ne contienne que des 0, ex : 0000-00-00</b>';
		  $form .= $this->create_field('Ce produit sera d&eacute;sactiv&eacute; le :', array('R','T','L','M'), 'input', 'text', 'champ[DateEnd]]', 18, 16, '', 0, 0, $textDateEnd);

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

			  </div>
			</div>';




		/** ----------------------------------------------------------------------------------------------------
		 * Deuxieme onglet - Gestion des options produit
		 */
		/*$form .= '
			<div class="tabbertab">
			  <h2>Gestion des attributs de l\'article</h2>
			  <div class="tab_2">

				<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">
				  <tr bgcolor="#F1F3F5">
					<td colspan="2">TEST TEST TEST</td>
				  </tr>
				</table>

			  </div>
			</div>';*/




		/** ----------------------------------------------------------------------------------------------------
		 * Troisieme onglet - Gestion des images
		 */
		$form .= '
			<div class="tabbertab">
			  <h2>Images de l\'article</h2>
			  <div class="tab_3">

				<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">
				  <tr bgcolor="#F1F3F5">
					<td colspan="2">&nbsp;</td>
				  </tr>

				  <tr bgcolor="#FFFFFF">
					<td align="center" colspan="2"><b class="red">Attention, vos images doivent &ecirc;tre au format JPG ou JPEG</b></td>
				  </tr>'.
		$this->create_field('1&egrave;re image :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_1', 0, 150).
		$this->create_field('2&egrave;me image :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_2', 0, 150).
		$this->create_field('3&egrave;me image :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_3', 0, 150).
		$this->create_field('4&egrave;me image :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_4', 0, 150).
		$this->create_field('5&egrave;me image :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_5', 0, 150);

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




		/** ----------------------------------------------------------------------------------------------------
		 *
		 */
		$form .= '
			</div>
		  </form>
		</div>';

		return $form;
	}




	/** ****************************************************************************************************
	 * Methode formModifProduct
	 *
	 * @desc   Methode permettant l'enregistrement et la modification d'un produit.
	 *         Cette methode prend en parametre l'ID_Product quand il s'agit d'une modification.
	 * @param  int $ID_Product Identifiant du produit
	 * @param  int $debut      Valeur servant pour la navigation avec la barre flechee
	 * @return string
	 * @access public
	 */
	public function formModifProduct($ID_Product, $debut = 0)
	{
		/**
		 * Recuperation des donnees a modifier
		 */
		$sql    = 'SELECT * FROM Product WHERE ID_Product = '.$ID_Product;
		$query  = $this->query($sql);
		$result = mysql_fetch_array($query);

		/**
		 * Liste des categories pour une boutique donnees
		 */
		$sqlListCategory = 'SELECT ID_Category, CategoryName
							FROM Category
							WHERE Status != "D"
							GROUP BY ID_Category
							ORDER BY CategoryName ASC';
		/*array('--[ Choix de cat&eacute;gorie ]--' => 'X')*/
		$listCategory = $this->liste_select('champ[ID_Category]', array('ID_Category', 'CategoryName'), 'Category', 'CategoryName', $result['ID_Category'], false, '', $sqlListCategory);

		/**
		 * Liste des sous-categories
		 */
		$sqlListSubCategory = 'SELECT ID_SubCategory, SubCategoryName
							   FROM SubCategory
							   WHERE Status != "D"
							   GROUP BY ID_SubCategory
							   ORDER BY SubCategoryName ASC';
		$listSubCategory = $this->liste_select('champ[ID_SubCategory]', array('ID_SubCategory', 'SubCategoryName'), 'SubCategory', 'SubCategoryName', $result['ID_SubCategory'], false, '', $sqlListSubCategory);

		/**
		 * Liste des taxes pour une boutique donnees
		 */
		$sqlTaxListSelect = 'SELECT ID_Tax, TaxName, TaxValue FROM Tax ORDER BY TaxName ASC';
		$queryTax = $this->query($sqlTaxListSelect);

		while ($resultTax = @mysql_fetch_array($queryTax)) {
			if ($resultTax['ID_Tax'] == $result['ID_Tax']) {
				$taxListSelect .= '<option value="'.$resultTax['ID_Tax'].'" selected="selected">'.$resultTax['TaxName'].' - '.$resultTax['TaxValue'].' %</option>'."\r\n";
			} else {
				$taxListSelect .= '<option value="'.$resultTax['ID_Tax'].'">'.$resultTax['TaxName'].' - '.$resultTax['TaxValue'].' %</option>'."\r\n";
			}
		}

		$form = '
		<div class="heading">Modification de l\'article "<b class="blue">'.$result['ProductName'].'</b>"</div>

		<form action="" method="POST" enctype="multipart/form-data" id="editProduct">
		  <input type="hidden" name="C1" value="CA" />
		  <input type="hidden" name="C2" value="AR" />
		  <input type="hidden" name="C3" value="update" />
		  <input type="hidden" name="champ[ID_Product]" value="'.$result['ID_Product'].'" />
		  <input type="hidden" name="NbImage" value="'.$result['NbImage'].'" />

		<div class="tabber">';

		/** ----------------------------------------------------------------------------------------------------
		 * Premier onglet - Details du produit
		 */
		$form .= '
		<div class="tabbertab">
		  <h2>Fiche article</h2>
		  <div class="tab_1">

		  <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">
			<tr bgcolor="#F1F3F5">
			  <td colspan="2">&nbsp;</td>
			</tr>

			<tr bgcolor="#FFFFFF">
			  <td align="right">Cat&eacute;gorie :</td>
			  <td align="left">'.$listCategory.'</td>
			</tr>

			<tr bgcolor="#FFFFFF">
			  <td align="right">Sous-at&eacute;gorie :</td>
			  <td align="left">'.$listSubCategory.'</td>
			</tr>'.
		$this->create_field('Produit :', array('R','T','L','M'), 'input', 'text', 'champ[ProductName]', 70, 120, $result['ProductName']).
		$this->create_field('R&eacute;f&eacute;rence :', array('R','T','L','M'), 'input', 'text', 'champ[Reference]', 30, 40, $result['Reference']).
		$this->create_field('Prix :', array('R','T','L','M'), 'input', 'text', 'champ[ProductPrice]', 10, 10, $result['ProductPrice'], 0, 0, '<b>&euro;</b> <b class="green">Hors taxes</b>');

		/**
		 * Champ pour la saisie du montant de la remise
		 */
		$selected  = 's'.$result['SalePriceType'];
		$$selected = ' selected="selected"';

		if ($result['SalePriceType'] == '0') {
			$SalePrice = '';
		} else {
			$SalePrice = $result['SalePrice'];
		}

		$form .= '
			<tr bgcolor="#FFFFFF">
			  <td align="right">Remise :</td>
			  <td align="left">
				<select name="champ[SalePriceType]" size="1">
				  <option value="0"'.$s0.'>Non d&eacute;finie</option>
				  <option value="1"'.$s1.'>En % sur le prix HT</option>
				  <option value="2"'.$s2.'>En &euro; sur le prix HT</option>
				</select>&nbsp;
				<input type="text" name="champ[SalePrice]" size="7" maxlength="12" value="'.$SalePrice.'" />
				<b class="green">Laissez vide ce champ si pas de remise</b>
			  </td>
			</tr>';

		$form .= '
			<tr bgcolor="#FFFFFF">
			  <td align="right">Taxe :</td>
			  <td align="left"><select name="champ[ID_Tax]" size="1">'.$taxListSelect.'</select></td>
			</tr>'.
		$this->create_field('Descriptif :', array('R','T','L','M'), 'textarea', '', 'champ[ProductDescription]', 0, 0, $result['ProductDescription'], 90, 12);


		/**
		 * Gestion et saisie du stock
		 */
		$MS  = 'ManageStock_'.$result['ManageStock'];
		$$MS = ' checked="checked"';
		$ES  = 'EmptyStockSale_'.$result['EmptyStockSale'];
		$$ES = ' checked="checked"';

		$form .= '
			<tr bgcolor="#FFFFFF">
			  <td align="right">G&eacute;rer le stock pour ce produit ? :</td>
			  <td align="left">
				<input type="radio" name="champ[ManageStock]" value="1"'.$ManageStock_1.' /> Oui
				<input type="radio" name="champ[ManageStock]" value="0"'.$ManageStock_0.' /> Non&nbsp;&nbsp;&nbsp;
				<b class="green">Si vous ne g&eacute;rez pas de stock pour ce produit, celui-ci sera mis &agrave; z&eacute;ro.
			  </td>
			</tr>

			<tr bgcolor="#FFFFFF">
			  <td align="right">Vente hors stock ? :</td>
			  <td align="left">
				<input type="radio" name="champ[EmptyStockSale]" value="1"'.$EmptyStockSale_1.' /> Oui
				<input type="radio" name="champ[EmptyStockSale]" value="0"'.$EmptyStockSale_0.' /> Non&nbsp;&nbsp;&nbsp;
				<b class="green">Autoriser la vente si le stock est &eacute;puis&eacute; ?
			  </td>
			</tr>';
		$form .= $this->create_field('Stock :', array('R','T','L','M'), 'input', 'number', 'champ[ProductStock]', 5, 5, $result['ProductStock']);


		/**
		 * Reformatage du poids si on a a faire a des kilos
		 */
		if ($result['WeightType'] == 'K') {
			$Weight = number_format($result['Weight'] / 1000, 2, ',', '');
		} else {
			$Weight = number_format($result['Weight'], 0);
		}

		$W  = 'WeightType_'.$result['WeightType'];
		$$W = ' checked="checked"';

		$form .= '
			<tr bgcolor="#FFFFFF">
			  <td align="right">Poids du produit :</td>
			  <td align="left">
				<input type="number" name="champ[Weight]" value="'.$Weight.'" size="12" maxlength="10" />
				<input type="radio" name="champ[WeightType]" value="G"'.$WeightType_G.' /> grammes
				<input type="radio" name="champ[WeightType]" value="K"'.$WeightType_K.' /> kilos
			  </td>
			</tr>';

		/**
		 * Status du produit
		 */
		$Status = 'selected_'.$result['Status'];
		$$Status = ' selected="selected"';

		$form .= '
			<tr bgcolor="#FFFFFF">
			  <td align="right">Status :</td>
			  <td align="left">
				<select name="champ[Status]">
				  <option value="E"'.$selected_E.'>Produit actif</option>
				  <option value="H"'.$selected_H.'>Produit cach&eacute;</option>
				</select>
			  </td>
			</tr>';

		$textDateEnd = 'Format : AAAA-MM-JJ<br />Si vous ne  souhaitez pas utiliser cette fonctionnalit&eacute;,<br />laissez vide ce champ ou assurez-vous qu\'il ne contienne que des 0, ex :<br />0000-00-00 00:00';
		$form .= $this->create_field('Ce produit sera d&eacute;sactiv&eacute; le :', array('R','T','L','M'), 'input', 'text', 'champ[DateEnd]]', 18, 16, substr($result['DateEnd'], 0, 10), 0, 0, $textDateEnd);

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

		  </div>
		</div>';




		/** ----------------------------------------------------------------------------------------------------
		 * Troisieme onglet - Gestion des images
		 */
		$form .= '
		<div class="tabbertab">
		<h2>Images de l\'article</h2>
		<div class="tab_3">

		  <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">
			<tr bgcolor="#F1F3F5">
			  <td colspan="2">&nbsp;</td>
			</tr>';

		/**
		 * Preparation de l'affichage de l'image
		 */
		if ($result['NbImage'] > 0) {
			$form .= '<tr bgcolor="#FFFFFF"><td align="center" colspan="2"><b class="red">Attention, vos images doivent &ecirc;tre au format JPG ou JPEG</b></td></tr>';

			for ($i = 1; $i < 6; $i++) {
				$imgName = $this->printImage('../images/product/', $result['ID_Product'].'_'.$i);

				if ($imgName != false) {
					$form .= '
					<tr bgcolor="#FFFFFF">
					  <td align="right">Image '.$i.' :</td>
					  <td align="left">
						<div id="delImg_'.$i.'">
						  --[ <a href="javascript:action(\'delImg\', \''.$result['ID_Product'].'\', \''.$i.'\')">Effacer cette image</a> ]--<br />
						  <img src="../images/product/'.$imgName['name'].'" class="productImage" />
						</div>
						<br />
						Image de remplacement : <input type="file" name="ImgProduct_'.$i.'" size="40" />
					  </td>
					</tr>';
				} else {
					$form .= $this->create_field('Image '.$i.' :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_'.$i, 40, 150);
				}
			}
		} else {
			$form .= '<tr bgcolor="#FFFFFF"><td align="center" colspan="2"><b class="red">Attention, vos images doivent &ecirc;tre au format JPG ou JPEG</b></td></tr>'.
			$this->create_field('Image 1 :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_1', 40, 150).
			$this->create_field('Image 2 :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_2', 40, 150).
			$this->create_field('Image 3 :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_3', 40, 150).
			$this->create_field('Image 4 :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_4', 40, 150).
			$this->create_field('Image 5 :', array('R','T','L','M'), 'input', 'file', 'ImgProduct_5', 40, 150);
		}

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




		/** ----------------------------------------------------------------------------------------------------
		 * Fin de la fiche article
		 */
		$form .= '
			</div>
		  </form>
		</div>';

		return $form;
	}




	/** ****************************************************************************************************
	 * Methode deleteProductImg
	 *
	 * @desc   Suppression d'une image d'un produit
	 * @param  int $ID_Product Identifiant du produit
	 * @param  int $imgNum     Numero de l'image a effacer
	 * @return true
	 * @access public
	 */
	public function deleteProductImg($ID_Product, $imgNum)
	{
		$sql = 'UPDATE Product SET NbImage = REPLACE(NbImage, "'.$imgNum.'", "") WHERE ID_Product = '.$ID_Product;
		$this->query($sql);
		$imgName = $ID_Product.'_'.$imgNum;

		system('rm -f ../images/product_mini/'.$imgName.'.*');
		system('rm -f ../images/product/'.$imgName.'.*');
		system('rm -f ../images/product_int/'.$imgName.'.*');
		system('rm -f ../images/product_big/'.$imgName.'.*');
	}











}
?>
