90445bb2c5bf3fd559268f0b7863088ee27faed0
[mw_sourceml] / app / data / modules / sql / sml_data_licences.php
1 <?php
2
3   class sml_data_licences extends mw_data{
4
5     function licences($start = null){
6       $sgbd = $this->sgbd();
7       $env = $this->env();
8       $licences = array("list" => array(), "total" => 0);
9       try{
10         $SELECT = "SELECT *";
11         $FROM = " FROM #--sml_licences";
12         $WHERE = "";
13         $LIMIT = (isset($start) && $env->config("max_list") ? " LIMIT ".$env->config("max_list")." OFFSET ".$start : "");
14         $sql = "SELECT count(*) as n FROM(".$SELECT.$FROM.$WHERE.") res";
15         $rst = $sgbd->query($sql);
16         if($v_rst = $sgbd->fetch_assoc($rst)) $licences["total"] = $v_rst["n"];
17         $sgbd->free_result($rst);
18         if($licences["total"] > 0){
19           $sql = "SELECT * FROM(".$SELECT.$FROM.$WHERE.$LIMIT.") res";
20           $rst = $sgbd->query($sql);
21           while($v_rst = $sgbd->fetch_assoc($rst)) $licences["list"][$v_rst["id"]] = $v_rst;
22           $sgbd->free_result($rst);
23         }
24       }
25       catch(Exception $e) { $licences = false; }
26       return $licences;
27     }
28
29     function licence($id){
30       $sgbd = $this->sgbd();
31       $licence = array();
32       try{
33         $sql = "SELECT * from #--sml_licences WHERE id=".$this->eq($id);
34         $rst = $sgbd->query($sql);
35         if($v_rst = $sgbd->fetch_assoc($rst)) $licence = $v_rst;
36         $sgbd->free_result($rst);
37       }
38       catch(Exception $e) { $licence = false; }
39       return $licence;
40     }
41
42     function add_licence($nom, $url){
43       $sgbd = $this->sgbd();
44       try{
45         $sql =
46          "INSERT INTO #--sml_licences(nom, url) VALUES"
47         ."( ".$this->eq($nom)
48         .", ".$this->eq($url)
49         .")";
50         $sgbd->query($sql);
51       }
52       catch(Exception $e) { return false; }
53       return true;
54     }
55
56     function set_licence($id, $nom, $url){
57       if(($licence = $this->licence($id)) !== false){
58         $sgbd = $this->sgbd();
59         try{
60           $sql =
61            "UPDATE #--sml_licences SET"
62           ."  nom=".$this->eq($nom)
63           .", url=".$this->eq($url)
64           ." WHERE id=".$id;
65           $sgbd->query($sql);
66           if($nom != $licence["nom"] || $url != $licence["url"]){
67             $licence["nom"] = $nom;
68             $licence["url"] = $url;
69             if(!$this->maj_source_xml_licence($licence)) return false;
70           }
71         }
72         catch(Exception $e) { return false; }
73         return true;
74       }
75       return false;
76     }
77
78     function del_licence($id){
79       $sgbd = $this->sgbd();
80       try{
81         $sql = "SELECT count(*) as n FROM #--sml_sources WHERE licence=".$this->eq($id);
82         $rst = $sgbd->query($sql);
83         if($v_rst = $sgbd->fetch_assoc($rst)) $USED = $v_rst["n"];
84         $sgbd->free_result($rst);
85         if($USED) return 1;
86         $sql = "DELETE FROM #--sml_licences WHERE id=".$this->eq($id);
87         $sgbd->query($sql);
88       }
89       catch(Exception $e) { return false; }
90       return true;
91     }
92
93   }
94
95 ?>