sourceml devient un plugin mtweb
[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             $groupes["list"][$v_rst["id"]] = $v_rst;
30             $groupes["list"][$v_rst["id"]]["image_uri"] = (
31               $v_rst["image"] ?
32                 $env->path("content")."uploads/".$v_rst["image"]
33               : ""
34             );
35           }
36           $sgbd->free_result($rst);
37         }
38       }
39       catch(Exception $e){
40         return false;
41       }
42       return $groupes;
43     }
44
45     function groupe($id){
46       $sgbd = $this->sgbd();
47       $env = $this->env();
48       $groupe = array();
49       try{
50         $sql = "SELECT * from #--sml_authors WHERE id=".$this->eq($id);
51         $rst = $sgbd->query($sql);
52         if($v_rst = $sgbd->fetch_assoc($rst)){
53           $groupe = $v_rst;
54           $groupe["image_uri"] = (
55             $groupe["image"] ?
56               $env->path("content")."uploads/".$groupe["image"]
57             : ""
58           );
59         }
60         $sgbd->free_result($rst);
61       }
62       catch(Exception $e) { return false; }
63       return $groupe;
64     }
65
66     function groupe_exists($nom, $other_than_id = null){
67       $sgbd = $this->sgbd();
68       $EXISTS = 0;
69       try{
70         $sql = "SELECT count(*) as n from #--sml_authors WHERE nom=".$this->eq($nom);
71         if(isset($other_than_id)) $sql .= " AND id!=".$this->eq($other_than_id);
72         $rst = $sgbd->query($sql);
73         if($v_rst = $sgbd->fetch_assoc($rst)) $EXISTS = $v_rst["n"];
74         $sgbd->free_result($rst);
75       }
76       catch(Exception $e){
77         return false;
78       }
79       return $EXISTS;
80     }
81
82     function add_groupe($id_user, $nom, $image, $description, $email, $contact_form, $captcha){
83       $sgbd = $this->sgbd();
84       try{
85         $sql =
86          "INSERT INTO #--sml_authors(id_user, nom, image, description, email, contact_form, captcha) VALUES"
87         ."( ".$this->eq($id_user)
88         .", ".$this->eq($nom)
89         .", ".$this->eq($image)
90         .", ".$this->eq($description)
91         .", ".$this->eq($email)
92         .", ".$this->eq($contact_form)
93         .", ".$this->eq($captcha)
94         .")";
95         $sgbd->query($sql);
96       }
97       catch(Exception $e){
98         return false;
99       }
100       return true;
101     }
102
103     function set_groupe($id, $nom, $image, $description, $email, $contact_form, $captcha){
104       if(($groupe = $this->groupe($id)) === false) return false;
105       $sgbd = $this->sgbd();
106       try{
107         $sql =
108          "UPDATE #--sml_authors SET"
109         ."  nom=".$this->eq($nom)
110         .", image=".$this->eq($image)
111         .", description=".$this->eq($description)
112         .", email=".$this->eq($email)
113         .", contact_form=".$this->eq($contact_form)
114         .", captcha=".$this->eq($captcha)
115         ." WHERE id=".$id;
116         $sgbd->query($sql);
117       }
118       catch(Exception $e){
119         return false;
120       }
121       if($nom != $groupe["nom"]){
122         $groupe["nom"] = $nom;
123         if(!$this->maj_source_xml_groupe($groupe)) return false;
124       }
125       return true;
126     }
127
128     function del_groupe($id){
129       $sgbd = $this->sgbd();
130       try{
131         $sql = "SELECT count(*) as n FROM #--sml_sources_authors WHERE id_author=".$this->eq($id);
132         $rst = $sgbd->query($sql);
133         if($v_rst = $sgbd->fetch_assoc($rst)) $HAS_SOURCES = $v_rst["n"];
134         $sgbd->free_result($rst);
135         if($HAS_SOURCES) return 1;
136         $sql = "DELETE FROM #--sml_authors WHERE id=".$this->eq($id);
137         $sgbd->query($sql);
138       }
139       catch(Exception $e){
140         return false;
141       }
142       return true;
143     }
144
145   }
146
147 ?>