81d308d08ddc2b3a16a2880840923ac9cdb7986b
[mw_sourceml] / app / data / modules / sql / sml_data_authors.php
1 <?php
2
3   class sml_data_authors extends mw_data
4   {
5
6     # ----------------------------------------------------------------------------------------
7     #                                                                                  groupes
8     #
9
10     function groupes($id_user = null, $start = null, $alpha = null){
11       $sgbd = $this->sgbd();
12       $env = $this->env();
13       $groupes = array("list" => array(), "total" => 0);
14       try{
15         $SELECT = "SELECT *";
16         $FROM = " FROM #--sml_authors";
17         $WHERE = "";
18         $WHERE .= (isset($id_user) ? ($WHERE ? " AND" : " WHERE")." id_user=".$id_user : "");
19         $WHERE .= (isset($alpha) ? ($WHERE ? " AND" : " WHERE")." LEFT(login, 1)=".$this->eq($alpha) : "");
20         $LIMIT = (isset($start) && $env->config("max_list") ? " LIMIT ".$env->config("max_list")." OFFSET ".$start : "");
21         $sql = "SELECT count(*) as n FROM(".$SELECT.$FROM.$WHERE.") res";
22         $rst = $sgbd->query($sql);
23         if($v_rst = $sgbd->fetch_assoc($rst)) $groupes["total"] = $v_rst["n"];
24         $sgbd->free_result($rst);
25         if($groupes["total"] > 0){
26           $sql = "SELECT * FROM(".$SELECT.$FROM.$WHERE.$LIMIT.") res";
27           $rst = $sgbd->query($sql);
28           while($v_rst = $sgbd->fetch_assoc($rst)){
29             if(!isset($v_rst["image"])) $v_rst["image"] = "";
30             $groupes["list"][$v_rst["id"]] = $v_rst;
31             $groupes["list"][$v_rst["id"]]["image_uri"] = (
32               $v_rst["image"] ?
33                 $env->path("content")."uploads/".$v_rst["image"]
34               : ""
35             );
36           }
37           $sgbd->free_result($rst);
38         }
39       }
40       catch(Exception $e){
41         return false;
42       }
43       return $groupes;
44     }
45
46     function groupe($id){
47       $sgbd = $this->sgbd();
48       $env = $this->env();
49       $groupe = array();
50       try{
51         $sql = "SELECT * from #--sml_authors WHERE id=".$this->eq($id);
52         $rst = $sgbd->query($sql);
53         if($v_rst = $sgbd->fetch_assoc($rst)){
54           if(!isset($v_rst["image"])) $v_rst["image"] = "";
55           $groupe = $v_rst;
56           $groupe["image_uri"] = (
57             $groupe["image"] ?
58               $env->path("content")."uploads/".$groupe["image"]
59             : ""
60           );
61         }
62         $sgbd->free_result($rst);
63       }
64       catch(Exception $e) { return false; }
65       return $groupe;
66     }
67
68     function groupe_exists($nom, $other_than_id = null){
69       $sgbd = $this->sgbd();
70       $EXISTS = 0;
71       try{
72         $sql = "SELECT count(*) as n from #--sml_authors WHERE nom=".$this->eq($nom);
73         if(isset($other_than_id)) $sql .= " AND id!=".$this->eq($other_than_id);
74         $rst = $sgbd->query($sql);
75         if($v_rst = $sgbd->fetch_assoc($rst)) $EXISTS = $v_rst["n"];
76         $sgbd->free_result($rst);
77       }
78       catch(Exception $e){
79         return false;
80       }
81       return $EXISTS;
82     }
83
84     function add_groupe($id_user, $nom, $image, $description, $email, $contact_form, $captcha){
85       $sgbd = $this->sgbd();
86       try{
87         $sql =
88          "INSERT INTO #--sml_authors(id_user, nom, image, description, email, contact_form, captcha) VALUES"
89         ."( ".$this->eq($id_user)
90         .", ".$this->eq($nom)
91         .", ".$this->eq($image)
92         .", ".$this->eq($description)
93         .", ".$this->eq($email)
94         .", ".$this->eq($contact_form)
95         .", ".$this->eq($captcha)
96         .")";
97         $sgbd->query($sql);
98       }
99       catch(Exception $e){
100         return false;
101       }
102       return true;
103     }
104
105     function set_groupe($id, $nom, $image, $description, $email, $contact_form, $captcha){
106       if(($groupe = $this->groupe($id)) === false) return false;
107       $sgbd = $this->sgbd();
108       try{
109         $sql =
110          "UPDATE #--sml_authors SET"
111         ."  nom=".$this->eq($nom)
112         .", image=".$this->eq($image)
113         .", description=".$this->eq($description)
114         .", email=".$this->eq($email)
115         .", contact_form=".$this->eq($contact_form)
116         .", captcha=".$this->eq($captcha)
117         ." WHERE id=".$id;
118         $sgbd->query($sql);
119       }
120       catch(Exception $e){
121         return false;
122       }
123       if($nom != $groupe["nom"]){
124         $groupe["nom"] = $nom;
125         if(!$this->maj_source_xml_groupe($groupe)) return false;
126       }
127       return true;
128     }
129
130     function del_groupe($id){
131       $sgbd = $this->sgbd();
132       try{
133         $sql = "SELECT count(*) as n FROM #--sml_sources_authors WHERE id_author=".$this->eq($id);
134         $rst = $sgbd->query($sql);
135         if($v_rst = $sgbd->fetch_assoc($rst)) $HAS_SOURCES = $v_rst["n"];
136         $sgbd->free_result($rst);
137         if($HAS_SOURCES) return 1;
138         $sql = "DELETE FROM #--sml_authors WHERE id=".$this->eq($id);
139         $sgbd->query($sql);
140       }
141       catch(Exception $e){
142         return false;
143       }
144       return true;
145     }
146
147   }
148
149 ?>