syntaxe POO (visibilite) et maj indentation
[mw_sourceml] / app / data / modules / sql / sml_data_licences.php
1 <?php
2
3   class sml_data_licences extends mw_data{
4
5     public 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){
26         $licences = false;
27       }
28       return $licences;
29     }
30
31     public function licence($id){
32       $sgbd = $this->sgbd();
33       $licence = array();
34       try{
35         $sql = "SELECT * from #--sml_licences WHERE id=".$this->eq($id);
36         $rst = $sgbd->query($sql);
37         if($v_rst = $sgbd->fetch_assoc($rst)) $licence = $v_rst;
38         $sgbd->free_result($rst);
39       }
40       catch(Exception $e){
41         $licence = false;
42       }
43       return $licence;
44     }
45
46     public function add_licence($nom, $url){
47       $sgbd = $this->sgbd();
48       try{
49         $sql =
50          "INSERT INTO #--sml_licences(nom, url) VALUES"
51         ."( ".$this->eq($nom)
52         .", ".$this->eq($url)
53         .")";
54         $sgbd->query($sql);
55       }
56       catch(Exception $e){
57         return false;
58       }
59       return true;
60     }
61
62     public function set_licence($id, $nom, $url){
63       if(($licence = $this->licence($id)) !== false){
64         $sgbd = $this->sgbd();
65         try{
66           $sql =
67            "UPDATE #--sml_licences SET"
68           ."  nom=".$this->eq($nom)
69           .", url=".$this->eq($url)
70           ." WHERE id=".$id;
71           $sgbd->query($sql);
72           if($nom != $licence["nom"] || $url != $licence["url"]){
73             $licence["nom"] = $nom;
74             $licence["url"] = $url;
75             if(!$this->maj_source_xml_licence($licence)) return false;
76           }
77         }
78         catch(Exception $e){
79           return false;
80         }
81         return true;
82       }
83       return false;
84     }
85
86     public function del_licence($id){
87       $sgbd = $this->sgbd();
88       try{
89         $sql = "SELECT count(*) as n FROM #--sml_sources WHERE licence=".$this->eq($id);
90         $rst = $sgbd->query($sql);
91         if($v_rst = $sgbd->fetch_assoc($rst)) $USED = $v_rst["n"];
92         $sgbd->free_result($rst);
93         if($USED) return 1;
94         $sql = "DELETE FROM #--sml_licences WHERE id=".$this->eq($id);
95         $sgbd->query($sql);
96       }
97       catch(Exception $e){
98         return false;
99       }
100       return true;
101     }
102
103   }