Code

Moved systems
[gosa.git] / gosa-plugins / systems / admin / systems / services / class_goService.inc
1 <?php
3 class goService extends plugin{
4         
5   var $cli_summary      = "This plugin is used within the ServerService plugin.";
6   var $cli_description  = "Some longer text\nfor help";
7   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* This plugin only writes its objectClass */
10   var $objectclasses    = array();
11   var $attributes       = array();
12   var $StatusFlag       = "";
13  
14   /* This class can't be assigned twice so it conflicts with itsself */
15   var $conflicts            = array();
16   var $dn                   = NULL;
17   var $cn                   = "";
18   var $DisplayName          = "";
19   var $view_logged  =FALSE;
21    
22   /* Construcktion */ 
23   function goService(&$config,$dn)
24   {
25     plugin::plugin($config,$dn);
26     $this->DisplayName = _("Empty service");
27   }
29   
30   /* Create content */
31   function execute()
32   {
33     if($this->is_account && !$this->view_logged){
34       $this->view_logged = TRUE;
35       new log("view","server/".get_class($this),$this->dn);
36     }
38     $str ="<div style='width:100%; text-align:right;'>".
39           "  <input type='submit' name='SaveService' value='"._("Save")."'>&nbsp;".
40           "  <input type='submit' name='CancelService' value='"._("Cancel")."'>".
41           "</div>";
42     return($str);
43   }
46   /* Get service information for serverService plugin */
47   function getListEntry()
48   {
49     
50     $this->updateStatusState();
52     /* Assign status flag */
53     if(!empty($this->StatusFlag)){
54       $flag                   = $this->StatusFlag;
55       $fields['Status']       = $this->$flag;
56     }else{
57       $fields['Status']       = "";
58     }
60     /* Name displayed in service overview */
61     $fields['Message']      = _("Empty service");
63     /* Allow/disallow some functions */
64     $fields['AllowStart']   = $this->acl_is_writeable("start");
65     $fields['AllowStop']    = $this->acl_is_writeable("stop");
66     $fields['AllowRestart'] = $this->acl_is_writeable("restart");
67     $fields['AllowRemove']  = $this->acl_is_removeable();
68     $fields['AllowEdit']    = true;
69     return($fields);
70   }
73   /* Remove service */
74   function remove_from_parent()
75   {
76     if(!$this->initially_was_account || !$this->acl_is_removeable()){
77       return;
78     }
79     
80     plugin::remove_from_parent();
82     /* Remove status flag, it is not a memeber of 
83         this->attributes, so ensure that it is deleted too */
84     if(!empty($this->StatusFlag)){
85       $this->attrs[$this->StatusFlag] = array();
86     }
88     /* Check if this is a new entry ... add/modify */
89     $ldap = $this->config->get_ldap_link();
90     $ldap->cat($this->dn,array("objectClass"));
91     if($ldap->count()){
92       $ldap->cd($this->dn);
93       $ldap->modify($this->attrs);
94     }else{
95       $ldap->cd($this->dn);
96       $ldap->add($this->attrs);
97       
98     }
100     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
102     show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
103     $this->handle_post_events("remove");
104   }
107   /* Save service */
108   function save()
109   {
110     plugin::save();
111     /* Check if this is a new entry ... add/modify */
112     $ldap = $this->config->get_ldap_link();
113     $ldap->cat($this->dn,array("objectClass"));
114     if($ldap->count()){
115       $ldap->cd($this->dn);
116       $ldap->modify($this->attrs);
117     }else{
118       $ldap->cd($this->dn);
119       $ldap->add($this->attrs);
120     }
121     if($this->initially_was_account){
122       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
123       $this->handle_post_events("modify");
124     }else{
125       $this->handle_post_events("add");
126       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
127     }
128     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
129   }
132   /* Directly save new status flag */
133   function setStatus($value)
134   {
135     if($value == "none") return;
137     /* Can't set status flag for new services (Object doesn't exists in ldap tree) */
138     if(!$this->initially_was_account) return;
140     /* Can't set status flag, if no flag is specified  */
141     if(empty($this->StatusFlag)){
142       return;
143     }
145     /* Get object (server), update status flag and save changes */
146     $ldap = $this->config->get_ldap_link();
147     $ldap->cd($this->dn);
148     $ldap->cat($this->dn,array("objectClass"));
149     if($ldap->count()){
151       $tmp = $ldap->fetch();
152       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
153         $attrs['objectClass'][] = $tmp['objectClass'][$i];
154       }
155       $flag = $this->StatusFlag;
156       $attrs[$flag] = $value;
157       $this->$flag = $value;
158       $ldap->modify($attrs);
159       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
160       $this->action_hook();
161     }
162   }
165   function check()
166   { 
167     $message = plugin::check();
168     return($message);
169   }
170   
172   function save_object()
173   {
174     plugin::save_object();
175   }  
177   
178   function action_hook($add_attrs= array())
179   {
180     /* Find postcreate entries for this class */
181     $command= $this->config->search(get_class($this), "ACTION_HOOK",array('menu','tabs'));
182     if ($command != ""){
184       /* Walk through attribute list */
185       foreach ($this->attributes as $attr){
186         if (!is_array($this->$attr)){
187           $command= preg_replace("/%$attr/", $this->$attr, $command);
188         }
189       }
190       $command= preg_replace("/%dn/", $this->dn, $command);
192       /* Additional attributes */
193       foreach ($add_attrs as $name => $value){
194         $command= preg_replace("/%$name/", $value, $command);
195       }
197       /* If there are still some %.. in our command, try to fill these with some other class vars */
198       if(preg_match("/%/",$command)){
199         $attrs = get_object_vars($this);
200         foreach($attrs as $name => $value){
201           if(!is_string($value)) continue;
202           $command= preg_replace("/%$name/", $value, $command);
203         }
204       }
206       if (check_command($command)){
207         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
208             $command, "Execute");
210         exec($command);
211       } else {
212         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
213         print_red ($message);
214       }
215     }
216   }
219   /* Get updates for status flag */
220   function updateStatusState()
221   {
222     if(empty($this->StatusFlag)) return;
224     $attrs = array();
225     $flag = $this->StatusFlag;
226     $ldap = $this->config->get_ldap_link();
227     $ldap->cd($this->cn);
228     $ldap->cat($this->dn,array($flag));
229     if($ldap->count()){
230       $attrs = $ldap->fetch();
231     }
232     if(isset($attrs[$flag][0])){
233       $this->$flag = $attrs[$flag][0];
234     }
235   }
237 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
238 ?>