Code

Updated 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;
24   public function __construct($config,$hostId)
25   {
26     $this->opsi = new opsi($config); 
27     $this->is_account =TRUE;
28   
29     /* Get hostId */
30     if($hostId != "new"){
31       $this->initially_was_account = TRUE;
32       $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
33     }
34   
35     /* Try to plugin */
36     $this->init();
37   }
38   
39   private function init()
40   {
41     $err = FALSE;
42     $this->init_failed = FALSE;
44     /* Get product settings */     
45     if(!$err){
46       $this->a_availableNetbootProducts = $this->opsi->get_netboot_products();
47       $err |= $this->opsi->is_error();
48     }
49     if(!$err) {
50       $this->a_availableLocalProducts   = $this->opsi->get_local_products();
51       $err |= $this->opsi->is_error();
52     }
54     /* Get selected products */
55     if(!$err && !empty($this->hostId)) {
56       $tmp = array_keys($this->opsi->get_netboot_products($this->hostId));
57       $this->s_selectedNetbootProduct = $tmp[0];
58       $err |= $this->opsi->is_error();
59     }
60     if(!$err && !empty($this->hostId)) {
61       $tmp = $this->opsi->get_local_products($this->hostId);  
62       $err |= $this->opsi->is_error();
63       $this->a_selectedLocalProducts = $tmp;
64     }
66     /* Load product configuration */
67     if(!$err && !empty($this->hostId)) {
68       foreach($this->a_selectedLocalProducts as $name => $data){
69         $CFG = $this->opsi->get_product_properties($name,$this->hostId);
70         $err |= $this->opsi->is_error();
71         $this->a_selectedLocalProducts[$name]['CFG'] = $CFG;
72       }
73     }
74   
75     /* Check if everything went fine else reset everything and display a retry button 
76      */
77     if($err){
78       $this->a_availableNetbootProducts = array();
79       $this->s_selectedNetbootProduct = "";  
80       $this->s_initial_selectedNetbootProduct = "";  
81       $this->a_availableLocalProducts = array();
82       $this->a_selectedLocalProducts = array();
83       $this->a_initial_selectedLocalProducts = array();
84       $this->init_failed = TRUE;
85     }else{
87       /* Remember initial settings */ 
88       $this->a_initial_selectedLocalProducts = $this->a_selectedLocalProducts;
89       $this->s_initial_selectedNetbootProduct = $this->s_selectedNetbootProduct;
90     }
91   }
93   public function check()
94   {
95     $messages = plugin::check();
96     if(!preg_match("/\./",$this->hostId)){
97       $messages[] = msgPool::invalid(_("Name"),$this->hostId,"",_("The client name must contain a domain part (e.g. '.company.de')."));
98     }
99     return($messages);
100   }
102   public function execute()
103   {
104     if($this->init_failed){
105       
106       $smarty = get_smarty();
107       $smarty->assign("init_failed",TRUE);
108       $smarty->assign("message",$this->opsi->get_error());
109       return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
110     }  
112     if(is_object($this->dialog)){
113       $this->dialog->save_object();
114       return($this->dialog->execute());
115     }
117     $smarty = get_smarty();
118     $smarty->assign("parent_mode", $this->parent_mode);
119     $smarty->assign("init_failed",FALSE);
120     $divSLP = new divSelectBox();
121     $divALP = new divSelectBox();
123     /* Create list of available local products 
124      */
125     ksort($this->a_availableLocalProducts);
126     foreach($this->a_availableLocalProducts as $name => $data){
127       if(isset($this->a_selectedLocalProducts[$name])) continue;
129       $add_tab  = array("string"   => "<input type='image' src='images/back.png' name='add_lp_".$name."'>");
130       $name_tab = array("string"   => $name);
131       $desc_tab = array("string"   => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
132           "attach"   => "title='".$data['DESC']."' style='border-right:0px;'");
133       $divALP->AddEntry(array($add_tab,$name_tab,$desc_tab));
134     }
136     /* Create list of selected local products 
137      */
138     ksort($this->a_selectedLocalProducts);
139     foreach($this->a_selectedLocalProducts as $name => $data){
141       $name_tab = array("string"   => $name);
142       $desc_tab = array(
143           "string" => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
144           "attach" => "title='".$data['DESC']."'");
146       /* Only display edit button, if there is something to edit 
147        */
148       $edit = "<img src='images/empty.png' alt=' '>";
149       if(count($data['CFG'])){
150         $edit = "<input type='image' src='images/lists/edit.png' name='edit_lp_".$name."'>";
151       }
152       $del  = "<input type='image' src='images/lists/trash.png' name='del_lp_".$name."'>";  
154       $opt_tab  = array("string" => $edit.$del,
155           "attach" => "style='border-right:0px; width: 40px; text-align:right;'");
156       $divSLP->AddEntry(array($name_tab,$desc_tab,$opt_tab));
157     }
159     ksort($this->a_availableNetbootProducts);
160     $smarty->assign("hostId", $this->hostId);
161     $smarty->assign("divSLP", $divSLP->DrawList());
162     $smarty->assign("divALP", $divALP->DrawList());
163     $smarty->assign("SNP", $this->s_selectedNetbootProduct);
164     $smarty->assign("ANP", $this->a_availableNetbootProducts);
165     return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
166   }
169   public function save()
170   {
171     /* Check if this a new opsi client 
172         -Do we have to create this client first?
173      */
174     if(!$this->initially_was_account && $this->is_account){
175       $res = $this->opsi->add_client($this->hostId,"0.0.0.0","00:00:00:00:00:00","Hallo","TZ");
176       if($this->opsi->is_error()){
177         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
178         return;
179       }
180     }
182     $add = array_diff_assoc($this->a_selectedLocalProducts,$this->a_initial_selectedLocalProducts);
183     $del = array_diff_assoc($this->a_initial_selectedLocalProducts,$this->a_selectedLocalProducts);
185     foreach($del as $name => $data){
186       $this->opsi->del_product_from_client($name,$this->hostId);
187     }
188     foreach($add as $name => $data){
189       $this->opsi->add_product_to_client($name,$this->hostId);
190       $this->opsi->set_product_properties($name,$data['CFG'],$this->hostId);
191     }
193     foreach($this->a_selectedLocalProducts as $name => $data){
194       if(isset($del[$name]) || isset($add[$name])) continue;
195       $diff = array_diff($data['CFG'],$this->a_initial_selectedLocalProducts[$name]['CFG']);
196       if(count($diff)){
197         $this->opsi->set_product_properties($name,$diff,$this->hostId);
198       }
199     }
200   
201     if($this->s_selectedNetbootProduct != $this->s_initial_selectedNetbootProduct){
202       if(!empty($this->s_initial_selectedNetbootProduct)){
203         $this->opsi->del_product_from_client($this->s_initial_selectedNetbootProduct,$this->hostId);
204         $this->opsi->add_product_to_client($this->s_selectedNetbootProduct,$this->hostId);
205       }
206     }
207   }
209   public function remove_from_parent()
210   {}
213   public function save_object()
214   {
215     if(isset($_POST['reinit']) && $this->init_failed){
216       $this->init();
217     }
219     if(isset($_POST['cancel_properties']) && is_object($this->dialog)){
220       $this->dialog = NULL;
221     }
222     if(isset($_POST['save_properties']) && ($this->dialog instanceof opsi_product_config)){
223       $this->dialog->save_object();
224       $pro = $this->dialog->get_product();
225       $CFG = $this->dialog->get_cfg();
226       if(isset($this->a_selectedLocalProducts[$pro])){
227         $this->a_selectedLocalProducts[$pro]['CFG'] = $CFG;
228       }
229       $this->dialog = NULL;
230     }
232     if(isset($_POST['opsigeneric_posted'])){
234       if(isset($_POST['hostId']) && $this->parent_mode){
235         $this->hostId = get_post('hostId');
236       }
238       if(isset($_POST['opsi_netboot_product'])){
239         $SNP = trim($_POST['opsi_netboot_product']);
240         if(isset($this->a_availableNetbootProducts[$SNP])){
241           $this->s_selectedNetbootProduct = $SNP;
242         }
243       }
245       foreach($_POST as $name => $value){
246         if(preg_match("/^add_lp_/",$name)){
247           $product = preg_replace("/^add_lp_(.*)_.$/","\\1",$name);
248           if(isset($this->a_availableLocalProducts[$product]) && !isset($this->a_selectedLocalProducts[$product])){
249             $this->a_selectedLocalProducts[$product] = $this->a_availableLocalProducts[$product];
250             $CFG = $this->opsi->get_product_properties($product);
251             $this->a_selectedLocalProducts[$product]['CFG'] = $CFG;
252           }
253           break;
254         }
255         if(preg_match("/^del_lp_/",$name)){
256           $product = preg_replace("/^del_lp_(.*)_.$/","\\1",$name);
257           if(isset($this->a_selectedLocalProducts[$product])){
258             unset($this->a_selectedLocalProducts[$product]);
259           }
260           break;
261         }
262         if(preg_match("/^edit_lp_/",$name)){
263           $product = preg_replace("/^edit_lp_(.*)_.$/","\\1",$name);
264           $this->dialog = new opsi_product_config($this->config,
265               $product,$this->a_selectedLocalProducts[$product]['CFG'],$this->hostId);
266           break;
267         }
268       }   
269     }
270   }
272   /* Return plugin informations for acl handling */
273   static function plInfo()
274   {
275     return (array(
276           "plShortName"   => _("Generic"),
277           "plDescription" => _("Opsi generic"),
278           "plSelfModify"  => FALSE,
279           "plDepends"     => array(),
280           "plPriority"    => 1,
281           "plSection"     => array("administration"),
282           "plCategory"    => array("opsi" => array("description"  => _("Opsi client"),
283                                                      "objectClass"  => "dummy_class_opsi")),
284           "plProvidedAcls"=> array()
285           ));
286   }
291 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
292 ?>