Code

some fixes
[gosa.git] / plugins / admin / systems / class_printGeneric.inc
1 <?php
3 class printgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage terminal base objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Generic terminal attributes */
11   var $interfaces     = array();
12   var $ignore_account = FALSE;
14   /* Needed values and lists */
15   var $base             = "";
16   var $cn               = "";
17   var $macAddress       = "";
18   var $ipHostNumber     = "";
19   var $l                = "";
20   var $description      = "";
21   var $labeledURI       = "";
22   var $gotoPrinterPPD   = "";
23   var $orig_dn          = "";
24   var $is_terminal      = false;
26   var $UserMember       ="";
27   var $UserMembers      =array();
28   var $UserMemberKeys   =array();
30   var $AdminMember      ="";
31   var $AdminMembers     =array();
32   var $AdminMemberKeys  =array();
33   
34   var $member           =array();
35   var $strings          = "";
36   var $type             = "";
37   var $dialog           =NULL;
39   /* attribute list for save action */
40   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","macAddress", "ipHostNumber");
41   var $objectclasses  = array("top", "gotoPrinter");
43   function printgeneric ($config, $dn= NULL)
44   {
45     plugin::plugin ($config, $dn);
46     $ldap= $this->config->get_ldap_link();
48     /* Set base */
49     if ($this->dn == "new"){
50       $ui= get_userinfo();
51       $this->base= dn2base($ui->dn);
52       $this->cn= "";
53     } else {
54       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
55     }
58     /* In case of gotoWorkstation this tab is calles from workstation plugin
59      * in case of gotoTerminal it is called from a terminal tab
60      * else it is a standalone printer
61      */
62     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
63       $this->is_terminal = "true";   
64       $this->dn   = preg_replace("/ou=workstation/","ou=printer",$this->dn);
65       $this->type = "station";
66     }elseif((in_array("gotoTerminal",$this->attrs['objectClass']))){
67       $this->type = "terminal";
68       $this->is_terminal = "true";
69       $this->dn   = preg_replace("/ou=terminal/","ou=printer",$this->dn);
70     }else{
71       /* Save dn for later references */
72       $this->orig_dn= $this->dn;
73     }
75     /* If it is no standalone printer 
76      */
77     if($this->is_terminal){
78       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
79       plugin::plugin ($this->config, $this->dn);
80       $ldap->cat($this->dn);
81       if(count($ldap->fetch())>0){
82         $this->orig_dn= $this->dn;
83         $this->is_account=true;
84         $this->initially_was_account = true;
85       }else{
86         $this->orig_dn = "new";
87         $this->is_account=false;
88         $this->initially_was_account = false;
89       }
90     }
92    
93     /* Prepare different member types 
94      */ 
95     foreach(array("AddUser"       =>"gotoUserPrinter",
96                   "AddGroup"      =>"gotoGroupPrinter",
97                   "AddAdminUser"  =>"gotoUserAdminPrinter",
98                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
100       /* $this->members contains all members */
101       $this->member[$type]=array();
103       unset($this->attrs[$attr]['count']);
105       if(isset($this->attrs[$attr])){
106         foreach($this->attrs[$attr] as $mem){
107           if(preg_match("/Group/",$type)){
108             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
109             $entry = $ldap->fetch();
110             if(isset($entry['description'])){
111               $this->member[$type][$entry['cn'][0]]=$entry;
112             }
113           }else{
114             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
115             $entry = $ldap->fetch();
116             if(isset($entry['uid'])){
117               $this->member[$type][$entry['uid'][0]]=$entry;
118             }
119           }
120         }
121       }
122     }
123   }
125   function execute()
126   {
127     $smarty= get_smarty();
128     $display="";
129     /* Template management.
130      * There are two ways to call this tab.
131      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
132      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
133      *    Toggle Account state will be shown in this case, to disable or enable this tab.
134      *
135      * $this->is_terminal indecates this two different types.
136      */
137     if($this->is_terminal){    
138       $smarty->assign("is_terminal","true");
139     }else{
140       $smarty->assign("is_terminal","false");
141     }
143     /* Do we need to flip is_account state? */
144     if (isset($_POST['modify_state'])){
145       $this->is_modified = true;
146       $this->is_account= !$this->is_account;
147     }
149     if($this->is_terminal){
150       if ($this->is_account){
151         if($this->type=="station"){
152           $display= $this->show_header(_("Remove printer extension"),
153               _("This workstation has printer extension enabled. You can disable it by clicking below."));
154         }else{
155           $display= $this->show_header(_("Remove printer extension"),
156               _("This terminal has printer extension enabled. You can disable it by clicking below."));
157         }
158       }else{
159         if($this->type=="station"){
160           $display= $this->show_header(_("Add printer extension"),
161               _("This workstation has printer extension disabled. You can enable it by clicking below."));
162         }else{
163           $display= $this->show_header(_("Add printer extension"),
164               _("This terminal has printer extension disabled. You can enable it by clicking below."));
165         }  
166         return ($display);
167       }
168     }
171     /* Do we represent a valid printer? */
172     if (!$this->is_account && $this->parent == NULL){
173       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
174         _("This 'dn' has no printer features.")."</b>";
175       return($display);
176     }
178     /* Fill templating stuff */
179     $smarty->assign("bases", $this->config->idepartments);
180     $smarty->assign("base_select", $this->base);
182     /* Assign attributes */
183     foreach ($this->attributes as $attr){
184       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
185       $smarty->assign("$attr", $this->$attr);
186     }
187     
188     if(isset($_POST['AddUser'])){
189       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
190     }
191     if(isset($_POST['AddGroup'])){
192       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
193     }
194     if(isset($_POST['AddAdminUser'])){
195       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
196     }
197     if(isset($_POST['AddAdminGroup'])){
198       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
199     }
201     if(isset($_POST['PrinterCancel'])){
202       unset($this->dialog);
203       $this->dialog= NULL;
204     }
206     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
207       if(isset($this->member['AddUser'][$_POST['UserMember']])){
208         unset($this->member['AddUser'][$_POST['UserMember']]);
209       }
210     }
212     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
213       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
214         unset($this->member['AddGroup'][$_POST['UserMember']]);
215       }
216     }
218     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
219       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
220         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
221       }
222     }
224     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
225       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
226         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
227       }
228     }
230     if(isset($_POST['PrinterSave'])){
231       $this->dialog->save_object();
232       if(count($this->dialog->check())){
233         foreach($this->dialog->check() as $msg){
234           print_red($msg);
235         }
236       }else{
237         $new = $this->dialog->save();
238         $data = $new;
239         unset($data['type']);
241         if(preg_match("/User/",$new['type'])){
242           $use = "uid";
243         }else{
244           $use = "cn";
245         }
247         foreach($data as $mem){
248           $this->member[$new['type']][$mem[$use][0]]=$mem;    
249         }
250         unset($this->dialog);
251         $this->dialog=NULL; 
252       }
253     }
255     if($this->dialog != NULL){
256       $display = $this->dialog->execute();
257       return $display;
258     }
260     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
261     $smarty->assign("staticAddress", "");
262     $smarty->assign("driverInfo", "Not implemented yet");
264     $list=$this->generateList();
265     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
266     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
268     asort($userlist);
269     asort($adminlist);
271     $smarty->assign("UserMember"    ,$this->UserMember);
272     $smarty->assign("UserMembers"   ,$userlist);
273     $smarty->assign("UserMemberKeys",array_flip($userlist));
275     $smarty->assign("AdminMember"    ,$this->AdminMember);
276     $smarty->assign("AdminMembers"   ,$adminlist);
277     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
278     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
280     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
281   }
283   function remove_from_parent()
284   {
285     $ldap= $this->config->get_ldap_link();
286     $ldap->rmdir($this->dn);
287     show_ldap_error($ldap->get_error());
288     $this->handle_post_events("remove");
290     /* Delete references to object groups */
291     $ldap->cd ($this->config->current['BASE']);
292     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
293     while ($ldap->fetch()){
294       $og= new ogroup($this->config, $ldap->getDN());
295       unset($og->member[$this->dn]);
296       $og->save ();
297     }
298   }
300   /* Save data to object */
301   function save_object()
302   {
303     plugin::save_object();
305     /* Save base, since this is no LDAP attribute */
306     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
307       $this->base= $_POST['base'];
308     }
309   }
311   /* Check supplied data */
312   function check()
313   {
314     $message= array();
315     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
317     /* must: cn */
318     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
319       $message[]= "The required field 'Printer name' is not set.";
320     }
322     $ui= get_userinfo();
323     $acl= get_permissions ($this->dn, $ui->subtreeACL);
324     $acl= get_module_permission($acl, "printer", $this->dn);
325     if (chkacl($acl, "create") != ""){
326       $message[]= _("You have no permissions to create a printer on this 'Base'.");
327     }
329     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
330       $ldap= $this->config->get_ldap_link();
331       $ldap->cd ($this->base);
332       $ldap->search ("(cn=".$this->cn.")", array("cn"));
333       if ($ldap->count() != 0){
334         while ($attrs= $ldap->fetch()){
335           if ($attrs['dn'] != $this->orig_dn){
336             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
337             break;
338           }
339         }
340       }
341     }
343     return ($message);
344   }
347   /* Save to LDAP */
348   function save()
349   {
350     $dn= $this->dn;
351     plugin::save();
352     $ldap= $this->config->get_ldap_link();
353    
354     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
355       $this->dn= preg_replace("/ou=terminal/","ou=printer",$this->dn);
356     }
357     
358     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
359       $this->dn= preg_replace("/ou=workstation/","ou=printer",$this->dn);
360     }
362     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
363     */
364     if($this->is_terminal){
365       
366       /* reduce objectClasses to minimun */
367       $this->attrs['objectClass']= $this->objectclasses;
369       /* If a printer with the given dn exists, modify else create new one */
370       $ldap->cat($this->dn);
371       if($ldap->fetch()){
372         $this->orig_dn= $this->dn;
373       }else{
374         $this->orig_dn = "new";
375       }
376     }
378     /* Remove all empty values */
379     if ($this->orig_dn == 'new'){
380       $attrs= array();
381       foreach ($this->attrs as $key => $val){
382         if (is_array($val) && count($val) == 0){
383           continue;
384         }
385         $attrs[$key]= $val;
386       }
387       $this->attrs= $attrs;
388     }
391     /* Append printer user 
392      */
393     foreach($this->member['AddUser'] as $mem){
394       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
395     }
397     /* Append printer group 
398      */
399     foreach($this->member['AddGroup'] as $mem){
400       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
401     }
403     /* Append printer admin user 
404      */
405     foreach($this->member['AddAdminUser'] as $mem){
406       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
407     }
408     
409     /* Append printer admin group 
410      */
411     foreach($this->member['AddAdminGroup'] as $mem){
412       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
413     }
415     /* Write back to ldap */
416     $ldap= $this->config->get_ldap_link();
417     if ($this->orig_dn == 'new'){
418       $ldap->cd($this->config->current['BASE']);
419       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
420       $ldap->cd($this->dn);
421       $ldap->add($this->attrs);
422       $this->handle_post_events("add");
423     } else {
424       if ($this->orig_dn != $this->dn){
425         $this->move($this->orig_dn, $this->dn);
426       }
428       $ldap->cd($this->dn);
429       $ldap->modify($this->attrs);
430       $this->handle_post_events("modify");
431     }
432     show_ldap_error($ldap->get_error());
434     /* Optionally execute a command after we're done */
435     $this->postcreate();
436   }
438   function generateList(){
439     $a_return=array();
440     foreach($this->member as $type => $values){
441       $a_return[$type]=array();
442       foreach($values as $value){
443         if((preg_match("/Group/i",$type))){
444           if(!isset($value['description'])){
445             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
446           }else{
447             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
448           }
449         }else{
450           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
451         }
452       }
453     }
454     return($a_return);
455   }
460 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
461 ?>