Code

update
[gosa.git] / plugins / admin / systems / class_glpiPrinterAccount.inc
1 <?php
3 class glpiPrinterAccount extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server basic 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   /* attribute list for save action */
11   var $ignore_account= FALSE;
12   var $attributes= array("ID","name","contact","ramSize","flags_serial","flags_par","flags_usb",
13       "tech_num","comments","date_mod","location","domain","network","contact_num","serial","otherserial",
14       "type","is_template","FK_glpi_enterprise","deleted");
16   var $ID                 ;       // Is set if this entry is edited 
17   var $name               = "";    // This should be the dn of this entry 
18   var $FK_glpi_enterprise = 0;     // Manufacturer id
19   var $tech_num           = "";    // Technical responsible person
20   var $contact_num        = "";    // Contact person
21   
22   var $comments           = "";    // Comment
23   
24   var $type               = 0;     // System type id
25   var $serial             = "";   
26   var $otherserial        = "";  
27   var $ramSize            = 0;
28   var $flags_serial       = false;
29   var $flags_par          = false;
30   var $flags_usb          = false;
32   var $date_mod           = "";    // Modification timestamp
33   
34   var $location           = 0;     // Not used yet
35   var $domain             = 0;     // ? Set to 0
36   var $network            = 0;     // ? Set to 0 
38   var $is_template        = 0;     // Used as template ?
39   var $contact            = "";    // Empty
40   var $deleted            = "N";   // Deleted entries should have this set to Y
42   /* Not necessary, cause we use mysql databse */
43   var $objectclasses= array("whatever");
45   /* Used to remember if this was an account (simply: is this an edited entry) */
46   var $initially_was_account = false;
48   /* Remember current dialog */
49   var $edit_type            = false;
50   var $edit_os              = false;
52   var $data;
53   var $handle = NULL;               // Glpi class handle used to query database
55   var $cur_dialog = NULL;           // This contains the sub dialog handle
57   var $orig_dn;                     // To check if dn, has changed 
58   var $ui;                          // Some GOsa specific user informations 
59   
60   var $usedDevices      = array();  // Which devices are currently selected 
61   var $usedAttachments  = array();  // Used Attachments 
64   /* Contructor 
65      Sets default values and checks if we already have an existing glpi account
66    */
67   function glpiPrinterAccount ($config, $dn= NULL)
68   {
69     plugin::plugin ($config, $dn);
70     $this->ui= get_userinfo();
72     $this->is_account = false;
74     /* Abort class construction, if no db is defined */
75     if(!isset($this->config->data['SERVERS']['GLPI'])){
76       return;
77     }
79     // Get informations about databse connection
80     $this->data = $this->config->data['SERVERS']['GLPI'];
82     // Abort if mysql extension is missing 
83     if(!is_callable("mysql_connect")){
84       return;
85     }
87     // Create handle of glpi class, and check if database connection is established 
88     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
90     if(!$this->handle->is_connected){
91       return;
92     } 
94     // If this dn is already used in database, then get all informations for this entry 
95     if($this->handle->is_printer_account($this->dn)){
96       $this->is_account = true;
97       $tmp = ($this->handle->getPrinterInformations($this->dn));
99       foreach(array("tech_num","FK_glpi_enterprise","type","comments","contact_num","flags_serial","flags_par","flags_usb","ramSize") as $attr){
100         $this->$attr = $tmp[0][$attr];
101       }
102       
103       $atts = $this->handle->getAssignPrinterAttachments($tmp[0]['ID']);
104       foreach($atts as $attachment){
105         $this->usedAttachments[$attachment['FK_doc']]=$attachment['FK_doc']; 
106       }
107     }else{
108       $this->is_account = false;
109     }
111     /* set defaults */
112     $this->name                 = $this->dn;
113     $this->orig_dn              = $this->dn;
114     $this->initially_was_account = $this->is_account;
115   }
117   function execute()
118   {
119     /* Call parent execute */
120     plugin::execute();
122     /* Fill templating stuff */
123     $smarty= get_smarty();
124     $display= "";
126     /*  Assign smarty defaults 
127         To avoid undefined indexes, if there is an error with the glpi db
128      */ 
129     foreach(array("PrinterTypeKeys","PrinterTypes","ManufacturerKeys","Manufacturers","Attachments","AttachmentKeys") as $attr){
130       $smarty->assign($attr,array());
131       $smarty->assign($attr."ACL"," disabled ");
132     }
133     foreach(array("type","FK_glpi_enterprise","tech_num","contact_num","comments","flags_serial","flags_par","flags_usb") as $attr){
134       $smarty->assign($attr,"");
135       $smarty->assign($attr."ACL"," disabled ");
136     }
138     /* Check if there is a glpi database server defined 
139       */
140     if(!isset($this->config->data['SERVERS']['GLPI'])){
141       print_red(_("There is no server with valid glpi database service."));
142       return($smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE)));
143     }
145     $this->data = $this->config->data['SERVERS']['GLPI'];
147     /* Check if we can call mysql_connect 
148        If we can't, there is no the mysql-php extension
149      */
150     if(!is_callable("mysql_connect")){
151       print_red(_("Can't connect to glpi database, the php-mysql extension is missing."));
152       return($smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE)));
153     }
155     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
157     /*  If handle == false, abort
158         Seems that the server, username and or password is wrong
159      */
160     if(!$this->handle->is_connected){
161       print_red(_("Can't connect to glpi database, check configuration twice."));
162       return($smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE)));
163     } 
165     /*  All checks are ok
166         Lets handle Posts, templates etc below ...
167      */
168     
169     $users = $this->handle->getUsers();
170     $ldap= $this->config->get_ldap_link();
172     
173     /*  ##########################################################################
174      *  Some tab management   
175      */
177     /* Do we need to flip is_account state? */
178     if (isset($_POST['modify_state'])){
179       $this->is_account= !$this->is_account;
180     }
182     /* Show tab dialog headers */
183     if ($this->is_account){
184       $display= $this->show_header(_("Remove inventory service"),
185           _("This server has inventory features enabled. You can disable them by clicking below."));
186     } else {
187       $display= $this->show_header(_("Add inventory service"),
188           _("This server has inventory features disabled. You can enable them by clicking below."));
189       return ($display);
190     }
192     
193     /*  ##########################################################################
194      *  Printer type management 
195      *  Dialog 
196      */
198     /* Printer type management
199      */
200     if(isset($_POST['edit_type'])){
201       $this->dialog = true;
202       $this->edit_type=true;
203     }
205     /* This closes the printer type editing dialog
206      */
207     if(isset($_POST['close_edit_type'])){
208       $this->edit_type=false;
209       $this->dialog = false;
210     }
212     /* This appends a new printer to our sytem types
213      */
214     if((isset($_POST['add_type']))&&(!empty($_POST['type_string']))){
215       $this->handle->addPrinterType($_POST['type_string']);  
216     }
218     /* Remove selected type from our printer types list
219      */
220     if((isset($_POST['del_type']))&&(!empty($_POST['select_type']))){
221       $this->handle->removePrinterType($_POST['select_type']);  
222     }
224     /* Rename selected printer type to given string
225      */
226     if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))&&(!empty($_POST['type_string']))){
227       $this->handle->updatePrinterType($_POST['type_string'],$_POST['select_type']);
228     }
230     /* Someone wants to edit the printer types ... 
231        So, lets open a new dialog which provides some buttons to edit the types
232      */
233     if($this->edit_type){
234       $smarty->assign("PrinterTypes",            $this->handle->getPrinterTypes());
235       $smarty->assign("PrinterTypeKeys",         array_flip($this->handle->getPrinterTypes()));
236       $display= $smarty->fetch(get_template_path('glpi_edit_printer_type.tpl', TRUE));
237       return($display);
238     }
241     /*  ##########################################################################
242      *  Edit manufacturers 
243      *  Dialog 
244      */
246     /* Open dialog which allows to edit the manufacturers
247      */
248     if(isset($_POST['edit_manufacturer'])){
249       $this->cur_dialog = new glpiManufacturer($this->config,$this->dn);
250       $this->dialog = true;
251     }
253     /* Close manufacturer editing dialog
254      */
255     if(isset($_POST['close_edit_manufacturer'])){
256       $this->dialog = false;
257       $this->cur_dialog = false;
258     }
261     /*  ##########################################################################
262      *  Technical responsible person
263      *  Contact person 
264      *  Dialog 
265      */
267     /* Show dialog to select a new contact person
268      * Select a contact person
269      */
270     if(isset($_POST['SelectContactPerson'])){
271       $this->addUser = "contact";
272       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
273     }
275     /* Selecte technical responsible person
276      */
277     if(isset($_POST['SelectTechPerson'])){
278       $this->addUser ="tech";
279       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
280     }
282     /* Abort user selection
283      */
284     if(isset($_POST['SelectUserCancel'])){
285       $this->dialog = false;
286       $this->addUser ="";
287       $this->cur_dialog = false;
288     }
290     /* Technical responsible/contact person selected */
291     if(isset($_GET['act'])&&($_GET['act']=="user_tech_num")){
293       /* Get posted id */
294       $id = base64_decode($_GET['id']);
296       /* Check if user is already created in glpi database */
297       if(!in_array($id,$users)){
299         /* If this user doesn't exists in glpi db, we must create him */
300         $atr = $ldap->fetch($ldap->cat($id));
301         $tmp = array();
302         $use = array( "cn"              =>"name",
303             "mail"            =>"email",
304             "telephoneNumber" =>"phone");
306         /* Create array */
307         foreach($use as $gosa => $glpi){
308           if(isset($atr[$gosa])){
309             $tmp[$glpi]= $atr[$gosa][0];
310           }
311         }
313         /* Add this user */
314         $this->handle->addUser($tmp,$id);
315       }
317       /* Re-read users */
318       $users = ($this->handle->getUsers());
320       /* Get user */
321       $tmp = array_flip($users);
322       $id=$tmp[$id];
324       /* Use user id, close dialog */
325       if($this->addUser == "tech"){
326         $this->tech_num = $id;
327       }else{
328         $this->contact_num = $id;
329       }
330       $this->cur_dialog   = false;
331       $this->dialog= false;
332     }
335     /*  ##########################################################################
336      *  Handle attachments 
337      */
338     
339     /* Attachment pool was closed with use
340      */
341     if(isset($_POST['UseAttachment'])){
342       if(count($this->cur_dialog->check())){
343         foreach($this->cur_dialog->check() as $msg){
344           print_red($msg);
345         }
346       }else{
347         $this->cur_dialog->save_object();
348         $this->usedAttachments = $this->cur_dialog->save();
349         $this->cur_dialog = false;
350         $this->edit_type = false; 
351       }
352     }
353    
354     /* Attachment pool was closed with abort
355      */ 
356     if(isset($_POST['AbortAttachment'])){
357       $this->cur_dialog = false;
358       $this->edit_type = false; 
359     }
361     /* Open Attachment pool to add/edit Attachments
362      */
363     if(isset($_POST['AddAttachment'])){
364       $this->cur_dialog = new glpiAttachmentPool($this->config,$this->dn,$this->usedAttachments);
365       $this->dialog = true;
366     }
367     
368     /* Remove Attachment from this tab 
369      */
370     if((isset($_POST['RemoveAttachment']))&&(isset($_POST['Attachments']))){
371       if(isset($this->usedAttachments[$_POST['Attachments']])){
372         unset($this->usedAttachments[$_POST['Attachments']]);
373       }
374     }
376     /*  ##########################################################################
377      *  Draw Dialogs
378      */
379     /* if( cur_dialog != false || cur_dialog != NULL) 
380      * There is a dialog which wants to be displayed 
381      */
382     if($this->cur_dialog){
383       $this->cur_dialog->save_object();
384       $this->dialog=true;
385       $this->cur_dialog->parent = &$this;
386       return($this->cur_dialog->execute());
387     }else{
388       $this->dialog= false;
389     }
392     /*  ##########################################################################
393      *  Assign listbox / checkbox .... values to smarty  
394      */
395     /* Assign smarty defaults */ 
396     foreach(array("PrinterTypes","PrinterTypeKeys","Manufacturers","TechnicalResponsibles","Attachments") as $attr){
397       $smarty->assign($attr,array());
398       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
399     }
401     /* Assign some vars to smarty 
402      */
403     foreach(array("type","FK_glpi_enterprise","tech_num","contact_num","flags_serial","flags_par","flags_usb") as $attr){
404       $smarty->assign($attr,"");
405       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
406     }
409     /* Assign ACLs to smarty*/
410     foreach($this->attributes as $attr){
411       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
412     }
414     $smarty->assign("comments",               $this->comments);
415     $smarty->assign("flags_serial",            $this->flags_serial);
416     $smarty->assign("flags_par",               $this->flags_par);
417     $smarty->assign("flags_usb",               $this->flags_usb);
419     /* Assign system types 
420      */
421     $smarty->assign("PrinterTypes",           $this->handle->getPrinterTypes());
422     $smarty->assign("PrinterTypeKeys",        array_flip($this->handle->getPrinterTypes()));
423     $smarty->assign("type",                   $this->type);
425     /* Append manufacturers
426      */
427     $smarty->assign("ManufacturerKeys",       array_flip($this->handle->getEnterprises()));
428     $smarty->assign("Manufacturers",          $this->handle->getEnterprises());
429     $smarty->assign("FK_glpi_enterprise",     $this->FK_glpi_enterprise);
431     /* Assign used Attachments
432     */
433     $smarty->assign("Attachments",            $this->getUsedAttachments());
434     $smarty->assign("AttachmentKeys",         array_flip($this->getUsedAttachments()));
437     /*  ##########################################################################
438      *  Assign contact and technical responsible person 
439      */
440     if(isset($users[$this->contact_num])){
441       $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
442       $str = "";
443       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
444       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
445       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
446       $smarty->assign("contact_num",               $str);
447     }else{
448       $smarty->assign("contact_num",               _("N/A"));
449     }
451     /* Handle tech person 
452        Assign name ... to smarty, if set
453      */ 
454     if(isset($users[$this->tech_num])){
455       $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
456       $str = "";
457       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
458       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
459       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
460       $smarty->assign("tech_num",               $str);
461     }else{
462       $smarty->assign("tech_num",               _("N/A"));
463     }
465     $display.= $smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE));
466     return($display);
467   }
469   function remove_from_parent()
470   {
471     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
472     if($this->initially_was_account){
473       $this->handle->removePrinterInformations($this->dn); 
474     }
475   }
478   /* Save data to object */
479   function save_object()
480   {
481     plugin::save_object();
482     foreach($this->attributes as $attrs){
483       if(isset($_POST[$attrs])){
484         $this->$attrs = $_POST[$attrs];
485       }
486     }
488     foreach(array("flags_serial","flags_par","flags_usb") as $checkboxes){
489       if(isset($_POST[$checkboxes])){
490         $this->$checkboxes = 1;
491       }else{
492         $this->$checkboxes = 0;
493       }
494     }
496   }
499   /* Check supplied data */
500   function check()
501   {
502     $message= array();
504     //    if($this->TechnicalResponsible == ""){
505     //      $message[] = _("Please select a technical responsible person for this entry.");
506     //    }
508     return ($message);
509   }
511   /* Save to LDAP */
512   function save()
513   {
514     if($this->is_account){
515         $attrs = array();
516       $this->date_mod = date("Y-m-d H:i:s");
517       foreach($this->attributes as $attr){
518         $attrs[$attr] = $this->$attr;
519       }
520       $attrs['name'] = $this->dn;
521       unset($attrs['ID']);
522       $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
523       if($this->initially_was_account&&$this->is_account){
524         $this->handle->updatePrinterInformations($attrs,$this->dn);
525       }elseif($this->is_account){
526         $this->handle->addPrinterInformations($attrs,$this->dn);
527       }
528       $tmp = $this->handle->getPrinterInformations($this->dn);
529       $this->handle->addAttachmentsToPrinter($this->usedAttachments,$tmp[0]['ID']);
530     }
531   }
533   /* Return used attachments */
534   function getUsedAttachments()
535   {
536     $atts =$this->handle->getAttachments();
537     $ret = array();
538     foreach($atts as $entry){
539       if(in_array($entry['ID'],$this->usedAttachments)){
541         $cm ="";
542         if(isset($entry['comment'])){
543           $cm=" [".$entry['comment']."]";
544         }
545         if(isset($entry['mime'])){
546           $cm.=" -".$entry['mime']."";
547         }
549         $ret[$entry['ID']]= $entry['name'].$cm;
550       }
551     }
552     return($ret);
553   }
554   
558 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
559 ?>