Code

idProduct- and idVendor-Inputfield should be caseinsensitive.
[gosa.git] / trunk / gosa-plugins / goto / admin / devices / class_deviceGeneric.inc
1 <?php
3 class deviceGeneric extends plugin
4 {
5   public $dn            = "";
6   public $cn            = "";
7   public $orig_cn       = "";
8   public $orig_base     = "";
9   public $orig_dn       = "";
10   public $description   = "";
11   public $vendor        = "";
12   public $devID        = "";
13   public $serial        = "";
14   public $base          = "";   
15   public $types;
16   public $type;
18   public $attributes      = array("cn","description","devID","serial","vendor", "type");
19   public $objectclasses   = array("top","gotoDevice");
21   public $CopyPasteVars   = array("orig_cn","description","vendor","devID","serial","base");
23   public function deviceGeneric(&$config,$dn = NULL)
24   {
25     plugin::plugin($config,$dn);
27     $this->is_account = TRUE;
29     $this->types= array("camera" => _("Digital camera"),
30                         "disc" => _("USB stick"),
31                         "cdrom" => _("CD/DVD drive"),);
32     asort($this->types);
34     /* Set class values */
35     if(isset($this->attrs['gotoHotplugDevice'][0])){
36       $tmp = preg_split("/\|/",$this->attrs['gotoHotplugDevice'][0]);
37       $this->cn         = $this->attrs['cn'][0];
38       $this->description= $tmp[0];
39       $this->devID     = $tmp[1];
40       $this->serial     = $tmp[2];
41       $this->vendor     = $tmp[3];
42       if (isset($tmp[4])){
43         $this->type= $tmp[4];
44       }
45     }     
47     $this->orig_cn = $this->cn;
49     /* Set Base */
50     if ($this->dn == "new"){
51       if(session::is_set('CurrentMainBase')){
52         $this->base = session::get('CurrentMainBase');
53       }else{
54         $ui= get_userinfo();
55         $this->base= dn2base($ui->dn);
56       }
57     } else {
58       $this->base =preg_replace ("/^[^,]+,".get_ou('deviceRDN')."/","",$this->dn);
59     }
60     $this->orig_base = $this->base;
61     $this->orig_dn   = $this->dn;
62   }
65   public function execute()
66   {
67     $smarty = get_smarty();
68     $smarty->assign("base",$this->base);
69     $smarty->assign("bases",$this->get_allowed_bases());
70     foreach($this->attributes as $attr){
71       $smarty->assign($attr,$this->$attr);
72     }
73   
74     $tmp = $this->plInfo();
75     foreach($tmp['plProvidedAcls'] as $attr => $desc){
76       $smarty->assign($attr."ACL", $this->getacl($attr));
77     }
79     $smarty->assign("type",$this->type);
80     $smarty->assign ("types", $this->types);
81     return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
82   }
84   
85   public function check()
86   {
87     $message = plugin::check();
89     if($this->cn == "" ||(preg_match("/[^a-z0-9]/i",$this->cn))){
90       $message[]= msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9]*$/i");
91     }
92     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
93       $message[]= msgPool::invalid(_("Description"),$this->cn,"/^[a-z0-9!\"?.,;:-_\(\) ]*$/i");
94     }
96     /* Skip serial check if vendor and product id are given */
97     if(preg_match("/^\s+$/i",$this->devID)){
98       $message[]= msgPool::invalid(_("iSerial"),"","01234");
99     }elseif(empty($this->devID) && $this->devID!="0"){
100       $message[]= msgPool::required(_("iSerial"));
101     }
102     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
103       $message[]= msgPool::invalid(_("Product ID"),"","","0x1234");
104     }
105     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
106       $message[]= msgPool::invalid(_("Vendor ID"),"","","0x1234");
107     }
108   
109     /* Check if entry already exists */ 
110     if($this->cn != $this->orig_cn || $this->dn == "new"){
111       $ldap = $this->config->get_ldap_link();
112       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
113       if($ldap->count()){
114         $message[]= msgPool::duplicated(_("Name"));
115       }
116     }
118     /* Check if we are allowed to create or move this object
119      */
120     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
121       $message[] = msgPool::permCreate();
122     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
123       $message[] = msgPool::permMove();
124     }
126     return($message);
127   }
130   public function save_object()
131   {
132     if(isset($_POST['deviceGeneric_posted'])){
133       plugin::save_object();
134   
135       if(isset($_POST['base'])){
136         $tmp = $this->get_allowed_bases();
137         if(isset($tmp[get_post("base")])){
138           $this->base = get_post("base");
139         }
140       }
141     }
142   }
144   
145   public function remove_from_parent()
146   {
147     plugin::remove_from_parent();
148     $ldap = $this->config->get_ldap_link();
149     $ldap->cd($this->config->current['BASE']);
151     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
152     $skip = FALSE;
153     $obj = array();
154     while($attrs = $ldap->fetch()){
155       $obj[$ldap->getDN()]= $attrs['cn'][0];
156       $skip =TRUE;
157     }
158     if($skip){
159       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
160     }else{
161       $ldap->rmdir_recursive($this->dn);
162     }
163   }
166   public function save()
167   {
168     plugin::save();
170     /* Unset non ldap attributes 
171      */
172     foreach(array("devID","serial","vendor", "type", "description") as $name){
173       if(isset($this->attrs[$name])){
174         unset($this->attrs[$name]);
175       }
176     }
178     $this->attrs['gotoHotplugDevice'] = "";
179     foreach(array("description","devID","serial","vendor", "type") as $post){
180       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
181     }
182     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
184     $ldap = $this->config->get_ldap_link();
185     $ldap->cd($this->config->current['BASE']);
186     $ldap->cat($this->dn);
187     if($ldap->count()){
188       $ldap->cd($this->dn);
189       $ldap->modify($this->attrs);
190     }else{
191       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
192       $ldap->cd($this->dn);
193       $ldap->add($this->attrs);
194     }
195     if (!$ldap->success()){
196       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
197     }
198   }
201   /* check if given str in like this 0xffff*/
202   function is_2byteHex($str)
203   {
204     return !strlen($str) || preg_match("/^(0x|)[A-Fa-f0-9]{4}$/i",$str);
205   }
208   function PrepareForCopyPaste($source)
209   {
210     plugin::PrepareForCopyPaste($source);
211     $source_o = new deviceGeneric($this->config,$source['dn']);
212     foreach($this->CopyPasteVars as $post){
213       $this->$post = $source_o->$post;
214     }
215   }
218   /* Return a dialog with all fields that must be changed,
219      if we want to copy this entry */
220   function getCopyDialog()
221   {
222     $str = "";
223     $smarty = get_smarty();
224     $smarty->assign("cn",         $this->cn);
225     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
227     $ret = array();
228     $ret['string'] = $str;
229     $ret['status'] = "";
230     return($ret);
231   }
234   /* Save all */
235   function saveCopyDialog()
236   {
237     $attrs = array("cn");
238     foreach($attrs as $attr){
239       if(isset($_POST[$attr])){
240         $this->$attr = $_POST[$attr];
241       }
242     }
243   }
247   /* Return plugin informations for acl handling  */
248   public static function plInfo()
249   {
250     return (array(
251           "plShortName"   => _("Generic"),
252           "plDescription" => _("Device generic")." ("._("Hotplug").")",
253           "plSelfModify"  => FALSE,
254           "plDepends"     => array(),
255           "plPriority"    => 0,
256           "plSection"     => array("administration"),
257           "plCategory"    => array("devices" => array("description"  => _("Devices"),
258                                                         "objectClass"  => "gotoHotplugDevice")),
259           "plProvidedAcls"=> array(
260             "cn"            => _("Name"),
261             "base"          => _("Base"),
262             "description"   => _("Description"),
263             "type"          => _("Type"),
264             "serial"        => _("Serial"),
265             "vendor"        => _("Vendor"), 
266             "devID"        => _("Device ID"))
267           ));
269   }
270         
272 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
273 ?>