Code

258499ff27c7d3c55650839148260a06bf4d2156
[gosa.git] / plugins / admin / systems / class_glpiAccount.inc
1 <?php
3 class glpiAccount 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",
13       "tech_num","comments","date_mod","os","location","domain","network","contact_num",
14       "model","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 $contact            = "";    // Empty
19     
20   var $comments           = "";    // Comment
21   
22   var $contact_num        = "";    // Contact person
23   var $tech_num           = "";    // Technical responsible person
24   
25   var $addUser            = "";    // This is used to remember if a dialog was opened for tech_num or contact_num 
27   var $date_mod           = "";    // Modification timestamp
28   var $os                 = 0;     // Operating system
29   var $location           = 0;     // Not used yet
30   var $domain             = 0;     // ? Set to 0
31   var $network            = 0;     // ? Set to 0 
33   var $model              = 0;     // ? Can't remember this, it isn't used in GOsa 
34   var $type               = 0;     // System type id
35   var $is_template        = 0;     // Used as template ?
36   var $FK_glpi_enterprise = 0;     // Manufacturer id
37   var $deleted            = "N";   // Deleted entries should have this set to Y
39   /* Not necessary, cause we use mysql databse */
40   var $objectclasses= array("whatever");
42   /* Used to remember if this was an account (simply: is this an edited entry) */
43   var $initialy_was_account = false;
45   /* Remember current dialog */
46   var $edit_type            = false;
47   var $edit_os              = false;
49   var $data;
50   var $handle = NULL;               // Glpi class handle used to query database
52   var $cur_dialog = NULL;           // This contains the sub dialog handle
54   var $orig_dn;                     // To check if dn, has changed 
55   var $ui;                          // Some GOsa specific user informations 
56   
57   var $usedDevices      = array();  // Which devices are currently selected 
58   var $usedAttachments  = array();  // Used Attachments 
60   /* Contructor 
61      Sets default values and checks if we already have an existing glpi account
62    */
63   function glpiAccount ($config, $dn= NULL)
64   {
65     plugin::plugin ($config, $dn);
66     $this->ui= get_userinfo();
68     /* Abort class construction, if no db is defined */
69     if(!isset($this->config->data['SERVERS']['GLPI'])){
70       return;
71     }
73     // Get informations about databse connection
74     $this->data = $this->config->data['SERVERS']['GLPI'];
76     // Abort if mysql extension is missing 
77     if(!is_callable("mysql_connect")){
78       return;
79     }
81     // Create handle of glpi class, and check if database connection is established 
82     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
84     if(!$this->handle->is_connected){
85       return;
86     } 
88     // If this dn is already used in database, then get all informations for this entry 
89     if($this->handle->is_account($this->dn)){
90       $this->is_account = true;
91       $tmp = ($this->handle->getComputerInformations($this->dn));
93       foreach(array("tech_num","os","FK_glpi_enterprise","type","comments","contact_num") as $attr){
94         $this->$attr = $tmp[0][$attr];
95       }
96       $this->usedDevices = $this->handle->getUsedDevices($tmp[0]['ID']);
97       $atts = $this->handle->getAssignAttachments($tmp[0]['ID']);
98       foreach($atts as $attachment){
99         
100         $this->usedAttachments[$attachment['FK_doc']]=$attachment['FK_doc']; 
101       }
102     }else{
103       $this->is_account = false;
104     }
106     /* set defaults */
107     $this->name                 = $this->dn;
108     $this->orig_dn              = $this->dn;
109     $this->initialy_was_account = $this->is_account;
112   }
114   function execute()
115   {
116     /* Call parent execute */
117     plugin::execute();
119     /* Fill templating stuff */
120     $smarty= get_smarty();
121     $display= "";
123     /*  Assign smarty defaults 
124         To avoid undefined indexes, if there is an error with the glpi db
125      */ 
126     foreach(array("SystemTypes","SystemTypeKeys","Manufacturers",
127                   "OSs","TechnicalResponsibles","InstalledDevices","Attachments",
128                   "OSKeys","OSs","ManufacturerKeys","InstalledDeviceKeys") as $attr){
129       $smarty->assign($attr,array());
130       $smarty->assign($attr."ACL"," disabled ");
131     }
132     foreach(array("type","FK_glpi_enterprise","os","tech_num","comments","contact_num") as $attr){
133       $smarty->assign($attr,"");
134       $smarty->assign($attr."ACL"," disabled ");
135     }
137     /* Check if there is a glpi database server defined 
138       */
139     if(!isset($this->config->data['SERVERS']['GLPI'])){
140       print_red(_("There is no server with valid glpi database service."));
141       return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
142     }
144     $this->data = $this->config->data['SERVERS']['GLPI'];
146     /* Check if we can call mysql_connect 
147        If we can't, there is no the mysql-php extension
148      */
149     if(!is_callable("mysql_connect")){
150       print_red(_("Can't connect to glpi database, the php-mysql extension is missing."));
151       return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
152     }
154     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
156     /*  If handle == false, abort
157         Seems that the server, username and or password is wrong
158      */
159     if(!$this->handle->is_connected){
160       print_red(_("Can't connect to glpi database, check configuration twice."));
161       return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
162     } 
164     /*  All checks are ok
165         Lets handle Posts, templates etc below ...
166      */
167     
168     $users = $this->handle->getUsers();
169     $ldap= $this->config->get_ldap_link();
170  
171     /* Check for Trading button Post
172      */
173     if(isset($_POST['Trading'])){
174       print_red(_("This feature is not implemented yet."));
175     }
177     /* Check for Software button Post
178      */
179     if(isset($_POST['Software'])){
180       print_red(_("This feature is not implemented yet."));
181     }
183     /* Check for Contract button Post
184      */
185     if(isset($_POST['Contracts'])){
186       print_red(_("This feature is not implemented yet."));
187     }
188  
189     /* Add Device was requested, open new dialog
190      */
191     if(isset($_POST['AddDevice'])){
192       $this->dialog =true;
193       $this->cur_dialog = new glpiDeviceManagement($this->config,$this->dn,$this->usedDevices);
194     }
196     /* Attachment pool was closed with use
197      */
198     if(isset($_POST['UseAttachment'])){
199       if(count($this->cur_dialog->check())){
200         foreach($this->cur_dialog->check() as $msg){
201           print_red($msg);
202         }
203       }else{
204         $this->cur_dialog->save_object();
205         $this->usedAttachments = $this->cur_dialog->save();
206         $this->cur_dialog = false;
207         $this->edit_type = false; 
208       }
209     }
210    
211     /* Attachment pool was closed with abort
212      */ 
213     if(isset($_POST['AbortAttachment'])){
214       $this->cur_dialog = false;
215       $this->edit_type = false; 
216     }
218     /* Open Attachment pool to add/edit Attachments
219      */
220     if(isset($_POST['AddAttachment'])){
221       $this->cur_dialog = new glpiAttachmentPool($this->config,$this->dn,$this->usedAttachments);
222       $this->dialog = true;
223     }
224     
225     /* Remove Attachment fro this tab 
226      */
227     if((isset($_POST['RemoveAttachment']))&&(isset($_POST['Attachments']))){
228       if(isset($this->usedAttachments[$_POST['Attachments']])){
229         unset($this->usedAttachments[$_POST['Attachments']]);
230       }
231     }
233     /* We have selected some devices and pressed use button 
234      */
235     if(isset($_POST['SelectDeviceSave'])){
236       $this->cur_dialog->save_object();
237       $this->usedDevices= ($this->cur_dialog->getSelected());
238       $this->cur_dialog = false;
239       $this->dialog = false;
240       $this->edit_type=false;
241     }
243     /* Aborted Device selction 
244      */
245     if(isset($_POST['SelectDeviceCancel'])){
246       $this->dialog = false;
247       $this->cur_dialog = false;
248       $this->edit_type=false;
249     }
251     /* System type management
252      */
253     if(isset($_POST['edit_type'])){
254       $this->dialog = true;
255       $this->edit_type=true;
256     }
258     /* This closes the system type editing dialog
259      */
260     if(isset($_POST['close_edit_type'])){
261       $this->edit_type=false;
262       $this->dialog = false;
263     }
265     /* This appends a new system to our sytem types
266      */
267     if((isset($_POST['add_type']))&&(!empty($_POST['type_string']))){
268       $this->handle->addSystemType($_POST['type_string']);  
269     }
271     /* Remove selected type from our system types list
272      */
273     if((isset($_POST['del_type']))&&(!empty($_POST['select_type']))){
274       $this->handle->removeSystemType($_POST['select_type']);  
275     }
277     /* Rename selected system type to given string
278      */
279     if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))&&(!empty($_POST['type_string']))){
280       $this->handle->updateSystemType($_POST['type_string'],$_POST['select_type']);
281     }
283     /* Someone wants to edit the system types ... 
284        So, lets open a new dialog which provides some buttons to edit the types
285      */
286     if($this->edit_type){
287       $smarty->assign("SystemTypes",            $this->handle->getSystemTypes());
288       $smarty->assign("SystemTypeKeys",         array_flip($this->handle->getSystemTypes()));
289       $display= $smarty->fetch(get_template_path('glpi_edit_type.tpl', TRUE));
290       return($display);
291     }
293     /* System os management
294      */
295     if(isset($_POST['edit_os'])){
296       $this->dialog = true;
297       $this->edit_os=true;
298     }
300     /* Close Operating system dialog
301      */
302     if(isset($_POST['close_edit_os'])){
303       $this->edit_os=false;
304       $this->dialog = false;
305     }
307     /* Add new os to the db
308      */
309     if((isset($_POST['add_os']))&&(!empty($_POST['is_string']))){
310       $this->handle->addOS($_POST['is_string']);  
311     }
313     /* Delete selected os from list and db
314      */
315     if((isset($_POST['del_os']))&&(!empty($_POST['select_os']))){
316       $this->handle->removeOS_byID($_POST['select_os']);  
317     }
319     /* Rename selected os to given string
320      */
321     if((isset($_POST['rename_os']))&&(!empty($_POST['select_os']))&&(!empty($_POST['is_string']))){
322       $this->handle->updateOS($_POST['is_string'],$_POST['select_os']);
323     }
325     /* Open dialog to edit os types 
326      */
327     if($this->edit_os){
328       $smarty->assign("OSKeys",                 array_flip($this->handle->getOSTypes()));
329       $smarty->assign("OSs",                    $this->handle->getOSTypes());
330       $display= $smarty->fetch(get_template_path('glpi_edit_os.tpl', TRUE));
331       return($display);
332     }
334     /* Show dialog to select a new contact person
335      * Select a contact person
336      */
337     if(isset($_POST['SelectContactPerson'])){
338       $this->addUser = "contact";
339       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
340     }
342     /* Open dialog which allows to edit the manufacturers
343      */
344     if(isset($_POST['edit_manufacturer'])){
345       $this->cur_dialog = new glpiManufacturer($this->config,$this->dn);
346       $this->dialog = true;
347     }
349     /* Close manufacturer editing dialog
350      */
351     if(isset($_POST['close_edit_manufacturer'])){
352       $this->dialog = false;
353       $this->cur_dialog = false;
354     }
356     /* Abort user selection
357      */
358     if(isset($_POST['SelectUserCancel'])){
359       $this->dialog = false;
360       $this->addUser ="";
361       $this->cur_dialog = false;
362     }
364     /* Selecte technical responsible person
365      */
366     if(isset($_POST['SelectTechPerson'])){
367       $this->addUser ="tech";
368       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
369     }
371     /* Technical responsible person selected*/
372     if(isset($_GET['act'])&&($_GET['act']=="user_tech_num")){
374       /* Get posted id */
375       $id = base64_decode($_GET['id']);
377       /* Check if user is already created in glpi database */
378       if(!in_array($id,$users)){
380         /* If this user doesn't exists in glpi db, we must create him */
381         $atr = $ldap->fetch($ldap->cat($id));
382         $tmp = array();
383         $use = array( "cn"              =>"name",
384             "mail"            =>"email",
385             "telephoneNumber" =>"phone");
387         /* Create array */
388         foreach($use as $gosa => $glpi){
389           if(isset($atr[$gosa])){
390             $tmp[$glpi]= $atr[$gosa][0];
391           }
392         }
394         /* Add this user */
395         $this->handle->addUser($tmp,$id);
396       }
398       /* Re-read users */
399       $users = ($this->handle->getUsers());
401       /* Get user */
402       $tmp = array_flip($users);
403       $id=$tmp[$id];
405       /* Use user id, close dialog */
406       if($this->addUser == "tech"){
407         $this->tech_num = $id;
408       }else{
409         $this->contact_num = $id;
410       }
411       $this->cur_dialog   = false;
412       $this->dialog= false;
413     }
415     /* if( cur_dialog != false || cur_dialog != NULL) 
416      * There is a dialog which wants to be displayed 
417      */
418     if($this->cur_dialog){
419       $this->cur_dialog->save_object();
420       $this->dialog=true;
421       $this->cur_dialog->parent = &$this;
422       return($this->cur_dialog->execute());
423     }else{
424       $this->dialog= false;
425     }
427     /* Assign smarty defaults */ 
428     foreach(array("SystemTypes","SystemTypeKeys","Manufacturers","OSs","TechnicalResponsibles","InstalledDevices","Attachments") as $attr){
429       $smarty->assign($attr,array());
430       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
431     }
433     /* Assign some vars to smarty 
434      */
435     foreach(array("type","FK_glpi_enterprise","os","tech_num","contact_num") as $attr){
436       $smarty->assign($attr,"");
437       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
438     }
440     /* Do we need to flip is_account state? */
441     if (isset($_POST['modify_state'])){
442       $this->is_account= !$this->is_account;
443     }
445     /* Show tab dialog headers */
446     if ($this->is_account){
447       $display= $this->show_header(_("Remove inventory service"),
448           _("This server has inventory features enabled. You can disable them by clicking below."));
449     } else {
450       $display= $this->show_header(_("Add inventory service"),
451           _("This server has inventory features disabled. You can enable them by clicking below."));
452       return ($display);
453     }
455     /* Assign ACLs to smarty*/
456     foreach($this->attributes as $attr){
457       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
458     }
460     /* Assign system types 
461      */
462     $smarty->assign("SystemTypeKeys",         array_flip($this->handle->getSystemTypes()));
463     $smarty->assign("SystemTypes",            $this->handle->getSystemTypes());
464     $smarty->assign("type",                   $this->type);
466     /* Assign os types
467      */
468     $smarty->assign("OSKeys",                 array_flip($this->handle->getOSTypes()));
469     $smarty->assign("OSs",                    $this->handle->getOSTypes());
470     $smarty->assign("os",                     $this->os);
472     /* Dispaly installed devices */
473     $smarty->assign("InstalledDevices"        ,$this->getDevices());
474     $smarty->assign("InstalledDeviceKeys"     ,array_flip($this->getDevices()));
476     /* Append manufacturers
477      */
478     $smarty->assign("ManufacturerKeys",       array_flip($this->handle->getEnterprises()));
479     $smarty->assign("Manufacturers",          $this->handle->getEnterprises());
480     $smarty->assign("FK_glpi_enterprise",     $this->FK_glpi_enterprise);
482     /* Assign used Attachments
483     */
484     $smarty->assign("Attachments",            $this->getUsedAttachments());
485     $smarty->assign("AttachmentKeys",         array_flip($this->getUsedAttachments()));
487     /* Handle contact person 
488        Assign name ... to smarty, if set
489      */ 
490     if(isset($users[$this->contact_num])){
491       $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
492       $str = "";
493       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
494       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
495       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
496       $smarty->assign("contact_num",               $str);
497     }else{
498       $smarty->assign("contact_num",               _("N/A"));
499     }
501     /* Handle tech person 
502        Assign name ... to smarty, if set
503      */ 
504     if(isset($users[$this->tech_num])){
505       $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
506       $str = "";
507       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
508       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
509       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
510       $smarty->assign("tech_num",               $str);
511     }else{
512       $smarty->assign("tech_num",               _("N/A"));
513     }
514     $smarty->assign("comments",               $this->comments);
516     $display.= $smarty->fetch(get_template_path('glpi.tpl', TRUE));
517     return($display);
518   }
520   function remove_from_parent()
521   {
522     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
523     if($this->initialy_was_account){
524       $this->handle->removeComputerInformations($this->dn); 
525     }
526   
527   }
529   function getDevices(){
530     $ret = array();
531     foreach($this->usedDevices as $type => $entries){
532       foreach($entries as $ent){
533         if(isset($ent['designation'])){
534           $ret[] = $ent['designation']." [".$type."]";
535         }else{
536           $ret[] = $ent['name']." [".$type."]";
537         }
538       }
539     }
540     return($ret); 
541   }
544   /* Save data to object */
545   function save_object()
546   {
547     plugin::save_object();
548     foreach($this->attributes as $attrs){
549       if(isset($_POST[$attrs])){
550         $this->$attrs = $_POST[$attrs];
551       }
552     }
553   }
556   /* Check supplied data */
557   function check()
558   {
559     $message= array();
561     //    if($this->TechnicalResponsible == ""){
562     //      $message[] = _("Please select a technical responsible person for this entry.");
563     //    }
565     return ($message);
566   }
568   /* Save to LDAP */
569   function save()
570   {
571     if($this->is_account){
572         $attrs = array();
573       $this->date_mod = date("Y-m-d H:i:s");
574       foreach($this->attributes as $attr){
575         $attrs[$attr] = $this->$attr;
576       }
577       $attrs['name'] = $this->dn;
578       unset($attrs['ID']);
579       $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
580       if($this->initialy_was_account&&$this->is_account){
581         $this->handle->updateComputerInformations($attrs,$this->dn);
582       }elseif($this->is_account){
583         $this->handle->addComputerInformations($attrs,$this->dn);
584       }
585       $tmp = $this->handle->getComputerInformations($this->dn);
586       $this->handle->addDevicesToComputer($this->usedDevices,$tmp[0]['ID']);
587       $this->handle->addAttachmentsToComputer($this->usedAttachments,$tmp[0]['ID']);
588     }
589   }
591   /* Return used attachments */
592   function getUsedAttachments()
593   {
594     $atts =$this->handle->getAttachments();
595     $ret = array();
596     foreach($atts as $entry){
597       if(in_array($entry['ID'],$this->usedAttachments)){
599         $cm ="";
600         if(isset($entry['comment'])){
601           $cm=" [".$entry['comment']."]";
602         }
603         if(isset($entry['mime'])){
604           $cm.=" -".$entry['mime']."";
605         }
607         $ret[$entry['ID']]= $entry['name'].$cm;
608       }
609     }
610     return($ret);
611   }
612   
616 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
617 ?>