Code

Renamed opsi class
[gosa.git] / gosa-plugins / opsi / admin / opsi / class_opsigeneric.inc
1 <?php
3 class opsiGeneric extends plugin
4 {
5   private $opsi;
6   private $hostId;  
8   /* Contains a list of all available netboot products 
9    */
10   private $a_availableNetbootProducts = array();
11   private $s_selectedNetbootProduct = "";  
12   private $s_initial_selectedNetbootProduct = "";  
14   /* Contains a list of all available local products
15    */
16   private $a_availableLocalProducts = array();
17   private $a_selectedLocalProducts = array();
18   private $a_initial_selectedLocalProducts = array();
20   private $init_failed = FALSE;
22   private $parent_mode = TRUE;
23   private $is_installed = FALSE;
25   public $mac          = "";
26   public $note         = "";
27   public $description  = "";
28   public $attributes = array("mac","note","description");
30   public $parent = NULL;
32   public function __construct($config,$hostId,&$parent = NULL)
33   {
34     $this->opsi = new opsi($config); 
35     $this->is_account =TRUE;
36     
37     /* Check if we are are part of a windows workstation 
38      */
39     $this->parent = $parent;
40     if($parent instanceof wingeneric){
41       $this->parent_mode = FALSE;
42     }
44     /* Get hostId */
45     if($hostId != "new"){
46       if(preg_match("/^opsi:/",$hostId)){
47         $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
48       }elseif($this->parent instanceof wingeneric){
49         $this->hostId = $this->parent->cn;
50         $this->hostId = preg_replace('/\$$/',"",$this->hostId);
51       }
52     }
53   
54     /* Try to plugin */
55     $this->init();
56   }
57   
58   private function init()
59   {
60     $err = FALSE;
61     $this->init_failed = FALSE;
62     $this->initially_was_account = FALSE; 
64     if($this->hostId != "new"){
65       $list = $this->opsi->list_clients($this->hostId);
66       $err |= $this->opsi->is_error();
67       foreach($list as $entry){
68         if(preg_match("/^".normalizePreg($this->hostId)."$/i",$entry['NAME'][0]['VALUE'])){
69           $this->initially_was_account = TRUE; 
70           foreach(array("description" => "DESCRIPTION","mac" => "MAC", "note" => "NOTES") as $des => $src){
71             $this->$des = $entry[$src][0]['VALUE'];
72           } 
73           break;
74         }
75       }
76     }
78     /* Get product settings */     
79     if(!$err){
80       $this->a_availableNetbootProducts = $this->opsi->get_netboot_products();
81       $err |= $this->opsi->is_error();
82     }
83     if(!$err) {
84       $this->a_availableLocalProducts   = $this->opsi->get_local_products();
85       $err |= $this->opsi->is_error();
86     }
88     /* Get selected products */
89     if(!$err && !empty($this->hostId)) {
90       $tmp = array_keys($this->opsi->get_netboot_products($this->hostId));
91       if(count($tmp)){
92         $this->s_selectedNetbootProduct = $tmp[0];
93       }
94       $err |= $this->opsi->is_error();
95     }
96     if(!$err && !empty($this->hostId)) {
97       $tmp = $this->opsi->get_local_products($this->hostId); 
98       $err |= $this->opsi->is_error();
99       $this->a_selectedLocalProducts = $tmp;
100     }
102     /* Load product configuration */
103     if(!$err && !empty($this->hostId)) {
104       foreach($this->a_selectedLocalProducts as $name => $data){
105         $CFG = $this->opsi->get_product_properties($name,$this->hostId);
106         $err |= $this->opsi->is_error();
107         $this->a_selectedLocalProducts[$name]['CFG'] = $CFG;
108       }
109     }
110   
111     /* Check if everything went fine else reset everything and display a retry button 
112      */
113     if($err){
114       $this->a_availableNetbootProducts = array();
115       $this->s_selectedNetbootProduct = "";  
116       $this->s_initial_selectedNetbootProduct = "";  
117       $this->a_availableLocalProducts = array();
118       $this->a_selectedLocalProducts = array();
119       $this->a_initial_selectedLocalProducts = array();
120       $this->init_failed = TRUE;
121     }else{
123       /* Remember initial settings */ 
124       $this->is_account = $this->initially_was_account;
125       $this->a_initial_selectedLocalProducts = $this->a_selectedLocalProducts;
126       $this->s_initial_selectedNetbootProduct = $this->s_selectedNetbootProduct;
127     }
128   }
130   public function check()
131   {
132     return(array());
133     $messages = plugin::check();
134     if(!preg_match("/\./",$this->hostId)){
135       $messages[] = msgPool::invalid(_("Name"),$this->hostId,"",_("The client name must contain a domain part (e.g. '.company.de')."));
136     }
137     if(!tests::is_mac($this->mac) || empty($this->mac)){
138       $messages[] = msgPool::invalid(_("MAC address"),$this->mac,"","00:0C:7F:31:33:F1");
139     }
140     return($messages);
141   }
143   public function execute()
144   {
145     $display ="";
147     /* The pluign initialization failed due to communication problems with the gosa daemon. 
148        A retry button will be displayed here.
149      */
150     if($this->init_failed){
151       $smarty = get_smarty();
152       $smarty->assign("init_failed",TRUE);
153       $smarty->assign("message",$this->opsi->get_error());
154       return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
155     }  
157     /* If we are not a stand alone opsi client, we must be a samba client 
158        which has the opsi tab enabled.
159        Check if the opsi is added or removed and display state buttons.
160      */
161     if(!$this->parent_mode){
162       if(isset($_POST['modify_state'])){
163         if($this->is_account){
164           $this->is_account= FALSE;
165         }elseif(!$this->is_account){
166           $this->is_account= TRUE;
167         }
168       }
169       if($this->is_account){
170         $display = $this->show_disable_header(msgPool::removeFeaturesButton(_("Opsi")), 
171             msgPool::featuresEnabled(_("Opsi")));
172       }else{
173         $display = $this->show_enable_header(msgPool::addFeaturesButton(_("Opsi")), 
174             msgPool::featuresDisabled(_("Opsi")));
175         return($display);
176       } 
177     } 
179     /* Check if we have a sub dialog opened
180      */
181     if(is_object($this->dialog)){
182       $this->dialog->save_object();
183       return($this->dialog->execute());
184     }
186     /* Create HTML output of this plugin
187      */
188     $smarty = get_smarty();
189     foreach($this->attributes as $attr){
190       $smarty->assign($attr,$this->$attr);
191     }
193     $smarty->assign("parent_mode", $this->parent_mode);
194     $smarty->assign("is_installed", $this->is_installed);
195     $smarty->assign("init_failed",FALSE);
196     $divSLP = new divSelectBox();
197     $divALP = new divSelectBox();
199     /* Create list of available local products 
200      */
201     ksort($this->a_availableLocalProducts);
202     foreach($this->a_availableLocalProducts as $name => $data){
203       if(isset($this->a_selectedLocalProducts[$name])) continue;
205       $add_tab  = array("string"   => "<input type='image' src='images/back.png' name='add_lp_".$name."'>");
206       $name_tab = array("string"   => $name);
207       $desc_tab = array("string"   => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
208           "attach"   => "title='".$data['DESC']."' style='border-right:0px;'");
209       $divALP->AddEntry(array($add_tab,$name_tab,$desc_tab));
210     }
212     /* Create list of selected local products 
213      */
214     ksort($this->a_selectedLocalProducts);
215     foreach($this->a_selectedLocalProducts as $name => $data){
217       $name_tab = array("string"   => $name);
218       $desc_tab = array(
219           "string" => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
220           "attach" => "title='".$data['DESC']."'");
222       /* Only display edit button, if there is something to edit 
223        */
224       $edit = "<img src='images/empty.png' alt=' '>";
225       if(count($data['CFG'])){
226         $edit = "<input type='image' src='images/lists/edit.png' name='edit_lp_".$name."'>";
227       }
228       $del  = "<input type='image' src='images/lists/trash.png' name='del_lp_".$name."'>";  
230       $opt_tab  = array("string" => $edit.$del,
231           "attach" => "style='border-right:0px; width: 40px; text-align:right;'");
232       $divSLP->AddEntry(array($name_tab,$desc_tab,$opt_tab));
233     }
235     ksort($this->a_availableNetbootProducts);
236     $smarty->assign("hostId", $this->hostId);
237     $smarty->assign("divSLP", $divSLP->DrawList());
238     $smarty->assign("divALP", $divALP->DrawList());
239     $smarty->assign("SNP", $this->s_selectedNetbootProduct);
240     $smarty->assign("ANP", $this->a_availableNetbootProducts);
241     return($display.$smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
242   }
245   public function save()
246   {
247     if(!$this->initially_was_account && $this->is_account){
248       $res = $this->opsi->add_client($this->hostId,$this->mac,$this->note,$this->description);
249       if($this->opsi->is_error()){
250         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
251         return;
252       }
253     }
254     $this->opsi->modify_client($this->hostId,$this->mac,$this->note,$this->description);
255     if($this->opsi->is_error()){
256       msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
257       return;
258     }
260     $add = array_diff_assoc($this->a_selectedLocalProducts,$this->a_initial_selectedLocalProducts);
261     $del = array_diff_assoc($this->a_initial_selectedLocalProducts,$this->a_selectedLocalProducts);
263     foreach($del as $name => $data){
264       $this->opsi->del_product_from_client($name,$this->hostId);
265       if($this->opsi->is_error()){
266         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
267         return;
268       }
269     }
270     foreach($add as $name => $data){
271       echo "Adding '$name' to ".$this->hostId."<br>";
272       $this->opsi->add_product_to_client($name,$this->hostId);
273       if($this->opsi->is_error()){
274         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
275         return;
276       }
277       if(!empty($data['CFG'])){
278         $this->opsi->set_product_properties($name,$data['CFG'],$this->hostId);
279         if($this->opsi->is_error()){
280           msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
281           return;
282         }
283       }
284     }
286     foreach($this->a_selectedLocalProducts as $name => $data){
287       if(isset($del[$name]) || isset($add[$name])) continue;
288       $diff = array_diff($data['CFG'],$this->a_initial_selectedLocalProducts[$name]['CFG']);
289       if(count($diff)){
290         $this->opsi->set_product_properties($name,$diff,$this->hostId);
291         if($this->opsi->is_error()){
292           msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
293           return;
294         }
295       }
296     }
298     if($this->s_selectedNetbootProduct != $this->s_initial_selectedNetbootProduct){
299       if(!empty($this->s_initial_selectedNetbootProduct)){
300         $this->opsi->del_product_from_client($this->s_initial_selectedNetbootProduct,$this->hostId);
301         if($this->opsi->is_error()){
302           msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
303           return;
304         }
305       }
306       $this->opsi->add_product_to_client($this->s_selectedNetbootProduct,$this->hostId);
307       if($this->opsi->is_error()){
308         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
309         return;
310       }
311     }
312   }
314   public function remove_from_parent()
315   {
316     $this->opsi->del_client($this->hostId);
317     if($this->opsi->is_error()){
318       msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
319       return;
320     }
321   }
324   public function save_object()
325   {
326     if(isset($_POST['reinit']) && $this->init_failed){
327       $this->init();
328     }
330     if(isset($_POST['cancel_properties']) && is_object($this->dialog)){
331       $this->dialog = NULL;
332     }
333     if(isset($_POST['save_properties']) && ($this->dialog instanceof opsiProductConfig)){
334       $this->dialog->save_object();
335       $pro = $this->dialog->get_product();
336       $CFG = $this->dialog->get_cfg();
337       if(isset($this->a_selectedLocalProducts[$pro])){
338         $this->a_selectedLocalProducts[$pro]['CFG'] = $CFG;
339       }
340       $this->dialog = NULL;
341     }
343     if(isset($_POST['opsiGeneric_posted'])){
345       plugin::save_object();
347       if(isset($_POST['hostId']) && $this->parent_mode){
348         $this->hostId = get_post('hostId');
349       }
351       /* Send actions like 'install' or 'wake' to the si server 
352        */
353       if(isset($_POST['opsi_action']) && isset($_POST['opsi_trigger_action']) && $this->parent_mode){
354         $action = $_POST['opsi_action'];
355         if(in_array($action,array("wake","install"))){
356           $this->opsi->send_action($action,$this->hostId,$this->mac);
357           if($this->opsi->is_error()){
358             msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
359           }
360         }
361       }
363       if(isset($_POST['opsi_netboot_product'])){
364         $SNP = trim($_POST['opsi_netboot_product']);
365         if(isset($this->a_availableNetbootProducts[$SNP])){
366           $this->s_selectedNetbootProduct = $SNP;
367         }
368       }
370       foreach($_POST as $name => $value){
371         if(preg_match("/^add_lp_/",$name)){
372           $product = preg_replace("/^add_lp_(.*)_.$/","\\1",$name);
373           if(isset($this->a_availableLocalProducts[$product]) && !isset($this->a_selectedLocalProducts[$product])){
374             $this->a_selectedLocalProducts[$product] = $this->a_availableLocalProducts[$product];
375             $CFG = $this->opsi->get_product_properties($product);
376             $this->a_selectedLocalProducts[$product]['CFG'] = $CFG;
377           }
378           break;
379         }
380         if(preg_match("/^del_lp_/",$name)){
381           $product = preg_replace("/^del_lp_(.*)_.$/","\\1",$name);
382           if(isset($this->a_selectedLocalProducts[$product])){
383             unset($this->a_selectedLocalProducts[$product]);
384           }
385           break;
386         }
387         if(preg_match("/^edit_lp_/",$name)){
388           $product = preg_replace("/^edit_lp_(.*)_.$/","\\1",$name);
389           $this->dialog = new opsiProductConfig($this->config,
390               $product,$this->a_selectedLocalProducts[$product]['CFG'],$this->hostId);
391           break;
392         }
393       }   
394     }
395   }
398   function install_client()
399   {
401   }
404   /* Return plugin informations for acl handling */
405   static function plInfo()
406   {
407     return (array(
408           "plShortName"   => _("Generic"),
409           "plDescription" => _("Opsi generic"),
410           "plSelfModify"  => FALSE,
411           "plDepends"     => array(),
412           "plPriority"    => 1,
413           "plSection"     => array("administration"),
414           "plCategory"    => array("opsi" => array("description"  => _("Opsi client"),
415                                                      "objectClass"  => "dummy_class_opsi")),
416           "plProvidedAcls"=> array()
417           ));
418   }
423 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
424 ?>