Code

Updated of opsi stuff
[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  = "";
29   public $attributes = array("mac","note","description");
31   public function __construct($config,$hostId)
32   {
33     $this->opsi = new opsi($config); 
34     $this->is_account =TRUE;
35   
36     /* Get hostId */
37     if($hostId != "new"){
38       $this->initially_was_account = TRUE;
39       $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
40     }
41   
42     /* Try to plugin */
43     $this->init();
44   }
45   
46   private function init()
47   {
48     $err = FALSE;
49     $this->init_failed = FALSE;
51     if($this->hostId != "new"){
52       $list = $this->opsi->list_clients($this->hostId);
53       $err |= $this->opsi->is_error();
54       foreach($list as $entry){
55         if($entry['NAME'][0]['VALUE'] == $this->hostId){  
56           foreach(array("description" => "DESCRIPTION","mac" => "MAC", "note" => "NOTES") as $des => $src){
57             $this->$des = $entry[$src][0]['VALUE'];
58           } 
59           break;
60         }
61       }
62     }
64     /* Get product settings */     
65     if(!$err){
66       $this->a_availableNetbootProducts = $this->opsi->get_netboot_products();
67       $err |= $this->opsi->is_error();
68     }
69     if(!$err) {
70       $this->a_availableLocalProducts   = $this->opsi->get_local_products();
71       $err |= $this->opsi->is_error();
72     }
74     /* Get selected products */
75     if(!$err && !empty($this->hostId)) {
76       $tmp = array_keys($this->opsi->get_netboot_products($this->hostId));
77       if(count($tmp)){
78         $this->s_selectedNetbootProduct = $tmp[0];
79       }
80       $err |= $this->opsi->is_error();
81     }
82     if(!$err && !empty($this->hostId)) {
83       $tmp = $this->opsi->get_local_products($this->hostId); 
84       $err |= $this->opsi->is_error();
85       $this->a_selectedLocalProducts = $tmp;
86     }
88     /* Load product configuration */
89     if(!$err && !empty($this->hostId)) {
90       foreach($this->a_selectedLocalProducts as $name => $data){
91         $CFG = $this->opsi->get_product_properties($name,$this->hostId);
92         $err |= $this->opsi->is_error();
93         $this->a_selectedLocalProducts[$name]['CFG'] = $CFG;
94       }
95     }
96   
97     /* Check if everything went fine else reset everything and display a retry button 
98      */
99     if($err){
100       $this->a_availableNetbootProducts = array();
101       $this->s_selectedNetbootProduct = "";  
102       $this->s_initial_selectedNetbootProduct = "";  
103       $this->a_availableLocalProducts = array();
104       $this->a_selectedLocalProducts = array();
105       $this->a_initial_selectedLocalProducts = array();
106       $this->init_failed = TRUE;
107     }else{
109       /* Remember initial settings */ 
110       $this->a_initial_selectedLocalProducts = $this->a_selectedLocalProducts;
111       $this->s_initial_selectedNetbootProduct = $this->s_selectedNetbootProduct;
112     }
113   }
115   public function check()
116   {
117     $messages = plugin::check();
118     if(!preg_match("/\./",$this->hostId)){
119       $messages[] = msgPool::invalid(_("Name"),$this->hostId,"",_("The client name must contain a domain part (e.g. '.company.de')."));
120     }
121     if(!tests::is_mac($this->mac)){
122       $messages[] = msgPool::invalid(_("MAC address"),$this->mac,"","00:0C:7F:31:33:F1");
123     }
124     return($messages);
125   }
127   public function execute()
128   {
129     if($this->init_failed){
130       $smarty = get_smarty();
131       $smarty->assign("init_failed",TRUE);
132       $smarty->assign("message",$this->opsi->get_error());
133       return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
134     }  
136     if(is_object($this->dialog)){
137       $this->dialog->save_object();
138       return($this->dialog->execute());
139     }
141     $smarty = get_smarty();
143     foreach($this->attributes as $attr){
144       $smarty->assign($attr,$this->$attr);
145     }
147     $smarty->assign("parent_mode", $this->parent_mode);
148     $smarty->assign("is_installed", $this->is_installed);
149     $smarty->assign("init_failed",FALSE);
150     $divSLP = new divSelectBox();
151     $divALP = new divSelectBox();
153     /* Create list of available local products 
154      */
155     ksort($this->a_availableLocalProducts);
156     foreach($this->a_availableLocalProducts as $name => $data){
157       if(isset($this->a_selectedLocalProducts[$name])) continue;
159       $add_tab  = array("string"   => "<input type='image' src='images/back.png' name='add_lp_".$name."'>");
160       $name_tab = array("string"   => $name);
161       $desc_tab = array("string"   => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
162           "attach"   => "title='".$data['DESC']."' style='border-right:0px;'");
163       $divALP->AddEntry(array($add_tab,$name_tab,$desc_tab));
164     }
166     /* Create list of selected local products 
167      */
168     ksort($this->a_selectedLocalProducts);
169     foreach($this->a_selectedLocalProducts as $name => $data){
171       $name_tab = array("string"   => $name);
172       $desc_tab = array(
173           "string" => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
174           "attach" => "title='".$data['DESC']."'");
176       /* Only display edit button, if there is something to edit 
177        */
178       $edit = "<img src='images/empty.png' alt=' '>";
179       if(count($data['CFG'])){
180         $edit = "<input type='image' src='images/lists/edit.png' name='edit_lp_".$name."'>";
181       }
182       $del  = "<input type='image' src='images/lists/trash.png' name='del_lp_".$name."'>";  
184       $opt_tab  = array("string" => $edit.$del,
185           "attach" => "style='border-right:0px; width: 40px; text-align:right;'");
186       $divSLP->AddEntry(array($name_tab,$desc_tab,$opt_tab));
187     }
189     ksort($this->a_availableNetbootProducts);
190     $smarty->assign("hostId", $this->hostId);
191     $smarty->assign("divSLP", $divSLP->DrawList());
192     $smarty->assign("divALP", $divALP->DrawList());
193     $smarty->assign("SNP", $this->s_selectedNetbootProduct);
194     $smarty->assign("ANP", $this->a_availableNetbootProducts);
195     return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
196   }
199   public function save()
200   {
201     /* Check if this a new opsi client 
202         -Do we have to create this client first?
203      */
204     if(!$this->initially_was_account && $this->is_account){
205       $res = $this->opsi->add_client($this->hostId,$this->mac,$this->note,$this->description);
206       if($this->opsi->is_error()){
207         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
208         return;
209       }
210     }
211     $this->opsi->modify_client($this->hostId,$this->mac,$this->note,$this->description);
212     if($this->opsi->is_error()){
213       msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
214       return;
215     }
217     $add = array_diff_assoc($this->a_selectedLocalProducts,$this->a_initial_selectedLocalProducts);
218     $del = array_diff_assoc($this->a_initial_selectedLocalProducts,$this->a_selectedLocalProducts);
220     foreach($del as $name => $data){
221       $this->opsi->del_product_from_client($name,$this->hostId);
222       if($this->opsi->is_error()){
223         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
224         return;
225       }
226     }
227     foreach($add as $name => $data){
228       $this->opsi->add_product_to_client($name,$this->hostId);
229       if($this->opsi->is_error()){
230         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
231         return;
232       }
233       $this->opsi->set_product_properties($name,$data['CFG'],$this->hostId);
234       if($this->opsi->is_error()){
235         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
236         return;
237       }
238     }
240     foreach($this->a_selectedLocalProducts as $name => $data){
241       if(isset($del[$name]) || isset($add[$name])) continue;
242       $diff = array_diff($data['CFG'],$this->a_initial_selectedLocalProducts[$name]['CFG']);
243       if(count($diff)){
244         $this->opsi->set_product_properties($name,$diff,$this->hostId);
245         if($this->opsi->is_error()){
246           msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
247           return;
248         }
249       }
250     }
252     if($this->s_selectedNetbootProduct != $this->s_initial_selectedNetbootProduct){
253       if(!empty($this->s_initial_selectedNetbootProduct)){
254         $this->opsi->del_product_from_client($this->s_initial_selectedNetbootProduct,$this->hostId);
255         if($this->opsi->is_error()){
256           msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
257           return;
258         }
259       }
260       $this->opsi->add_product_to_client($this->s_selectedNetbootProduct,$this->hostId);
261       if($this->opsi->is_error()){
262         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
263         return;
264       }
265     }
266   }
268   public function remove_from_parent()
269   {
270     $this->opsi->del_client($this->hostId);
271     if($this->opsi->is_error()){
272       msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
273       return;
274     }
275   }
278   public function save_object()
279   {
280     if(isset($_POST['reinit']) && $this->init_failed){
281       $this->init();
282     }
284     if(isset($_POST['cancel_properties']) && is_object($this->dialog)){
285       $this->dialog = NULL;
286     }
287     if(isset($_POST['save_properties']) && ($this->dialog instanceof opsi_product_config)){
288       $this->dialog->save_object();
289       $pro = $this->dialog->get_product();
290       $CFG = $this->dialog->get_cfg();
291       if(isset($this->a_selectedLocalProducts[$pro])){
292         $this->a_selectedLocalProducts[$pro]['CFG'] = $CFG;
293       }
294       $this->dialog = NULL;
295     }
297     if(isset($_POST['opsigeneric_posted'])){
299       plugin::save_object();
301       if(isset($_POST['hostId']) && $this->parent_mode){
302         $this->hostId = get_post('hostId');
303       }
305       /* Send actions like 'install' or 'wake' to the si server 
306        */
307       if(isset($_POST['opsi_action']) && isset($_POST['opsi_trigger_action']) && $this->parent_mode){
308         $action = $_POST['opsi_action'];
309         if(in_array($action,array("wake","install"))){
310           $this->opsi->send_action($action,$this->hostId);
311           if($this->opsi->is_error()){
312             msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
313           }
314         }
315       }
317       if(isset($_POST['opsi_netboot_product'])){
318         $SNP = trim($_POST['opsi_netboot_product']);
319         if(isset($this->a_availableNetbootProducts[$SNP])){
320           $this->s_selectedNetbootProduct = $SNP;
321         }
322       }
324       foreach($_POST as $name => $value){
325         if(preg_match("/^add_lp_/",$name)){
326           $product = preg_replace("/^add_lp_(.*)_.$/","\\1",$name);
327           if(isset($this->a_availableLocalProducts[$product]) && !isset($this->a_selectedLocalProducts[$product])){
328             $this->a_selectedLocalProducts[$product] = $this->a_availableLocalProducts[$product];
329             $CFG = $this->opsi->get_product_properties($product);
330             $this->a_selectedLocalProducts[$product]['CFG'] = $CFG;
331           }
332           break;
333         }
334         if(preg_match("/^del_lp_/",$name)){
335           $product = preg_replace("/^del_lp_(.*)_.$/","\\1",$name);
336           if(isset($this->a_selectedLocalProducts[$product])){
337             unset($this->a_selectedLocalProducts[$product]);
338           }
339           break;
340         }
341         if(preg_match("/^edit_lp_/",$name)){
342           $product = preg_replace("/^edit_lp_(.*)_.$/","\\1",$name);
343           $this->dialog = new opsi_product_config($this->config,
344               $product,$this->a_selectedLocalProducts[$product]['CFG'],$this->hostId);
345           break;
346         }
347       }   
348     }
349   }
352   function install_client()
353   {
355   }
358   /* Return plugin informations for acl handling */
359   static function plInfo()
360   {
361     return (array(
362           "plShortName"   => _("Generic"),
363           "plDescription" => _("Opsi generic"),
364           "plSelfModify"  => FALSE,
365           "plDepends"     => array(),
366           "plPriority"    => 1,
367           "plSection"     => array("administration"),
368           "plCategory"    => array("opsi" => array("description"  => _("Opsi client"),
369                                                      "objectClass"  => "dummy_class_opsi")),
370           "plProvidedAcls"=> array()
371           ));
372   }
377 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
378 ?>