Code

Call save_object was missing
[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();
34   var $is_terminalBased = false; 
35  
36   var $member           =array();
37   var $strings          = "";
38   var $type             = "";
39   var $dialog           =NULL;
41   /* attribute list for save action */
42   var $attributes     = array("cn", "description", "l", "labeledURI", "macAddress", "ipHostNumber","gotoPrinterPPD");
43   var $objectclasses  = array("top", "gotoPrinter");
45   function printgeneric ($config, $dn= NULL)
46   {
47     plugin::plugin ($config, $dn);
48     $ldap= $this->config->get_ldap_link();
50     /* Set base */
51     if ($this->dn == "new"){
52       $ui= get_userinfo();
53       $this->base= dn2base($ui->dn);
54       $this->cn= "";
55       $this->is_terminal = true;
56     } else {
57       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
58     }
61     /* In case of gotoWorkstation this tab is calles from workstation plugin
62      * in case of gotoTerminal it is called from a terminal tab
63      * else it is a standalone printer
64      */
65     if((isset($this->attrs['objectClass']))&&(in_array("gotoWorkstation",$this->attrs['objectClass']))){
66       /* Fix for new Workstations */
67       if(preg_match("/ou=incoming/",$this->dn)){    
68         $this->is_terminal = "true";   
69         $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
70         $this->type = "station";
71       }else{
72         $this->is_terminal = "true";   
73         $this->dn   = preg_replace("/ou=workstations/","ou=printers",$this->dn);
74         $this->type = "station";
75       }
76     }elseif((isset($this->attrs['objectClass']))&&(in_array("gotoTerminal",$this->attrs['objectClass']))){
77       if(preg_match("/ou=incoming/",$this->dn)){    
78         $this->is_terminal = "true";   
79         $this->dn   = preg_replace("/ou=incoming/","ou=printers",$this->dn);
80         $this->type = "terminal";
81       }else{
82         $this->type = "terminal";
83         $this->is_terminal = "true";
84         $this->dn   = preg_replace("/ou=terminal/","ou=printers",$this->dn);
85       }
86     }else{
87       /* Save dn for later references */
88       $this->orig_dn= $this->dn;
89     }
91     /* If it is no standalone printer 
92      */
93     if($this->is_terminal){
94       // Reload plugin with new dn... (ou=printers instead of ou=terminals)
95       plugin::plugin ($this->config, $this->dn);
96       $ldap->cat($this->dn);
97       if(count($ldap->fetch())>0){
98         $this->orig_dn= $this->dn;
99         $this->is_account=true;
100         $this->initially_was_account = true;
101       }else{
102         $this->orig_dn = "new";
103         $this->is_account=false;
104         $this->initially_was_account = false;
105       }
106     }
108     /* Prepare different member types 
109      */ 
110     foreach(array("AddUser"       =>"gotoUserPrinter",
111                   "AddGroup"      =>"gotoGroupPrinter",
112                   "AddAdminUser"  =>"gotoUserAdminPrinter",
113                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
115       /* $this->members contains all members */
116       $this->member[$type]=array();
118       unset($this->attrs[$attr]['count']);
120       if(isset($this->attrs[$attr])){
121         foreach($this->attrs[$attr] as $mem){
122           if(preg_match("/Group/",$type)){
123             $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
124             $entry = $ldap->fetch();
125             if(isset($entry['description'])){
126               $this->member[$type][$entry['cn'][0]]=$entry;
127             }
128           }else{
129             $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
130             $entry = $ldap->fetch();
131             if(isset($entry['uid'])){
132               $this->member[$type][$entry['uid'][0]]=$entry;
133             }
134           }
135         }
136       }
137     }
138   
139     $ldap->search("(&(cn=".$this->cn.")(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)))",array("cn","objectClass"));
140     if($attrs = $ldap->fetch()){
141       if(in_array("gotoTerminal",$attrs['objectClass'])){
142         $this->is_terminalBased = "Terminal";
143       }else{
144         $this->is_terminalBased = "Workstation";
145       }
146     }
148     /* A printer was only saved if the printer belongs to a workstation. 
149      * If there was a new single printer created, it was never saved.
150      * -This allows new printers to be saved.
151      */
152     if($this->dn == "new"){
153       $this->is_account = true;
154     }
155   }
157   function execute()
158   {
159     $smarty= get_smarty();
160     $display="";
161     /* Template management.
162      * There are two ways to call this tab.
163      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
164      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
165      *    Toggle Account state will be shown in this case, to disable or enable this tab.
166      *
167      * $this->is_terminal indecates this two different types.
168      */
169     if($this->is_terminal){    
170       $smarty->assign("is_terminal","true");
171     }else{
172       $smarty->assign("is_terminal","false");
173     }
175     /* Do we need to flip is_account state? */
176     if (isset($_POST['modify_state'])){
177       $this->is_modified = true;
178       $this->is_account= !$this->is_account;
179     }
181     if($this->is_terminal){
182       if(empty($this->cn)){
183         if(!in_array("gotoPrinter",$this->objectclasses)){
184           $display= $this->show_header(_("Add printer extension"),
185               _("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);
186           $this->is_account= false;
187           return $display;
188         }else{
189           $smarty->assign("is_terminal","false");
190           $skip = true;
191         }
192       }
194       if(!isset($skip)){
195         if (($this->is_account)){
196           if($this->type=="station"){
197             $display= $this->show_header(_("Remove printer extension"),
198                 _("This workstation has printer extension enabled.You can disable it by clicking below."));
199           }else{
200             $display= $this->show_header(_("Remove printer extension"),
201                 _("This terminal has printer extension enabled. You can disable it by clicking below."));
202           }
203         }else{
204           if($this->type=="station"){
205             $display= $this->show_header(_("Add printer extension"),
206                 _("This workstation has printer extension disabled. You can enable it by clicking below."));
207           }else{
208             $display= $this->show_header(_("Add printer extension"),
209                 _("This terminal has printer extension disabled. You can enable it by clicking below."));
210           }  
211           return ($display);
212         }
213       }
214     }
217     /* Do we represent a valid printer? */
218     if (!$this->is_account && $this->parent == NULL){
219       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
220         _("This 'dn' has no printer features.")."</b>";
221       return($display);
222     }
224     /* Fill templating stuff */
225     $smarty->assign("bases", $this->config->idepartments);
226     $smarty->assign("base_select", $this->base);
228     /* Assign attributes */
229     foreach ($this->attributes as $attr){
230       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
231       $smarty->assign("$attr", $this->$attr);
232     }
234     if(isset($_POST['AddUser'])){
235       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
236     }
237     if(isset($_POST['AddGroup'])){
238       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
239     }
240     if(isset($_POST['AddAdminUser'])){
241       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
242     }
243     if(isset($_POST['AddAdminGroup'])){
244       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
245     }
247     if(isset($_POST['EditDriver'])){
248       $this->dialog = new printerPPDDialog($this->config, $this->dn,$this->gotoPrinterPPD);
249     }
251     if(isset($_POST['PrinterCancel'])){
252       unset($this->dialog);
253       $this->dialog= NULL;
254     }
256     if(isset($_POST['SavePPD'])){
257       $this->dialog->save_object();
258       if(count($this->dialog->check())){
259         foreach($this->dialog->check() as $msg){
260           print_red($msg);
261         }
262       }else{
263         $this->gotoPrinterPPD = array();
264         
265         $this->gotoPrinterPPD = $this->dialog->save();
266         unset($this->dialog);
267         $this->dialog=NULL;
268       }
270     }
272     if(isset($_POST['ClosePPD'])){
273       unset($this->dialog);
274       $this->dialog=NULL;
275     }
277     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
278       if(isset($this->member['AddUser'][$_POST['UserMember']])){
279         unset($this->member['AddUser'][$_POST['UserMember']]);
280       }
281     }
283     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
284       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
285         unset($this->member['AddGroup'][$_POST['UserMember']]);
286       }
287     }
289     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
290       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
291         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
292       }
293     }
295     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
296       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
297         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
298       }
299     }
301     if(isset($_POST['PrinterSave'])){
302       $this->dialog->save_object();
303       if(count($this->dialog->check())){
304         foreach($this->dialog->check() as $msg){
305           print_red($msg);
306         }
307       }else{
308         $new = $this->dialog->save();
309         $data = $new;
310         unset($data['type']);
312         if(preg_match("/User/",$new['type'])){
313           $use = "uid";
314         }else{
315           $use = "cn";
316         }
318         foreach($data as $mem){
319           $this->member[$new['type']][$mem[$use][0]]=$mem;    
320         }
321         unset($this->dialog);
322         $this->dialog=NULL; 
323       }
324     }
326     if($this->dialog != NULL){
327       $this->dialog->save_object();
328       $display = $this->dialog->execute();
329       return $display;
330     }
332     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
333     $smarty->assign("staticAddress", "");
336     require_once ("class_ppdManager.inc");
337     $ppdManager= new ppdManager('/var/spool/ppd/');
338     if(!empty($this->gotoPrinterPPD)){
339       $smarty->assign("driverInfo", $ppdManager->loadDescription($this->gotoPrinterPPD));
340     }else{
341       $smarty->assign("driverInfo", _("Undefined"));
342     }
344     $list=$this->generateList();
345     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
346     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
348     asort($userlist);
349     asort($adminlist);
350     if($this->is_terminalBased){
351       if($this->is_terminalBased == "Terminal"){
352         $smarty->assign("desc"    ,sprintf(_("This printer belongs to terminal %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
353       }else{
354         $smarty->assign("desc"    ,sprintf(_("This printer belongs to workstation %s. You can't rename this printer."),"<b>".$this->cn."</b>"));
355       }
356       $smarty->assign("cnACL"    ," disabled ");
357     }else{
358       $smarty->assign("desc"    ,"");
359     }
360     $smarty->assign("UserMember"    ,$this->UserMember);
361     $smarty->assign("UserMembers"   ,$userlist);
362     $smarty->assign("UserMemberKeys",array_flip($userlist));
364     $smarty->assign("AdminMember"    ,$this->AdminMember);
365     $smarty->assign("AdminMembers"   ,$adminlist);
366     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
367     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
369     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
370   }
372   function remove_from_parent()
373   {
374     $this->dn= preg_replace('/ou=workstations,/', 'ou=printers,', $this->dn);
375     $ldap= $this->config->get_ldap_link();
376     $ldap->rmdir($this->dn);
377     show_ldap_error($ldap->get_error());
378     $this->handle_post_events("remove");
380     /* Delete references to object groups */
381     $ldap->cd ($this->config->current['BASE']);
382     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
383     while ($ldap->fetch()){
384       $og= new ogroup($this->config, $ldap->getDN());
385       unset($og->member[$this->dn]);
386       $og->save ();
387     }
388   }
390   /* Save data to object */
391   function save_object()
392   {
393     plugin::save_object();
395     /* Save base, since this is no LDAP attribute */
396     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
397       $this->base= $_POST['base'];
398     }
399   }
401   /* Check supplied data */
402   function check()
403   {
404     $message= array();
405     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
407     /* must: cn */
408     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
409       $message[]= "The required field 'Printer name' is not set.";
410     }
412     $ui= get_userinfo();
413     $acl= get_permissions ($this->dn, $ui->subtreeACL);
414     $acl= get_module_permission($acl, "printer", $this->dn);
415     if (chkacl($acl, "create") != ""){
416       $message[]= _("You have no permissions to create a printer on this 'Base'.");
417     }
419     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
420       $ldap= $this->config->get_ldap_link();
421       $ldap->cd ($this->base);
422       $ldap->search ("(cn=".$this->cn.")", array("cn"));
423       if ($ldap->count() != 0){
424         while ($attrs= $ldap->fetch()){
425           if ($attrs['dn'] != $this->orig_dn){
426             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
427             break;
428           }
429         }
430       }
431     }
433     return ($message);
434   }
437   /* Save to LDAP */
438   function save()
439   {
440     if (!$this->is_account){
441       return;
442     }
443     
444     $dn= $this->dn;
445     plugin::save();
446     $ldap= $this->config->get_ldap_link();
447    
448     if((in_array("gotoTerminal",$this->attrs['objectClass']))){
449       $this->dn= preg_replace("/ou=terminals,/","ou=printers,",$this->dn);
450     }
451     
452     if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
453       $this->dn= preg_replace("/ou=workstations,/","ou=printers,",$this->dn);
454     }
456     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
457     */
458     if($this->is_terminal){
459       
460       /* reduce objectClasses to minimun */
461       $this->attrs['objectClass']= $this->objectclasses;
463       /* If a printer with the given dn exists, modify else create new one */
464       $ldap->cat($this->dn);
465       if($ldap->fetch()){
466         $this->orig_dn= $this->dn;
467       }else{
468         $this->orig_dn = "new";
469       }
470     }
472     /* Remove all empty values */
473     if ($this->orig_dn == 'new'){
474       $attrs= array();
475       foreach ($this->attrs as $key => $val){
476         if (is_array($val) && count($val) == 0){
477           continue;
478         }
479         $attrs[$key]= $val;
480       }
481       $this->attrs= $attrs;
482     }
485     /* Append printer user 
486      */
487     foreach($this->member['AddUser'] as $mem){
488       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
489     }
491     /* Append printer group 
492      */
493     foreach($this->member['AddGroup'] as $mem){
494       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
495     }
497     /* Append printer admin user 
498      */
499     foreach($this->member['AddAdminUser'] as $mem){
500       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
501     }
502     
503     /* Append printer admin group 
504      */
505     foreach($this->member['AddAdminGroup'] as $mem){
506       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
507     }
509     /* Write back to ldap */
510     $ldap= $this->config->get_ldap_link();
511     if ($this->orig_dn == 'new'){
512       $ldap->cd($this->config->current['BASE']);
513       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
514       $ldap->cd($this->dn);
515       $ldap->add($this->attrs);
516       $this->handle_post_events("add");
517     } else {
518       if ($this->orig_dn != $this->dn){
519         $this->move($this->orig_dn, $this->dn);
520       }
522       $ldap->cd($this->dn);
523       $ldap->modify($this->attrs);
524       $this->handle_post_events("modify");
525     }
526     show_ldap_error($ldap->get_error());
528     /* Optionally execute a command after we're done */
529     $this->postcreate();
530   }
532   function generateList(){
533     $a_return=array();
534     foreach($this->member as $type => $values){
535       $a_return[$type]=array();
536       foreach($values as $value){
537         if((preg_match("/Group/i",$type))){
538           if(!isset($value['description'])){
539             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
540           }else{
541             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
542           }
543         }else{
544           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
545         }
546       }
547     }
548     return($a_return);
549   }
554 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
555 ?>