début restructuration du code

This commit is contained in:
Regis Houssin 2006-02-21 11:08:34 +00:00
parent f575fc20ed
commit dbfa477d25
3 changed files with 199 additions and 217 deletions

View File

@ -32,13 +32,14 @@ class Categorie
var $id; var $id;
var $label; var $label;
var $description; var $description;
var $statut;
/** /**
* Constructeur * Constructeur
* db : accès base de données * db : accès base de données
* id : id de la catégorie * id : id de la catégorie
*/ */
function Categorie ($db, $id=-1) function Categorie($db, $id=-1)
{ {
$this->db = $db; $this->db = $db;
$this->id = $id; $this->id = $id;
@ -50,10 +51,10 @@ class Categorie
* Charge la catégorie * Charge la catégorie
* id : id de la catégorie à charger * id : id de la catégorie à charger
*/ */
function fetch ($id) function fetch($id)
{ {
$sql = "SELECT rowid,label,description "; $sql = "SELECT rowid, label, description, fk_statut";
$sql .= "FROM ".MAIN_DB_PREFIX."categorie WHERE rowid = ".$id; $sql.= " FROM ".MAIN_DB_PREFIX."categorie WHERE rowid = ".$id;
$resql = $this->db->query ($sql); $resql = $this->db->query ($sql);
@ -64,6 +65,7 @@ class Categorie
$this->id = $res['rowid']; $this->id = $res['rowid'];
$this->label = $res['label']; $this->label = $res['label'];
$this->description = stripslashes($res['description']); $this->description = stripslashes($res['description']);
$this->statut = $res['fk_statut'];
$this->db->free($resql); $this->db->free($resql);
} }
@ -80,7 +82,7 @@ class Categorie
* -2 : nouvel ID inconnu * -2 : nouvel ID inconnu
* -3 : catégorie invalide * -3 : catégorie invalide
*/ */
function create () function create()
{ {
if (!$this->check () || $this->already_exists ($this->label)) if (!$this->check () || $this->already_exists ($this->label))
{ {
@ -119,28 +121,29 @@ class Categorie
* -1 : erreur SQL * -1 : erreur SQL
* -2 : catégorie invalide * -2 : catégorie invalide
*/ */
function update () function update()
{ {
if (!$this->check () || $this->id < 0) if (!$this->check() || $this->id < 0)
{ {
return -2; return -2;
} }
$sql = "UPDATE ".MAIN_DB_PREFIX."categorie "; $sql = "UPDATE ".MAIN_DB_PREFIX."categorie";
$sql .= "SET label = '".trim ($this->label)."'"; $sql.= " SET label = '".trim($this->label)."'";
if (strlen (trim ($this->description)) > 0)
if (strlen (trim($this->description)) > 0)
{ {
$sql .= ", description = '".trim ($this->description)."'"; $sql .= ", description = '".trim($this->description)."'";
} }
$sql .= " WHERE rowid = ".$this->id; $sql .= " WHERE rowid = ".$this->id;
if ($this->db->query ($sql)) if ($this->db->query($sql))
{ {
return 1; return 1;
} }
else else
{ {
dolibarr_print_error ($this->db); dolibarr_print_error($this->db);
return -1; return -1;
} }
} }
@ -153,33 +156,36 @@ class Categorie
*/ */
function remove ($all = false) function remove ($all = false)
{ {
if (!$this->check ()) if (!$this->check())
{ {
return -2; return -2;
} }
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product "; $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product ";
$sql .= "WHERE fk_categorie = ".$this->id; $sql .= "WHERE fk_categorie = ".$this->id;
if (!$this->db->query ($sql))
if (!$this->db->query($sql))
{ {
dolibarr_print_error ($this->db); dolibarr_print_error($this->db);
return -1; return -1;
} }
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association "; $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association ";
$sql .= "WHERE fk_categorie_mere = ".$this->id; $sql .= "WHERE fk_categorie_mere = ".$this->id;
$sql .= " OR fk_categorie_fille = ".$this->id; $sql .= " OR fk_categorie_fille = ".$this->id;
if (!$this->db->query ($sql))
if (!$this->db->query($sql))
{ {
dolibarr_print_error ($this->db); dolibarr_print_error($this->db);
return -1; return -1;
} }
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie "; $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie ";
$sql .= "WHERE rowid = ".$this->id; $sql .= "WHERE rowid = ".$this->id;
if (!$this->db->query ($sql))
if (!$this->db->query($sql))
{ {
dolibarr_print_error ($this->db); dolibarr_print_error($this->db);
return -1; return -1;
} }
else else
@ -193,9 +199,9 @@ class Categorie
* Vérifie si la catégorie est correcte (prête à être * Vérifie si la catégorie est correcte (prête à être
* enregistrée ou mise à jour * enregistrée ou mise à jour
*/ */
function check () function check()
{ {
if (strlen (trim ($this->label)) == 0) if (strlen(trim($this->label)) == 0)
{ {
return false; return false;
} }
@ -210,27 +216,27 @@ class Categorie
* -2 : $fille est déjà une fille de $this * -2 : $fille est déjà une fille de $this
* -3 : catégorie ($this ou $fille) invalide * -3 : catégorie ($this ou $fille) invalide
*/ */
function add_fille ($fille) function add_fille($fille)
{ {
if (!$this->check () || !$fille->check ()) if (!$this->check() || !$fille->check())
{ {
return -3; return -3;
} }
else if ($this->is_fille ($fille)) else if ($this->is_fille($fille))
{ {
return -2; return -2;
} }
$sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association (fk_categorie_mere, fk_categorie_fille) "; $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association (fk_categorie_mere, fk_categorie_fille)";
$sql .= "VALUES (".$this->id.", ".$fille->id.")"; $sql.= " VALUES (".$this->id.", ".$fille->id.")";
if ($this->db->query ($sql)) if ($this->db->query($sql))
{ {
return 1; return 1;
} }
else else
{ {
dolibarr_print_error ($this->db); dolibarr_print_error($this->db);
return -1; return -1;
} }
} }
@ -241,23 +247,23 @@ class Categorie
* retour : 1 : OK * retour : 1 : OK
* -3 : catégorie ($this ou $fille) invalide * -3 : catégorie ($this ou $fille) invalide
*/ */
function del_fille ($fille) function del_fille($fille)
{ {
if (!$this->check () || !$fille->check ()) if (!$this->check() || !$fille->check())
{ {
return -3; return -3;
} }
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association "; $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association";
$sql .= "WHERE fk_categorie_mere = ".$this->id." and fk_categorie_fille = ".$fille->id; $sql .= " WHERE fk_categorie_mere = ".$this->id." and fk_categorie_fille = ".$fille->id;
if ($this->db->query ($sql)) if ($this->db->query($sql))
{ {
return 1; return 1;
} }
else else
{ {
dolibarr_print_error ($this->db); dolibarr_print_error($this->db);
return -1; return -1;
} }
} }
@ -268,23 +274,23 @@ class Categorie
* -1 : erreur SQL * -1 : erreur SQL
* -2 : id non renseigné * -2 : id non renseigné
*/ */
function add_product ($prod) function add_product($prod)
{ {
if ($this->id == -1) if ($this->id == -1)
{ {
return -2; return -2;
} }
$sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_product (fk_categorie, fk_product) "; $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_product (fk_categorie, fk_product)";
$sql .= "VALUES (".$this->id.", ".$prod->id.")"; $sql .= " VALUES (".$this->id.", ".$prod->id.")";
if ($this->db->query ($sql)) if ($this->db->query($sql))
{ {
return 1; return 1;
} }
else else
{ {
dolibarr_print_error ($this->db); dolibarr_print_error($this->db);
return -1; return -1;
} }
} }
@ -295,19 +301,19 @@ class Categorie
* retour : 1 : OK * retour : 1 : OK
* -1 : erreur SQL * -1 : erreur SQL
*/ */
function del_product ($prod) function del_product($prod)
{ {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product";
$sql .= " WHERE fk_categorie = ".$this->id; $sql .= " WHERE fk_categorie = ".$this->id;
$sql .= " AND fk_product = ".$prod->id; $sql .= " AND fk_product = ".$prod->id;
if ($this->db->query ($sql)) if ($this->db->query($sql))
{ {
return 1; return 1;
} }
else else
{ {
dolibarr_print_error ($this->db); dolibarr_print_error($this->db);
return -1; return -1;
} }
} }
@ -315,16 +321,16 @@ class Categorie
/** /**
* Retourne les produits de la catégorie * Retourne les produits de la catégorie
*/ */
function get_products () function get_products()
{ {
$sql = "SELECT fk_product FROM ".MAIN_DB_PREFIX."categorie_product "; $sql = "SELECT fk_product FROM ".MAIN_DB_PREFIX."categorie_product ";
$sql .= "WHERE fk_categorie = ".$this->id; $sql .= "WHERE fk_categorie = ".$this->id;
$res = $this->db->query ($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
$prods = array (); $prods = array();
while ($rec = $this->db->fetch_array ($res)) while ($rec = $this->db->fetch_array ($res))
{ {
$prod = new Product ($this->db, $rec['fk_product']); $prod = new Product ($this->db, $rec['fk_product']);
@ -477,32 +483,6 @@ class Categorie
function get_main_categories () function get_main_categories ()
{ {
$allcats = $this->get_all_categories (); $allcats = $this->get_all_categories ();
/* $sql = "SELECT rowid,label,description FROM ".MAIN_DB_PREFIX."categorie ";
$sql .= "WHERE ".MAIN_DB_PREFIX."categorie.rowid NOT IN (";
$sql .= "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association)";
$res = $this->db->query ($sql);
if ($res)
{
$cats = array ();
while ($record = $this->db->fetch_array ($res))
{
$cat = array ("rowid" => $record['rowid'],
"label" => $record['label'],
"description" => $record['description']);
$cats[$record['rowid']] = $cat;
}
return $cats;
}
else
{
return -1;
}
*/ // pas de NOT IN avec MySQL ?
$maincats = array (); $maincats = array ();
$filles = array (); $filles = array ();

View File

@ -72,9 +72,9 @@ print '<tr><td valign="top" width="30%" class="notopnoleft">';
<td><?php print $langs->trans ("In"); ?> <select name="choix"> <td><?php print $langs->trans ("In"); ?> <select name="choix">
<?php <?php
// création d'un objet de type catégorie pour faire des requêtes sur la table // création d'un objet de type catégorie pour faire des requêtes sur la table
$c = new Categorie ($db); $categorie = new Categorie ($db);
$nb = $c->get_nb_categories (); $nb = $categorie->get_nb_categories();
for ($i = 0 ; $i <= $nb ; $i++) for ($i = 0 ; $i <= $nb ; $i++)
{ {
@ -94,7 +94,7 @@ print '<tr><td valign="top" width="30%" class="notopnoleft">';
<input type="submit" value="<?php print $langs->trans ("modify"); ?>" name="ok" id="ok" /> <input type="submit" value="<?php print $langs->trans ("modify"); ?>" name="ok" id="ok" />
</td> </td>
<?php <?php
$cats = $c->get_all_categories ();//on récupère toutes les catégories et leurs attributs $all_categories = $categorie->get_all_categories();//on récupère toutes les catégories et leurs attributs
for ($i = 0; $i < $nbcats ; $i++) for ($i = 0; $i < $nbcats ; $i++)
{ {
@ -102,7 +102,7 @@ print '<tr><td valign="top" width="30%" class="notopnoleft">';
echo "<option value='-1' id='choix'>".$langs->trans ("Choose")."</option>\n"; echo "<option value='-1' id='choix'>".$langs->trans ("Choose")."</option>\n";
foreach ($cats as $id => $cat) foreach ($all_categories as $id => $cat)
{ //ajout des categories dans la liste { //ajout des categories dans la liste
echo "<option value='$id' id='$id'"; echo "<option value='$id' id='$id'";

View File

@ -26,7 +26,7 @@ $user->getrights('categorie');
$langs->load("categories"); $langs->load("categories");
if (!isset ($_REQUEST["nom"]) || !isset ($_REQUEST["description"])) if (!isset($_REQUEST["nom"]) || !isset($_REQUEST["description"]))
accessforbidden(); accessforbidden();
@ -42,26 +42,28 @@ print '<table border="0" width="100%">';
print '<tr><td valign="top" width="30%">'; print '<tr><td valign="top" width="30%">';
$cat = new Categorie ($db); $categorie = new Categorie($db);
$cat->label = $_REQUEST["nom"]; $categorie->label = $_REQUEST["nom"];
$cat->description = $_REQUEST["description"]; $categorie->description = $_REQUEST["description"];
$cats_meres = isset ($_REQUEST['cats_meres']) ? $_REQUEST['cats_meres'] : array (); $cats_meres = isset($_REQUEST['cats_meres']) ? $_REQUEST['cats_meres'] : array ();
$res = $cat->create (); $res = $categorie->create();
if ($res < 0)
if ($res < 0)
{ {
print "<p>Impossible d'ajouter la catégorie ".$cat->label.".</p>"; print "<p>Impossible d'ajouter la catégorie ".$categorie->label.".</p>";
} }
else else
{ {
print "<p>La catégorie ".$cat->label." a été ajoutée avec succès.</p>"; print "<p>La catégorie ".$categorie->label." a été ajoutée avec succès.</p>";
foreach ($cats_meres as $id) foreach ($cats_meres as $id)
{ {
$mere = new Categorie ($db, $id); $mere = new Categorie($db, $id);
$res = $mere->add_fille ($cat); $res = $mere->add_fille($categorie);
if ($res < 0) if ($res < 0)
{ {
print "<p>Impossible d'associer la catégorie à \"".$mere->label."\" ($res).</p>"; print "<p>Impossible d'associer la catégorie à \"".$mere->label."\" ($res).</p>";