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

Source for file tangra_module_dbc.class.php

Documentation is available at tangra_module_dbc.class.php

  1. <?php
  2.  
  3. // $Id$
  4.  
  5. /**
  6.  * Contains class Tangra_Module_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.  */
  21. require_once('tangra_module.class.php');
  22.  
  23.  
  24. /**
  25.  * Tangra_Module_DBC
  26.  *
  27.  * @package  tangra_lib
  28.  * @subpackage  modules_manager
  29.  */
  30. class Tangra_Module_DBC extends Tangra_Module implements I_DB_Storable {
  31.     /**
  32.      * Saves module into DB
  33.      *
  34.      * @param DB_Connection $dbc 
  35.      * @return integer On success returns module ID, on failure - false
  36.      */
  37.     public function save(DB_Connection $dbc{
  38.         $ret false;
  39.  
  40.         if ($this->get_id()) {
  41.             $ret $this->update($dbc);
  42.         else {
  43.             $ret $this->insert($dbc);
  44.             $this->set_id($ret);
  45.  
  46.         }
  47.  
  48.         return $ret;
  49.     }
  50.  
  51.  
  52.     /**
  53.      * Loads by ID
  54.      *
  55.      * @param DB_Connection $dbc 
  56.      * @param integer $id 
  57.      * @return integer On success returns module ID, on failure - false
  58.      */
  59.     public function load_by_id(DB_Connection $dbc$id{
  60.         $ret false;
  61.  
  62.  
  63.         $sql "select hid, ".
  64.                         "description, ".
  65.                         "maintainer, ".
  66.                         "url, ".
  67.                         "license ".
  68.                     "from tmods ".
  69.                     "where id = $id";
  70.         $rez $dbc->execute($sql);
  71.  
  72.         if (!$rez->is_eof()) {
  73.             $rez_obj $rez->fetch_object();
  74.  
  75.             $this->set_id($id);
  76.             $this->set_hid($rez_obj->HID);
  77.             $this->set_description(stripslashes($rez_obj->DESCRIPTION));
  78.             $this->set_maintainer(stripslashes($rez_obj->MAINTAINER));
  79.             $this->set_url(stripslashes($rez_obj->URL));
  80.             $this->set_license($rez_obj->LICENSE);
  81.  
  82.             $ret $id;
  83.         }
  84.  
  85.         return $ret;
  86.     }
  87.  
  88.  
  89.     /**
  90.      * Inserts new module
  91.      *
  92.      * @param DB_Connection $dbc 
  93.      * @return integer On success returns module ID, on failure - false
  94.      * @internal
  95.      */
  96.     protected function insert(DB_Connection $dbc{
  97.         $ret false;
  98.  
  99.         $id $dbc->generate_id('tmods_seq');
  100.  
  101.         $hid $this->get_hid();
  102.         $description addslashes($this->get_description());
  103.         $maintainer addslashes($this->get_maintainer());
  104.         $url addslashes($this->get_url());
  105.         $license $this->get_license();
  106.  
  107.         if ($id{
  108.             $sql "insert into tmods (id, ".
  109.                                             "hid, ".
  110.                                             "description, ".
  111.                                             "maintainer, ".
  112.                                             "url, ".
  113.                                             "license ".
  114.                                           ") ".
  115.                                 "values ".
  116.                                           "($id,".
  117.                                           "'$hid', ".
  118.                                           "'$description', ".
  119.                                           "'$maintainer', ".
  120.                                           "'$url', ".
  121.                                           "$license ".
  122.                                           ")";
  123.             $dbc->execute($sql);
  124.             $ret $id;
  125.         else {
  126.             throw new TE_Exception('ID not generated - tmods_seq');
  127.         }
  128.  
  129.         return $ret;
  130.     }
  131.  
  132.  
  133.     /**
  134.      * Updates module DB record
  135.      *
  136.      * @param DB_Connection $dbc 
  137.      * @return integer On success returns module ID, on failure - false
  138.      * @internal
  139.      */
  140.     protected function update(DB_Connection $dbc{
  141.         $id $this->get_id();
  142.  
  143.         $hid $this->get_hid();
  144.         $description addslashes($this->get_description());
  145.         $maintainer addslashes($this->get_maintainer());
  146.         $url addslashes($this->get_url());
  147.         $license $this->get_license();
  148.  
  149.         $sql "update tmods set ".
  150.                                   "hid = '$hid', ".
  151.                                   "description = '$description', ".
  152.                                   "maintainer = '$maintainer', ".
  153.                                   "url = '$url', ".
  154.                                   "license = $license ".
  155.                     "where id = ".$id;
  156.  
  157.         $dbc->execute($sql);
  158.         $ret $id;
  159.  
  160.  
  161.         return $ret;
  162.     }
  163.  
  164.  
  165.     /**
  166.      * Gets SQL for grid
  167.      *
  168.      * @return string SQL statement
  169.      */
  170.     public static function get_sql_for_grid({
  171.         $sql "select id, hid from tmods order by hid asc";
  172.  
  173.         return $sql;
  174.     }
  175.  
  176.  
  177.     /**
  178.      * Gets count SQL for grid
  179.      *
  180.      * @return string SQL statement
  181.      */
  182.     public static function get_sql_count_for_grid({
  183.         $sql "select count(id) as total_rows from tmods";
  184.  
  185.         return $sql;
  186.     }
  187.  
  188.  
  189.     /**
  190.      * Checks if HID is unique
  191.      *
  192.      * @param DB_Connection $dbc 
  193.      * @param string $hid HID to check if exists
  194.      * @param integer $id ID of current category
  195.      * @return boolean 
  196.      */
  197.     public static function is_unique_hid(DB_Connection $dbc$hid$id 0{
  198.         tangra_if_not_int_throw_e($id);
  199.         $hid addslashes($hid);
  200.  
  201.         $sql "select id from tmods where hid = '$hid' and id <> $id";
  202.         $rez $dbc->execute($sql);
  203.  
  204.         return $rez->is_eod();
  205.     }
  206.  
  207.  
  208.     /**
  209.      * Loads options labels map for Form_field_select
  210.      *
  211.      * @param DB_Connection $dbc 
  212.      * @param integer $current_module_id 
  213.      * @return array 
  214.      */
  215.     public static function load_modules_ol_map(DB_Connection $dbc$current_module_id{
  216.         tangra_if_not_int_throw_e($current_module_id);
  217.         $sql "select id, hid from tmods where id <> $current_module_id order by id asc";
  218.         $rez $dbc->execute($sql);
  219.  
  220.         $ret array();
  221.         $ids array();
  222.  
  223.         while($rez_obj $rez->fetch_object()) {
  224.             array_push($ids$rez_obj->ID);
  225.             $ret['ol_map'][$rez_obj->ID$rez_obj->HID;
  226.         }
  227.  
  228.         if ($ids{
  229.             $ret['ids'$ids;
  230.         }
  231.  
  232.         return $ret;
  233.     }
  234.  
  235.  
  236.     /**
  237.      * Loads module by HID
  238.      *
  239.      * @param DB_Connection $dbc 
  240.      * @param string $hid 
  241.      * @return integer On success returns module ID, on failure - false
  242.      */
  243.     public function load_by_hid(DB_Connection $dbc$hid{
  244.         $hid addslashes($hid);
  245.         $ret false;
  246.  
  247.         $sql "select id from tmods where hid = '$hid'";
  248.         $rez $dbc->execute($sql);
  249.  
  250.         if (!$rez->is_eod()) {
  251.             $rez_obj $rez->fetch_object();
  252.             $ret $this->load_by_id($dbc$rez_obj->ID);
  253.         }
  254.  
  255.         return $ret;
  256.     }
  257.  
  258.  
  259.     /**
  260.      * Loads all modules
  261.      *
  262.      * @param DB_Connection $dbc 
  263.      * @return array Array of Tangra_Module_DBC objects
  264.      */
  265.     public static function load_all(DB_Connection $dbc{
  266.         $sql "select id from tmods";
  267.         $rez $dbc->execute($sql);
  268.         $ret array();
  269.         while ($rez_obj $rez->fetch_object()) {
  270.             $tmp new Tangra_Module_DBC();
  271.             $tmp->load_by_id($dbc$rez_obj->ID);
  272.  
  273.             $ret[clone $tmp;
  274.         }
  275.  
  276.         return $ret;
  277.     }
  278.  
  279.  
  280.     /**
  281.      * Loads all HIDs as array
  282.      *
  283.      * @param DB_Connection $dbc 
  284.      * @return array 
  285.      */
  286.     public static function load_all_hids_as_array(DB_Connection $dbc{
  287.         $sql "select hid from tmods";
  288.         $rez $dbc->execute($sql);
  289.         $ret array();
  290.         while ($rez_obj $rez->fetch_object()) {
  291.             $ret[$rez_obj->HIDfalse;
  292.         }
  293.  
  294.         return $ret;
  295.     }
  296.  
  297.  
  298.     /**
  299.      * Deletes module
  300.      *
  301.      * @param DB_Connection $dbc 
  302.      * @param integer $id 
  303.      */
  304.     public static function delete(DB_Connection $dbc$id{
  305.         tangra_if_not_int_throw_e($id);
  306.  
  307.         $sql "delete from tmods where id = $id";
  308.         $dbc->execute($sql);
  309.     }
  310.  
  311. }