Code

ed2263c21f9f229c18a7fb8a2d498720f8509fba
[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();
34   var $member           =array();
36   var $dialog           =NULL;
38   /* attribute list for save action */
39   var $attributes     = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","macAddress", "ipHostNumber");
40   var $objectclasses  = array("top", "gotoPrinter");
42   function printgeneric ($config, $dn= NULL)
43   {
44     plugin::plugin ($config, $dn);
45     $ldap= $this->config->get_ldap_link();
47     /* Set base */
48     if ($this->dn == "new"){
49       $ui= get_userinfo();
50       $this->base= dn2base($ui->dn);
51       $this->cn= "";
52     } else {
53       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
54     }
57     if(in_array("gotoTerminal",$this->attrs['objectClass'])){
58       $this->is_terminal = "true";
59       $this->dn   = preg_replace("/ou=terminal/","ou=printer",$this->dn);
61       // Reload plugin with new dn... (ou=printers instead of ou=terminals) 
62       plugin::plugin ($this->config, $this->dn);
63       $ldap->cat($this->dn);
64       if(count($ldap->fetch())>0){
65         $this->orig_dn= $this->dn;
66         $this->is_account=true;
67         $this->initially_was_account = true;
68       }else{
69         $this->orig_dn = "new";
70         $this->is_account=false;
71         $this->initially_was_account = false;
72       }
74     }else{
76       /* Save dn for later references */
77       $this->orig_dn= $this->dn;
78     }
79     
80     foreach(array("AddUser"       =>"gotoUserPrinter",
81                   "AddGroup"      =>"gotoGroupPrinter",
82                   "AddAdminUser"  =>"gotoUserAdminPrinter",
83                   "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
85       $this->member[$type]=array();
86       unset($this->attrs[$attr]['count']);
87       if(isset($this->attrs[$attr]))
88       foreach($this->attrs[$attr] as $mem){
89         if(preg_match("/Group/",$type)){
90           $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
91           $entry = $ldap->fetch();
92           if(isset($entry['description'])){
93             $this->member[$type][$entry['cn'][0]]=$entry;
94           }
95         }else{
96           $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
97           $entry = $ldap->fetch();
98           if(isset($entry['uid'])){
99             $this->member[$type][$entry['uid'][0]]=$entry;
100           }
101         }
102       }
103     }
104     
105   }
107   function execute()
108   {
109     $smarty= get_smarty();
110     $display="";
111     /* Template management.
112      * There are two ways to call this tab.
113      * 1. From a printer Dialog, here we will see the full template, without a toggle state button
114      * 2. As a terminal tab, here we hide the top (name,base,description) of the template. 
115      *    Toggle Account state will be shown in this case, to disable or enable this tab.
116      *
117      * $this->is_terminal indecates this two different types.
118      */
119     if($this->is_terminal){    
120       $smarty->assign("is_terminal","true");
121     }else{
122       $smarty->assign("is_terminal","false");
123     }
125     /* Do we need to flip is_account state? */
126     if (isset($_POST['modify_state'])){
127       $this->is_modified = true;
128       $this->is_account= !$this->is_account;
129     }
131     if($this->is_terminal){
132       if ($this->is_account){
133         $display= $this->show_header(_("Remove environment extension"),
134             _("This server has environment extension enabled. You can disable it by clicking below."));
135       }else{
136         $display= $this->show_header(_("Add environment extension"),
137             _("This server has environment extension disabled. You can enable it by clicking below."));
138         return ($display);
139       }
140     }
143     /* Do we represent a valid printer? */
144     if (!$this->is_account && $this->parent == NULL){
145       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
146         _("This 'dn' has no printer features.")."</b>";
147       return($display);
148     }
150     /* Fill templating stuff */
151     $smarty->assign("bases", $this->config->idepartments);
152     $smarty->assign("base_select", $this->base);
154     /* Assign attributes */
155     foreach ($this->attributes as $attr){
156       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
157       $smarty->assign("$attr", $this->$attr);
158     }
159     
160     if(isset($_POST['AddUser'])){
161       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
162     }
163     if(isset($_POST['AddGroup'])){
164       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
165     }
166     if(isset($_POST['AddAdminUser'])){
167       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
168     }
169     if(isset($_POST['AddAdminGroup'])){
170       $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
171     }
173     if(isset($_POST['PrinterCancel'])){
174       unset($this->dialog);
175       $this->dialog= NULL;
176     }
178     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
179       if(isset($this->member['AddUser'][$_POST['UserMember']])){
180         unset($this->member['AddUser'][$_POST['UserMember']]);
181       }
182     }
184     if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
185       if(isset($this->member['AddGroup'][$_POST['UserMember']])){
186         unset($this->member['AddGroup'][$_POST['UserMember']]);
187       }
188     }
190     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
191       if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
192         unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
193       }
194     }
196     if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
197       if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
198         unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
199       }
200     }
202     if(isset($_POST['PrinterSave'])){
203       $this->dialog->save_object();
204       if(count($this->dialog->check())){
205         foreach($this->dialog->check() as $msg){
206           print_red($msg);
207         }
208       }else{
209         $new = $this->dialog->save();
210         $data = $new;
211         unset($data['type']);
213         if(preg_match("/User/",$new['type'])){
214           $use = "uid";
215         }else{
216           $use = "cn";
217         }
219         foreach($data as $mem){
220           $this->member[$new['type']][$mem[$use][0]]=$mem;    
221         }
222         unset($this->dialog);
223         $this->dialog=NULL; 
224       }
225     }
227     if($this->dialog != NULL){
228       $display = $this->dialog->execute();
229       return $display;
230     }
232     /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
233     $smarty->assign("staticAddress", "");
234     $smarty->assign("driverInfo", "Not implemented yet");
236     $list=$this->generateList();
237     $userlist   = array_merge($list['AddUser'],$list['AddGroup']);
238     $adminlist  = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
240     $smarty->assign("UserMember"    ,$this->UserMember);
241     $smarty->assign("UserMembers"   ,$userlist);
242     $smarty->assign("UserMemberKeys",array_flip($userlist));
244     $smarty->assign("AdminMember"    ,$this->AdminMember);
245     $smarty->assign("AdminMembers"   ,$adminlist);
246     $smarty->assign("AdminMemberKeys",array_flip($adminlist));
247     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
250     return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
251   }
253   function remove_from_parent()
254   {
255     $ldap= $this->config->get_ldap_link();
256     $ldap->rmdir($this->dn);
257     show_ldap_error($ldap->get_error());
258     $this->handle_post_events("remove");
260     /* Delete references to object groups */
261     $ldap->cd ($this->config->current['BASE']);
262     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
263     while ($ldap->fetch()){
264       $og= new ogroup($this->config, $ldap->getDN());
265       unset($og->member[$this->dn]);
266       $og->save ();
267     }
268   }
270   /* Save data to object */
271   function save_object()
272   {
273     plugin::save_object();
275     /* Save base, since this is no LDAP attribute */
276     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
277       $this->base= $_POST['base'];
278     }
279   }
281   /* Check supplied data */
282   function check()
283   {
284     $message= array();
285     $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
287     /* must: cn */
288     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
289       $message[]= "The required field 'Printer name' is not set.";
290     }
292     $ui= get_userinfo();
293     $acl= get_permissions ($this->dn, $ui->subtreeACL);
294     $acl= get_module_permission($acl, "printer", $this->dn);
295     if (chkacl($acl, "create") != ""){
296       $message[]= _("You have no permissions to create a printer on this 'Base'.");
297     }
299     if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
300       $ldap= $this->config->get_ldap_link();
301       $ldap->cd ($this->base);
302       $ldap->search ("(cn=".$this->cn.")", array("cn"));
303       if ($ldap->count() != 0){
304         while ($attrs= $ldap->fetch()){
305           if ($attrs['dn'] != $this->orig_dn){
306             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
307             break;
308           }
309         }
310       }
311     }
313     return ($message);
314   }
317   /* Save to LDAP */
318   function save()
319   {
320     plugin::save();
321     
322     $ldap= $this->config->get_ldap_link();
323   
324     /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn 
325     */
326     if($this->is_terminal){
327       
328       /* Save in ou=printers instead of ou=terminals */
329       $this->dn   = preg_replace("/ou=terminal/","ou=printer",$this->dn);
331       /* reduce objectClasses to minimun */
332       $this->attrs['objectClass']= $this->objectclasses;
334       /* If a printer with the given dn exists, modify else create new one */
335       $ldap->cat($this->dn);
336       if($ldap->fetch()){
337         $this->orig_dn= $this->dn;
338       }else{
339         $this->orig_dn = "new";
340       }
341     }
343     /* Remove all empty values */
344     if ($this->orig_dn == 'new'){
345       $attrs= array();
346       foreach ($this->attrs as $key => $val){
347         if (is_array($val) && count($val) == 0){
348           continue;
349         }
350         $attrs[$key]= $val;
351       }
352       $this->attrs= $attrs;
353     }
356     /* Append printer user 
357      */
358     foreach($this->member['AddUser'] as $mem){
359       $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
360     }
362     /* Append printer group 
363      */
364     foreach($this->member['AddGroup'] as $mem){
365       $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
366     }
368     /* Append printer admin user 
369      */
370     foreach($this->member['AddAdminUser'] as $mem){
371       $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
372     }
373     
374     /* Append printer admin group 
375      */
376     foreach($this->member['AddAdminGroup'] as $mem){
377       $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
378     }
380     /* Write back to ldap */
381     $ldap= $this->config->get_ldap_link();
382     if ($this->orig_dn == 'new'){
383       $ldap->cd($this->config->current['BASE']);
384       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
385       $ldap->cd($this->dn);
386       $ldap->add($this->attrs);
387       $this->handle_post_events("add");
388     } else {
389       if ($this->orig_dn != $this->dn){
390         $this->move($this->orig_dn, $this->dn);
391       }
393       $ldap->cd($this->dn);
394       $ldap->modify($this->attrs);
395       $this->handle_post_events("modify");
396     }
397     show_ldap_error($ldap->get_error());
399     /* Optionally execute a command after we're done */
400     $this->postcreate();
401   }
403   function generateList(){
404     $a_return=array();
405     foreach($this->member as $type => $values){
406       $a_return[$type]=array();
407       foreach($values as $value){
408         if((preg_match("/Group/i",$type))){
409           if(!isset($value['description'])){
410             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
411           }else{
412             $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
413           }
414         }else{
415           $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
416         }
417       }
418     }
419     return($a_return);
420   }
427 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
428 ?>