Nouveau fichier
This commit is contained in:
parent
cbd3b5f879
commit
0bc998a749
690
htdocs/categories/categorie.class.php
Normal file
690
htdocs/categories/categorie.class.php
Normal file
@ -0,0 +1,690 @@
|
||||
<?php
|
||||
/* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
|
||||
* Copyright (C) 2005 Davoleau Brice <brice.davoleau@gmail.com>
|
||||
*
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* $Id$
|
||||
* $Source$
|
||||
*
|
||||
*/
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT."/product.class.php";
|
||||
|
||||
class Categorie
|
||||
{
|
||||
var $db;
|
||||
|
||||
var $id;
|
||||
var $label;
|
||||
var $description;
|
||||
|
||||
/**
|
||||
* Constructeur
|
||||
* db : accès base de données
|
||||
* id : id de la catégorie
|
||||
*/
|
||||
function Categorie ($db, $id=-1)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->id = $id;
|
||||
|
||||
if ($id != -1) $this->fetch ($this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge la catégorie
|
||||
* id : id de la catégorie à charger
|
||||
*/
|
||||
function fetch ($id)
|
||||
{
|
||||
$sql = "SELECT rowid,label,description ";
|
||||
$sql .= "FROM ".MAIN_DB_PREFIX."categorie WHERE rowid = ".$id;
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
$res = $this->db->fetch_array ();
|
||||
|
||||
$this->id = $res['rowid'];
|
||||
$this->label = $res['label'];
|
||||
$this->description = $res['description'];
|
||||
|
||||
$this->db->free ();
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute la catégorie dans la base de données
|
||||
* retour : -1 : erreur SQL
|
||||
* -2 : nouvel ID inconnu
|
||||
* -3 : catégorie invalide
|
||||
*/
|
||||
function create ()
|
||||
{
|
||||
if (!$this->check () || $this->already_exists ($this->label))
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
$sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (label, description) ";
|
||||
$sql .= "VALUES ('".$this->label."', '".$this->description."')";
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
$id = $this->db->last_insert_id (MAIN_DB_PREFIX."categorie");
|
||||
|
||||
if ($id > 0)
|
||||
{
|
||||
$this->id = $id;
|
||||
return $id;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mise à jour de la catégorie
|
||||
* retour : 1 : OK
|
||||
* -1 : erreur SQL
|
||||
* -2 : catégorie invalide
|
||||
*/
|
||||
function update ()
|
||||
{
|
||||
if (!$this->check () || $this->id < 0)
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
|
||||
$sql = "UPDATE ".MAIN_DB_PREFIX."categorie ";
|
||||
$sql .= "SET label = '".trim ($this->label)."'";
|
||||
if (strlen (trim ($this->description)) > 0)
|
||||
{
|
||||
$sql .= ", description = '".trim ($this->description)."'";
|
||||
}
|
||||
$sql .= " WHERE rowid = ".$this->id;
|
||||
|
||||
if ($this->db->query ($sql))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprime la catégorie
|
||||
* Les produits et sous-catégories deviennent orphelins
|
||||
* si $all = false, et sont (seront :) supprimés sinon
|
||||
* TODO : imp. $all
|
||||
*/
|
||||
function remove ($all = false)
|
||||
{
|
||||
if (!$this->check ())
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
|
||||
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product ";
|
||||
$sql .= "WHERE fk_categorie = ".$this->id;
|
||||
if (!$this->db->query ($sql))
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
|
||||
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association ";
|
||||
$sql .= "WHERE fk_categorie_mere = ".$this->id;
|
||||
$sql .= " OR fk_categorie_fille = ".$this->id;
|
||||
if (!$this->db->query ($sql))
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
|
||||
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie ";
|
||||
$sql .= "WHERE rowid = ".$this->id;
|
||||
if (!$this->db->query ($sql))
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si la catégorie est correcte (prête à être
|
||||
* enregistrée ou mise à jour
|
||||
*/
|
||||
function check ()
|
||||
{
|
||||
if (strlen (trim ($this->label)) == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajout d'une sous-catégorie
|
||||
* $fille : objet catégorie
|
||||
* retour : 1 : OK
|
||||
* -2 : $fille est déjà une fille de $this
|
||||
* -3 : catégorie ($this ou $fille) invalide
|
||||
*/
|
||||
function add_fille ($fille)
|
||||
{
|
||||
if (!$this->check () || !$fille->check ())
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
else if ($this->is_fille ($fille))
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
|
||||
$sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association (fk_categorie_mere, fk_categorie_fille) ";
|
||||
$sql .= "VALUES (".$this->id.", ".$fille->id.")";
|
||||
|
||||
if ($this->db->query ($sql))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Suppression d'une sous-catégorie (seulement "désassociation")
|
||||
* $fille : objet catégorie
|
||||
* retour : 1 : OK
|
||||
* -3 : catégorie ($this ou $fille) invalide
|
||||
*/
|
||||
function del_fille ($fille)
|
||||
{
|
||||
if (!$this->check () || !$fille->check ())
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association ";
|
||||
$sql .= "WHERE fk_categorie_mere = ".$this->id." and fk_categorie_fille = ".$fille->id;
|
||||
|
||||
if ($this->db->query ($sql))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajout d'un produit à la catégorie
|
||||
* retour : 1 : OK
|
||||
* -1 : erreur SQL
|
||||
* -2 : id non renseigné
|
||||
*/
|
||||
function add_product ($prod)
|
||||
{
|
||||
if ($this->id == -1)
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
|
||||
$sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_product (fk_categorie, fk_product) ";
|
||||
$sql .= "VALUES (".$this->id.", ".$prod->id.")";
|
||||
|
||||
if ($this->db->query ($sql))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Suppresion d'un produit de la catégorie
|
||||
* @param $prod est un objet de type produit
|
||||
* retour : 1 : OK
|
||||
* -1 : erreur SQL
|
||||
*/
|
||||
function del_product ($prod)
|
||||
{
|
||||
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product";
|
||||
$sql .= " WHERE fk_categorie = ".$this->id;
|
||||
$sql .= " AND fk_product = ".$prod->id;
|
||||
|
||||
if ($this->db->query ($sql))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne les produits de la catégorie
|
||||
*/
|
||||
function get_products ()
|
||||
{
|
||||
$sql = "SELECT fk_product FROM ".MAIN_DB_PREFIX."categorie_product ";
|
||||
$sql .= "WHERE fk_categorie = ".$this->id;
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
$prods = array ();
|
||||
while ($rec = $this->db->fetch_array ($res))
|
||||
{
|
||||
$prod = new Product ($this->db, $rec['fk_product']);
|
||||
$prod->fetch ($prod->id);
|
||||
$prods[] = $prod;
|
||||
}
|
||||
return $prods;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne les filles de la catégorie
|
||||
*/
|
||||
function get_filles ()
|
||||
{
|
||||
$sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association ";
|
||||
$sql .= "WHERE fk_categorie_mere = ".$this->id;
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
$cats = array ();
|
||||
while ($rec = $this->db->fetch_array ($res))
|
||||
{
|
||||
$cat = new Categorie ($this->db, $rec['fk_categorie_fille']);
|
||||
$cats[] = $cat;
|
||||
}
|
||||
return $cats;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* La catégorie $fille est-elle une fille de cette catégorie ?
|
||||
*/
|
||||
function is_fille ($fille)
|
||||
{
|
||||
$sql = "SELECT count(fk_categorie_fille) FROM ".MAIN_DB_PREFIX."categorie_association ";
|
||||
$sql .= "WHERE fk_categorie_mere = ".$this->id." AND fk_categorie_fille = ".$fille->id;
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
$n = $this->db->fetch_array ($res);
|
||||
|
||||
return ($n[0] > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne toutes les catégories
|
||||
*/
|
||||
function get_all_categories ()
|
||||
{
|
||||
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
$cats = array ();
|
||||
while ($record = $this->db->fetch_array ($res))
|
||||
{
|
||||
$cat = new Categorie ($this->db, $record['rowid']);
|
||||
$cats[$record['rowid']] = $cat;
|
||||
}
|
||||
return $cats;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne toutes les catégories qui ont au moins 1 fille
|
||||
*/
|
||||
function get_all_meres ()
|
||||
{
|
||||
$sql = "SELECT DISTINCT fk_categorie_mere FROM ".MAIN_DB_PREFIX."categorie_association";
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
$cats = array ();
|
||||
while ($record = $this->db->fetch_array ($res))
|
||||
{
|
||||
$cat = new Categorie ($this->db, $record['fk_categorie_mere']);
|
||||
$cats[$record['fk_categorie_mere']] = $cat;
|
||||
}
|
||||
return $cats;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne le nombre total de catégories
|
||||
*/
|
||||
function get_nb_categories ()
|
||||
{
|
||||
$sql = "SELECT count(rowid) FROM ".MAIN_DB_PREFIX."categorie";
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
$res = $this->db->fetch_array ();
|
||||
return $res[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si une catégorie porte le label $label
|
||||
*/
|
||||
function already_exists ($label)
|
||||
{
|
||||
$sql = "SELECT count(rowid) FROM ".MAIN_DB_PREFIX."categorie ";
|
||||
$sql .= "WHERE label = '".$label."'";
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
$res = $this->db->fetch_array ($res);
|
||||
|
||||
return ($res[0] > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne les catégories de premier niveau
|
||||
*/
|
||||
function get_main_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 ();
|
||||
$filles = array ();
|
||||
|
||||
$sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association";
|
||||
$res = $this->db->query ($sql);
|
||||
while ($res = $this->db->fetch_array ($res))
|
||||
{
|
||||
$filles[] = $res['fk_categorie_fille'];
|
||||
}
|
||||
|
||||
foreach ($allcats as $cat)
|
||||
{
|
||||
if (!in_array ($cat->id, $filles))
|
||||
{
|
||||
$maincats[] = $cat;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return $maincats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne les chemin de la catégorie, avec les noms des catégories
|
||||
* séparés par $sep (" >> " par défaut)
|
||||
*/
|
||||
function print_all_ways ($sep = " >> ")
|
||||
{
|
||||
$ways = array ();
|
||||
|
||||
foreach ($this->get_all_ways () as $way)
|
||||
{
|
||||
$w = array ();
|
||||
foreach ($way as $cat)
|
||||
{
|
||||
$w[] = "<a href='".DOL_URL_ROOT."/categories/viewcat.php?id=".$cat->id."'>".$cat->label."</a>";
|
||||
}
|
||||
$ways[] = implode ($sep, $w);
|
||||
}
|
||||
|
||||
return $ways;
|
||||
}
|
||||
|
||||
/**
|
||||
* get_primary_way() affiche le chemin le plus court pour se rendre à un produit
|
||||
*/
|
||||
function get_primary_way($id)
|
||||
{
|
||||
$primary_way = Array("taille"=>-1,"chemin"=>Array());
|
||||
$meres = $this->containing($id);
|
||||
foreach ($meres as $mere)
|
||||
{
|
||||
foreach ($mere->get_all_ways() as $way)
|
||||
{
|
||||
if(sizeof($way)<$primary_way["taille"] || $primary_way["taille"]<0)
|
||||
{
|
||||
$primary_way["taille"] = sizeOf($way);
|
||||
$primary_way["chemin"] = $way;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $primary_way["chemin"];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* print_primary_way() affiche le chemin le plus court pour se rendre à un produit
|
||||
*/
|
||||
function print_primary_way($id, $sep= " >> ")
|
||||
{
|
||||
$primary_way = Array();
|
||||
$way = $this->get_primary_way($id);
|
||||
foreach ($way as $cat)
|
||||
{
|
||||
$primary_way[] = "<a href='".DOL_URL_ROOT."/categories/viewcat.php?id=".$cat->id."'>".$cat->label."</a>";
|
||||
}
|
||||
|
||||
return implode($sep, $primary_way);
|
||||
|
||||
}
|
||||
/**
|
||||
* Retourne un tableau contenant la liste des catégories mères
|
||||
*/
|
||||
function get_meres ()
|
||||
{
|
||||
$meres = array ();
|
||||
|
||||
$sql = "SELECT fk_categorie_mere FROM ".MAIN_DB_PREFIX."categorie_association ";
|
||||
$sql .= "WHERE fk_categorie_fille = ".$this->id;
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
while ($cat = $this->db->fetch_array ($res))
|
||||
{
|
||||
$meres[] = new Categorie ($this->db, $cat['fk_categorie_mere']);
|
||||
}
|
||||
|
||||
return $meres;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne dans un tableau tous les chemins possibles pour arriver à la catégorie
|
||||
* en partant des catégories principales, représentés par des tableaux de catégories
|
||||
*/
|
||||
function get_all_ways ()
|
||||
{
|
||||
$ways = array ();
|
||||
|
||||
foreach ($this->get_meres () as $mere)
|
||||
{
|
||||
foreach ($mere->get_all_ways () as $way)
|
||||
{
|
||||
$w = $way;
|
||||
$w[] = $this;
|
||||
|
||||
$ways[] = $w;
|
||||
}
|
||||
}
|
||||
|
||||
if (sizeof ($ways) == 0)
|
||||
$ways[0][0] = $this;
|
||||
|
||||
return $ways;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne les catégories contenant le produit $id
|
||||
*/
|
||||
function containing ($id)
|
||||
{
|
||||
$cats = array ();
|
||||
|
||||
$sql = "SELECT fk_categorie FROM ".MAIN_DB_PREFIX."categorie_product ";
|
||||
$sql .= "WHERE fk_product = ".$id;
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
while ($cat = $this->db->fetch_array ($res))
|
||||
{
|
||||
$cats[] = new Categorie ($this->db, $cat['fk_categorie']);
|
||||
}
|
||||
|
||||
return $cats;
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error ($this->db);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne les catégories dont le nom correspond à $nom
|
||||
* ajoute des wildcards sauf si $exact = true
|
||||
*/
|
||||
function rechercher_par_nom ($nom, $exact = false)
|
||||
{
|
||||
$cats = array ();
|
||||
|
||||
if (!$exact)
|
||||
{
|
||||
$nom = '%'.str_replace ('*', '%', $nom).'%';
|
||||
}
|
||||
|
||||
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie ";
|
||||
$sql .= "WHERE label LIKE '".$nom."'";
|
||||
|
||||
$res = $this->db->query ($sql);
|
||||
|
||||
if ($res)
|
||||
{
|
||||
while ($id = $this->db->fetch_array ($res))
|
||||
{
|
||||
$cats[] = new Categorie ($this->db, $id['rowid']);
|
||||
}
|
||||
|
||||
return $cats;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
187
htdocs/categories/create.php
Normal file
187
htdocs/categories/create.php
Normal file
@ -0,0 +1,187 @@
|
||||
<?php
|
||||
/* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* $Id$
|
||||
* $Source$
|
||||
*/
|
||||
|
||||
require "./pre.inc.php";
|
||||
|
||||
if (!$user->rights->categorie->creer) accessforbidden();
|
||||
|
||||
if (isset ($_REQUEST['choix']))
|
||||
{
|
||||
$nbcats = $_REQUEST['choix'];
|
||||
}
|
||||
else
|
||||
{ // par défault, une nouvelle catégorie sera dans une seule catégorie mère
|
||||
$nbcats = 1;
|
||||
}
|
||||
|
||||
llxHeader("","",$langs->trans("Categories"));
|
||||
|
||||
print_titre($langs->trans("CreateCat"));
|
||||
|
||||
print '<table border="0" width="100%">';
|
||||
|
||||
print '<tr><td valign="top" width="30%">';
|
||||
?>
|
||||
<form method="post" action="<?php print $_SERVER['REQUEST_URI']; ?>">
|
||||
<table class="border">
|
||||
<tr>
|
||||
<td><?php print $langs->trans ("Label"); ?> :</td>
|
||||
|
||||
<td><input type='text' size='25' id='nom' name ='nom' value='<?php
|
||||
print htmlspecialchars (stripslashes ($_REQUEST['nom']), ENT_QUOTES);
|
||||
?>' />
|
||||
</tr>
|
||||
<tr>
|
||||
<td><?php print $langs->trans ("Description"); ?> :</td>
|
||||
|
||||
<td><textarea name='description' cols='40' rows='6' id='description'><?php
|
||||
print htmlspecialchars (stripslashes ($_REQUEST['description']), ENT_QUOTES);
|
||||
?></textarea></td>
|
||||
|
||||
</tr>
|
||||
<tr>
|
||||
<td><?php print $langs->trans ("In"); ?> <select name="choix">
|
||||
<?php
|
||||
// création d'un objet de type catégorie pour faire des requêtes sur la table
|
||||
$c = new Categorie ($db);
|
||||
|
||||
$nb = $c->get_nb_categories ();
|
||||
|
||||
for ($i = 0 ; $i <= $nb ; $i++)
|
||||
{
|
||||
echo "<option value='$i' ";//creation d'une valeur dans la liste
|
||||
|
||||
if ($_REQUEST['choix'] == $i)
|
||||
echo "selected='selected'"; // permet de rendre le choix toujours selectionne
|
||||
else if (!isset ($_REQUEST['choix']) && $i == $nbcats) // nombre de catégories mères par défaut.
|
||||
echo "selected='selected'";
|
||||
|
||||
echo ">$i</option>";
|
||||
}
|
||||
?>
|
||||
</select> <?php print $langs->trans ("categories"); ?>
|
||||
</td>
|
||||
<td>
|
||||
<input type="submit" value="<?php print $langs->trans ("modify"); ?>" name="ok" id="ok" />
|
||||
</td>
|
||||
<?php
|
||||
$cats = $c->get_all_categories ();//on récupère toutes les catégories et leurs attributs
|
||||
|
||||
for ($i = 0; $i < $nbcats ; $i++)
|
||||
{
|
||||
echo "<tr><td>".$langs->trans ("Categorie")." ".($i+1)."</td><td><select name='catsMeres[".$i."]'>"; //creation des categories meres
|
||||
|
||||
echo "<option value='-1' id='choix'>".$langs->trans ("Choose")."</option>\n";
|
||||
|
||||
foreach ($cats as $id => $cat)
|
||||
{ //ajout des categories dans la liste
|
||||
echo "<option value='$id' id='$id'";
|
||||
|
||||
if ($_REQUEST['catsMeres'][$i] == $id)
|
||||
echo " selected='selected'";
|
||||
|
||||
echo ">".$cat->label."</option>\n";
|
||||
}
|
||||
|
||||
echo "</select></td></tr>";
|
||||
}
|
||||
?>
|
||||
<tr>
|
||||
<td colspan="2"><input type='submit' value='<?php print $langs->trans ("CreateThisCat"); ?>' id="creation" name="creation"/></td>
|
||||
</tr>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
if (isset ($_REQUEST['creation']))
|
||||
{
|
||||
// doit être à true pour valider la saisie de l'utilisateur
|
||||
$OK = true;
|
||||
|
||||
$cats_meres = isset ($_REQUEST['catsMeres']) ? $_REQUEST['catsMeres'] : array ();
|
||||
|
||||
if (sizeof ($cats_meres) > 1 && sizeof (array_unique ($cats_meres)) != sizeof ($cats_meres))
|
||||
{ // alors il y a des valeurs en double
|
||||
echo "<p>".$langs->trans ("ErrSameCatSelected")."</p>";
|
||||
$OK = false;
|
||||
}
|
||||
|
||||
// vérification des champs renseignés par l'utilisateur: si il y a un problème, on affiche un message d'erreur
|
||||
foreach ($cats_meres as $nb => $cat_mere)
|
||||
{
|
||||
if ($cat_mere == -1)
|
||||
{
|
||||
echo "<p>".$langs->trans ("ErrForgotCat")." ".($nb+1)."</p>";
|
||||
$OK = false;
|
||||
}
|
||||
}
|
||||
|
||||
// si les champs de description sont mal renseignés
|
||||
if ($_POST["nom"] == '')
|
||||
{
|
||||
echo "<p>".$langs->trans ("ErrForgotField")." \"".$langs->trans ("Label")."\"</p>";
|
||||
$OK = false;
|
||||
}
|
||||
else if ($c->already_exists ($_POST["nom"]))
|
||||
{
|
||||
// on regarde si le champ nom n'est pas déjà dans la table catégorie (rappel: un nom est unique dans la table catégorie
|
||||
// nb a déjà été renseigné, il contient le nombre de catégories
|
||||
echo "<p>".$langs->trans ("ErrCatAlreadyExists")."</p>";
|
||||
$OK = false;
|
||||
}
|
||||
|
||||
if ($_POST["description"] == '')
|
||||
{
|
||||
echo "<p>".$langs->trans ("ErrForgotField")." \"".$langs->trans ("Description")."\"</p>";
|
||||
$OK = false;
|
||||
}
|
||||
|
||||
// vérification pour savoir si tous les champs sont corrects
|
||||
if ($OK)
|
||||
{
|
||||
$nom = htmlspecialchars(stripslashes($_REQUEST['nom']) ,ENT_QUOTES);
|
||||
$description = htmlspecialchars(stripslashes($_REQUEST['description']),ENT_QUOTES);
|
||||
// creation de champs caches pour etre appele dans la classe de traitement
|
||||
?>
|
||||
<table class="noborder"><tr><td>
|
||||
<form method="post" action="docreate.php">
|
||||
<p><?php print $langs->trans ("ValidateFields"); ?> ?
|
||||
<input type='submit' value='<?php print $langs->trans ("Valid"); ?>'/>
|
||||
<input type='hidden' name='nom' value="<?php print $nom; ?>">
|
||||
<input type='hidden' name='description' value="<?php print $description; ?>">
|
||||
|
||||
<?php
|
||||
foreach ($cats_meres as $id => $cat_mere)
|
||||
{
|
||||
echo "<input type='hidden' name='cats_meres[$id]' value='$cat_mere'>";
|
||||
}
|
||||
?>
|
||||
</form></p>
|
||||
</td></tr></table>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
print '</td></tr></table>';
|
||||
|
||||
$db->close();
|
||||
|
||||
llxFooter("<em>Dernière modification $Date$ révision $Revision$</em>");
|
||||
?>
|
||||
76
htdocs/categories/docreate.php
Normal file
76
htdocs/categories/docreate.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* $Id$
|
||||
* $Source$
|
||||
*/
|
||||
|
||||
require "./pre.inc.php";
|
||||
|
||||
$user->getrights('categorie');
|
||||
|
||||
$langs->load("categories");
|
||||
|
||||
|
||||
if (!isset ($_REQUEST["nom"]) || !isset ($_REQUEST["description"]))
|
||||
accessforbidden();
|
||||
|
||||
|
||||
/**
|
||||
* Affichage page accueil
|
||||
*/
|
||||
|
||||
llxHeader("","",$langs->trans("Categories"));
|
||||
|
||||
print_titre($langs->trans("CatCreated"));
|
||||
|
||||
print '<table border="0" width="100%">';
|
||||
|
||||
print '<tr><td valign="top" width="30%">';
|
||||
|
||||
$cat = new Categorie ($db);
|
||||
|
||||
$cat->label = $_REQUEST["nom"];
|
||||
$cat->description = $_REQUEST["description"];
|
||||
|
||||
$cats_meres = isset ($_REQUEST['cats_meres']) ? $_REQUEST['cats_meres'] : array ();
|
||||
|
||||
$res = $cat->create ();
|
||||
if ($res < 0)
|
||||
{
|
||||
print "<p>Impossible d'ajouter la catégorie ".$cat->label.".</p>";
|
||||
}
|
||||
else
|
||||
{
|
||||
print "<p>La catégorie ".$cat->label." a été ajoutée avec succès.</p>";
|
||||
|
||||
foreach ($cats_meres as $id)
|
||||
{
|
||||
$mere = new Categorie ($db, $id);
|
||||
$res = $mere->add_fille ($cat);
|
||||
if ($res < 0)
|
||||
{
|
||||
print "<p>Impossible d'associer la catégorie à \"".$mere->label."\" ($res).</p>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
print '</td></tr></table>';
|
||||
|
||||
$db->close();
|
||||
?>
|
||||
92
htdocs/categories/index.php
Normal file
92
htdocs/categories/index.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
require "./pre.inc.php";
|
||||
|
||||
if (!$user->rights->categorie->lire) accessforbidden();
|
||||
|
||||
/**
|
||||
* Affichage page accueil
|
||||
*/
|
||||
|
||||
llxHeader("","",$langs->trans("Categories"));
|
||||
|
||||
print_titre($langs->trans("CategoriesArea"));
|
||||
|
||||
print '<table border="0" width="100%">';
|
||||
|
||||
print '<tr><td valign="top" width="30%">';
|
||||
|
||||
$c = new Categorie ($db);
|
||||
|
||||
/*
|
||||
* Zone recherche produit/service
|
||||
*/
|
||||
print '<form method="post" action="search.php">';
|
||||
print '<table class="noborder" width="100%">';
|
||||
print '<tr class="liste_titre">';
|
||||
print '<td colspan="3">'.$langs->trans("Search").'</td>';
|
||||
print '</tr>';
|
||||
print '<tr '.$bc[0].'><td>';
|
||||
print $langs->trans("Name").' :</td><td><input class="flat" type="text" size="20" name="catname" /></td><td><input type="submit" value="'.$langs->trans ("Search").'"></td></tr>';
|
||||
print '<tr '.$bc[0].'><td>';
|
||||
print $langs->trans("SubCatOf").' :</td><td><select class="flat" name="subcatof" />';
|
||||
print '<option value="-1">'.$langs->trans("Choose").'</option>';
|
||||
|
||||
$cats = $c->get_all_meres ();
|
||||
|
||||
foreach ($cats as $cat)
|
||||
{
|
||||
print "<option value='".$cat->id."'>".htmlentities ($cat->label, ENT_QUOTES)."</option>\n";
|
||||
}
|
||||
|
||||
print '</select></td><td><input type="submit" value="'.$langs->trans ("Search").'"></td></tr>';
|
||||
print '</table></form>';
|
||||
|
||||
print '</td><td valign="top" width="70%">';
|
||||
|
||||
/*
|
||||
* Catégories principales
|
||||
*/
|
||||
$cats = $c->get_main_categories ();
|
||||
|
||||
if ($cats != -1)
|
||||
{
|
||||
print '<table class="noborder" width="100%">';
|
||||
print '<tr class="liste_titre"><td colspan="2">'.$langs->trans("MainCats").'</td></tr>';
|
||||
|
||||
foreach ($cats as $cat)
|
||||
{
|
||||
$i = !$i;
|
||||
print "\t<tr ".$bc[$i].">\n";
|
||||
print "\t\t<td><a href='viewcat.php?id=".$cat->id."'>".$cat->label."</a></td>\n";
|
||||
print "\t\t<td>".$cat->description."</td>\n";
|
||||
print "\t</tr>\n";
|
||||
}
|
||||
print "</table>";
|
||||
}
|
||||
else
|
||||
{
|
||||
dolibarr_print_error();
|
||||
}
|
||||
|
||||
print '</td></tr></table>';
|
||||
|
||||
$db->close();
|
||||
|
||||
llxFooter("<em>Dernière modification $Date$ révision $Revision$</em>");
|
||||
?>
|
||||
108
htdocs/categories/viewcat.php
Normal file
108
htdocs/categories/viewcat.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
require "./pre.inc.php";
|
||||
|
||||
llxHeader ("","",$langs->trans("Categories"));
|
||||
|
||||
if ($_REQUEST['id'] == "")
|
||||
{
|
||||
dolibarr_print_error ();
|
||||
exit ();
|
||||
}
|
||||
|
||||
$c = new Categorie ($db, $_REQUEST['id']);
|
||||
|
||||
print_titre ($langs->trans("Categorie")." ".$c->label);
|
||||
?>
|
||||
<table border="0" width="100%">
|
||||
<tr><td valign="top" width="30%">
|
||||
<?php
|
||||
|
||||
$ways = $c->print_all_ways ();
|
||||
print "<div id='ways'>";
|
||||
foreach ($ways as $way)
|
||||
{
|
||||
print $way."<br />\n";
|
||||
}
|
||||
print "</div>";
|
||||
|
||||
$cats = $c->get_filles ();
|
||||
|
||||
if ($cats < 0)
|
||||
{
|
||||
dolibarr_print_error();
|
||||
}
|
||||
else if (sizeof ($cats) > 0)
|
||||
{
|
||||
print "<table class='noborder' width='100%'>\n";
|
||||
print "<tr class='liste_titre'><td colspan='2'>".$langs->trans("SubCats")."</td></tr>\n";
|
||||
|
||||
foreach ($cats as $cat)
|
||||
{
|
||||
$i++;
|
||||
print "\t<tr ".$bc[$i%2].">\n";
|
||||
print "\t\t<td><a href='viewcat.php?id=".$cat->id."'>".$cat->label."</a></td>\n";
|
||||
print "\t\t<td>".$cat->description."</td>\n";
|
||||
print "\t</tr>\n";
|
||||
}
|
||||
print "</table>\n<br/>\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
print "<p>".$langs->trans("NoSubCat")."</p>";
|
||||
}
|
||||
|
||||
$i = 0;
|
||||
|
||||
$prods = $c->get_products ();
|
||||
|
||||
if ($prods < 0)
|
||||
{
|
||||
dolibarr_print_error();
|
||||
}
|
||||
else if (sizeof ($prods) > 0)
|
||||
{
|
||||
print "<table class='noborder' width='100%'>\n";
|
||||
print "<tr class='liste_titre'><td colspan='2'>".$langs->trans("Products")."</td></tr>\n";
|
||||
|
||||
foreach ($prods as $prod)
|
||||
{
|
||||
$i++;
|
||||
print "\t<tr ".$bc[$i%2].">\n";
|
||||
print "\t\t<td><a href='".DOL_URL_ROOT."/product/fiche.php?id=".$prod->id."'>".$prod->libelle."</a></td>\n";
|
||||
print "\t\t<td>".$prod->description."</td>\n";
|
||||
print "\t</tr>\n";
|
||||
}
|
||||
print "</table>\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
print "<p>".$langs->trans ("NoProd")."</p>";
|
||||
}
|
||||
|
||||
print "<div class='tabsAction'>\n";
|
||||
print "<a class='tabAction' href='edit.php?id=".$c->id."'>Éditer</a>";
|
||||
print "<a class='tabAction' href='delete.php?id=".$c->id."'>Supprimer</a>";
|
||||
print "</div>";
|
||||
print '</td></tr></table>';
|
||||
|
||||
$db->close();
|
||||
|
||||
llxFooter("<em>Dernière modification $Date$ révision $Revision$</em>");
|
||||
?>
|
||||
27
mysql/tables/llx_categorie_product.key.sql
Normal file
27
mysql/tables/llx_categorie_product.key.sql
Normal file
@ -0,0 +1,27 @@
|
||||
-- ============================================================================
|
||||
-- Copyright (C) 2005 Brice Davoleau <e1davole@iu-vannes.fr>
|
||||
-- Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
|
||||
--
|
||||
-- This program is free software; you can redistribute it and/or modify
|
||||
-- it under the terms of the GNU General Public License as published by
|
||||
-- the Free Software Foundation; either version 2 of the License, or
|
||||
-- (at your option) any later version.
|
||||
--
|
||||
-- This program is distributed in the hope that it will be useful,
|
||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
-- GNU General Public License for more details.
|
||||
--
|
||||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
--
|
||||
-- ============================================================================
|
||||
|
||||
ALTER TABLE llx_categorie_product ADD INDEX (fk_categorie);
|
||||
ALTER TABLE llx_categorie_product ADD INDEX (fk_product);
|
||||
|
||||
ALTER TABLE llx_categorie_product ADD FOREIGN KEY (fk_categorie)
|
||||
REFERENCES llx_categorie (rowid);
|
||||
ALTER TABLE llx_categorie_product ADD FOREIGN KEY (fk_product)
|
||||
REFERENCES llx_product (rowid);
|
||||
25
mysql/tables/llx_categorie_product.sql
Normal file
25
mysql/tables/llx_categorie_product.sql
Normal file
@ -0,0 +1,25 @@
|
||||
-- ============================================================================
|
||||
-- Copyright (C) 2005 Brice Davoleau <e1davole@iu-vannes.fr>
|
||||
-- Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
|
||||
--
|
||||
-- This program is free software; you can redistribute it and/or modify
|
||||
-- it under the terms of the GNU General Public License as published by
|
||||
-- the Free Software Foundation; either version 2 of the License, or
|
||||
-- (at your option) any later version.
|
||||
--
|
||||
-- This program is distributed in the hope that it will be useful,
|
||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
-- GNU General Public License for more details.
|
||||
--
|
||||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
--
|
||||
-- ============================================================================
|
||||
|
||||
create table llx_categorie_product
|
||||
(
|
||||
fk_categorie integer NOT NULL,
|
||||
fk_product integer NOT NULL
|
||||
)type=innodb;
|
||||
Loading…
Reference in New Issue
Block a user