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