Code

Fixed error, when creating terminal template
[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();
29   
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", "macAddress", "ipHostNumber","gotoPrinterPPD");
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       $this->is_terminal = true;
54     } else {
55       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
56     }
59     /* In case of gotoWorkstation this tab is calles from workstation plugin
60      * in case of gotoTerminal it is called from a terminal tab
61      * else it is a standalone printer
62      */
63     if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
64       $this->is_terminal = "true";   
65       $this->dn   = preg_replace("/ou=workstation/","ou=printer",$this->dn);
66       $this->type = "station";
67     }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
68       $this->type = "terminal";
69       $this->is_terminal = "true";
70       $this->dn   = preg_replace("/ou=terminal/","ou=printer",$this->dn);
71     }else{
72       /* Save dn for later references */
73       $this->orig_dn= $this->dn;
74     }
76     /* If it is no standalone printer 
77      */
78     if($this->is_terminal){
79       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
80       plugin::plugin ($this->config, $this->dn);
81       $ldap->cat($this->dn);
82       if(count($ldap->fetch())>0){
83         $this->orig_dn= $this->dn;
84         $this->is_account=true;
85         $this->initially_was_account = true;
86       }else{
87         $this->orig_dn = "new";
88         $this->is_account=false;
89         $this->initially_was_account = false;
90       }
91     }
93    
94     /* Prepare different member types 
95      */ 
96     foreach(array("AddUser"       =>"gotoUserPrinter",
97                   "AddGroup"      =>"gotoGroupPrinter",
98                   "AddAdminUser"  =>"gotoUserAdminPrinter",
99                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
101       /* $this->members contains all members */
102       $this->member[$type]=array();
104       unset($this->attrs[$attr]['count']);
106       if(isset($this->attrs[$attr])){
107         foreach($this->attrs[$attr] as $mem){
108           if(preg_match("/Group/",$type)){
109             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
110             $entry = $ldap->fetch();
111             if(isset($entry['description'])){
112               $this->member[$type][$entry['cn'][0]]=$entry;
113             }
114           }else{
115             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
116             $entry = $ldap->fetch();
117             if(isset($entry['uid'])){
118               $this->member[$type][$entry['uid'][0]]=$entry;
119             }
120           }
121         }
122       }
123     }
124   }
126   function execute()
127   {
129     $smarty= get_smarty();
130     $display="";
131     /* Template management.
132      * There are two ways to call this tab.
133      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
134      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
135      *    Toggle Account state will be shown in this case, to disable or enable this tab.
136      *
137      * $this->is_terminal indecates this two different types.
138      */
139     if($this->is_terminal){    
140       $smarty->assign("is_terminal","true");
141     }else{
142       $smarty->assign("is_terminal","false");
143     }
145     /* Do we need to flip is_account state? */
146     if (isset($_POST['modify_state'])){
147       $this->is_modified = true;
148       $this->is_account= !$this->is_account;
149     }
151     if($this->is_terminal){
152       if(empty($this->cn))
153         $display= $this->show_header(_("Add printer extension"),
154               _("This workstation has printer extension disabled. You can't enable it while 'cn' is not present in entry. Possibly you are currently creating a new terminal template"),TRUE,TRUE);
155         $this->is_account= false;
156         return $display;
158        if ($this->is_account){
159         if($this->type=="station"){
160           $display= $this->show_header(_("Remove printer extension"),
161               _("This workstation has printer extension enabled.You can disable it by clicking below."));
162         }else{
163           $display= $this->show_header(_("Remove printer extension"),
164               _("This terminal has printer extension enabled. You can disable it by clicking below."));
165         }
166       }else{
167         if($this->type=="station"){
168           $display= $this->show_header(_("Add printer extension"),
169               _("This workstation has printer extension disabled. You can enable it by clicking below."));
170         }else{
171           $display= $this->show_header(_("Add printer extension"),
172               _("This terminal has printer extension disabled. You can enable it by clicking below."));
173         }  
174         return ($display);
175       }
176     }
179     /* Do we represent a valid printer? */
180     if (!$this->is_account && $this->parent == NULL){
181       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
182         _("This 'dn' has no printer features.")."</b>";
183       return($display);
184     }
186     /* Fill templating stuff */
187     $smarty->assign("bases", $this->config->idepartments);
188     $smarty->assign("base_select", $this->base);
190     /* Assign attributes */
191     foreach ($this->attributes as $attr){
192       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
193       $smarty->assign("$attr", $this->$attr);
194     }
195     
196     if(isset($_POST['AddUser'])){
197       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
198     }
199     if(isset($_POST['AddGroup'])){
200       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
201     }
202     if(isset($_POST['AddAdminUser'])){
203       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
204     }
205     if(isset($_POST['AddAdminGroup'])){
206       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
207     }
209     if(isset($_POST['EditDriver'])){
210       $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
211     }
213     if(isset($_POST['PrinterCancel'])){
214       unset($this->dialog);
215       $this->dialog= NULL;
216     }
218     if(isset($_POST['SavePPD'])){
219       if(count($this->dialog->check())){
220         foreach($this->dialog->check() as $msg){
221           print_red($msg);
222         }
223       }else{
224         $this->gotoPrinterPPD = array();
225         $this->gotoPrinterPPD = $this->dialog->save();
226         unset($this->dialog);
227         $this->dialog=NULL;
228       }
230     }
231     
232     if(isset($_POST['ClosePPD'])){
233       unset($this->dialog);
234       $this->dialog=NULL;
235     }
237     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
238       if(isset($this->member['AddUser'][$_POST['UserMember']])){
239         unset($this->member['AddUser'][$_POST['UserMember']]);
240       }
241     }
243     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
244       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
245         unset($this->member['AddGroup'][$_POST['UserMember']]);
246       }
247     }
249     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
250       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
251         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
252       }
253     }
255     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
256       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
257         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
258       }
259     }
261     if(isset($_POST['PrinterSave'])){
262       $this->dialog->save_object();
263       if(count($this->dialog->check())){
264         foreach($this->dialog->check() as $msg){
265           print_red($msg);
266         }
267       }else{
268         $new = $this->dialog->save();
269         $data = $new;
270         unset($data['type']);
272         if(preg_match("/User/",$new['type'])){
273           $use = "uid";
274         }else{
275           $use = "cn";
276         }
278         foreach($data as $mem){
279           $this->member[$new['type']][$mem[$use][0]]=$mem;    
280         }
281         unset($this->dialog);
282         $this->dialog=NULL; 
283       }
284     }
286     if($this->dialog != NULL){
287       $display = $this->dialog->execute();
288       return $display;
289     }
291     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
292     $smarty->assign("staticAddress", "");
295     require_once ("class_ppdManager.inc");
296     $ppdManager= new ppdManager('/var/spool/ppd/');
297     if(!empty($this->gotoPrinterPPD)){
298       $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
299     }else{
300       $smarty->assign("driverInfo", _("Undefined"));
301     }
303     $list=$this->generateList();
304     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
305     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
307     asort($userlist);
308     asort($adminlist);
310     $smarty->assign("UserMember"    ,$this->UserMember);
311     $smarty->assign("UserMembers"   ,$userlist);
312     $smarty->assign("UserMemberKeys",array_flip($userlist));
314     $smarty->assign("AdminMember"    ,$this->AdminMember);
315     $smarty->assign("AdminMembers"   ,$adminlist);
316     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
317     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
319     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
320   }
322   function remove_from_parent()
323   {
324     $ldap= $this->config->get_ldap_link();
325     $ldap->rmdir($this->dn);
326     show_ldap_error($ldap->get_error());
327     $this->handle_post_events("remove");
329     /* Delete references to object groups */
330     $ldap->cd ($this->config->current['BASE']);
331     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
332     while ($ldap->fetch()){
333       $og= new ogroup($this->config, $ldap->getDN());
334       unset($og->member[$this->dn]);
335       $og->save ();
336     }
337   }
339   /* Save data to object */
340   function save_object()
341   {
342     plugin::save_object();
344     /* Save base, since this is no LDAP attribute */
345     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
346       $this->base= $_POST['base'];
347     }
348   }
350   /* Check supplied data */
351   function check()
352   {
353     $message= array();
354     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
356     /* must: cn */
357     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
358       $message[]= "The required field 'Printer name' is not set.";
359     }
361     $ui= get_userinfo();
362     $acl= get_permissions ($this->dn, $ui->subtreeACL);
363     $acl= get_module_permission($acl, "printer", $this->dn);
364     if (chkacl($acl, "create") != ""){
365       $message[]= _("You have no permissions to create a printer on this 'Base'.");
366     }
368     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
369       $ldap= $this->config->get_ldap_link();
370       $ldap->cd ($this->base);
371       $ldap->search ("(cn=".$this->cn.")", array("cn"));
372       if ($ldap->count() != 0){
373         while ($attrs= $ldap->fetch()){
374           if ($attrs['dn'] != $this->orig_dn){
375             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
376             break;
377           }
378         }
379       }
380     }
382     return ($message);
383   }
386   /* Save to LDAP */
387   function save()
388   {
389     $dn= $this->dn;
390     plugin::save();
391     $ldap= $this->config->get_ldap_link();
392    
393     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
394       $this->dn= preg_replace("/ou=terminal/","ou=printer",$this->dn);
395     }
396     
397     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
398       $this->dn= preg_replace("/ou=workstation/","ou=printer",$this->dn);
399     }
401     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
402     */
403     if($this->is_terminal){
404       
405       /* reduce objectClasses to minimun */
406       $this->attrs['objectClass']= $this->objectclasses;
408       /* If a printer with the given dn exists, modify else create new one */
409       $ldap->cat($this->dn);
410       if($ldap->fetch()){
411         $this->orig_dn= $this->dn;
412       }else{
413         $this->orig_dn = "new";
414       }
415     }
417     /* Remove all empty values */
418     if ($this->orig_dn == 'new'){
419       $attrs= array();
420       foreach ($this->attrs as $key => $val){
421         if (is_array($val) && count($val) == 0){
422           continue;
423         }
424         $attrs[$key]= $val;
425       }
426       $this->attrs= $attrs;
427     }
430     /* Append printer user 
431      */
432     foreach($this->member['AddUser'] as $mem){
433       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
434     }
436     /* Append printer group 
437      */
438     foreach($this->member['AddGroup'] as $mem){
439       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
440     }
442     /* Append printer admin user 
443      */
444     foreach($this->member['AddAdminUser'] as $mem){
445       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
446     }
447     
448     /* Append printer admin group 
449      */
450     foreach($this->member['AddAdminGroup'] as $mem){
451       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
452     }
454     /* Write back to ldap */
455     $ldap= $this->config->get_ldap_link();
456     if ($this->orig_dn == 'new'){
457       $ldap->cd($this->config->current['BASE']);
458       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
459       $ldap->cd($this->dn);
460       $ldap->add($this->attrs);
461       $this->handle_post_events("add");
462     } else {
463       if ($this->orig_dn != $this->dn){
464         $this->move($this->orig_dn, $this->dn);
465       }
467       $ldap->cd($this->dn);
468       $ldap->modify($this->attrs);
469       $this->handle_post_events("modify");
470     }
471     show_ldap_error($ldap->get_error());
473     /* Optionally execute a command after we're done */
474     $this->postcreate();
475   }
477   function generateList(){
478     $a_return=array();
479     foreach($this->member as $type => $values){
480       $a_return[$type]=array();
481       foreach($values as $value){
482         if((preg_match("/Group/i",$type))){
483           if(!isset($value['description'])){
484             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
485           }else{
486             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
487           }
488         }else{
489           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
490         }
491       }
492     }
493     return($a_return);
494   }
499 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
500 ?>