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
22 var $comments = ""; // Comment
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
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
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 }
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 */
181 $users = $this->handle->getUsers();
182 $ldap= $this->config->get_ldap_link();
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 }
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 */
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 }
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 }
383 /* Remove Attachment from this tab
384 */
385 $once = true;
386 foreach($_POST as $name => $value){
387 if((preg_match("/^delAttachment_/",$name))&&($once)){
388 $once= false;
389 $name = preg_replace("/^delAttachment_/","",$name);
390 $entry = preg_replace("/_.*$/","",$name);
391 if(isset($this->usedAttachments[$entry])){
392 unset($this->usedAttachments[$entry]);
393 }
394 }
395 }
396 if((isset($_POST['RemoveAttachment']))&&(isset($_POST['Attachments']))){
397 if(isset($this->usedAttachments[$_POST['Attachments']])){
398 unset($this->usedAttachments[$_POST['Attachments']]);
399 }
400 }
402 /* ##########################################################################
403 * Printer Cartridge handling
404 */
406 /* Abort cartridge select dialog
407 */
408 if(isset($_POST['SelectCartridgeCancel'])){
409 $this->cur_dialog = false;
410 $this->edit_type = false;
411 }
413 /* Get selected cartridges and add them to our list
414 */
415 if(isset($_POST['SelectCartridgeSave'])){
416 $this->cur_dialog->save_object();
417 $carts = $this->cur_dialog->save();
418 foreach($carts as $cart){
419 $cart['status'] = "new";
420 $this->usedCartridges[] = $cart;
421 }
422 $this->cur_dialog = false;
423 $this->edit_type = false;
424 }
426 /* Remove cartridge
427 */
428 if((isset($_POST['RemoveCartridge']))&&(isset($_POST['Cartridges']))){
430 foreach($_POST['Cartridges'] as $cartID){
432 if(isset($this->usedCartridges[$cartID])){
433 if($this->usedCartridges[$cartID]['status'] == "exists"){
434 $this->usedCartridges[$cartID]['status'] = "deleted";
435 }else{
436 unset($this->usedCartridges[$cartID]);
437 }
438 }
439 }
440 }
442 /* Open Attachment pool to add/edit Attachments
443 */
444 if(isset($_POST['AddCartridge'])){
445 $this->cur_dialog = new glpiPrinterCartridges($this->config,$this->dn,$this->type);
446 $this->dialog = true;
447 }
450 /* ##########################################################################
451 * Draw Dialogs
452 */
453 /* if( cur_dialog != false || cur_dialog != NULL)
454 * There is a dialog which wants to be displayed
455 */
456 if($this->cur_dialog){
457 $this->cur_dialog->save_object();
458 $this->dialog=true;
459 $this->cur_dialog->parent = &$this;
460 return($this->cur_dialog->execute());
461 }else{
462 $this->dialog= false;
463 }
466 /* ##########################################################################
467 * Assign listbox / checkbox .... values to smarty
468 */
469 /* Assign smarty defaults */
470 foreach(array("PrinterTypes","PrinterTypeKeys","Manufacturers","TechnicalResponsibles","Attachments") as $attr){
471 $smarty->assign($attr,array());
472 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
473 }
475 /* Assign some vars to smarty
476 */
477 foreach(array("type","FK_glpi_enterprise","tech_num","contact_num","flags_serial","flags_par","flags_usb") as $attr){
478 $smarty->assign($attr,"");
479 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
480 }
483 /* Assign ACLs to smarty*/
484 foreach($this->attributes as $attr){
485 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
486 }
488 $smarty->assign("comments", $this->comments);
489 $smarty->assign("flags_serial", $this->flags_serial);
490 $smarty->assign("flags_par", $this->flags_par);
491 $smarty->assign("flags_usb", $this->flags_usb);
493 /* Assign system types
494 */
495 $smarty->assign("PrinterTypes", $this->handle->getPrinterTypes());
496 $smarty->assign("PrinterTypeKeys", array_flip($this->handle->getPrinterTypes()));
497 $smarty->assign("type", $this->type);
499 /* Append manufacturers
500 */
501 $smarty->assign("ManufacturerKeys", array_flip($this->handle->getEnterprises()));
502 $smarty->assign("Manufacturers", $this->handle->getEnterprises());
503 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
505 /* Assign used Attachments
506 */
508 $divlist = new divSelectBox("glpiAttachmentsList");
509 $divlist-> SetHeight(120);
510 $atts = $this->getUsedAttachments(true);
511 $downlink = "<a href='get_attachment.php?id=%s' target='_blank'>%s</a>";
512 $del_link = "<input type='image' src='images/edittrash.png' name='delAttachment_%s'>";
513 foreach($atts as $id => $attachment){
514 $divlist->AddEntry
515 (
516 array(
517 array("string"=>$attachment['name']),
518 array("string"=>$attachment['mime']),
519 array("string"=>sprintf($downlink,$id,$attachment['filename'])),
520 array("string"=>sprintf($del_link,$attachment['ID']),"attach"=>"style='border-right:0px;'"),
521 )
522 );
523 }
525 $smarty->assign("AttachmentsDiv" ,$divlist->DrawList());
526 $smarty->assign("Attachments", $this->getUsedAttachments());
527 $smarty->assign("AttachmentKeys", array_flip($this->getUsedAttachments()));
529 /* Assign Cartridges
530 */
531 $smarty->assign("Cartridges", $this->getUsedCartridges());
532 $smarty->assign("CartridgeKeys", $this->getUsedCartridges(true));
534 /* ##########################################################################
535 * Assign contact and technical responsible person
536 */
537 if(isset($users[$this->contact_num])){
538 $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
539 $str = "";
540 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
541 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
542 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
543 $smarty->assign("contact_num", $str);
544 }else{
545 $smarty->assign("contact_num", _("N/A"));
546 }
548 /* Handle tech person
549 Assign name ... to smarty, if set
550 */
551 if(isset($users[$this->tech_num])){
552 $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
553 $str = "";
554 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
555 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
556 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
557 $smarty->assign("tech_num", $str);
558 }else{
559 $smarty->assign("tech_num", _("N/A"));
560 }
562 /* If theres a cartridge selected, you can't change the printer type.
563 */
564 $disp = true;
566 foreach($this->usedCartridges as $cart){
567 if($cart['status'] != "deleted"){
568 $disp = false;
569 }
570 }
571 if($disp==false){
572 $smarty->assign("typeACL","disabled");
573 }
575 $display.= $smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE));
576 return($display);
577 }
579 function remove_from_parent()
580 {
581 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
582 if($this->initially_was_account){
583 $this->handle->removePrinterInformations($this->dn);
584 }
585 }
588 /* Save data to object */
589 function save_object()
590 {
591 if(isset($_POST['glpiPrinterFlagsPosted'])){
592 plugin::save_object();
593 foreach($this->attributes as $attrs){
594 if(isset($_POST[$attrs])){
595 $this->$attrs = $_POST[$attrs];
596 }
597 }
599 foreach(array("flags_serial","flags_par","flags_usb") as $checkboxes){
600 if(isset($_POST[$checkboxes])){
601 $this->$checkboxes = 1;
602 }else{
603 $this->$checkboxes = 0;
604 }
605 }
606 }
608 }
611 /* Check supplied data */
612 function check()
613 {
614 $message= array();
616 // if($this->TechnicalResponsible == ""){
617 // $message[] = _("Please select a technical responsible person for this entry.");
618 // }
620 return ($message);
621 }
623 /* Save to LDAP */
624 function save()
625 {
626 if($this->is_account){
627 $attrs = array();
628 $this->date_mod = date("Y-m-d H:i:s");
629 foreach($this->attributes as $attr){
630 $attrs[$attr] = $this->$attr;
631 }
632 $attrs['name'] = $this->dn;
633 unset($attrs['ID']);
634 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
635 if($this->initially_was_account&&$this->is_account){
636 $this->handle->updatePrinterInformations($attrs,$this->dn);
637 }elseif($this->is_account){
638 $this->handle->addPrinterInformations($attrs,$this->dn);
639 }
640 $tmp = $this->handle->getPrinterInformations($this->dn);
641 $this->handle->addAttachmentsToPrinter($this->usedAttachments,$tmp[0]['ID']);
643 foreach($this->usedCartridges as $cart){
644 if($cart['status'] == "deleted"){
645 $this->handle->removeCartridgeFromPrinter($cart['ID']);
646 }elseif($cart['status'] == "new"){
647 $this->handle->addCartridgeFromPrinter($tmp[0]['ID'],$cart['type_ID']);
648 }
649 }
650 }
651 }
653 /* Return used attachments */
654 function getUsedAttachments($divlist = false)
655 {
656 $atts =$this->handle->getAttachments();
657 $ret = array();
658 foreach($atts as $entry){
659 if(in_array($entry['ID'],$this->usedAttachments)){
660 if($divlist){
661 $ret[$entry['ID']] = $entry;
662 }else{
663 $cm ="";
664 if(isset($entry['comment'])){
665 $cm=" [".$entry['comment']."]";
666 }
667 if(isset($entry['mime'])){
668 $cm.=" -".$entry['mime']."";
669 }
671 $ret[$entry['ID']]= $entry['name'].$cm;
672 }
673 }
674 }
675 return($ret);
676 }
678 function getUsedCartridges($flip = false)
679 {
680 $ret = array();
681 foreach($this->usedCartridges as $key => $value){
682 if($value['status'] == "deleted") continue;
683 if($flip){
684 $ret[$key] = $key;
685 }else{
686 $ret[$key] = $value['name']." [".$value['type_name']."] "._("since")." :".$value['date_use'];
687 }
688 }
689 return($ret);
690 }
692 }
694 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
695 ?>