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;
29 /* Get hostId */
30 if($hostId != "new"){
31 $this->initially_was_account = TRUE;
32 $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
33 }
35 /* Try to plugin */
36 $this->init();
37 }
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 if(count($tmp)){
58 $this->s_selectedNetbootProduct = $tmp[0];
59 }
60 $err |= $this->opsi->is_error();
61 }
62 if(!$err && !empty($this->hostId)) {
63 $tmp = $this->opsi->get_local_products($this->hostId);
64 $err |= $this->opsi->is_error();
65 $this->a_selectedLocalProducts = $tmp;
66 }
68 /* Load product configuration */
69 if(!$err && !empty($this->hostId)) {
70 foreach($this->a_selectedLocalProducts as $name => $data){
71 $CFG = $this->opsi->get_product_properties($name,$this->hostId);
72 $err |= $this->opsi->is_error();
73 $this->a_selectedLocalProducts[$name]['CFG'] = $CFG;
74 }
75 }
77 /* Check if everything went fine else reset everything and display a retry button
78 */
79 if($err){
80 $this->a_availableNetbootProducts = array();
81 $this->s_selectedNetbootProduct = "";
82 $this->s_initial_selectedNetbootProduct = "";
83 $this->a_availableLocalProducts = array();
84 $this->a_selectedLocalProducts = array();
85 $this->a_initial_selectedLocalProducts = array();
86 $this->init_failed = TRUE;
87 }else{
89 /* Remember initial settings */
90 $this->a_initial_selectedLocalProducts = $this->a_selectedLocalProducts;
91 $this->s_initial_selectedNetbootProduct = $this->s_selectedNetbootProduct;
92 }
93 }
95 public function check()
96 {
97 $messages = plugin::check();
98 if(!preg_match("/\./",$this->hostId)){
99 $messages[] = msgPool::invalid(_("Name"),$this->hostId,"",_("The client name must contain a domain part (e.g. '.company.de')."));
100 }
101 return($messages);
102 }
104 public function execute()
105 {
106 if($this->init_failed){
108 $smarty = get_smarty();
109 $smarty->assign("init_failed",TRUE);
110 $smarty->assign("message",$this->opsi->get_error());
111 return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
112 }
114 if(is_object($this->dialog)){
115 $this->dialog->save_object();
116 return($this->dialog->execute());
117 }
119 $smarty = get_smarty();
120 $smarty->assign("parent_mode", $this->parent_mode);
121 $smarty->assign("init_failed",FALSE);
122 $divSLP = new divSelectBox();
123 $divALP = new divSelectBox();
125 /* Create list of available local products
126 */
127 ksort($this->a_availableLocalProducts);
128 foreach($this->a_availableLocalProducts as $name => $data){
129 if(isset($this->a_selectedLocalProducts[$name])) continue;
131 $add_tab = array("string" => "<input type='image' src='images/back.png' name='add_lp_".$name."'>");
132 $name_tab = array("string" => $name);
133 $desc_tab = array("string" => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
134 "attach" => "title='".$data['DESC']."' style='border-right:0px;'");
135 $divALP->AddEntry(array($add_tab,$name_tab,$desc_tab));
136 }
138 /* Create list of selected local products
139 */
140 ksort($this->a_selectedLocalProducts);
141 foreach($this->a_selectedLocalProducts as $name => $data){
143 $name_tab = array("string" => $name);
144 $desc_tab = array(
145 "string" => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
146 "attach" => "title='".$data['DESC']."'");
148 /* Only display edit button, if there is something to edit
149 */
150 $edit = "<img src='images/empty.png' alt=' '>";
151 if(count($data['CFG'])){
152 $edit = "<input type='image' src='images/lists/edit.png' name='edit_lp_".$name."'>";
153 }
154 $del = "<input type='image' src='images/lists/trash.png' name='del_lp_".$name."'>";
156 $opt_tab = array("string" => $edit.$del,
157 "attach" => "style='border-right:0px; width: 40px; text-align:right;'");
158 $divSLP->AddEntry(array($name_tab,$desc_tab,$opt_tab));
159 }
161 ksort($this->a_availableNetbootProducts);
162 $smarty->assign("hostId", $this->hostId);
163 $smarty->assign("divSLP", $divSLP->DrawList());
164 $smarty->assign("divALP", $divALP->DrawList());
165 $smarty->assign("SNP", $this->s_selectedNetbootProduct);
166 $smarty->assign("ANP", $this->a_availableNetbootProducts);
167 return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
168 }
171 public function save()
172 {
173 /* Check if this a new opsi client
174 -Do we have to create this client first?
175 */
176 if(!$this->initially_was_account && $this->is_account){
177 $res = $this->opsi->add_client($this->hostId,"0.0.0.0","00:00:00:00:00:00","Hallo","TZ");
178 if($this->opsi->is_error()){
179 msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
180 return;
181 }
182 }
184 $add = array_diff_assoc($this->a_selectedLocalProducts,$this->a_initial_selectedLocalProducts);
185 $del = array_diff_assoc($this->a_initial_selectedLocalProducts,$this->a_selectedLocalProducts);
187 foreach($del as $name => $data){
188 $this->opsi->del_product_from_client($name,$this->hostId);
189 if($this->opsi->is_error()){
190 msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
191 return;
192 }
193 }
194 foreach($add as $name => $data){
195 $this->opsi->add_product_to_client($name,$this->hostId);
196 if($this->opsi->is_error()){
197 msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
198 return;
199 }
200 $this->opsi->set_product_properties($name,$data['CFG'],$this->hostId);
201 if($this->opsi->is_error()){
202 msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
203 return;
204 }
205 }
207 foreach($this->a_selectedLocalProducts as $name => $data){
208 if(isset($del[$name]) || isset($add[$name])) continue;
209 $diff = array_diff($data['CFG'],$this->a_initial_selectedLocalProducts[$name]['CFG']);
210 if(count($diff)){
211 $this->opsi->set_product_properties($name,$diff,$this->hostId);
212 if($this->opsi->is_error()){
213 msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
214 return;
215 }
216 }
217 }
219 if($this->s_selectedNetbootProduct != $this->s_initial_selectedNetbootProduct){
220 if(!empty($this->s_initial_selectedNetbootProduct)){
221 $this->opsi->del_product_from_client($this->s_initial_selectedNetbootProduct,$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 $this->opsi->add_product_to_client($this->s_selectedNetbootProduct,$this->hostId);
228 if($this->opsi->is_error()){
229 msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
230 return;
231 }
232 }
233 }
235 public function remove_from_parent()
236 {}
239 public function save_object()
240 {
241 if(isset($_POST['reinit']) && $this->init_failed){
242 $this->init();
243 }
245 if(isset($_POST['cancel_properties']) && is_object($this->dialog)){
246 $this->dialog = NULL;
247 }
248 if(isset($_POST['save_properties']) && ($this->dialog instanceof opsi_product_config)){
249 $this->dialog->save_object();
250 $pro = $this->dialog->get_product();
251 $CFG = $this->dialog->get_cfg();
252 if(isset($this->a_selectedLocalProducts[$pro])){
253 $this->a_selectedLocalProducts[$pro]['CFG'] = $CFG;
254 }
255 $this->dialog = NULL;
256 }
258 if(isset($_POST['opsigeneric_posted'])){
260 if(isset($_POST['hostId']) && $this->parent_mode){
261 $this->hostId = get_post('hostId');
262 }
264 if(isset($_POST['opsi_netboot_product'])){
265 $SNP = trim($_POST['opsi_netboot_product']);
266 if(isset($this->a_availableNetbootProducts[$SNP])){
267 $this->s_selectedNetbootProduct = $SNP;
268 }
269 }
271 foreach($_POST as $name => $value){
272 if(preg_match("/^add_lp_/",$name)){
273 $product = preg_replace("/^add_lp_(.*)_.$/","\\1",$name);
274 if(isset($this->a_availableLocalProducts[$product]) && !isset($this->a_selectedLocalProducts[$product])){
275 $this->a_selectedLocalProducts[$product] = $this->a_availableLocalProducts[$product];
276 $CFG = $this->opsi->get_product_properties($product);
277 $this->a_selectedLocalProducts[$product]['CFG'] = $CFG;
278 }
279 break;
280 }
281 if(preg_match("/^del_lp_/",$name)){
282 $product = preg_replace("/^del_lp_(.*)_.$/","\\1",$name);
283 if(isset($this->a_selectedLocalProducts[$product])){
284 unset($this->a_selectedLocalProducts[$product]);
285 }
286 break;
287 }
288 if(preg_match("/^edit_lp_/",$name)){
289 $product = preg_replace("/^edit_lp_(.*)_.$/","\\1",$name);
290 $this->dialog = new opsi_product_config($this->config,
291 $product,$this->a_selectedLocalProducts[$product]['CFG'],$this->hostId);
292 break;
293 }
294 }
295 }
296 }
298 /* Return plugin informations for acl handling */
299 static function plInfo()
300 {
301 return (array(
302 "plShortName" => _("Generic"),
303 "plDescription" => _("Opsi generic"),
304 "plSelfModify" => FALSE,
305 "plDepends" => array(),
306 "plPriority" => 1,
307 "plSection" => array("administration"),
308 "plCategory" => array("opsi" => array("description" => _("Opsi client"),
309 "objectClass" => "dummy_class_opsi")),
310 "plProvidedAcls"=> array()
311 ));
312 }
314 }
317 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
318 ?>