Code

Fixed headpage
[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;     // printer 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   var $editManufacturer   = false;
44   /* Not necessary, cause we use mysql databse */
45   var $objectclasses= array("whatever");
47   /* Used to remember if this was an account (simply: is this an edited entry) */
48   var $initially_was_account = false;
50   /* Remember current dialog */
51   var $edit_type            = false;
52   var $edit_os              = false;
54   var $data;
55   var $handle = NULL;               // Glpi class handle used to query database
57   var $cur_dialog = NULL;           // This contains the sub dialog handle
59   var $orig_dn;                     // To check if dn, has changed 
60   var $ui;                          // Some GOsa specific user informations 
61   
62   var $usedDevices      = array();  // Which devices are currently selected 
63   var $usedAttachments  = array();  // Used Attachments 
64   var $usedCartridges   = array();  // Used Cartridges
66   /* Contructor 
67      Sets default values and checks if we already have an existing glpi account
68    */
69   function glpiPrinterAccount ($config, $dn= NULL)
70   {
71     plugin::plugin ($config, $dn);
72     $this->ui= get_userinfo();
74     $this->is_account = false;
76     /* Abort class construction, if no db is defined */
77     if(!isset($this->config->data['SERVERS']['GLPI'])){
78       return;
79     }
81     // Get informations about databse connection
82     $this->data = $this->config->data['SERVERS']['GLPI'];
84     // Abort if mysql extension is missing 
85     if(!is_callable("mysql_connect")){
86       return;
87     }
89     // Create handle of glpi class, and check if database connection is established 
90     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
92     if(!$this->handle->is_connected){
93       return;
94     } 
96     // If this dn is already used in database, then get all informations for this entry 
97     if($this->handle->is_printer_account($this->dn)){
98       $this->is_account = true;
99       $tmp = ($this->handle->getPrinterInformations($this->dn));
101       foreach(array("tech_num","FK_glpi_enterprise","type","comments","contact_num","flags_serial","flags_par","flags_usb","ramSize") as $attr){
102         $this->$attr = $tmp[0][$attr];
103       }
104       
105       $atts = $this->handle->getAssignPrinterAttachments($tmp[0]['ID']);
106       foreach($atts as $attachment){
107         $this->usedAttachments[$attachment['FK_doc']]=$attachment['FK_doc']; 
108       }
110       $cart= $this->handle->getUsedCartridges($tmp[0]['ID']);
111       foreach($cart as $key => $cartridge){
112         $this->usedCartridges[$key]=$cartridge; 
113         $this->usedCartridges[$key]['status']="exists"; 
114       }
117     }else{
118       $this->is_account = false;
119     }
121     /* set defaults */
122     $this->name                 = $this->dn;
123     $this->orig_dn              = $this->dn;
124     $this->initially_was_account = $this->is_account;
125   }
127   function execute()
128   {
129     /* Call parent execute */
130     plugin::execute();
132     /* Fill templating stuff */
133     $smarty= get_smarty();
134     $display= "";
136     $smarty->assign("CartridgesACL",chkacl($this->acl,"Cartridges"));
138     /*  Assign smarty defaults 
139         To avoid undefined indexes, if there is an error with the glpi db
140      */ 
141     foreach(array("PrinterTypeKeys","PrinterTypes","ManufacturerKeys","Manufacturers","Attachments","AttachmentKeys") as $attr){
142       $smarty->assign($attr,array());
143       $smarty->assign($attr."ACL"," disabled ");
144     }
145     foreach(array("type","FK_glpi_enterprise","tech_num","contact_num","comments","flags_serial","flags_par","flags_usb") as $attr){
146       $smarty->assign($attr,"");
147       $smarty->assign($attr."ACL"," disabled ");
148     }
150     /* Check if there is a glpi database server defined 
151       */
152     if(!isset($this->config->data['SERVERS']['GLPI'])){
153       print_red(_("There is no server with valid glpi database service."));
154       return($smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE)));
155     }
157     $this->data = $this->config->data['SERVERS']['GLPI'];
159     /* Check if we can call mysql_connect 
160        If we can't, there is no the mysql-php extension
161      */
162     if(!is_callable("mysql_connect")){
163       print_red(_("Can't connect to glpi database, the php-mysql extension is missing."));
164       return($smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE)));
165     }
167     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
169     /*  If handle == false, abort
170         Seems that the server, username and or password is wrong
171      */
172     if(!$this->handle->is_connected){
173       print_red(_("Can't connect to glpi database, check configuration twice."));
174       return($smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE)));
175     } 
177     /*  All checks are ok
178         Lets handle Posts, templates etc below ...
179      */
180     
181     $users = $this->handle->getUsers();
182     $ldap= $this->config->get_ldap_link();
184     
185     /*  ##########################################################################
186      *  Some tab management   
187      */
189     /* Do we need to flip is_account state? */
190     if (isset($_POST['modify_state'])){
191       $this->is_account= !$this->is_account;
192     }
194     /* Show tab dialog headers */
195     if ($this->is_account){
196       $display= $this->show_header(_("Remove inventory service"),
197           _("This server has inventory features enabled. You can disable them by clicking below."));
198     } else {
199       $display= $this->show_header(_("Add inventory service"),
200           _("This server has inventory features disabled. You can enable them by clicking below."));
201       return ($display);
202     }
204     
205     /*  ##########################################################################
206      *  Printer type management 
207      *  Dialog 
208      */
210     /* Printer type management
211      */
212     if(isset($_POST['edit_type'])){
213       $this->dialog = true;
214       $this->edit_type=true;
215     }
217     /* This closes the printer type editing dialog
218      */
219     if(isset($_POST['close_edit_type'])){
220       $this->edit_type=false;
221       $this->dialog = false;
222     }
224     /* This appends a new printer to our sytem types
225      */
226     if((isset($_POST['add_type']))&&(!empty($_POST['type_string']))){
227       $this->handle->addPrinterType($_POST['type_string']);  
228     }
230     /* Remove selected type from our printer types list
231      */
232     if((isset($_POST['del_type']))&&(!empty($_POST['select_type']))){
233       $this->handle->removePrinterType($_POST['select_type']);  
234     }
236     /* Rename selected printer type to given string
237      */
238     if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))&&(!empty($_POST['type_string']))){
239       $this->handle->updatePrinterType($_POST['type_string'],$_POST['select_type']);
240     }
242     /* Someone wants to edit the printer types ... 
243        So, lets open a new dialog which provides some buttons to edit the types
244      */
245     if($this->edit_type){
246       $smarty->assign("PrinterTypes",            $this->handle->getPrinterTypes());
247       $smarty->assign("PrinterTypeKeys",         array_flip($this->handle->getPrinterTypes()));
248       $display= $smarty->fetch(get_template_path('glpi_edit_printer_type.tpl', TRUE));
249       return($display);
250     }
253     /*  ##########################################################################
254      *  Edit manufacturers 
255      *  Dialog 
256      */
258     /* Open dialog which allows to edit the manufacturers
259      */
260     if(isset($_POST['edit_manufacturer'])){
261       $this->cur_dialog = new glpiManufacturer($this->config,$this->dn);
262       $this->dialog = true;
263       $this->editManufacturer =true;
264     }
266     /* Close manufacturer editing dialog
267      */
268     if((isset($_POST['close_edit_manufacturer']))&&($this->editManufacturer)){
269       $this->dialog = false;
270       $this->cur_dialog = false;
271       $this->editManufacturer=false;
272     }
275     /*  ##########################################################################
276      *  Technical responsible person
277      *  Contact person 
278      *  Dialog 
279      */
281     /* Show dialog to select a new contact person
282      * Select a contact person
283      */
284     if(isset($_POST['SelectContactPerson'])){
285       $this->addUser = "contact";
286       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_tech_num");
287     }
289     /* Selecte technical responsible person
290      */
291     if(isset($_POST['SelectTechPerson'])){
292       $this->addUser ="tech";
293       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_tech_num");
294     }
296     /* Abort user selection
297      */
298     $smarty->assign("AbortSelectUser","SelectUserCancel");
299     if(isset($_POST['SelectUserCancel'])){
300       $this->dialog = false;
301       $this->addUser ="";
302       $this->cur_dialog = false;
303     }
305     /* Technical responsible/contact person selected */
306     if(isset($_GET['act'])&&($_GET['act']=="user_tech_num")){
308       /* Get posted id */
309       $id = base64_decode($_GET['id']);
311       /* Check if user is already created in glpi database */
312       if(!in_array($id,$users)){
314         /* If this user doesn't exists in glpi db, we must create him */
315         $atr = $ldap->fetch($ldap->cat($id));
316         $tmp = array();
317         $use = array( "cn"              =>"name",
318             "mail"            =>"email",
319             "telephoneNumber" =>"phone");
321         /* Create array */
322         foreach($use as $gosa => $glpi){
323           if(isset($atr[$gosa])){
324             $tmp[$glpi]= $atr[$gosa][0];
325           }
326         }
328         /* Add this user */
329         $this->handle->addUser($tmp,$id);
330       }
332       /* Re-read users */
333       $users = ($this->handle->getUsers());
335       /* Get user */
336       $tmp = array_flip($users);
337       $id=$tmp[$id];
339       /* Use user id, close dialog */
340       if($this->addUser == "tech"){
341         $this->tech_num = $id;
342       }else{
343         $this->contact_num = $id;
344       }
345       $this->cur_dialog   = false;
346       $this->dialog= false;
347     }
350     /*  ##########################################################################
351      *  Handle attachments 
352      */
353     
354     /* Attachment pool was closed with use
355      */
356     if(isset($_POST['UseAttachment'])){
357       if(count($this->cur_dialog->check())){
358         foreach($this->cur_dialog->check() as $msg){
359           print_red($msg);
360         }
361       }else{
362         $this->cur_dialog->save_object();
363         $this->usedAttachments = $this->cur_dialog->save();
364         $this->cur_dialog = false;
365         $this->edit_type = false; 
366       }
367     }
368    
369     /* Attachment pool was closed with abort
370      */ 
371     if(isset($_POST['AbortAttachment'])){
372       $this->cur_dialog = false;
373       $this->edit_type = false; 
374     }
376     /* Open Attachment pool to add/edit Attachments
377      */
378     if(isset($_POST['AddAttachment'])){
379       $this->cur_dialog = new glpiAttachmentPool($this->config,$this->dn,$this->usedAttachments);
380       $this->dialog = true;
381     }
382     
383     /* Remove Attachment from this tab 
384      */
385     if((isset($_POST['RemoveAttachment']))&&(isset($_POST['Attachments']))){
386       if(isset($this->usedAttachments[$_POST['Attachments']])){
387         unset($this->usedAttachments[$_POST['Attachments']]);
388       }
389     }
391     /*  ##########################################################################
392      *  Printer Cartridge handling 
393      */
395     /* Abort cartridge select dialog
396      */
397     if(isset($_POST['SelectCartridgeCancel'])){
398       $this->cur_dialog = false;
399       $this->edit_type = false; 
400     }
402     /* Get selected cartridges and add them to our list 
403      */ 
404     if(isset($_POST['SelectCartridgeSave'])){
405       $this->cur_dialog->save_object();
406       $carts = $this->cur_dialog->save();
407       foreach($carts as $cart){
408         $cart['status'] = "new";
409         $this->usedCartridges[] = $cart;
410       }
411       $this->cur_dialog = false;
412       $this->edit_type = false; 
413     }
415     /* Remove cartridge  
416      */
417     if((isset($_POST['RemoveCartridge']))&&(isset($_POST['Cartridges']))){
419       foreach($_POST['Cartridges'] as $cartID){
421         if(isset($this->usedCartridges[$cartID])){
422           if($this->usedCartridges[$cartID]['status'] == "exists"){
423             $this->usedCartridges[$cartID]['status'] = "deleted";
424           }else{
425             unset($this->usedCartridges[$cartID]);
426           }
427         }
428       }
429     }
431     /* Open Attachment pool to add/edit Attachments
432      */
433     if(isset($_POST['AddCartridge'])){
434       $this->cur_dialog = new glpiPrinterCartridges($this->config,$this->dn,$this->type);
435       $this->dialog = true;
436     }
439     /*  ##########################################################################
440      *  Draw Dialogs
441      */
442     /* if( cur_dialog != false || cur_dialog != NULL) 
443      * There is a dialog which wants to be displayed 
444      */
445     if($this->cur_dialog){
446       $this->cur_dialog->save_object();
447       $this->dialog=true;
448       $this->cur_dialog->parent = &$this;
449       return($this->cur_dialog->execute());
450     }else{
451       $this->dialog= false;
452     }
455     /*  ##########################################################################
456      *  Assign listbox / checkbox .... values to smarty  
457      */
458     /* Assign smarty defaults */ 
459     foreach(array("PrinterTypes","PrinterTypeKeys","Manufacturers","TechnicalResponsibles","Attachments") as $attr){
460       $smarty->assign($attr,array());
461       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
462     }
464     /* Assign some vars to smarty 
465      */
466     foreach(array("type","FK_glpi_enterprise","tech_num","contact_num","flags_serial","flags_par","flags_usb") as $attr){
467       $smarty->assign($attr,"");
468       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
469     }
472     /* Assign ACLs to smarty*/
473     foreach($this->attributes as $attr){
474       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
475     }
477     $smarty->assign("comments",               $this->comments);
478     $smarty->assign("flags_serial",            $this->flags_serial);
479     $smarty->assign("flags_par",               $this->flags_par);
480     $smarty->assign("flags_usb",               $this->flags_usb);
482     /* Assign system types 
483      */
484     $smarty->assign("PrinterTypes",           $this->handle->getPrinterTypes());
485     $smarty->assign("PrinterTypeKeys",        array_flip($this->handle->getPrinterTypes()));
486     $smarty->assign("type",                   $this->type);
488     /* Append manufacturers
489      */
490     $smarty->assign("ManufacturerKeys",       array_flip($this->handle->getEnterprises()));
491     $smarty->assign("Manufacturers",          $this->handle->getEnterprises());
492     $smarty->assign("FK_glpi_enterprise",     $this->FK_glpi_enterprise);
494     /* Assign used Attachments
495     */
496     $smarty->assign("Attachments",            $this->getUsedAttachments());
497     $smarty->assign("AttachmentKeys",         array_flip($this->getUsedAttachments()));
499     /* Assign Cartridges 
500      */
501     $smarty->assign("Cartridges",            $this->getUsedCartridges());
502     $smarty->assign("CartridgeKeys",         $this->getUsedCartridges(true));
504     /*  ##########################################################################
505      *  Assign contact and technical responsible person 
506      */
507     if(isset($users[$this->contact_num])){
508       $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
509       $str = "";
510       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
511       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
512       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
513       $smarty->assign("contact_num",               $str);
514     }else{
515       $smarty->assign("contact_num",               _("N/A"));
516     }
518     /* Handle tech person 
519        Assign name ... to smarty, if set
520      */ 
521     if(isset($users[$this->tech_num])){
522       $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
523       $str = "";
524       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
525       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
526       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
527       $smarty->assign("tech_num",               $str);
528     }else{
529       $smarty->assign("tech_num",               _("N/A"));
530     }
532     /* If theres a cartridge selected, you can't change the printer type.
533      */ 
534     $disp = true;
536     foreach($this->usedCartridges as $cart){
537       if($cart['status'] != "deleted"){
538         $disp = false;
539       }
540     }
541     if($disp==false){
542       $smarty->assign("typeACL","disabled");
543     }
545     $display.= $smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE));
546     return($display);
547   }
549   function remove_from_parent()
550   {
551     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
552     if($this->initially_was_account){
553       $this->handle->removePrinterInformations($this->dn); 
554     }
555   }
558   /* Save data to object */
559   function save_object()
560   {
561     if(isset($_POST['glpiPrinterFlagsPosted'])){
562       plugin::save_object();
563       foreach($this->attributes as $attrs){
564         if(isset($_POST[$attrs])){
565           $this->$attrs = $_POST[$attrs];
566         }
567       }
569       foreach(array("flags_serial","flags_par","flags_usb") as $checkboxes){
570         if(isset($_POST[$checkboxes])){
571           $this->$checkboxes = 1;
572         }else{
573           $this->$checkboxes = 0;
574         }
575       }
576     }
578   }
581   /* Check supplied data */
582   function check()
583   {
584     $message= array();
586     //    if($this->TechnicalResponsible == ""){
587     //      $message[] = _("Please select a technical responsible person for this entry.");
588     //    }
590     return ($message);
591   }
593   /* Save to LDAP */
594   function save()
595   {
596     if($this->is_account){
597         $attrs = array();
598       $this->date_mod = date("Y-m-d H:i:s");
599       foreach($this->attributes as $attr){
600         $attrs[$attr] = $this->$attr;
601       }
602       $attrs['name'] = $this->dn;
603       unset($attrs['ID']);
604       $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
605       if($this->initially_was_account&&$this->is_account){
606         $this->handle->updatePrinterInformations($attrs,$this->dn);
607       }elseif($this->is_account){
608         $this->handle->addPrinterInformations($attrs,$this->dn);
609       }
610       $tmp = $this->handle->getPrinterInformations($this->dn);
611       $this->handle->addAttachmentsToPrinter($this->usedAttachments,$tmp[0]['ID']);
613       foreach($this->usedCartridges as $cart){
614         if($cart['status'] == "deleted"){
615           $this->handle->removeCartridgeFromPrinter($cart['ID']);
616         }elseif($cart['status'] == "new"){
617           $this->handle->addCartridgeFromPrinter($tmp[0]['ID'],$cart['type_ID']);
618         }
619       }
620     }
621   }
623   /* Return used attachments */
624   function getUsedAttachments()
625   {
626     $atts =$this->handle->getAttachments();
627     $ret = array();
628     foreach($atts as $entry){
629       if(in_array($entry['ID'],$this->usedAttachments)){
631         $cm ="";
632         if(isset($entry['comment'])){
633           $cm=" [".$entry['comment']."]";
634         }
635         if(isset($entry['mime'])){
636           $cm.=" -".$entry['mime']."";
637         }
639         $ret[$entry['ID']]= $entry['name'].$cm;
640       }
641     }
642     return($ret);
643   }
645   function getUsedCartridges($flip = false)
646   {
647     $ret = array();
648     foreach($this->usedCartridges as $key => $value){
649       if($value['status'] == "deleted") continue;
650       if($flip){
651         $ret[$key] = $key;    
652       }else{
653         $ret[$key] = $value['name']." [".$value['type_name']."] "._("since")." :".$value['date_use'];
654       }
655     }
656     return($ret);
657   }  
661 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
662 ?>