syntaxe POO (visibilite) et maj indentation
[mw_sourceml] / app / data / modules / share / sml_data_sources_xml.php
1 <?php
2
3   class sml_data_sources_xml extends mw_data{
4
5     public function get_source_xml_from_url($url, $IGNORE_UPDATE = false){
6       $env = $this->env();
7       if($env->config("cache_actif")) return $this->get_source_xml_from_cache($url, $IGNORE_UPDATE);
8       else return @file_get_contents($url);
9     }
10
11     # ----------------------------------------------------------------------------------------
12     #                                                                               XML source
13     #
14
15     public function set_source_xml($id, $params = array()){
16       $env = $this->env();
17       $OK = false;
18       if(strlen($id) > 0 && ($fh = @fopen($this->source_xml_path($id), "wb")) !== false){
19         if(($content = $this->source_xml_content($id, $params)) !== false){
20           if(@fwrite($fh, $content)) $OK = true;
21         }
22         @fclose($fh);
23       }
24       return $OK;
25     }
26
27     public function maj_source_xml_groupe($groupe){
28       $OK = true;
29       if($groupe !== false){
30         $sgbd = $this->sgbd();
31         $env = $this->env();
32         if(($sources = $this->sources(array("id_groupe" => $groupe["id"]))) !== false){
33           foreach($sources["list"] as $id_source => $source){
34             if(!$this->set_source_xml($source["id"], array("source" => $source))){
35               $OK = false;
36               break;
37             }
38           }
39         }
40         else $OK = false;
41       }
42       else $OK = false;
43       return $OK;
44     }
45
46     public function maj_source_xml_licence($licence){
47       $OK = true;
48       if($licence !== false){
49         $sgbd = $this->sgbd();
50         $env = $this->env();
51         if(($sources = $this->sources(array("id_licence" => $licence["id"]))) !== false){
52           foreach($sources["list"] as $id_source => $source){
53             if(!$this->set_source_xml($source["id"], array("source" => $source))){
54               $OK = false;
55               break;
56             }
57           }
58         }
59         else $OK = false;
60       }
61       else $OK = false;
62       return $OK;
63     }
64
65     public function maj_source_xml_all(){
66       $OK = true;
67       $sgbd = $this->sgbd();
68       $env = $this->env();
69       if(($sources = $this->sources(array())) !== false){
70         foreach($sources["list"] as $id_source => $source){
71           if(!$this->set_source_xml($source["id"], array("source" => $source))){
72             $OK = false;
73             break;
74           }
75         }
76       }
77       else $OK = false;
78       return $OK;
79     }
80
81     public function get_source_xml($id_source){
82       $source_file = $this->source_xml_path($id_source);
83       if(file_exists($source_file)){
84         return @file_get_contents($source_file);
85       }
86       return "";
87     }
88
89     public function del_source_xml($id_source){
90       $env = $this->env();
91       if(file_exists($this->source_xml_path($id_source))){
92         return
93            $this->del_edit_derivations(array("id_source" => $id_source))
94         && $this->del_edit_reference_content($id_source)
95         && @unlink($this->source_xml_path($id_source));
96       }
97       return true;
98     }
99
100     # ----------------------------------------------------------------------------------------
101     #                                                                   edition XML derivation
102     #
103
104     public function set_edit_derivation_content($id_source, $id_source_derivation, $content){
105       $env = $this->env();
106       $derivations_dir = $this->derivations_edit_xml_dir_path();
107       if(!file_exists($derivations_dir) || !is_dir($derivations_dir)) @mkdir($derivations_dir);
108       if(file_exists($derivations_dir) && is_dir($derivations_dir)){
109         $derivations_dir = $this->derivations_edit_xml_dir_path($id_source);
110         if(!file_exists($derivations_dir) || !is_dir($derivations_dir)) @mkdir($derivations_dir);
111         if(file_exists($derivations_dir) && is_dir($derivations_dir)){
112           if($fh = @fopen($this->derivation_edit_xml_path($id_source, $id_source_derivation), "w+")){
113             if(@fwrite($fh, $content)){
114               @fclose($fh);
115               return true;
116             }
117             @fclose($fh);
118           }
119         }
120       }
121       return false;
122     }
123     
124     public function get_edit_derivation_content($id_source, $id_source_derivation){
125       $env = $this->env();
126       $derivation_file = $this->derivation_edit_xml_path($id_source, $id_source_derivation);
127       $derivation_content = "";
128       if(file_exists($derivation_file)){
129         $derivation_content = @file_get_contents($derivation_file);
130       }
131       return $derivation_content;
132     }
133
134     public function del_edit_derivation_content($id_source, $id_source_derivation){
135       $env = $this->env();
136       $derivation_file = $this->derivation_edit_xml_path($id_source, $id_source_derivation);
137       $OK = true;
138       if(file_exists($derivation_file)){
139         $OK = @unlink($derivation_file);
140       }
141       return $OK;
142     }
143
144     public function del_edit_derivations($id_source){
145       $env = $this->env();
146       $derivations_dir = $this->derivations_edit_xml_dir_path($id_source);
147       $OK = true;
148       if(is_dir($derivations_dir)){
149         if($dh = opendir($derivations_dir)){
150           while($OK && (($file = readdir($dh)) !== false)){
151             if(preg_match("/^.+\.xml$/", $file)){
152               $OK = @unlink($derivations_dir.$file) && $OK;
153             }
154           }
155           closedir($dh);
156 //          $OK = @rmdir($derivations_dir) && $OK;
157           @rmdir($derivations_dir);
158         }
159         else $OK = false;
160       }
161       return $OK;
162     }
163
164     # ----------------------------------------------------------------------------------------
165     #                                                                    edition XML reference
166     #
167
168     public function set_edit_reference_content($id_source, $content){
169       $env = $this->env();
170       $references_dir = $this->reference_edit_xml_dir_path();
171       if(!file_exists($references_dir) || !is_dir($references_dir)) @mkdir($references_dir);
172       if(file_exists($references_dir) && is_dir($references_dir)){
173         if($fh = @fopen($this->reference_edit_xml_path($id_source), "w+")){
174           if(@fwrite($fh, $content)){
175             @fclose($fh);
176             return true;
177           }
178           @fclose($fh);
179         }
180       }
181       return false;
182     }
183
184     public function get_edit_reference_content($id_source){
185       $env = $this->env();
186       $reference_file = $this->reference_edit_xml_path($id_source);
187       $reference_content = "";
188       if(file_exists($reference_file)){
189         $reference_content = @file_get_contents($reference_file);
190       }
191       return $reference_content;
192     }
193
194     public function del_edit_reference_content($id_source){
195       $env = $this->env();
196       $reference_file = $this->reference_edit_xml_path($id_source);
197       $OK = true;
198       if(file_exists($reference_file)){
199         $OK = @unlink($reference_file);
200       }
201       return $OK;
202     }
203
204   }