c6f3f281da4c3578ee2ea983076288b8c49b998d
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 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']))){
418 if(isset($this->usedCartridges[$_POST['Cartridges']])){
419 if($this->usedCartridges[$_POST['Cartridges']]['status'] == "exists"){
420 $this->usedCartridges[$_POST['Cartridges']]['status'] = "deleted";
421 }else{
422 unset($this->usedCartridges[$_POST['Cartridges']]);
423 }
424 }
425 }
427 /* Open Attachment pool to add/edit Attachments
428 */
429 if(isset($_POST['AddCartridge'])){
430 $this->cur_dialog = new glpiPrinterCartridges($this->config,$this->dn,$this->type);
431 $this->dialog = true;
432 }
435 /* ##########################################################################
436 * Draw Dialogs
437 */
438 /* if( cur_dialog != false || cur_dialog != NULL)
439 * There is a dialog which wants to be displayed
440 */
441 if($this->cur_dialog){
442 $this->cur_dialog->save_object();
443 $this->dialog=true;
444 $this->cur_dialog->parent = &$this;
445 return($this->cur_dialog->execute());
446 }else{
447 $this->dialog= false;
448 }
451 /* ##########################################################################
452 * Assign listbox / checkbox .... values to smarty
453 */
454 /* Assign smarty defaults */
455 foreach(array("PrinterTypes","PrinterTypeKeys","Manufacturers","TechnicalResponsibles","Attachments") as $attr){
456 $smarty->assign($attr,array());
457 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
458 }
460 /* Assign some vars to smarty
461 */
462 foreach(array("type","FK_glpi_enterprise","tech_num","contact_num","flags_serial","flags_par","flags_usb") as $attr){
463 $smarty->assign($attr,"");
464 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
465 }
468 /* Assign ACLs to smarty*/
469 foreach($this->attributes as $attr){
470 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
471 }
473 $smarty->assign("comments", $this->comments);
474 $smarty->assign("flags_serial", $this->flags_serial);
475 $smarty->assign("flags_par", $this->flags_par);
476 $smarty->assign("flags_usb", $this->flags_usb);
478 /* Assign system types
479 */
480 $smarty->assign("PrinterTypes", $this->handle->getPrinterTypes());
481 $smarty->assign("PrinterTypeKeys", array_flip($this->handle->getPrinterTypes()));
482 $smarty->assign("type", $this->type);
484 /* Append manufacturers
485 */
486 $smarty->assign("ManufacturerKeys", array_flip($this->handle->getEnterprises()));
487 $smarty->assign("Manufacturers", $this->handle->getEnterprises());
488 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
490 /* Assign used Attachments
491 */
492 $smarty->assign("Attachments", $this->getUsedAttachments());
493 $smarty->assign("AttachmentKeys", array_flip($this->getUsedAttachments()));
495 /* Assign Cartridges
496 */
497 $smarty->assign("Cartridges", $this->getUsedCartridges());
498 $smarty->assign("CartridgeKeys", $this->getUsedCartridges(true));
500 /* ##########################################################################
501 * Assign contact and technical responsible person
502 */
503 if(isset($users[$this->contact_num])){
504 $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
505 $str = "";
506 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
507 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
508 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
509 $smarty->assign("contact_num", $str);
510 }else{
511 $smarty->assign("contact_num", _("N/A"));
512 }
514 /* Handle tech person
515 Assign name ... to smarty, if set
516 */
517 if(isset($users[$this->tech_num])){
518 $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
519 $str = "";
520 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
521 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
522 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
523 $smarty->assign("tech_num", $str);
524 }else{
525 $smarty->assign("tech_num", _("N/A"));
526 }
528 $display.= $smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE));
529 return($display);
530 }
532 function remove_from_parent()
533 {
534 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
535 if($this->initially_was_account){
536 $this->handle->removePrinterInformations($this->dn);
537 }
538 }
541 /* Save data to object */
542 function save_object()
543 {
544 if(isset($_POST['glpiPrinterFlagsPosted'])){
545 plugin::save_object();
546 foreach($this->attributes as $attrs){
547 if(isset($_POST[$attrs])){
548 $this->$attrs = $_POST[$attrs];
549 }
550 }
552 foreach(array("flags_serial","flags_par","flags_usb") as $checkboxes){
553 if(isset($_POST[$checkboxes])){
554 $this->$checkboxes = 1;
555 }else{
556 $this->$checkboxes = 0;
557 }
558 }
559 }
561 }
564 /* Check supplied data */
565 function check()
566 {
567 $message= array();
569 // if($this->TechnicalResponsible == ""){
570 // $message[] = _("Please select a technical responsible person for this entry.");
571 // }
573 return ($message);
574 }
576 /* Save to LDAP */
577 function save()
578 {
579 if($this->is_account){
580 $attrs = array();
581 $this->date_mod = date("Y-m-d H:i:s");
582 foreach($this->attributes as $attr){
583 $attrs[$attr] = $this->$attr;
584 }
585 $attrs['name'] = $this->dn;
586 unset($attrs['ID']);
587 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
588 if($this->initially_was_account&&$this->is_account){
589 $this->handle->updatePrinterInformations($attrs,$this->dn);
590 }elseif($this->is_account){
591 $this->handle->addPrinterInformations($attrs,$this->dn);
592 }
593 $tmp = $this->handle->getPrinterInformations($this->dn);
594 $this->handle->addAttachmentsToPrinter($this->usedAttachments,$tmp[0]['ID']);
596 foreach($this->usedCartridges as $cart){
597 if($cart['status'] == "deleted"){
598 $this->handle->removeCartridgeFromPrinter($cart['ID']);
599 }elseif($cart['status'] == "new"){
600 $this->handle->addCartridgeFromPrinter($tmp[0]['ID'],$cart['type_ID']);
601 }
602 }
603 }
604 }
606 /* Return used attachments */
607 function getUsedAttachments()
608 {
609 $atts =$this->handle->getAttachments();
610 $ret = array();
611 foreach($atts as $entry){
612 if(in_array($entry['ID'],$this->usedAttachments)){
614 $cm ="";
615 if(isset($entry['comment'])){
616 $cm=" [".$entry['comment']."]";
617 }
618 if(isset($entry['mime'])){
619 $cm.=" -".$entry['mime']."";
620 }
622 $ret[$entry['ID']]= $entry['name'].$cm;
623 }
624 }
625 return($ret);
626 }
628 function getUsedCartridges($flip = false)
629 {
630 $ret = array();
631 foreach($this->usedCartridges as $key => $value){
632 if($value['status'] == "deleted") continue;
633 if($flip){
634 $ret[$key] = $key;
635 }else{
636 $ret[$key] = $value['name']." [".$value['type_name']."] "._("since")." :".$value['date_use'];
637 }
638 }
639 return($ret);
640 }
642 }
644 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
645 ?>