tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file tangra_module_category_dbc.class.php

Documentation is available at tangra_module_category_dbc.class.php

  1. <?php
  2.  
  3. // $Id$
  4.  
  5. /**
  6.  * Contains class Tangra_Module_Category_DBC
  7.  *
  8.  * @package  tangra_lib
  9.  * @subpackage  modules_manager
  10.  */
  11.  
  12. /**
  13.  *
  14.  */
  15. require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');
  16.  
  17. /**
  18.  *
  19.  */
  20. require_once('tangra_module_category.class.php');
  21.  
  22.  
  23. /**
  24.  * Tangra_Module_Category_DBC
  25.  *
  26.  * @package  tangra_lib
  27.  * @subpackage  modules_manager
  28.  */
  29.     /**
  30.      * Saves category into DB
  31.      *
  32.      * @param DB_Connection $dbc 
  33.      * @return integer On success returns category ID, on failure - false
  34.      */
  35.     public function save(DB_Connection $dbc{
  36.         $ret false;
  37.  
  38.         if ($this->get_id()) {
  39.             $ret $this->update($dbc);
  40.         else {
  41.             $ret $this->insert($dbc);
  42.             $this->set_id($ret);
  43.  
  44.         }
  45.  
  46.         return $ret;
  47.     }
  48.  
  49.  
  50.     /**
  51.      * Loads category by ID
  52.      *
  53.      * @param DB_Connection $dbc 
  54.      * @param integer $id 
  55.      * @return integer On success returns category ID, on failure - false
  56.      */
  57.     public function load_by_id(DB_Connection $dbc$id{
  58.         $ret false;
  59.  
  60.  
  61.         $sql "select hid, "
  62.                         ." name,"
  63.                         ." description"
  64.                     ." from tmod_categories "
  65.                     ."where id = $id";
  66.         $rez $dbc->execute($sql);
  67.  
  68.         if (!$rez->is_eof()) {
  69.             $rez_obj $rez->fetch_object();
  70.  
  71.             $this->set_id($id);
  72.             $this->set_hid($rez_obj->HID);
  73.             $this->set_name(stripslashes($rez_obj->NAME));
  74.             $this->set_description($rez_obj->DESCRIPTION);
  75.  
  76.             $ret $id;
  77.         }
  78.  
  79.         return $ret;
  80.     }
  81.  
  82.  
  83.     /**
  84.      * Inserts new category
  85.      *
  86.      * @param DB_Connection $dbc 
  87.      * @return integer On success returns category ID, on failure - false
  88.      * @internal
  89.      */
  90.     protected function insert(DB_Connection $dbc{
  91.         $ret false;
  92.  
  93.         $id $dbc->generate_id('tmod_categories_seq');
  94.  
  95.         $hid $this->get_hid();
  96.         $name addslashes($this->get_name());
  97.         $description addslashes($this->get_description());
  98.  
  99.         if ($id{
  100.             $sql "insert into tmod_categories (id, "
  101.                                             ."hid, "
  102.                                             ."name, "
  103.                                             ."description "
  104.                                           .") ".
  105.                                 "values ".
  106.                                           "($id,".
  107.                                           "'$hid', ".
  108.                                           "'$name', ".
  109.                                           "'$description".
  110.                                           ")";
  111.             $dbc->execute($sql);
  112.             $ret $id;
  113.         else {
  114.             throw new TE_Exception('ID not generated - tmod_categories_seq');
  115.         }
  116.  
  117.         return $ret;
  118.     }
  119.  
  120.  
  121.     /**
  122.      * Updates existing category DB record
  123.      *
  124.      * @param DB_Connection $dbc 
  125.      * @return integer On success returns category ID, on failure - false
  126.      * @internal
  127.      */
  128.     protected function update(DB_Connection $dbc{
  129.         $id $this->get_id();
  130.  
  131.         $hid $this->get_hid();
  132.         $name addslashes($this->get_name());
  133.         $description addslashes($this->get_description());
  134.  
  135.         $sql "update tmod_categories set ".
  136.                                   "hid = '$hid', ".
  137.                                   "name = '$name', ".
  138.                                   "description = '$description".
  139.                     "where id = ".$id;
  140.  
  141.         $dbc->execute($sql);
  142.         $ret $id;
  143.  
  144.  
  145.         return $ret;
  146.     }
  147.  
  148.  
  149.     /**
  150.      * Gets SQL for grid
  151.      *
  152.      * @return string 
  153.      */
  154.     public static function get_sql_for_grid({
  155.         $sql "select id, hid, name, description from tmod_categories order by name asc";
  156.  
  157.         return $sql;
  158.     }
  159.  
  160.  
  161.     /**
  162.      * Gets count SQL for grid
  163.      *
  164.      * @return string 
  165.      */
  166.     public static function get_sql_count_for_grid({
  167.         $sql "select count(id) as total_rows from tmod_categories";
  168.  
  169.         return $sql;
  170.     }
  171.  
  172.  
  173.     /**
  174.      * Checks if HID is unique
  175.      *
  176.      * @param DB_Connection $dbc 
  177.      * @param string $hid HID to check if exists
  178.      * @param integer $id ID of current category
  179.      * @return boolean 
  180.      */
  181.     public static function is_unique_hid(DB_Connection $dbc$hid$id 0{
  182.         tangra_if_not_int_throw_e($id);
  183.         $hid addslashes($hid);
  184.  
  185.         $sql "select id from tmod_categories where hid = '$hid' and id <> $id";
  186.         $rez $dbc->execute($sql);
  187.  
  188.         return $rez->is_eod();
  189.     }
  190.  
  191.  
  192.     /**
  193.      * Loads category by HID
  194.      *
  195.      * @param DB_Connection $dbc 
  196.      * @param string $hid 
  197.      * @return integer On success returns category ID, on failure - false
  198.      */
  199.     public function load_by_hid(DB_Connection $dbc$hid{
  200.         $hid addslashes($hid);
  201.         $ret false;
  202.  
  203.         $sql "select id from tmod_categories where hid = '$hid'";
  204.         $rez $dbc->execute($sql);
  205.  
  206.         if (!$rez->is_eod()) {
  207.             $rez_obj $rez->fetch_object();
  208.             $ret $this->load_by_id($dbc$rez_obj->ID);
  209.         }
  210.  
  211.         return $ret;
  212.     }
  213. }