MCMS
[ class tree: MCMS ] [ index: MCMS ] [ all elements ]

Source for file Category.php

Documentation is available at Category.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | MCMS: a PHP Content Management System for creating accessible sites. |
  5. // | Copyright (C) 2005  Silvio Moioli                                    |
  6. // |                                                                      |
  7. // | This program is free software; you can redistribute it and/or modify |
  8. // | it under the terms of the GNU General Public License as published by |
  9. // | the Free Software Foundation; either version 2 of the License, or    |
  10. // | (at your option) any later version.                                  |
  11. // |                                                                      |
  12. // | This program is distributed in the hope that it will be useful,      |
  13. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  14. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  15. // | GNU General Public License for more details.                         |
  16. // |                                                                      |
  17. // | You should have received a copy of the GNU General Public License    |
  18. // | along with this program; if not, write to the Free Software          |
  19. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 |
  20. // +----------------------------------------------------------------------+
  21. // | Authors: Silvio Moioli <silvio at moioli dot net> www.moioli.net     |
  22. // +----------------------------------------------------------------------+
  23. //
  24. //$Id:
  25. /**
  26.  * Interfaccia per la gestione delle categorie in cui sono divisi
  27.  * gli articoli del sito.
  28.  *
  29.  * @package  MCMS
  30.  * @version  2
  31.  * @author   Silvio Moioli <silvio at moioli dot net>
  32.  *
  33.  */
  34.  
  35. /** Carica la classe base di MCMS per l'interfacciamento al Database */
  36. require_once $_SERVER["DOCUMENT_ROOT"]."/admin/lib/model/Engine.php";
  37. /**
  38.  * Carica la classe per gli articoli (Article)
  39.  */
  40. require_once $_SERVER["DOCUMENT_ROOT"]."/admin/lib/model/Article.php";
  41.  
  42. /**
  43.  * Interfaccia per la gestione delle categorie in cui sono divisi
  44.  * gli articoli del sito.
  45.  *
  46.  * Permette di utilizzare il database in modo trasparente: creare oggetti
  47.  * di questa classe e operare sugli attributi tramite gli appositi metodi
  48.  * modificherà automaticamente il database sottostante.
  49.  * 
  50.  * @package  MCMS
  51.  * @version  2
  52.  * @author   Silvio Moioli <silvio at moioli dot net>
  53.  *
  54.  */
  55.  
  56. class Category extends PEAR
  57. {
  58.     //Attributi
  59.         /**
  60.      * @var int $ID ID del database sottostante per questa categoria.
  61.      */
  62.     var $ID = 0;
  63.     /**
  64.      * @var Engine il motore del database sottostante per questa categoria.
  65.      */
  66.     var $engine = NULL;
  67.  
  68.     //Costruttori   
  69.         /**
  70.      * Costruttore di lettura
  71.      *
  72.      * Costruttore "di lettura" (costruisce una categoria da un record del
  73.      * database già esistente).
  74.      * Nota: non può essere creata una nuova categoria da qui, ma solo dal
  75.      * Factory Method nella classe Site.
  76.      * Per i costruttori "di scrittura", quindi, vedere la classe Site
  77.      * nel file Site.php.
  78.      *
  79.      * @param int $ID l'ID del database sottostante
  80.      * @param Engine $engine un motore per interagire con il database
  81.      *  sottostante
  82.      * @return mixed la categoria se la costruzione è andata a buon fine,
  83.      *  altrimenti un PEAR_Error
  84.      * @see Engine
  85.      */
  86.     function Category($ID$engine)
  87.     {
  88.         if (settype($ID"integer"&& $ID>0){
  89.              $this->ID = $ID;
  90.              $this->engine = $engine;
  91.         }
  92.         else{
  93.              $this->raiseError("Category: invalid ID: ".$ID);
  94.         }
  95.     }
  96.     
  97.     //Metodi
  98.         /**
  99.      * Ritorna l'ID di questa categoria.
  100.      * 
  101.      * Solo le classi del package MCMS potrebbero avere bisogno di
  102.      * questo parametro, per le interrogazioni al Database.
  103.      *
  104.      * @access protected
  105.      * @return int l'ID del database sottostante
  106.      */
  107.      function getID()
  108.      {
  109.          return $this->ID;
  110.      }
  111.      
  112.     /**
  113.      * Ritorna il motore sottostante a questa categoria.
  114.      * 
  115.      * Solo le classi del package MCMS potrebbero avere bisogno di
  116.      * questo parametro, per le interrogazioni al Database.
  117.      *
  118.      * @access protected
  119.      * @return int l'ID del database sottostante
  120.      */
  121.      function getEngine()
  122.      {
  123.          return $this->engine;
  124.      }
  125.      
  126.     /**
  127.      * Getter per il campo nome dalla tabella categorie.
  128.      * 
  129.      * Ritorna un oggetto corrispondente al nome di questa categoria
  130.      * Permette di utilizzare il database in modo trasparente (è come se si
  131.      * usassero normali attributi di una classe).
  132.      *
  133.      * @return string il nome di questa categoria.
  134.      */
  135.      function getName()
  136.      {
  137.         $thisEngine $this->getEngine();
  138.         $thisDB $thisEngine->getDB();
  139.         $thisID $thisDB->escapeSimple($this->getID());
  140.         $res $thisDB->getOne("SELECT `nome categoria` FROM `categorie`".
  141.                                "WHERE `id categoria` = '$thisIDLIMIT 1;");
  142.         
  143.         #$thisDB->disconnect();
  144.         return $res;
  145.      }
  146.     
  147.     /**
  148.      * Setter per il campo nome dalla tabella categorie.
  149.      * 
  150.      * Cambia il nome di questa categoria.
  151.      * Permette di utilizzare il database in modo trasparente (è come se si
  152.      * usassero normali attributi di una classe).
  153.      *
  154.      * @param string $name l'nome a cui questa categoria appartiene.
  155.      * @return mixed true se l'operazione è andata a buon fine, altrimenti
  156.      *  un DB_ERROR
  157.      */
  158.      function setName($name)
  159.      {
  160.         $thisEngine $this->getEngine();
  161.         $thisDB $thisEngine->getDB();
  162.         $thisID $thisDB->escapeSimple($this->getID());
  163.         $name $thisDB->escapeSimple($name);
  164.         $thisDB->query("UPDATE `categorieSET `nome categoria`='$name".
  165.                        "WHERE `id categoria` = '$thisID';");
  166.         
  167.         #$thisDB->disconnect();
  168.         return true;
  169.      }   
  170.      
  171.     /**
  172.      * Getter per il campo descrizione dalla tabella categorie.
  173.      * 
  174.      * Ritorna una stringa corrispondente alla descrizione di questa categoria.
  175.      * Permette di utilizzare il database in modo trasparente (è come se si
  176.      * usassero normali attributi di una classe).
  177.      *
  178.      * @return string la descrizione di questa categoria.
  179.      */
  180.      function getDescription()
  181.      {
  182.         $thisEngine $this->getEngine();
  183.         $thisDB $thisEngine->getDB();
  184.         $thisID $thisDB->escapeSimple($this->getID());
  185.         $res $thisDB->getOne("SELECT `descrizione categoria` FROM `categorie`".
  186.                                "WHERE `id categoria` = '$thisIDLIMIT 1;");
  187.         
  188.         #$thisDB->disconnect();
  189.         return $res;
  190.      }
  191.  
  192.     /**
  193.      * Setter per il campo descrizione dalla tabella categorie.
  194.      * 
  195.      * Cambia la stringa corrispondente alla descrizione di questa categoria.
  196.      * Permette di utilizzare il database in modo trasparente (è come se si
  197.      * usassero normali attributi di una classe).
  198.      *
  199.      * @param string $description la descrizione di questa categoria.
  200.      * @return mixed true se l'operazione è andata a buon fine, altrimenti
  201.      *  un DB_ERROR
  202.      */
  203.      function setDescription($description)
  204.      {
  205.         $thisEngine $this->getEngine();
  206.         $thisDB $thisEngine->getDB();
  207.         $thisID $this->getID();
  208.         $description $thisDB->escapeSimple($description);
  209.         $thisDB->query("UPDATE `categorieSET `descrizione categoria`='$description'".
  210.                        "WHERE `id categoria` = '$thisID';");
  211.         
  212.         #$thisDB->disconnect();
  213.         return true;
  214.      }
  215.      
  216.     /**
  217.      * Getter per il campo id sito dalla tabella categorie.
  218.      * 
  219.      * Ritorna un oggetto corrispondente al sito a cui questa categoria
  220.      * appartiene
  221.      * Permette di utilizzare il database in modo trasparente (è come se si
  222.      * usassero normali attributi di una classe).
  223.      *
  224.      * @return mixed un oggetto Site o un PEAR_ERROR
  225.      */
  226.      function getSite()
  227.      {
  228.         $thisEngine $this->getEngine();
  229.         $thisDB $thisEngine->getDB();
  230.         $thisID $thisDB->escapeSimple($this->getID());
  231.         $id $thisDB->getOne("SELECT `id sito` FROM `categorie`".
  232.                                "WHERE `id categoria` = '$thisIDLIMIT 1;");
  233.         
  234.         #$thisDB->disconnect();
  235.         return new Site($id$thisEngine);
  236.      }
  237.      
  238.     /**
  239.      * Ritorna l'autore di questa sezione (fondatore del sito in cui si trova).
  240.      * 
  241.      * Ritorna un oggetto corrispondente all'autore principale di questo sito
  242.      * Permette di utilizzare il database in modo trasparente (è come se si
  243.      * usassero normali attributi di una classe).
  244.      *
  245.      * @return Author l'autore a cui questa pagina appartiene.
  246.      * @see Author
  247.      */
  248.      function getAuthor()
  249.      {
  250.         $thisEngine $this->getEngine();
  251.         $thisDB $thisEngine->getDB();
  252.         $thisID $thisDB->escapeSimple($this->getID());
  253.         $hpID $thisDB->getOne(
  254.         "SELECT `id autore`
  255.              FROM `siti`
  256.                  JOIN `pagineON `siti`.`id sito`=`pagine`.`id sito`
  257.                  JOIN `homepagesON `homepages`.`id pagina`=`pagine`.`id pagina`
  258.                  JOIN `categorieON `categorie`.`id sito`=`siti`.`id sito`
  259.              WHERE `categorie`.`id categoria`='$thisIDLIMIT 1;");
  260.         
  261.         #$thisDB->disconnect();
  262.         return new Author($hpID$thisEngine);
  263.      }
  264.      
  265.     /**
  266.      * Ritorna gli articoli di questa categoria.
  267.      * 
  268.      * Ritorna un array contenente gli oggetti corrispondenti a tutti gli
  269.      * articoli contenuti in questo Database per questo sito in questa
  270.      * categoria.
  271.      *
  272.      * @return mixed un array contenente gli articoli in questo Database se
  273.      *  l'operazione è andata a buon fine, altrimenti un DB_ERROR
  274.      * @see Article
  275.      */
  276.      function getArticles()
  277.      {
  278.         $thisEngine $this->getEngine();
  279.         $thisDB $thisEngine->getDB();
  280.         $categoryID $thisDB->escapeSimple($this->getID());
  281.  
  282.         $ids $thisDB->getCol(
  283.             "SELECT `pagine`.`id paginaFROM `pagine`,`articoli`
  284.                  WHERE `pagine`.`id pagina`=`articoli`.`id pagina`
  285.                      AND `articoli`.`id categoria`='$categoryID'
  286.                  ORDER BY `pagine`.`data paginaDESC;");
  287.         #$thisDB->disconnect();
  288.                 
  289.         $articles array();
  290.         foreach ($ids as $i{
  291.             $articles[new Article($i$thisEngine);
  292.         }
  293.         return $articles;       
  294.      }
  295.      
  296.      /**
  297.      * Ritorna il numero di articoli di questa categoria.
  298.      * 
  299.      * @return mixed un int se l'operazione è andata a buon fine,
  300.      *  altrimenti un DB_ERROR
  301.      * @see Article
  302.      */
  303.      function getArticlesCount()
  304.      {
  305.         $thisEngine $this->getEngine();
  306.         $thisDB $thisEngine->getDB();
  307.         $thisSite $this->getSite();
  308.         $siteID $thisDB->escapeSimple($thisSite->getID());
  309.         $categoryID $thisDB->escapeSimple($this->getID());
  310.         $result $thisDB->getOne(
  311.             "SELECT COUNT(`pagine`.`id pagina`) FROM `pagine`,`articoli`".
  312.             "WHERE `pagine`.`id pagina`=`articoli`.`id pagina` AND".
  313.             "`pagine`.`id sito`='$siteIDAND ".
  314.             "`articoli`.`id categoria`='$categoryIDORDER BY ".
  315.             "`pagine`.`data pagina` DESC;");
  316.  
  317.         #$thisDB->disconnect();
  318.         return $result;
  319.      }
  320. }
  321. ?>

Documentation generated on Wed, 26 Jul 2006 21:44:50 +0200 by phpDocumentor 1.3.0RC6