Code

fa3ea53988faeae3ec17ec3fbc754bb0feb2ff74
[gosa.git] / plugins / personal / environment / class_hotplugDialog.inc
1 <?php
3 class hotplugDialog extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account       = TRUE;
7   var $attributes           = array("HOT_name","HOT_description","HOT_id","HOT_produkt","HOT_vendor");
8   var $objectclasses        = array("whatever");
9   var $use_existing         = false;  
11   var $HOT_name                 = "";
12   var $HOT_description          = "";
13   var $HOT_id                   = "0x0000";
14   var $HOT_produkt              = "";
15   var $HOT_vendor               = "";
17   var $hotplugDevices           = ""; 
18   var $hotplugDeviceList        = array(); 
19   var $regex                    = "*";
20   var $depselect                = "/";
21   var $deplist                  = "/";
22   var $skipThese                = array();
24   function hotplugDialog ($config, $dn= NULL,$use_existing=false ,$skipThese = array())
25   {
26     $this->use_existing = $use_existing;
27     $this->skipThese    = $skipThese;
28     plugin::plugin ($config, $dn);
29     $this->depselect = $this->config->current['BASE'];
30   }
32   function execute()
33   {
34         /* Call parent execute */
35         plugin::execute();
37     /* Fill templating stuff */
38     $smarty= get_smarty();
39     $display= "";
41     foreach($this->attributes as $s_attr){
42       $smarty->assign($s_attr,$this->$s_attr);
43     }
45     if($this->use_existing){
47       if(isset($_POST['dialogissubmitted'])){
48         $this->regex=$_POST['regexHot'];
49         $this->depselect = $_POST['depselectHot'];
50       }
52       if((isset($_GET['search']))&&(!empty($_GET['search']))){
53         $this->regex=$_GET['search']."*";
54         $this->regex=preg_replace("/\*\*/","*",$this->regex);
55       }
57       $this->deplist=$this->config->idepartments;
59       $this->hotplugDevices = $this->getHotplugs();
60       $smarty->assign("regexHot"    ,$this->regex);
61       $smarty->assign("deplistHot"  ,$this->deplist);
62       $smarty->assign("depselectHot",$this->depselect);
63       $smarty->assign("apply", apply_filter());
64       $smarty->assign("alphabet", generate_alphabet());
65       $smarty->assign("search_image", get_template_path('images/search.png'));
66       $smarty->assign("tree_image", get_template_path('images/tree.png'));
67       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
68       $smarty->assign("deplist", $this->config->idepartments);
71       $smarty->assign("hotplugDevices",$this->hotplugDevices);
72       $smarty->assign("hotplugDeviceKeys",array_flip($this->hotplugDevices));
73       $display.= $smarty->fetch(get_template_path('hotplugDialog.tpl', TRUE,dirname(__FILE__)));
74     }else{
75       $display.= $smarty->fetch(get_template_path('hotplugDialogNew.tpl', TRUE,dirname(__FILE__)));
76     }
77     return($display);
78   }
80   function remove_from_parent()
81   {
82     /* This cannot be removed... */
83   }
86   /* Save data to object */
87   function save_object()
88   {
89     if(isset($_POST['dialogissubmitted'])){
90       plugin::save_object();
91       foreach($this->attributes as $s_attr){
92         if(isset($_POST[$s_attr])){
93           if(in_array($s_attr,array("HOT_produkt","HOT_vendor"))){
94             $this->$s_attr = strtolower(stripslashes($_POST[$s_attr]));
95           }else{
96             $this->$s_attr = stripslashes($_POST[$s_attr]);
97           }
98         }else{
99           $this->$s_attr = false;
100         }
101       }
102     }
103   }
106   /* check if given str in like this 0xffff*/
107   function is_2byteHex($str)
108   {
109     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
110   }
113   /* Check supplied data */
114   function check()
115   {
116     /* Call common method to give check the hook */
117     $message= plugin::check();
119     if(!$this->use_existing){
121       if(empty($this->HOT_name)||(preg_match("/[^a-z0-9]/i",$this->HOT_name))){
122         $message[]=_("Please specify a valid name. Only 0-9 a-Z is allowed.");
123       }
124       if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->HOT_description)){
125         $message[]=_("Invalid character in description. Please specify a valid description.");
126       }
128       /* Skip serial check if vendor and product id are given */
129       if(!preg_match("/^[a-z0-9]+$/i",$this->HOT_id)){
130         $message[]=_("Please specify a valid iSerial.");
131       }
132       if(empty($this->HOT_vendor) || !$this->is_2byteHex($this->HOT_vendor)){
133         $message[]=_("Please specify a valid vendor ID. (2 byte hex like '0xFFFF')");
134       }
135       if(empty($this->HOT_produkt) || !$this->is_2byteHex($this->HOT_produkt)){
136         $message[]=_("Please specify a valid product ID. (2 byte hex like '0xFFFF')");
137       }
138       $ldap = $this->config->get_ldap_link();
139       $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDevice=".$this->HOT_name."*))",array("gotoHotplugDevice"));
140       if($ldap->count()){
141         $message[]=_("An Entry with this name already exists.");
142       }
144     }else{
145       if((!isset($_POST['hotplugName']))||(empty($_POST['hotplugName']))){
146         $message[] = _("Please select an entry or press cancel.");
147       }
148     }
150     return ($message);
151   }
154   /* Save to LDAP */
155   function save()
156   {
157     /* return generated entry from input fields*/
158     if(!$this->use_existing){
159       $a_return=array();
160       $a_return[$this->HOT_name]['name']        = $this->HOT_name; 
161       $a_return[$this->HOT_name]['description'] = $this->HOT_description; 
162       $a_return[$this->HOT_name]['id']          = $this->HOT_id;
163       $a_return[$this->HOT_name]['produkt']     = $this->HOT_produkt;
164       $a_return[$this->HOT_name]['vendor']      = $this->HOT_vendor;
165       return($a_return); 
166     }else{
167       $entries = array();
168       foreach($_POST['hotplugName'] as $name){
169         $entries[$name] = $this->hotplugDeviceList[$name];
170       }
171       return $entries;
172     }
173   }
175   function getHotplugs(){
176     $ldap= $this->config->get_ldap_link();
177     $ldap->cd($this->depselect);
179     /* Set tag attribute if we've tagging activated */
180     $tag= "";
181     $ui= get_userinfo();
182     if ($ui->gosaUnitTag != "" && isset($this->config->current['STRICT_UNITS']) &&
183         preg_match('/TRUE/i', $this->config->current['STRICT_UNITS'])){
184       $tag= "(gosaUnitTag=".$ui->gosaUnitTag.")";
185     }
186     $ldap->search("(&(objectClass=gotoEnvironment)$tag(gotoHotplugDevice=".$this->regex."))",array("gotoHotplugDevice")); 
188     $a_return = array();  
189     $this->hotplugDeviceList = array();
190     while($attr = $ldap->fetch()){
191       if(isset($attr['gotoHotplugDevice'])){
192         unset($attr['gotoHotplugDevice']['count']);
193         foreach($attr['gotoHotplugDevice'] as $device){
194           $tmp =$tmp2= array();
195           $tmp = split("\|",$device);
196           
197           if(preg_match("/^".str_replace("*","",$this->regex).".*/i",$tmp[0])){
198   
199             if(in_array($tmp[0],$this->skipThese)) continue;
201             $a_return[$tmp[0]]= $tmp[0]." [".$tmp[1]."] ".$tmp[2];
202             
203             $tmp2['name']         = $tmp[0];
204             $tmp2['description']  = $tmp[1];
205             $tmp2['id']           = $tmp[2];
207             /* Produkt ID */
208             if(!isset($tmp[3])){
209               $tmp[3] = "";
210             }
211             /* Vendor ID */
212             if(!isset($tmp[4])){
213               $tmp[4] = "";
214             }
216             $tmp2['produkt']      = $tmp[3];
217             $tmp2['vendor']       = $tmp[4];
219             $this->hotplugDeviceList[$tmp[0]]=$tmp2;
220           }
221         }
222       }
223     }
224   return($a_return);
225   }
229 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
230 ?>