Code

Added seperate list handler for gotomasses.
[gosa.git] / plugins / 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          = "";   
14   public $posts           = array("description","dev_id","serial","vendor");
15   public $attributes      = array("cn");
16   public $objectclasses   = array("top","gotoDevice");
18   public $CopyPasteVars   = array("orig_cn","description","vendor","dev_id","serial","base");
20   public function deviceGeneric(&$config,$dn = NULL)
21   {
22     plugin::plugin($config,$dn);
24     $this->is_account = TRUE;
26     /* Set class values */
27     if(isset($this->attrs['gotoHotplugDevice'][0])){
28       $tmp = preg_split("/\|/",$this->attrs['gotoHotplugDevice'][0]);
29       $this->cn         = $this->attrs['cn'][0];
30       $this->description= $tmp[0];
31       $this->dev_id     = $tmp[1];
32       $this->serial     = $tmp[2];
33       $this->vendor     = $tmp[3];
34     }     
36     $this->orig_cn = $this->cn;
38     /* Set Base */
39     if ($this->dn == "new"){
40       if(isset($_SESSION['CurrentMainBase'])){
41         $this->base= $_SESSION['CurrentMainBase'];
42       }else{
43         $ui= get_userinfo();
44         $this->base= dn2base($ui->dn);
45       }
46     } else {
47       $this->base =preg_replace ("/^[^,]+,ou=devices,/","",$this->dn);
48     }
49   }
52   public function execute()
53   {
54     $smarty = get_smarty();
55     $smarty->assign("base",$this->base);
56     $smarty->assign("bases",$this->get_allowed_bases());
57     foreach($this->attributes as $attr){
58       $smarty->assign($attr,$this->$attr);
59     }
60     foreach($this->posts as $attr){
61       $smarty->assign($attr,$this->$attr);
62     }
63     return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
64   }
66   
67   public function check()
68   {
69     $message = plugin::check();
71     if(empty($this->cn)||(preg_match("/[^a-z0-9]/i",$this->cn))){
72       $message[]=_("Please specify a valid name. Only 0-9 a-Z is allowed.");
73     }
74     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
75       $message[]=_("Invalid character in description. Please specify a valid description.");
76     }
78     /* Skip serial check if vendor and product id are given */
79     if(preg_match("/^\s+$/i",$this->dev_id)){
80       $message[]=_("Please specify a valid iSerial.");
81     }
82     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
83       $message[]=_("Please specify a valid vendor ID. (2 byte hex like '0xFFFF')");
84     }
85     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
86       $message[]=_("Please specify a valid product ID. (2 byte hex like '0xFFFF')");
87     }
88    
89     /* Check if entry already exists */ 
90     if($this->cn != $this->orig_cn){
91       $ldap = $this->config->get_ldap_link();
92       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
93       if($ldap->count()){
94         $message[]=_("An Entry with this name already exists.");
95       }
96     }
98     return($message);
99   }
102   public function save_object()
103   {
104     if(isset($_POST['deviceGeneric_posted'])){
105       plugin::save_object();
106   
107       if(isset($_POST['base'])){
108         $tmp = $this->get_allowed_bases();
109         if(isset($tmp[get_post("base")])){
110           $this->base = get_post("base");
111         }
112       }
113     
114       foreach($this->posts as $post){
115         if(isset($_POST[$post])){
116           $this->$post = get_post($post);
117         }
118       }
119     }
120   }
122   
123   public function remove_from_parent()
124   {
125     plugin::remove_from_parent();
126     $ldap = $this->config->get_ldap_link();
127     $ldap->cd($this->config->current['BASE']);
129     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
130     $skip = FALSE;
131     $str ="";
132     $cnt = 3;
133     while($cnt && $attrs = $ldap->fetch()){
134       $skip =TRUE;
135       $str .= $attrs['cn'][0].", ";
136       $cnt --;
137     }
138     if($skip){
139       $str  = preg_replace("/, $/","",$str);
140       if($cnt == 0){
141         $str .= "...";
142       }
143       print_red(sprintf(_("Can't remove the device '%s' it is still in use be this user(s) : %s"),$this->cn,$str));
144     }else{
145       $ldap->rmdir_recursive($this->dn);
146     }
147   }
150   public function save()
151   {
152     plugin::save();
154     $this->attrs['gotoHotplugDevice'] = "";
155     foreach($this->posts as $post){
156       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
157     }
158     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
160     $ldap = $this->config->get_ldap_link();
161     $ldap->cd($this->config->current['BASE']);
162     $ldap->cat($this->dn);
163     if($ldap->count()){
164       $ldap->cd($this->dn);
165       $ldap->modify($this->attrs);
166     }else{
167       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
168       $ldap->cd($this->dn);
169       $ldap->add($this->attrs);
170     }
171     show_ldap_error($ldap->get_error(),_("Device could not be saved."));
172   }
175   /* check if given str in like this 0xffff*/
176   function is_2byteHex($str)
177   {
178     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
179   }
182   function PrepareForCopyPaste($source)
183   {
184     plugin::PrepareForCopyPaste($source);
185     $source_o = new deviceGeneric($this->config,$source['dn']);
186     foreach($this->CopyPasteVars as $post){
187       $this->$post = $source_o->$post;
188     }
189   }
192   /* Return a dialog with all fields that must be changed,
193      if we want to copy this entry */
194   function getCopyDialog()
195   {
196     $str = "";
197     $smarty = get_smarty();
198     $smarty->assign("cn",         $this->cn);
199     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
201     $ret = array();
202     $ret['string'] = $str;
203     $ret['status'] = "";
204     return($ret);
205   }
208   /* Save all */
209   function saveCopyDialog()
210   {
211     $attrs = array("cn");
212     foreach($attrs as $attr){
213       if(isset($_POST[$attr])){
214         $this->$attr = $_POST[$attr];
215       }
216     }
217   }
221   /* Return plugin informations for acl handling  */
222   public function plInfo()
223   {
224     return (array(
225           "plShortName"   => _("Generic"),
226           "plDescription" => _("Device generic"),
227           "plSelfModify"  => FALSE,
228           "plDepends"     => array(),
229           "plPriority"    => 0,
230           "plSection"     => array("administration"),
231           "plCategory"    => array("devices" => array("description"  => _("Devices"),
232                                                         "objectClass"  => "gotoHotplugDevice")),
233           "plProvidedAcls"=> array(
234             "cn"                          => _("Name"))
235           ));
237   }
238         
240 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
241 ?>