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 public function __construct($config,$hostId)
23 {
24 $this->opsi = new opsi($config);
25 $this->is_account =TRUE;
26 $this->initially_was_account = TRUE;
28 /* Get hostId */
29 if($hostId != "new"){
30 $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
31 }
33 /* Try to plugin */
34 $this->init();
35 }
37 private function init()
38 {
39 $err = FALSE;
40 $this->init_failed = FALSE;
42 /* Get product settings */
43 if(!$err){
44 $this->a_availableNetbootProducts = $this->opsi->get_netboot_products();
45 $err |= $this->opsi->is_error();
46 }
47 if(!$err) {
48 $this->a_availableLocalProducts = $this->opsi->get_local_products();
49 $err |= $this->opsi->is_error();
50 }
52 /* Get selected products */
53 if(!$err && !empty($this->hostId)) {
54 $tmp = array_keys($this->opsi->get_netboot_products($this->hostId));
55 $this->s_selectedNetbootProduct = $tmp[0];
56 $err |= $this->opsi->is_error();
57 }
58 if(!$err && !empty($this->hostId)) {
59 $tmp = $this->opsi->get_local_products($this->hostId);
60 $err |= $this->opsi->is_error();
61 $this->a_selectedLocalProducts = $tmp;
62 }
64 /* Load product configuration */
65 if(!$err && !empty($this->hostId)) {
66 foreach($this->a_selectedLocalProducts as $name => $data){
67 $CFG = $this->opsi->get_product_properties($name,$this->hostId);
68 $err |= $this->opsi->is_error();
69 $this->a_selectedLocalProducts[$name]['CFG'] = $CFG;
70 }
71 }
73 /* Check if everything went fine else reset everything and display a retry button
74 */
75 if($err){
76 $this->a_availableNetbootProducts = array();
77 $this->s_selectedNetbootProduct = "";
78 $this->s_initial_selectedNetbootProduct = "";
79 $this->a_availableLocalProducts = array();
80 $this->a_selectedLocalProducts = array();
81 $this->a_initial_selectedLocalProducts = array();
82 $this->init_failed = TRUE;
83 }else{
85 /* Remember initial settings */
86 $this->a_initial_selectedLocalProducts = $this->a_selectedLocalProducts;
87 $this->s_initial_selectedNetbootProduct = $this->s_selectedNetbootProduct;
88 }
89 }
91 public function execute()
92 {
93 if($this->init_failed){
95 $smarty = get_smarty();
96 $smarty->assign("init_failed",TRUE);
97 $smarty->assign("message",$this->opsi->get_error());
98 return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
99 }
102 if(is_object($this->dialog)){
103 $this->dialog->save_object();
104 return($this->dialog->execute());
105 }
107 $smarty = get_smarty();
108 $smarty->assign("init_failed",FALSE);
109 $divSLP = new divSelectBox();
110 $divALP = new divSelectBox();
112 /* Create list of available local products
113 */
114 ksort($this->a_availableLocalProducts);
115 foreach($this->a_availableLocalProducts as $name => $data){
116 if(isset($this->a_selectedLocalProducts[$name])) continue;
118 $add_tab = array("string" => "<input type='image' src='images/back.png' name='add_lp_".$name."'>");
119 $name_tab = array("string" => $name);
120 $desc_tab = array("string" => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
121 "attach" => "title='".$data['DESC']."' style='border-right:0px;'");
122 $divALP->AddEntry(array($add_tab,$name_tab,$desc_tab));
123 }
125 /* Create list of selected local products
126 */
127 ksort($this->a_selectedLocalProducts);
128 foreach($this->a_selectedLocalProducts as $name => $data){
130 $name_tab = array("string" => $name);
131 $desc_tab = array(
132 "string" => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
133 "attach" => "title='".$data['DESC']."'");
135 /* Only display edit button, if there is something to edit
136 */
137 $edit = "<img src='images/empty.png' alt=' '>";
138 if(count($data['CFG'])){
139 $edit = "<input type='image' src='images/lists/edit.png' name='edit_lp_".$name."'>";
140 }
141 $del = "<input type='image' src='images/lists/trash.png' name='del_lp_".$name."'>";
143 $opt_tab = array("string" => $edit.$del,
144 "attach" => "style='border-right:0px; width: 40px; text-align:right;'");
145 $divSLP->AddEntry(array($name_tab,$desc_tab,$opt_tab));
146 }
148 $smarty->assign("divSLP", $divSLP->DrawList());
149 $smarty->assign("divALP", $divALP->DrawList());
150 $smarty->assign("SNP", $this->s_selectedNetbootProduct);
152 ksort($this->a_availableNetbootProducts);
153 $smarty->assign("ANP", $this->a_availableNetbootProducts);
154 return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
155 }
158 public function save()
159 {
160 $add = array_diff_assoc($this->a_selectedLocalProducts,$this->a_initial_selectedLocalProducts);
161 $del = array_diff_assoc($this->a_initial_selectedLocalProducts,$this->a_selectedLocalProducts);
163 foreach($del as $name => $data){
164 $this->opsi->del_product_from_client($name,$this->hostId);
165 }
166 foreach($add as $name => $data){
167 $this->opsi->add_product_to_client($name,$this->hostId);
168 $this->opsi->set_product_properties($name,$data['CFG'],$this->hostId);
169 }
171 foreach($this->a_selectedLocalProducts as $name => $data){
172 if(isset($del[$name]) || isset($add[$name])) continue;
173 $diff = array_diff($data['CFG'],$this->a_initial_selectedLocalProducts[$name]['CFG']);
174 if(count($diff)){
175 $this->opsi->set_product_properties($name,$diff,$this->hostId);
176 }
177 }
179 if($this->s_selectedNetbootProduct != $this->s_initial_selectedNetbootProduct){
180 if(!empty($this->s_initial_selectedNetbootProduct)){
181 $this->opsi->del_product_from_client($this->s_initial_selectedNetbootProduct,$this->hostId);
182 $this->opsi->add_product_to_client($this->s_selectedNetbootProduct,$this->hostId);
183 }
184 }
185 }
187 public function remove_from_parent()
188 {}
191 public function save_object()
192 {
193 if(isset($_POST['reinit'])){
194 $this->init();
195 }
197 if(isset($_POST['cancel_properties']) && is_object($this->dialog)){
198 $this->dialog = NULL;
199 }
200 if(isset($_POST['save_properties']) && ($this->dialog instanceof opsi_product_config)){
201 $this->dialog->save_object();
202 $pro = $this->dialog->get_product();
203 $CFG = $this->dialog->get_cfg();
204 if(isset($this->a_selectedLocalProducts[$pro])){
205 $this->a_selectedLocalProducts[$pro]['CFG'] = $CFG;
206 }
207 $this->dialog = NULL;
208 }
210 if(isset($_POST['opsigeneric'])){
212 if(isset($_POST['opsi_netboot_product'])){
213 $SNP = trim($_POST['opsi_netboot_product']);
214 if(isset($this->a_availableNetbootProducts[$SNP])){
215 $this->s_selectedNetbootProduct = $SNP;
216 }
217 }
219 foreach($_POST as $name => $value){
220 if(preg_match("/^add_lp_/",$name)){
221 $product = preg_replace("/^add_lp_(.*)_.$/","\\1",$name);
222 if(isset($this->a_availableLocalProducts[$product]) && !isset($this->a_selectedLocalProducts[$product])){
223 $this->a_selectedLocalProducts[$product] = $this->a_availableLocalProducts[$product];
224 $CFG = $this->opsi->get_product_properties($product);
225 $this->a_selectedLocalProducts[$product]['CFG'] = $CFG;
226 }
227 break;
228 }
229 if(preg_match("/^del_lp_/",$name)){
230 $product = preg_replace("/^del_lp_(.*)_.$/","\\1",$name);
231 if(isset($this->a_selectedLocalProducts[$product])){
232 unset($this->a_selectedLocalProducts[$product]);
233 }
234 break;
235 }
236 if(preg_match("/^edit_lp_/",$name)){
237 $product = preg_replace("/^edit_lp_(.*)_.$/","\\1",$name);
238 $this->dialog = new opsi_product_config($this->config,
239 $product,$this->a_selectedLocalProducts[$product]['CFG'],$this->hostId);
240 break;
241 }
242 }
243 }
244 }
246 /* Return plugin informations for acl handling */
247 static function plInfo()
248 {
249 return (array(
250 "plShortName" => _("Generic"),
251 "plDescription" => _("Opsi generic"),
252 "plSelfModify" => FALSE,
253 "plDepends" => array(),
254 "plPriority" => 1,
255 "plSection" => array("administration"),
256 "plCategory" => array("opsi" => array("description" => _("Opsi client"),
257 "objectClass" => "dummy_class_opsi")),
258 "plProvidedAcls"=> array()
259 ));
260 }
262 }
265 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
266 ?>