Code

4fee0e65ed54c90a21df4a80201884141852e636
[gosa.git] / trunk / gosa-plugins / fai / admin / systems / services / repository / class_servRepository.inc
1 <?php
3 class servrepository extends goService
4 {
5   /* attribute list for save action */
6   //  var $ignore_account         = TRUE;
7   var $attributes             = array("FAIrepository");
8   var $objectclasses          = array("FAIrepositoryServer");
9   
10   /* Repositories */
11   var $repositories          = array();
12   var $FAIrepository         = array();
13   var $conflicts             = array("servrepository");
14   var $DisplayName           = "";
15   var $StatusFlag            = "";
16   
17   var $view_logged            = FALSE;
18   var $fai_activated          = FALSE;
20   var $divlist = NULL;
22   function servrepository (&$config, $dn= NULL, $parent= NULL)
23   {
24     plugin::plugin ($config, $dn,$parent);
26     $this->DisplayName = _("Repository service");
28     /* Skip this if fai is deactivated */
29     $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
30     if(!empty($tmp)){
31       $this->fai_activated = TRUE;    
32     }else{
33       return;
34     }
36     $this->repositories = array();
37     if(isset($this->attrs['FAIrepository'])){
38       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
39         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
40         $tmp2 = array();  
41         $tmp3 = array();   
43         if(isset($tmp[1])){
44           $tmp2['ParentServer'] = $tmp[1];
45           if(empty($tmp[1])){
46             $tmp2['ParentServer'] = "none";
47           }
48         }else{
49           $tmp2['ParentServer'] = "none";
50         }
52         if(isset($tmp[0])){
53           $tmp2['Url']          = $tmp[0];
54         }else{
55           $tmp2['Url']          = "";
56         }
57   
58         if(isset($tmp[2])){
59           $tmp2['Release']      = $tmp[2];
60         }else{
61           $tmp2['Release']      = "";
62         }
64         if(isset($tmp[3])){
65           $tmp3 = split(",",$tmp[3]);
66           foreach($tmp3 as $sec){
67             $tmp2['Sections'][$sec]=$sec;
68           }    
69         }else{
70           $tmp['Section']=array();
71         }
73         $this->repositories[$tmp[2]]=$tmp2;      
74       }
75     }
78     /* Create divlist */
79     $this->divlist = new divListRepository($this->config,$this);
80   }
82   function execute()
83   {
84     /* Call parent execute */
85     plugin::execute();
87     if($this->is_account && !$this->view_logged){
88       $this->view_logged = TRUE;
89       new log("view","server/".get_class($this),$this->dn);
90     }
92     if(!$this->fai_activated){
93       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
94       return $str;
95     }
97     /* Fill templating stuff */
98     $smarty= get_smarty();
99     $smarty->assign("is_createable",$this->acl_is_createable());
100     $display= "";
102     /* Show tab dialog headers */
103     /*
104        ADD / EDIT Repository
105        Dialog Handling
106      */
107     $once = false;
108     if(isset($_POST['servRepository'])){
109       foreach($_POST as $name => $value){
111         if(preg_match("/AddRepository/",$name) && $this->acl_is_createable()){
112           $once = true;
113           $this->dialog = new servRepositorySetup($this->config,$this->dn);
114           $this->dialog->parent = $this;
115         }
117         if((preg_match("/^delete_/",$name)) && (!$once) && $this->acl_is_removeable()){
118           $once = true;
119           $value = preg_replace("/delete_/","",$name);
120           $value = base64_decode(preg_replace("/_.*$/","",$value));
122           $url = $this->repositories[$value]['Url'];
123           $release = $this->repositories[$value]['Release'];
125           $ldap = $this->config->get_ldap_link();
126           $ldap->cd ($this->config->current['BASE']);
128           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
129           if ($ldap->count() != 0){
130             $obj= array();
131             $found= false;
132             while($attrs = $ldap->fetch()){
133               foreach($attrs['FAIclass'] as $class){
134                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
135                   $obj[$ldap->getDN()]= $attrs['cn'][0];
136                   $found= true;
137                 }
138               }
139             }
141             if ($found){
142               msg_dialog::display(_("Error"), msgPool::stillInUse(_("FAI release"), msgPool::buildList($obj)), ERROR_DIALOG);
143             }else{
144               if(isset($this->repositories[$value])){
145                 unset($this->repositories[$value]);
146               }
147             }
148           }
149         }
151         if((preg_match("/^edit_/",$name))&&(!$once)){
152           $value = preg_replace("/edit_/","",$name);
153           $value = base64_decode(preg_replace("/_.$/","",$value));
155           if(isset($this->repositories[$value])){
156             $once = true;
157             $obj = $this->repositories[$value];
159             /* to be able to detect if this was renamed */
160             $obj['initialy_was'] = $obj['Release'];
161             $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
162             $this->dialog->parent = $this;
163           }
164         }
165       }
166     }
167     if((isset($_GET['act']))&&($_GET['act']=="open_repository")&&(isset($_GET['id']))){
168       $obj = $this->repositories[base64_decode($_GET['id'])];
169       $obj['initialy_was'] = $obj['Release'];
170       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
171       $this->dialog->parent = $this;
172     }
174     if(isset($_POST['repository_setup_save']) && is_object($this->dialog)){
175       $this->dialog->save_object();
176       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
177         msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
178       }else
180       if(count($this->dialog->check())!=0){
181         foreach($this->dialog->check() as $msg){
182           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
183         }
184       }else{
185         $obj = $this->dialog->save();
186         if($this->dialog->is_new_name()){
187           $oldname = $this->dialog->initialy_was;
188           $this->repositories[$obj['Release']]=$obj;        
189           unset($this->repositories[$oldname]);
190         }else{ 
191           $this->repositories[$obj['Release']]=$obj;        
192         }
193         $this->dialog = FALSE;
194         $this->is_dialog= false;
195       }
196     }
198     if(isset($_POST['repository_setup_cancel'])){
199       $this->dialog=FALSE;
200       $this->is_dialog = false;
201     }
202    
203     if(is_object($this->dialog)){
204       $this->dialog->save_object();
205       $this->is_dialog = true;
206       return($this->dialog->execute());
207     }
209     /*
210       Repository setup dialog handling /END
211     */
214     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open_repository&amp;id=%s'>%s</a>";
215     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/lists/edit.png'>&nbsp;";
216    
217     /* Hide delete icon, if delete is not allowed */ 
218     if($this->acl_is_removeable()){
219       $delete = "<input type='image' value='%s' name='delete_%s' src='images/lists/trash.png'>";
220     }else{
221       $delete = "<img src='images/empty.png' alt='&nbsp;'>";
222     }
224     $this->divlist->execute(); 
225     $this->divlist->setEntries($this->repositories);
226     $smarty->assign("Repositories",$this->divlist->Draw());
227     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE,dirname(__FILE__)));
228     return($display);
229   }
232   /* Save data to object */
233   function save_object()
234   {
235     plugin::save_object();
236     if(is_object($this->divlist)){
237       $this->divlist->save_object();
238     }
239   }
241   function allow_remove()
242   {
243     /* Check if the service is still in use */
244     $ldap = $this->config->get_ldap_link();
245     $ldap->cd($this->config->current['BASE']);
246     foreach($this->repositories as $repository) {
247       $url = $repository['Url'];
248       $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
249       if ($ldap->count() != 0){
250         $obj= array();
251         $found= false;
252         while($attrs = $ldap->fetch()){
253           foreach($attrs['FAIclass'] as $class){
254               $obj[$ldap->getDN()]= $attrs['cn'][0];
255               $found= true;
256           }
257         }
259         if ($found){
260           $message = msgPool::stillInUse(_("Repository service")) . msgPool::buildList($obj);
261           return $message;
262         }
263       }
264     }
265    }
268   /* Check supplied data */
269   function check()
270   {
271     /* Call common method to give check the hook */
272     $message= plugin::check();
273     return ($message);
274   }
277   /* Save to LDAP */
278   function save()
279   {
280     if(!$this->fai_activated) return;
282     plugin::save();
284     $arr = array();
285     foreach($this->repositories as $servername => $conf){
286       $str = "";
287       foreach($conf['Sections'] as $sec){
288         $str.=$sec.",";
289       }
290       $str=preg_replace("/,$/","",$str);
291         
292       if($conf['ParentServer']=="none"){
293         $conf['ParentServer'] ="";
294       }    
295     
296       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
297     }
298     $this->attrs['FAIrepository'] = $arr;
300     $ldap= $this->config->get_ldap_link();
301     $ldap->cd ($this->config->current['BASE']);
302     
303     $ldap->cat($this->dn, array('dn'));
304     
305     if($ldap->count()){
306       $ldap->cd($this->dn);
307       $this->cleanup();
308       $ldap->modify ($this->attrs);       
309       $this->handle_post_events("modify");
310     }else{
311       $ldap->cd ($this->config->current['BASE']);
312       $ldap->create_missing_trees($this->dn);
313       $ldap->cd($this->dn);
314       $ldap->add($this->attrs);
315       $this->handle_post_events("add");
316     }
318     # If there were changes, just tell the server to reload information
319     if(count($this->attrs)){
320             $this->trigger_si_fai_server_reload();
321     }
323     if($this->initially_was_account){
324       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
325     }else{
326       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
327     }
328   }
331   function getListEntry()
332   {
333     $fields = goService::getListEntry();
334     $fields['Message']    = _("Repository service");
335     return($fields);
336   }
339   function trigger_si_fai_server_reload()
340   {
341     /* Reload GOsa si FAI DB/cache
342      */
343     if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
344       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);        
345       if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_server_db'])){
346         $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_server_db']; 
347         $tmp = new $evt['CLASS_NAME']($this->config);
348         $tmp->set_type(TRIGGERED_EVENT);
349         $tmp->add_targets(array("GOSA"));
350         $o_queue = new gosaSupportDaemon();
351         if(!$o_queue->append($tmp)){
352           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
353         }
354       }
355     }
356   }
359   function remove_from_parent()
360   {
361     goService::remove_from_parent();
362     $this->trigger_si_fai_server_reload();
363   }
365   /* Return plugin informations for acl handling */
366   static function plInfo()
367   {
368     return (array(
369           "plShortName"   => _("Repository"),
370           "plDescription" => _("Repository service")." ("._("Services").")",
371           "plSelfModify"  => FALSE,
372           "plDepends"     => array(),
373           "plPriority"    => 84,
374           "plSection"     => array("administration"),
375           "plCategory"    => array("server"),
377           "plProvidedAcls"=> array(
378 #            "start"         => _("Service start"),  // Remove this to hide the start button at all.
379 #            "stop"          => _("Service stop"),   // Remove this to hide the stop button at all.
380 #            "restart"       => _("Service restart"),// Remove this to hide the restart button at all.
382 #            "cn"            => _("Name"),
383             "ParentServer"  => _("Parent server"),
384             "Release"       => _("Releases"),
385             "Url"           => _("URL"),
386             "Section"       => _("Sections"))
387           ));
388   }
391 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
392 ?>