Code

Removed show_ldap_error() calls
[gosa.git] / 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 $description   = "";
9   public $vendor        = "";
10   public $dev_id        = "";
11   public $serial        = "";
12   public $base          = "";   
13   public $types;
14   public $type;
16   public $posts           = array("description","dev_id","serial","vendor", "type");
17   public $attributes      = array("cn");
18   public $objectclasses   = array("top","gotoDevice");
20   public $CopyPasteVars   = array("orig_cn","description","vendor","dev_id","serial","base");
22   public function deviceGeneric(&$config,$dn = NULL)
23   {
24     plugin::plugin($config,$dn);
26     $this->is_account = TRUE;
28     $this->types= array("camera" => _("Digital camera"),
29                         "harddisk" => _("Harddisk"),
30                         "stick" => _("USB stick"),
31                         "cd" => _("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->dev_id     = $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('deviceou')."/","",$this->dn);
59     }
60   }
63   public function execute()
64   {
65     $smarty = get_smarty();
66     $smarty->assign("base",$this->base);
67     $smarty->assign("bases",$this->get_allowed_bases());
68     foreach($this->attributes as $attr){
69       $smarty->assign($attr,$this->$attr);
70     }
71     foreach($this->posts as $attr){
72       $smarty->assign($attr,$this->$attr);
73     }
75     $smarty->assign("type",$this->type);
76     $smarty->assign ("types", $this->types);
78     return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
79   }
81   
82   public function check()
83   {
84     $message = plugin::check();
86     if(empty($this->cn)||(preg_match("/[^a-z0-9]/i",$this->cn))){
87       $message[]=_("Please specify a valid name. Only 0-9 a-Z is allowed.");
88     }
89     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
90       $message[]=_("Invalid character in description. Please specify a valid description.");
91     }
93     /* Skip serial check if vendor and product id are given */
94     if(preg_match("/^\s+$/i",$this->dev_id)){
95       $message[]=_("Please specify a valid iSerial.");
96     }
97     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
98       $message[]=_("Please specify a valid vendor ID. (2 byte hex like '0xFFFF')");
99     }
100     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
101       $message[]=_("Please specify a valid product ID. (2 byte hex like '0xFFFF')");
102     }
103    
104     /* Check if entry already exists */ 
105     if($this->cn != $this->orig_cn){
106       $ldap = $this->config->get_ldap_link();
107       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
108       if($ldap->count()){
109         $message[]=_("An Entry with this name already exists.");
110       }
111     }
113     return($message);
114   }
117   public function save_object()
118   {
119     if(isset($_POST['deviceGeneric_posted'])){
120       plugin::save_object();
121   
122       if(isset($_POST['base'])){
123         $tmp = $this->get_allowed_bases();
124         if(isset($tmp[get_post("base")])){
125           $this->base = get_post("base");
126         }
127       }
128     
129       foreach($this->posts as $post){
130         if(isset($_POST[$post])){
131           $this->$post = get_post($post);
132         }
133       }
134     }
135   }
137   
138   public function remove_from_parent()
139   {
140     plugin::remove_from_parent();
141     $ldap = $this->config->get_ldap_link();
142     $ldap->cd($this->config->current['BASE']);
144     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
145     $skip = FALSE;
146     $str ="";
147     $cnt = 3;
148     while($cnt && $attrs = $ldap->fetch()){
149       $skip =TRUE;
150       $str .= $attrs['cn'][0].", ";
151       $cnt --;
152     }
153     if($skip){
154       $str  = preg_replace("/, $/","",$str);
155       if($cnt == 0){
156         $str .= "...";
157       }
158       print_red(sprintf(_("Can't remove the device '%s' it is still in use be this user(s) : %s"),$this->cn,$str));
159     }else{
160       $ldap->rmdir_recursive($this->dn);
161     }
162   }
165   public function save()
166   {
167     plugin::save();
169     $this->attrs['gotoHotplugDevice'] = "";
170     foreach($this->posts as $post){
171       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
172     }
173     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
175     $ldap = $this->config->get_ldap_link();
176     $ldap->cd($this->config->current['BASE']);
177     $ldap->cat($this->dn);
178     if($ldap->count()){
179       $ldap->cd($this->dn);
180       $ldap->modify($this->attrs);
181     }else{
182       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
183       $ldap->cd($this->dn);
184       $ldap->add($this->attrs);
185     }
186     if (!$ldap->success()){
187       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
188     }
189   }
192   /* check if given str in like this 0xffff*/
193   function is_2byteHex($str)
194   {
195     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
196   }
199   function PrepareForCopyPaste($source)
200   {
201     plugin::PrepareForCopyPaste($source);
202     $source_o = new deviceGeneric($this->config,$source['dn']);
203     foreach($this->CopyPasteVars as $post){
204       $this->$post = $source_o->$post;
205     }
206   }
209   /* Return a dialog with all fields that must be changed,
210      if we want to copy this entry */
211   function getCopyDialog()
212   {
213     $str = "";
214     $smarty = get_smarty();
215     $smarty->assign("cn",         $this->cn);
216     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
218     $ret = array();
219     $ret['string'] = $str;
220     $ret['status'] = "";
221     return($ret);
222   }
225   /* Save all */
226   function saveCopyDialog()
227   {
228     $attrs = array("cn");
229     foreach($attrs as $attr){
230       if(isset($_POST[$attr])){
231         $this->$attr = $_POST[$attr];
232       }
233     }
234   }
238   /* Return plugin informations for acl handling  */
239   public static function plInfo()
240   {
241     return (array(
242           "plShortName"   => _("Generic"),
243           "plDescription" => _("Device generic"),
244           "plSelfModify"  => FALSE,
245           "plDepends"     => array(),
246           "plPriority"    => 0,
247           "plSection"     => array("administration"),
248           "plCategory"    => array("devices" => array("description"  => _("Devices"),
249                                                         "objectClass"  => "gotoHotplugDevice")),
250           "plProvidedAcls"=> array(
251             "cn"                          => _("Name"))
252           ));
254   }
255         
257 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
258 ?>