Code

First layout adaptions
[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                 ;
17   var $name               ="";
18   var $contact            ="";
19   var $contact_num        ="";
21   var $tech_num           ="";
22   var $comments           ="";
24   var $addUser            ="";
26   var $date_mod           ="";
27   var $os                 =0;
28   var $location           =0;
29   var $domain             =0;
30   var $network            =0; 
32   var $model              =0;
33   var $type               =0;
34   var $is_template        =0;
35   var $FK_glpi_enterprise =0;
36   var $deleted            ="N";
38   var $objectclasses= array("whatever");
40   var $initialy_was_account = false;
42   var $edit_type    =false;
43   var $edit_os    =false;
45   var $data;
46   var $handle = NULL;
48   var $cur_dialog = NULL;
50   var $orig_dn;
51   var $ui;
52   
53   var $usedDevices      = array();
54   var $usedAttachments  = array();
56   /* Contructor 
57      Sets default values and checks if we already have an existing glpi account
58    */
59   function glpiAccount ($config, $dn= NULL)
60   {
61     plugin::plugin ($config, $dn);
62     $this->ui= get_userinfo();
64     if(!isset($this->config->data['SERVERS']['GLPI'])){
65       return;
66     }
68     $this->data = $this->config->data['SERVERS']['GLPI'];
70     if(!is_callable("mysql_connect")){
71       return;
72     }
74     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
76     if(!$this->handle->is_connected){
77       return;
78     } 
80     if($this->handle->is_account($this->dn)){
81       $this->is_account = true;
82       $tmp = ($this->handle->getComputerInformations($this->dn));
84       foreach(array("tech_num","os","FK_glpi_enterprise","type","comments","contact_num") as $attr){
85         $this->$attr = $tmp[0][$attr];
86       }
87       $this->usedDevices = $this->handle->getUsedDevices($tmp[0]['ID']);
88       $atts = $this->handle->getAssignAttachments($tmp[0]['ID']);
89       foreach($atts as $attachment){
90         
91         $this->usedAttachments[$attachment['FK_doc']]=$attachment['FK_doc']; 
92       }
93     }else{
94       $this->is_account = false;
95     }
97     $this->name = $this->dn;
98     $this->orig_dn = $this->dn;
99     $this->initialy_was_account = $this->is_account;
102   }
104   function execute()
105   {
106     /* Call parent execute */
107     plugin::execute();
109     /* Fill templating stuff */
110     $smarty= get_smarty();
111     $display= "";
113     /*  Assign smarty defaults 
114         To avoid undefined indexes, if there is an error with the glpi db
115      */ 
116     foreach(array("SystemTypes","SystemTypeKeys","Manufacturers",
117                   "OSs","TechnicalResponsibles","InstalledDevices","Attachments",
118                   "OSKeys","OSs","ManufacturerKeys","InstalledDeviceKeys") as $attr){
119       $smarty->assign($attr,array());
120       $smarty->assign($attr."ACL"," disabled ");
121     }
122     foreach(array("type","FK_glpi_enterprise","os","tech_num","comments","contact_num") as $attr){
123       $smarty->assign($attr,"");
124       $smarty->assign($attr."ACL"," disabled ");
125     }
127     /* Check if there is a glpi database server defined 
128       */
129     if(!isset($this->config->data['SERVERS']['GLPI'])){
130       print_red(_("There is no server with valid glpi database service."));
131       return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
132     }
134     $this->data = $this->config->data['SERVERS']['GLPI'];
136     /* Check if we can call mysql_connect 
137        If we can't, there is no the mysql-php extension
138      */
139     if(!is_callable("mysql_connect")){
140       print_red(_("Can't connect to glpi database, the php-mysql extension is missing."));
141       return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
142     }
144     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
146     /*  If handle == false, abort
147         Seems that the server, username and or password is wrong
148      */
149     if(!$this->handle->is_connected){
150       print_red(_("Can't connect to glpi database, check configuration twice."));
151       return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
152     } 
154     /*  All checks are ok
155         Lets handle Posts, templates etc below ...
156      */
157     
158     $users = $this->handle->getUsers();
159     $ldap= $this->config->get_ldap_link();
160  
161     /* Check for Trading button Post
162      */
163     if(isset($_POST['Trading'])){
164       print_red(_("This feature is not implemented yet."));
165     }
167     /* Check for Software button Post
168      */
169     if(isset($_POST['Software'])){
170       print_red(_("This feature is not implemented yet."));
171     }
173     /* Check for Contract button Post
174      */
175     if(isset($_POST['Contracts'])){
176       print_red(_("This feature is not implemented yet."));
177     }
178  
179     /* Add Device was requested, open new dialog
180      */
181     if(isset($_POST['AddDevice'])){
182       $this->dialog =true;
183       $this->cur_dialog = new glpiDeviceManagement($this->config,$this->dn,$this->usedDevices);
184     }
186     /* Attachment pool was closed with use
187      */
188     if(isset($_POST['UseAttachment'])){
189       if(count($this->cur_dialog->check())){
190         foreach($this->cur_dialog->check() as $msg){
191           print_red($msg);
192         }
193       }else{
194         $this->cur_dialog->save_object();
195         $this->usedAttachments = $this->cur_dialog->save();
196         $this->cur_dialog = false;
197         $this->edit_type = false; 
198       }
199     }
200    
201     /* Attachment pool was closed with abort
202      */ 
203     if(isset($_POST['AbortAttachment'])){
204       $this->cur_dialog = false;
205       $this->edit_type = false; 
206     }
208     /* Open Attachment pool to add/edit Attachments
209      */
210     if(isset($_POST['AddAttachment'])){
211       $this->cur_dialog = new glpiAttachmentPool($this->config,$this->dn,$this->usedAttachments);
212       $this->dialog = true;
213     }
214     
215     /* Remove Attachment fro this tab 
216      */
217     if((isset($_POST['RemoveAttachment']))&&(isset($_POST['Attachments']))){
218       if(isset($this->usedAttachments[$_POST['Attachments']])){
219         unset($this->usedAttachments[$_POST['Attachments']]);
220       }
221     }
223     /* We have selected some devices and pressed use button 
224      */
225     if(isset($_POST['SelectDeviceSave'])){
226       $this->cur_dialog->save_object();
227       $this->usedDevices= ($this->cur_dialog->getSelected());
228       $this->cur_dialog = false;
229       $this->dialog = false;
230       $this->edit_type=false;
231     }
233     /* Aborted Device selction 
234      */
235     if(isset($_POST['SelectDeviceCancel'])){
236       $this->dialog = false;
237       $this->cur_dialog = false;
238       $this->edit_type=false;
239     }
241     /* System type management
242      */
243     if(isset($_POST['edit_type'])){
244       $this->dialog = true;
245       $this->edit_type=true;
246     }
248     /* This closes the system type editing dialog
249      */
250     if(isset($_POST['close_edit_type'])){
251       $this->edit_type=false;
252       $this->dialog = false;
253     }
255     /* This appends a new system to our sytem types
256      */
257     if((isset($_POST['add_type']))&&(!empty($_POST['type_string']))){
258       $this->handle->addSystemType($_POST['type_string']);  
259     }
261     /* Remove selected type from our system types list
262      */
263     if((isset($_POST['del_type']))&&(!empty($_POST['select_type']))){
264       $this->handle->removeSystemType($_POST['select_type']);  
265     }
267     /* Rename selected system type to given string
268      */
269     if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))&&(!empty($_POST['type_string']))){
270       $this->handle->updateSystemType($_POST['type_string'],$_POST['select_type']);
271     }
273     /* Someone wants to edit the system types ... 
274        So, lets open a new dialog which provides some buttons to edit the types
275      */
276     if($this->edit_type){
277       $smarty->assign("SystemTypes",            $this->handle->getSystemTypes());
278       $smarty->assign("SystemTypeKeys",         array_flip($this->handle->getSystemTypes()));
279       $display= $smarty->fetch(get_template_path('glpi_edit_type.tpl', TRUE));
280       return($display);
281     }
283     /* System os management
284      */
285     if(isset($_POST['edit_os'])){
286       $this->dialog = true;
287       $this->edit_os=true;
288     }
290     /* Close Operating system dialog
291      */
292     if(isset($_POST['close_edit_os'])){
293       $this->edit_os=false;
294       $this->dialog = false;
295     }
297     /* Add new os to the db
298      */
299     if((isset($_POST['add_os']))&&(!empty($_POST['is_string']))){
300       $this->handle->addOS($_POST['is_string']);  
301     }
303     /* Delete selected os from list and db
304      */
305     if((isset($_POST['del_os']))&&(!empty($_POST['select_os']))){
306       $this->handle->removeOS_byID($_POST['select_os']);  
307     }
309     /* Rename selected os to given string
310      */
311     if((isset($_POST['rename_os']))&&(!empty($_POST['select_os']))&&(!empty($_POST['is_string']))){
312       $this->handle->updateOS($_POST['is_string'],$_POST['select_os']);
313     }
315     /* Open dialog to edit os types 
316      */
317     if($this->edit_os){
318       $smarty->assign("OSKeys",                 array_flip($this->handle->getOSTypes()));
319       $smarty->assign("OSs",                    $this->handle->getOSTypes());
320       $display= $smarty->fetch(get_template_path('glpi_edit_os.tpl', TRUE));
321       return($display);
322     }
324     /* Show dialog to select a new contact person
325      * Select a contact person
326      */
327     if(isset($_POST['SelectContactPerson'])){
328       $this->addUser = "contact";
329       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
330     }
332     /* Open dialog which allows to edit the manufacturers
333      */
334     if(isset($_POST['edit_manufacturer'])){
335       $this->cur_dialog = new glpiManufacturer($this->config,$this->dn);
336       $this->dialog = true;
337     }
339     /* Close manufacturer editing dialog
340      */
341     if(isset($_POST['close_edit_manufacturer'])){
342       $this->dialog = false;
343       $this->cur_dialog = false;
344     }
346     /* Abort user selection
347      */
348     if(isset($_POST['SelectUserCancel'])){
349       $this->dialog = false;
350       $this->addUser ="";
351       $this->cur_dialog = false;
352     }
354     /* Selecte technical responsible person
355      */
356     if(isset($_POST['SelectTechPerson'])){
357       $this->addUser ="tech";
358       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
359     }
361     /* Technical responsible person selected*/
362     if(isset($_GET['act'])&&($_GET['act']=="user_tech_num")){
364       /* Get posted id */
365       $id = base64_decode($_GET['id']);
367       /* Check if user is already created in glpi database */
368       if(!in_array($id,$users)){
370         /* If this user doesn't exists in glpi db, we must create him */
371         $atr = $ldap->fetch($ldap->cat($id));
372         $tmp = array();
373         $use = array( "cn"              =>"name",
374             "mail"            =>"email",
375             "telephoneNumber" =>"phone");
377         /* Create array */
378         foreach($use as $gosa => $glpi){
379           if(isset($atr[$gosa])){
380             $tmp[$glpi]= $atr[$gosa][0];
381           }
382         }
384         /* Add this user */
385         $this->handle->addUser($tmp,$id);
386       }
388       /* Re-read users */
389       $users = ($this->handle->getUsers());
391       /* Get user */
392       $tmp = array_flip($users);
393       $id=$tmp[$id];
395       /* Use user id, close dialog */
396       if($this->addUser == "tech"){
397         $this->tech_num = $id;
398       }else{
399         $this->contact_num = $id;
400       }
401       $this->cur_dialog   = false;
402       $this->dialog= false;
403     }
405     /* if( cur_dialog != false || cur_dialog != NULL) 
406      * There is a dialog which wants to be displayed 
407      */
408     if($this->cur_dialog){
409       $this->cur_dialog->save_object();
410       $this->dialog=true;
411       $this->cur_dialog->parent = &$this;
412       return($this->cur_dialog->execute());
413     }else{
414       $this->dialog= false;
415     }
417     /* Assign smarty defaults */ 
418     foreach(array("SystemTypes","SystemTypeKeys","Manufacturers","OSs","TechnicalResponsibles","InstalledDevices","Attachments") as $attr){
419       $smarty->assign($attr,array());
420       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
421     }
423     /* Assign some vars to smarty 
424      */
425     foreach(array("type","FK_glpi_enterprise","os","tech_num","contact_num") as $attr){
426       $smarty->assign($attr,"");
427       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
428     }
430     /* Do we need to flip is_account state? */
431     if (isset($_POST['modify_state'])){
432       $this->is_account= !$this->is_account;
433     }
435     /* Show tab dialog headers */
436     if ($this->is_account){
437       $display= $this->show_header(_("Remove inventory service"),
438           _("This server has inventory features enabled. You can disable them by clicking below."));
439     } else {
440       $display= $this->show_header(_("Add inventory service"),
441           _("This server has inventory features disabled. You can enable them by clicking below."));
442       return ($display);
443     }
445     /* Assign ACLs to smarty*/
446     foreach($this->attributes as $attr){
447       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
448     }
450     /* Assign system types 
451      */
452     $smarty->assign("SystemTypeKeys",         array_flip($this->handle->getSystemTypes()));
453     $smarty->assign("SystemTypes",            $this->handle->getSystemTypes());
454     $smarty->assign("type",                   $this->type);
456     /* Assign os types
457      */
458     $smarty->assign("OSKeys",                 array_flip($this->handle->getOSTypes()));
459     $smarty->assign("OSs",                    $this->handle->getOSTypes());
460     $smarty->assign("os",                     $this->os);
462     /* Dispaly installed devices */
463     $smarty->assign("InstalledDevices"        ,$this->getDevices());
464     $smarty->assign("InstalledDeviceKeys"     ,array_flip($this->getDevices()));
466     /* Append manufacturers
467      */
468     $smarty->assign("ManufacturerKeys",       array_flip($this->handle->getEnterprises()));
469     $smarty->assign("Manufacturers",          $this->handle->getEnterprises());
470     $smarty->assign("FK_glpi_enterprise",     $this->FK_glpi_enterprise);
472     /* Assign used Attachments
473     */
474     $smarty->assign("Attachments",            $this->getUsedAttachments());
475     $smarty->assign("AttachmentKeys",         array_flip($this->getUsedAttachments()));
477     /* Handle contact person 
478        Assign name ... to smarty, if set
479      */ 
480     if(isset($users[$this->contact_num])){
481       $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
482       $str = "";
483       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
484       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
485       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
486       $smarty->assign("contact_num",               $str);
487     }else{
488       $smarty->assign("contact_num",               _("N/A"));
489     }
491     /* Handle tech person 
492        Assign name ... to smarty, if set
493      */ 
494     if(isset($users[$this->tech_num])){
495       $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
496       $str = "";
497       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
498       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
499       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
500       $smarty->assign("tech_num",               $str);
501     }else{
502       $smarty->assign("tech_num",               _("N/A"));
503     }
504     $smarty->assign("comments",               $this->comments);
506     $display.= $smarty->fetch(get_template_path('glpi.tpl', TRUE));
507     return($display);
508   }
510   function remove_from_parent()
511   {
512     $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
513     if($this->initialy_was_account){
514       $this->handle->removeComputerInformations($this->dn); 
515     }
516   
517   }
519   function getDevices(){
520     $ret = array();
521     foreach($this->usedDevices as $type => $entries){
522       foreach($entries as $ent){
523         if(isset($ent['designation'])){
524           $ret[] = $ent['designation']." [".$type."]";
525         }else{
526           $ret[] = $ent['name']." [".$type."]";
527         }
528       }
529     }
530     return($ret); 
531   }
534   /* Save data to object */
535   function save_object()
536   {
537     plugin::save_object();
538     foreach($this->attributes as $attrs){
539       if(isset($_POST[$attrs])){
540         $this->$attrs = $_POST[$attrs];
541       }
542     }
543   }
546   /* Check supplied data */
547   function check()
548   {
549     $message= array();
551     //    if($this->TechnicalResponsible == ""){
552     //      $message[] = _("Please select a technical responsible person for this entry.");
553     //    }
555     return ($message);
556   }
558   /* Save to LDAP */
559   function save()
560   {
561     if($this->is_account){
562         $attrs = array();
563       $this->date_mod = date("Y-m-d H:i:s");
564       foreach($this->attributes as $attr){
565         $attrs[$attr] = $this->$attr;
566       }
567       $attrs['name'] = $this->dn;
568       unset($attrs['ID']);
569       $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
570       if($this->initialy_was_account&&$this->is_account){
571         $this->handle->updateComputerInformations($attrs,$this->dn);
572       }elseif($this->is_account){
573         $this->handle->addComputerInformations($attrs,$this->dn);
574       }
575       $tmp = $this->handle->getComputerInformations($this->dn);
576       $this->handle->addDevicesToComputer($this->usedDevices,$tmp[0]['ID']);
577       $this->handle->addAttachmentsToComputer($this->usedAttachments,$tmp[0]['ID']);
578     }
579   }
581   function getUsedAttachments()
582   {
583     $atts =$this->handle->getAttachments();
584     $ret = array();
585     foreach($atts as $entry){
586       if(in_array($entry['ID'],$this->usedAttachments)){
588         $cm ="";
589         if(isset($entry['comment'])){
590           $cm=" [".$entry['comment']."]";
591         }
592         if(isset($entry['mime'])){
593           $cm.=" -".$entry['mime']."";
594         }
596         $ret[$entry['ID']]= $entry['name'].$cm;
597       }
598     }
599     return($ret);
600   }
601   
605 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
606 ?>