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