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

Source for file tangra_module_license_dbc.class.php

Documentation is available at tangra_module_license_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. require_once('tangra_module_license.class.php');
  21.  
  22. /**
  23.  * Tangra_Module_License_DBC
  24.  *
  25.  * @package  tangra_lib
  26.  * @subpackage  modules_manager
  27.  
  28.  */
  29.     /**
  30.      * Saves license into DB
  31.      *
  32.      * @param DB_Connection $dbc 
  33.      * @return integer On success returns module 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 license from DB
  52.      *
  53.      * @param DB_Connection $dbc 
  54.      * @param integer $id 
  55.      * @return integer On success returns module ID, on failure - false
  56.      */
  57.     public function load_by_id(DB_Connection $dbc$id{
  58.         $ret false;
  59.  
  60.         $sql "select name, ".
  61.                         "foss, ".
  62.                         "hid ".
  63.                     "from tmods_licenses ".
  64.                     "where id = $id";
  65.         $rez $dbc->execute($sql);
  66.  
  67.         if (!$rez->is_eof()) {
  68.             $rez_obj $rez->fetch_object();
  69.  
  70.             $this->set_id($id);
  71.             $this->set_name(stripslashes($rez_obj->NAME));
  72.             $this->set_hid($rez_obj->HID);
  73.             $this->set_foss($rez_obj->FOSS);
  74.  
  75.             $ret $id;
  76.         }
  77.  
  78.         return $ret;
  79.     }
  80.  
  81.  
  82.     /**
  83.      * Inserts DB record for license
  84.      *
  85.      * @param DB_Connection $dbc 
  86.      * @return integer On success returns module ID, on failure - false
  87.      * @internal
  88.      */
  89.     protected function insert(DB_Connection $dbc{
  90.         $ret false;
  91.  
  92.         $id $dbc->generate_id('tmods_licenses_seq');
  93.  
  94.         $name addslashes($this->get_name());
  95.         $foss $this->get_foss(0;
  96.         $hid $this->get_hid();
  97.  
  98.         if ($id{
  99.             $sql "insert into tmods_licenses (id, ".
  100.                                             "name, ".
  101.                                             "foss, ".
  102.                                             "hid ".
  103.                                           ") ".
  104.                                 "values ".
  105.                                           "($id,".
  106.                                           "'$name', ".
  107.                                           "$foss".
  108.                                           "'$hid".
  109.                                           ")";
  110.             $dbc->execute($sql);
  111.             $ret $id;
  112.         else {
  113.             throw new TE_Exception('ID not generated - tmods_licenses_seq');
  114.         }
  115.  
  116.         return $ret;
  117.     }
  118.  
  119.  
  120.     /**
  121.      * Updated license DB record
  122.      *
  123.      * @param DB_Connection $dbc 
  124.      * @return integer On success returns module ID, on failure - false
  125.      * @internal
  126.      */
  127.     protected function update(DB_Connection $dbc{
  128.         $id $this->get_id();
  129.  
  130.         $name addslashes($this->get_name());
  131.         $foss $this->get_foss(0;
  132.         $hid $this->get_hid();
  133.  
  134.         $sql "update tmods_licenses set ".
  135.                                   "name = '$name', ".
  136.                                   "foss = $foss".
  137.                                   "hid = '$hid".
  138.                     "where id = ".$id;
  139.  
  140.         $dbc->execute($sql);
  141.         $ret $id;
  142.  
  143.  
  144.         return $ret;
  145.     }
  146.  
  147.  
  148.     /**
  149.      * Gets SQL for grid
  150.      *
  151.      * @return string SQL statement
  152.      */
  153.     public static function get_sql_for_grid({
  154.         $sql "select id, name, foss from tmods_licenses order by name asc";
  155.  
  156.         return $sql;
  157.     }
  158.  
  159.  
  160.     /**
  161.      * Gets count SQL for grid
  162.      *
  163.      * @return string SQL statement
  164.      */
  165.     public static function get_sql_count_for_grid({
  166.         $sql "select count(id) as total_rows from tmods_licenses";
  167.  
  168.         return $sql;
  169.     }
  170.  
  171.  
  172.     /**
  173.      * Loads options labels map for form_field_select
  174.      *
  175.      * @param DB_Connection $dbc 
  176.      * @return array 
  177.      */
  178.     public static function load_types_ol_map(DB_Connection $dbc{
  179.         $sql "select id, name from tmods_licenses order by id asc";
  180.         $rez $dbc->execute($sql);
  181.  
  182.         $ret array();
  183.         $ids array();
  184.  
  185.         while($rez_obj $rez->fetch_object()) {
  186.             array_push($ids$rez_obj->ID);
  187.             $ret['ol_map'][$rez_obj->ID$rez_obj->NAME;
  188.         }
  189.  
  190.         if ($ids{
  191.             $ret['ids'$ids;
  192.         }
  193.  
  194.         return $ret;
  195.     }
  196.  
  197.  
  198.     /**
  199.      * Checks if HID is unique
  200.      *
  201.      * @param DB_Connection $dbc 
  202.      * @param string $hid HID to check if exists
  203.      * @param integer $id ID of current category
  204.      * @return boolean 
  205.      */
  206.     public static function is_unique_hid(DB_Connection $dbc$hid$id 0{
  207.         tangra_if_not_int_throw_e($id);
  208.         $hid addslashes($hid);
  209.  
  210.         $sql "select id from tmods_licenses where hid = '$hid' and id <> $id";
  211.         $rez $dbc->execute($sql);
  212.  
  213.         return $rez->is_eod();
  214.     }
  215.  
  216.  
  217.     /**
  218.      * Loads license by HID
  219.      *
  220.      * @param DB_Connection $dbc 
  221.      * @param string $hid 
  222.      * @return integer On success returns module ID, on failure - false
  223.      */
  224.     public function load_by_hid(DB_Connection $dbc$hid{
  225.         $hid addslashes($hid);
  226.  
  227.         $ret false;
  228.  
  229.         $sql "select id from tmods_licenses where hid = '$hid'";
  230.         $rez $dbc->execute($sql);
  231.         if (!$rez->is_eod()) {
  232.             $rez_obj $rez->fetch_object();
  233.             $ret $this->load_by_id($dbc$rez_obj->ID);
  234.         }
  235.  
  236.         return $ret;
  237.     }
  238. }