[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/htdocs/categories/ -> categorie.class.php (source)

   1  <?php
   2  /* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
   3   * Copyright (C) 2005 Davoleau Brice    <brice.davoleau@gmail.com>
   4   * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License
  17   * along with this program; if not, write to the Free Software
  18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19   *
  20   * $Id: categorie.class.php,v 1.6 2005/09/11 16:51:41 eldy Exp $
  21   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/categories/categorie.class.php,v $
  22   *
  23   */
  24  
  25  require_once(DOL_DOCUMENT_ROOT."/product.class.php");
  26  
  27  
  28  class Categorie
  29  {
  30    var $db;
  31  
  32    var $id;
  33    var $label;
  34    var $description;
  35  
  36    /**
  37     * Constructeur
  38     * db : accès base de données
  39     * id : id de la catégorie
  40     */
  41    function Categorie ($db, $id=-1)
  42    {
  43      $this->db = $db;
  44      $this->id = $id;
  45  
  46      if ($id != -1) $this->fetch ($this->id);
  47    }
  48      
  49    /**
  50     * Charge la catégorie
  51     * id : id de la catégorie à charger
  52     */
  53    function fetch ($id)
  54    {
  55      $sql  = "SELECT rowid,label,description ";
  56      $sql .= "FROM ".MAIN_DB_PREFIX."categorie WHERE rowid = ".$id;
  57  
  58      $resql  = $this->db->query ($sql);
  59  
  60      if ($resql)
  61        {
  62      $res = $this->db->fetch_array($resql);
  63  
  64      $this->id        = $res['rowid'];
  65      $this->label        = $res['label'];
  66      $this->description    = stripslashes($res['description']);
  67  
  68      $this->db->free($resql);
  69        }
  70      else
  71        {
  72      dolibarr_print_error ($this->db);
  73      return -1;
  74        }
  75    }
  76  
  77    /**
  78     * Ajoute la catégorie dans la base de données
  79     * retour : -1 : erreur SQL
  80     *          -2 : nouvel ID inconnu
  81     *          -3 : catégorie invalide
  82     */
  83    function create ()
  84    {
  85      if (!$this->check () || $this->already_exists ($this->label))
  86        {
  87      return -3;
  88        }
  89  
  90      $sql  = "INSERT INTO ".MAIN_DB_PREFIX."categorie (label, description) ";
  91      $sql .= "VALUES ('".$this->label."', '".$this->description."')";
  92          
  93      $res  = $this->db->query ($sql);
  94  
  95      if ($res)
  96        {
  97      $id = $this->db->last_insert_id (MAIN_DB_PREFIX."categorie");
  98  
  99      if ($id > 0)
 100        {
 101          $this->id = $id;
 102          return $id;
 103        }
 104      else
 105        {
 106          return -2;
 107        }
 108        }
 109      else
 110        {
 111      dolibarr_print_error ($this->db);
 112      return -1;
 113        }
 114    }
 115      
 116    /**
 117     * Mise à jour de la catégorie
 118     * retour :  1 : OK
 119     *          -1 : erreur SQL
 120     *          -2 : catégorie invalide
 121     */
 122    function update ()
 123    {
 124      if (!$this->check () || $this->id < 0)
 125        {
 126      return -2;
 127        }
 128  
 129      $sql  = "UPDATE ".MAIN_DB_PREFIX."categorie ";
 130      $sql .= "SET label = '".trim ($this->label)."'";
 131      if (strlen (trim ($this->description)) > 0)
 132        {
 133      $sql .= ", description = '".trim ($this->description)."'";
 134        }
 135      $sql .= " WHERE rowid = ".$this->id;
 136  
 137      if ($this->db->query ($sql))
 138        {
 139      return 1;
 140        }
 141      else
 142        {
 143      dolibarr_print_error ($this->db);
 144      return -1;
 145        }
 146    }
 147  
 148    /**
 149     * Supprime la catégorie
 150     * Les produits et sous-catégories deviennent orphelins
 151     * si $all = false, et sont (seront :) supprimés sinon
 152     * TODO : imp. $all
 153     */
 154    function remove ($all = false)
 155    {
 156      if (!$this->check ())
 157        {
 158      return -2;
 159        }
 160  
 161      $sql  = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product ";
 162      $sql .= "WHERE fk_categorie = ".$this->id;
 163      if (!$this->db->query ($sql))
 164        {
 165      dolibarr_print_error ($this->db);
 166      return -1;
 167        }
 168  
 169      $sql  = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association ";
 170      $sql .= "WHERE fk_categorie_mere  = ".$this->id;
 171      $sql .= "   OR fk_categorie_fille = ".$this->id;
 172      if (!$this->db->query ($sql))
 173        {
 174      dolibarr_print_error ($this->db);
 175      return -1;
 176        }
 177  
 178      $sql  = "DELETE FROM ".MAIN_DB_PREFIX."categorie ";
 179      $sql .= "WHERE rowid = ".$this->id;
 180      if (!$this->db->query ($sql))
 181        {
 182      dolibarr_print_error ($this->db);
 183      return -1;
 184        }
 185      else
 186        {
 187      return 1;
 188        }
 189  
 190    }
 191      
 192    /**
 193     * Vérifie si la catégorie est correcte (prête à être
 194     * enregistrée ou mise à jour
 195     */
 196    function check ()
 197    {
 198      if (strlen (trim ($this->label)) == 0)
 199        {
 200      return false;
 201        }
 202  
 203      return true;
 204    }
 205      
 206    /**
 207     * Ajout d'une sous-catégorie
 208     * $fille : objet catégorie
 209     * retour :  1 : OK
 210     *          -2 : $fille est déjà une fille de $this
 211     *          -3 : catégorie ($this ou $fille) invalide
 212     */
 213    function add_fille ($fille)
 214    {
 215      if (!$this->check () || !$fille->check ())
 216        {
 217      return -3;
 218        }
 219      else if ($this->is_fille ($fille))
 220        {
 221      return -2;
 222        }
 223  
 224      $sql  = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association (fk_categorie_mere, fk_categorie_fille) ";
 225      $sql .= "VALUES (".$this->id.", ".$fille->id.")";
 226  
 227      if ($this->db->query ($sql))
 228        {
 229      return 1;
 230        }
 231      else
 232        {
 233      dolibarr_print_error ($this->db);
 234      return -1;
 235        }
 236    }
 237       
 238    /**
 239     * Suppression d'une sous-catégorie (seulement "désassociation")
 240     * $fille : objet catégorie
 241     * retour :  1 : OK
 242     *          -3 : catégorie ($this ou $fille) invalide
 243     */
 244    function del_fille ($fille)
 245    {
 246      if (!$this->check () || !$fille->check ())
 247        {
 248      return -3;
 249        }
 250  
 251      $sql  = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association ";
 252      $sql .= "WHERE fk_categorie_mere = ".$this->id." and fk_categorie_fille = ".$fille->id;
 253  
 254      if ($this->db->query ($sql))
 255        {
 256      return 1;
 257        }
 258      else
 259        {
 260      dolibarr_print_error ($this->db);
 261      return -1;
 262        }
 263    }
 264       
 265    /**
 266     * Ajout d'un produit à la catégorie
 267     * retour :  1 : OK
 268     *          -1 : erreur SQL
 269     *          -2 : id non renseigné
 270     */
 271    function add_product ($prod)
 272    {
 273      if ($this->id == -1)
 274        {
 275      return -2;
 276        }
 277          
 278      $sql  = "INSERT INTO ".MAIN_DB_PREFIX."categorie_product (fk_categorie, fk_product) ";
 279      $sql .= "VALUES (".$this->id.", ".$prod->id.")";
 280  
 281      if ($this->db->query ($sql))
 282        {
 283      return 1;
 284        }
 285      else
 286        {
 287      dolibarr_print_error ($this->db);
 288      return -1;
 289        }
 290    }
 291      
 292    /**
 293     * Suppresion d'un produit de la catégorie 
 294     * @param $prod est un objet de type produit
 295     * retour :  1 : OK
 296     *          -1 : erreur SQL
 297     */
 298    function del_product ($prod)
 299    {
 300      $sql  = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product";
 301      $sql .= " WHERE fk_categorie = ".$this->id;
 302      $sql .= " AND   fk_product   = ".$prod->id;
 303  
 304      if ($this->db->query ($sql))
 305        {
 306      return 1;
 307        }
 308      else
 309        {
 310      dolibarr_print_error ($this->db);
 311      return -1;
 312        }
 313    }
 314      
 315    /**
 316     * Retourne les produits de la catégorie
 317     */
 318    function get_products ()
 319    {
 320      $sql  = "SELECT fk_product FROM ".MAIN_DB_PREFIX."categorie_product ";
 321      $sql .= "WHERE fk_categorie = ".$this->id;
 322  
 323      $res  = $this->db->query ($sql);
 324  
 325      if ($res)
 326        {
 327      $prods = array ();
 328      while ($rec = $this->db->fetch_array ($res))
 329        {
 330          $prod = new Product ($this->db, $rec['fk_product']);
 331          $prod->fetch ($prod->id);
 332          $prods[] = $prod;
 333        }
 334      return $prods;
 335        }
 336      else
 337        {
 338      dolibarr_print_error ($this->db);
 339      return -1;
 340        }
 341    }
 342      
 343    /**
 344     * Retourne les filles de la catégorie
 345     */
 346    function get_filles ()
 347    {
 348      $sql  = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association ";
 349      $sql .= "WHERE fk_categorie_mere = ".$this->id;
 350  
 351      $res  = $this->db->query ($sql);
 352  
 353      if ($res)
 354        {
 355      $cats = array ();
 356      while ($rec = $this->db->fetch_array ($res))
 357        {
 358          $cat = new Categorie ($this->db, $rec['fk_categorie_fille']);
 359          $cats[] = $cat;
 360        }
 361      return $cats;
 362        }
 363      else
 364        {
 365      dolibarr_print_error ($this->db);
 366      return -1;
 367        }
 368    }
 369      
 370    /**
 371     * La catégorie $fille est-elle une fille de cette catégorie ?
 372     */
 373    function is_fille ($fille)
 374    {
 375      $sql  = "SELECT count(fk_categorie_fille) FROM ".MAIN_DB_PREFIX."categorie_association ";
 376      $sql .= "WHERE fk_categorie_mere = ".$this->id." AND fk_categorie_fille = ".$fille->id;
 377  
 378      $res  = $this->db->query ($sql);
 379          
 380      $n    = $this->db->fetch_array ($res);
 381          
 382      return ($n[0] > 0);
 383    }
 384      
 385    /**
 386     * Retourne toutes les catégories
 387     */
 388    function get_all_categories ()
 389    {
 390      $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
 391  
 392      $res = $this->db->query ($sql);
 393  
 394      if ($res)
 395        {
 396      $cats = array ();
 397      while ($record = $this->db->fetch_array ($res))
 398        {
 399          $cat = new Categorie ($this->db, $record['rowid']);
 400          $cats[$record['rowid']] = $cat;
 401        }
 402      return $cats;
 403        }
 404      else
 405        {
 406      dolibarr_print_error ($this->db);
 407      return -1;
 408        }
 409    }
 410  
 411    /**
 412     * Retourne toutes les catégories qui ont au moins 1 fille
 413     */
 414    function get_all_meres ()
 415    {
 416      $sql  = "SELECT DISTINCT fk_categorie_mere FROM ".MAIN_DB_PREFIX."categorie_association";
 417  
 418      $res = $this->db->query ($sql);
 419  
 420      if ($res)
 421        {
 422      $cats = array ();
 423      while ($record = $this->db->fetch_array ($res))
 424        {
 425          $cat = new Categorie ($this->db, $record['fk_categorie_mere']);
 426          $cats[$record['fk_categorie_mere']] = $cat;
 427        }
 428      return $cats;
 429        }
 430      else
 431        {
 432      dolibarr_print_error ($this->db);
 433      return -1;
 434        }
 435    }
 436      
 437    /**
 438     * Retourne le nombre total de catégories
 439     */
 440    function get_nb_categories ()
 441    {
 442      $sql = "SELECT count(rowid) FROM ".MAIN_DB_PREFIX."categorie";
 443      $res = $this->db->query ($sql);
 444  
 445      if ($res)
 446        {
 447      $res = $this->db->fetch_array ();
 448      return $res[0];
 449        }
 450      else
 451        {
 452      dolibarr_print_error ($this->db);
 453      return -1;
 454        }
 455    }
 456      
 457    /**
 458     * Vérifie si une catégorie porte le label $label
 459     */
 460    function already_exists ($label)
 461    {
 462      $sql  = "SELECT count(rowid) FROM ".MAIN_DB_PREFIX."categorie ";
 463      $sql .= "WHERE label = '".$label."'";
 464  
 465      $res  = $this->db->query ($sql);
 466      $res  = $this->db->fetch_array ($res);
 467  
 468      return ($res[0] > 0);
 469    }
 470  
 471    /**
 472     * Retourne les catégories de premier niveau
 473     */
 474    function get_main_categories ()
 475    {
 476      $allcats = $this->get_all_categories ();
 477  
 478      /*    $sql  = "SELECT rowid,label,description FROM ".MAIN_DB_PREFIX."categorie ";
 479      $sql .= "WHERE ".MAIN_DB_PREFIX."categorie.rowid NOT IN (";
 480      $sql .= "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association)";
 481  
 482      $res = $this->db->query ($sql);
 483  
 484      if ($res)
 485      {
 486      $cats = array ();
 487      while ($record = $this->db->fetch_array ($res))
 488      {
 489      $cat = array ("rowid" => $record['rowid'],
 490      "label" => $record['label'],
 491      "description" => $record['description']);
 492      $cats[$record['rowid']] = $cat;
 493      }
 494      return $cats;
 495      }
 496      else
 497      {
 498      return -1;
 499      }
 500      */    // pas de NOT IN avec MySQL ?
 501  
 502          
 503      $maincats = array ();
 504      $filles   = array ();
 505          
 506      $sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association";
 507      $res = $this->db->query ($sql);
 508      while ($res = $this->db->fetch_array ($res))
 509        {
 510      $filles[] = $res['fk_categorie_fille'];
 511        }
 512  
 513      foreach ($allcats as $cat)
 514        {
 515      if (!in_array ($cat->id, $filles))
 516        {
 517          $maincats[] = $cat;
 518        }
 519      else
 520        {
 521        }
 522        }
 523          
 524      return $maincats;
 525    }
 526  
 527    /**
 528     * Retourne les chemin de la catégorie, avec les noms des catégories
 529     * séparés par $sep (" >> " par défaut)
 530     */
 531    function print_all_ways ($sep = " &gt;&gt; ", $url='')
 532    {
 533      $ways = array ();
 534          
 535      foreach ($this->get_all_ways () as $way)
 536        {
 537      $w = array ();
 538      foreach ($way as $cat)
 539        {
 540          if ($url == '')
 541            {
 542          $w[] = "<a href='".DOL_URL_ROOT."/categories/viewcat.php?id=".$cat->id."'>".$cat->label."</a>";
 543            }
 544          else
 545            {
 546          $w[] = "<a href='".DOL_URL_ROOT."/$url?catid=".$cat->id."'>".$cat->label."</a>";
 547            }
 548        }
 549      $ways[] = implode ($sep, $w);
 550        }
 551  
 552      return $ways;
 553    }
 554      
 555  
 556    /**
 557     * get_primary_way() affiche le chemin le plus court pour se rendre à un produit
 558     */
 559    function get_primary_way($id)
 560    {
 561      $primary_way = Array("taille"=>-1,"chemin"=>Array());
 562      $meres = $this->containing($id);
 563      foreach ($meres as $mere)
 564        {
 565      foreach ($mere->get_all_ways() as $way)
 566        {
 567          if(sizeof($way)<$primary_way["taille"] || $primary_way["taille"]<0)
 568            {
 569          $primary_way["taille"] = sizeOf($way);
 570          $primary_way["chemin"] = $way;
 571            }
 572        }
 573        }
 574      return $primary_way["chemin"];
 575  
 576    }
 577  
 578    /**
 579     * print_primary_way() affiche le chemin le plus court pour se rendre à un produit
 580     */
 581    function print_primary_way($id, $sep= " &gt;&gt; ",$url)
 582    {
 583      $primary_way = Array();
 584      $way = $this->get_primary_way($id);
 585      $w = array();
 586      foreach ($way as $cat)
 587        {
 588      if ($url == '')
 589        {
 590          $w[] = "<a href='".DOL_URL_ROOT."/categories/viewcat.php?id=".$cat->id."'>".$cat->label."</a>";
 591        }
 592      else
 593        {
 594          $w[] = "<a href='".DOL_URL_ROOT."/$url?catid=".$cat->id."'>".$cat->label."</a>";
 595        }
 596        }
 597      
 598      return implode($sep, $w);
 599    }
 600    /**
 601     * Retourne un tableau contenant la liste des catégories mères
 602     */
 603    function get_meres ()
 604    {
 605      $meres = array ();
 606  
 607      $sql  = "SELECT fk_categorie_mere FROM ".MAIN_DB_PREFIX."categorie_association ";
 608      $sql .= "WHERE fk_categorie_fille = ".$this->id;
 609  
 610      $res  = $this->db->query ($sql);
 611  
 612      while ($cat = $this->db->fetch_array ($res))
 613        {
 614      $meres[] = new Categorie ($this->db, $cat['fk_categorie_mere']);
 615        }
 616  
 617      return $meres;
 618    }
 619      
 620    /**
 621     * Retourne dans un tableau tous les chemins possibles pour arriver à la catégorie
 622     * en partant des catégories principales, représentés par des tableaux de catégories
 623     */
 624    function get_all_ways ()
 625    {
 626      $ways = array ();
 627  
 628      foreach ($this->get_meres () as $mere)
 629        {
 630      foreach ($mere->get_all_ways () as $way)
 631        {
 632          $w   = $way;
 633          $w[] = $this;
 634  
 635          $ways[] = $w;
 636        }
 637        }
 638  
 639      if (sizeof ($ways) == 0)
 640        $ways[0][0] = $this;
 641  
 642      return $ways;
 643    }
 644      
 645    /**
 646     * Retourne les catégories contenant le produit $id
 647     */
 648    function containing ($id)
 649    {
 650      $cats = array ();
 651          
 652      $sql  = "SELECT fk_categorie FROM ".MAIN_DB_PREFIX."categorie_product ";
 653      $sql .= "WHERE  fk_product = ".$id;
 654  
 655      $res = $this->db->query ($sql);
 656          
 657      if ($res)
 658        {
 659      while ($cat = $this->db->fetch_array ($res))
 660        {
 661          $cats[] = new Categorie ($this->db, $cat['fk_categorie']);
 662        }
 663  
 664      return $cats;
 665        }
 666      else
 667        {
 668      dolibarr_print_error ($this->db);
 669      return -1;
 670        }
 671    }
 672      
 673    /**
 674     * Retourne les catégories dont le nom correspond à $nom
 675     * ajoute des wildcards sauf si $exact = true
 676     */
 677    function rechercher_par_nom ($nom, $exact = false)
 678    {
 679      $cats = array ();
 680          
 681      if (!$exact)
 682        {
 683      $nom = '%'.str_replace ('*', '%', $nom).'%';
 684        }
 685  
 686      $sql  = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie ";
 687      $sql .= "WHERE label LIKE '".$nom."'";
 688  
 689      $res  = $this->db->query ($sql);
 690  
 691      if ($res)
 692        {
 693      while ($id = $this->db->fetch_array ($res))
 694        {
 695          $cats[] = new Categorie ($this->db, $id['rowid']);
 696        }
 697  
 698      return $cats;
 699        }
 700      else
 701        {
 702      return 0;
 703        }
 704    }
 705  }
 706  ?>


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics