Code

Removed br
[gosa.git] / plugins / admin / systems / class_glpiDeviceManagement.inc
1 <?php
3 class glpiDeviceManagement 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= TRUE;
12   var $attributes= array();
13   var $objectclasses= array("whatever");
15   var $devices = array();
16   var $ui;
18   var $parent   ;
19   var $EditEntry;
20   var $editMode =false;
22   var $DeviceAttrs = array();
23   var $AllowedDevices = array();
25   var $Selected = array();
27   var $delete = array();
29   function glpiDeviceManagement ($config, $dn= NULL,$used=NULL)
30   {
31     plugin::plugin ($config, $dn);
33     if(!isset($_SESSION['glpiDeviceRegex'])){
34       $tmp['device_regex'] = "*";
35       $_SESSION['glpiDeviceRegex'] = $tmp;
36     }
37     $this->ui = get_userinfo();  
39     if(isset($used)){
40     $this->Selected = $used;
41     }
42  
43     /* Specify which vars are allowed for a sepcific type of device */ 
44     $this->DeviceAttrs['case']      = array("designation","format","comment","FK_glpi_enterprise");
45     $this->DeviceAttrs['moboard']   = array("designation","chipset","comment","FK_glpi_enterprise");
46     $this->DeviceAttrs['processor'] = array("designation","frequence","comment","FK_glpi_enterprise","specif_default");
47     $this->DeviceAttrs['iface']     = array("designation","bandwidth","comment","FK_glpi_enterprise","specif_default");
48     $this->DeviceAttrs['ram']       = array("designation","frequence","comment","FK_glpi_enterprise","specif_default","type");
49     $this->DeviceAttrs['hdd']       = array("designation","rpm","interface","cache","comment","FK_glpi_enterprise","specif_default");
50     $this->DeviceAttrs['drive']     = array("designation","speed","interface","is_writer","comment","FK_glpi_enterprise","specif_default");
51     $this->DeviceAttrs['control']   = array("designation","interface","raid","comment","FK_glpi_enterprise");
52     $this->DeviceAttrs['gfxcard']   = array("designation","ram","interface","comment","FK_glpi_enterprise","specif_default");
53     $this->DeviceAttrs['sndcard']   = array("designation","type","comment","FK_glpi_enterprise","specif_default");
54     $this->DeviceAttrs['power']     = array("designation","power","comment","FK_glpi_enterprise","atx");
55     $this->DeviceAttrs['pci']       = array("designation","comment","FK_glpi_enterprise","specif_default");
56     $this->DeviceAttrs['monitor']   = array("name","comments","serial","otherserial","size",
57                                             "flags_micro","flags_speaker","flags_subd","flags_bnc",
58                                             "location","type","FK_glpi_enterprise","is_global","deleted","is_template","tplname");
60     $this->AllowedDevices=array("case","moboard","sndcard","processor","iface","ram","hdd","drive","control","gfxcard","power","pci","monitor");
61   }
63   function execute()
64   {
65     /* Call parent execute */
66     plugin::execute();
68     /* Get all defined devices */
69     $this->reload();
71     /* Fill templating stuff */
72     $smarty= get_smarty();
73     $display= "";
75     /* this var is used to ensure that every post is only performed once */
76     $only_once = true;
78     /* Check Post for some actions */
79     foreach($_POST as $name => $value){
80       
81       /* If new_ then we should create an new device */
82       if((preg_match("/^new_/",$name))&&($only_once)){
83     
84         /* don't do this twice */
85         $only_once = false;
87         /*extract device device_type */
88         $deviceType = preg_replace("/_.*$/","",preg_replace("/^new_/","",$name));
90         /* Check if type is allowed, and create empty entry */
91         $tmp = array();
92         if((!isset($this->DeviceAttrs[$deviceType]))||((!in_array($deviceType,$this->AllowedDevices)))){
93           print_red(sprintf(_("Internal Error can't create device of type '%s'"),$deviceType));
94         }else{
95           foreach($this->DeviceAttrs[$deviceType] as $attr){
96             $tmp[$attr] = "";
97           }
98           $tmp['device_type'] = $deviceType;
99           $this->EditEntry = $tmp;
100           $this->editMode =true;
101         }
102       }
103     }
105     /* delete was requested ... show dialog   */
106     if((isset($_GET['act']))&&($_GET['act']=="del_device")){
107       $id = base64_decode($_GET['id']);
108       $tmp = $this->devices[$id];
110       $this->delete = $tmp;
112       if(isset($tmp['name'])){
113         $smarty->assign("warning", sprintf(_("You're about to delete the glpi device '%s'."), $tmp['name']));
114       }elseif(isset($tmp['designation'])){
115         $smarty->assign("warning", sprintf(_("You're about to delete the glpi device '%s'."), $tmp['designation']));
116       }else{
117         print_a(_("Can't detect object name."));
118       }
119       
120       return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
121     }
123     /* Delete entry, but check if this device is in currently in use */
124     if(isset($_POST['delete_glpi_confirm'])){
125       if(count($this->parent->handle->is_deviceUsed($this->delete))){
127         $tmp = $this->parent->handle->is_deviceUsed($this->delete);    
128   
129         $names = "";
130         foreach($tmp as $name){
131           $names .= ", ".$name;
132         }
133         $names = preg_replace("/^, /","",$names);
134         $names = trim($names);
135         if(count($tmp) == 3){
136           $names .= " ...";
137         }
138         print_red(sprintf(_("You can't delete this device, it is still in use by these system(s) '%s'"),$names));
139       }else{
140         $this->parent->handle->deleteDevice($this->delete);
141         $this->reload();
142       }
143     }
145     /* Open entry for editing if requested */
146     if((isset($_GET['act']))&&($_GET['act']=="edit_device")){
147       $id = base64_decode($_GET['id']);
148       $this->editMode =true;
149       $this->EditEntry = $this->devices[$id];
150     }
152     /* Abort editing this entry */
153     if(isset($_POST['AbortDeviceChanges'])){
154       $this->EditEntry = array();
155       $this->editMode = false;
156     }
158     /* Save all changes made on currently selected entry */
159     if(isset($_POST['SaveDeviceChanges'])){
160     
161       /* First check if all changes made are allowed */
162       if(count($this->check($this->EditEntry))==0){
163         $this->save($this->EditEntry);
164         $this->editMode = false;
165         $this->reload();
166       }else{
167         foreach($this->check($this->EditEntry) as $msg){
168           print_red($msg);
169         }
170       }
171       
172     }
174     /* Check if we are currently editing something ? */
175     if($this->editMode == true){
176       return ($this->editDevice($this->EditEntry));
177     }
179     /*  ENDE :  GET / POST handling 
180      *  Below, only output generation for headpage
181      */
183     $divlist = new divlist("glpi devices");
184     $divlist->SetEntriesPerPage(0);
185     $divlist->SetHeader(array(
186           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
187           array("string" => _("devices"), "attach" => "style=''"),
188           array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
190     $edit = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'>%s</a>";
191     $editdel = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
192     $editdel.= "<a href='?plug=".$_GET['plug']."&amp;act=del_device&amp;id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
193     
194     $useDevice = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useDevice_%s' %USE%>";
196     foreach($this->devices as $key=>$user){
198       if(isset($user['designation'])){
199         $str = "designation";
200       }else{
201         $str = "name";
202       }
204       if(isset($this->Selected[$user['device_type']][$user[$str]])){
205         $use = " checked ";
206       }else{
207         $use ="";
208       }
210       /* Dawn databse struckture ....*/
211       if(empty($user['comment']) && isset($user['comments'])) {
212         $user['comment'] = $user['comments'];
213       }
215       $field1 = array("string" => preg_replace("/%s/",base64_encode($key),preg_replace("/%USE%/",$use,$useDevice)), "attach" => "style='text-align:center;width:20px;'");
216       $field2 = array("string" => sprintf($edit,base64_encode($key),$user[$str]."&nbsp;[".$user['comment']."]"), "attach" => "style=''");
217       $field3 = array("string" => sprintf($editdel,base64_encode($key),base64_encode($key)), 
218         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
219       $divlist->AddEntry(array($field1,$field2,$field3));
220       
221     }
222     
223   $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
224       " <input class='center' type='image' align='middle' 
225       src='images/monitor.png'  title='"._("New monitor")."' alt='"._("M")."' name='new_monitor'>&nbsp;".
226       " <input class='center' type='image' align='middle' 
227       src='images/mainboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'>&nbsp;".
228       " <input class='center' type='image' align='middle' 
229       src='images/processor.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'>&nbsp;".
230       " <input class='center' type='image' align='middle' 
231       src='images/server.png' title='"._("New case")."' alt='"._("C")."' name='new_case'>&nbsp;".
232       " <input class='center' type='image' align='middle' 
233       src='images/net_hardware.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'>&nbsp;".
234       " <input class='center' type='image' align='middle' 
235       src='images/memory.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'>&nbsp;".
236       " <input class='center' type='image' align='middle' 
237       src='images/fai_partitionTable.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'>&nbsp;".
238       " <input class='center' type='image' align='middle' 
239       src='images/drives.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'>&nbsp;".
240       " <input class='center' type='image' align='middle' 
241       src='images/hardware.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'>&nbsp;".
242       " <input class='center' type='image' align='middle' 
243       src='images/gfx_hardware.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'>&nbsp;".
244       " <input class='center' type='image' align='middle' 
245       src='images/snd_hardware.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'>&nbsp;".
246       " <input class='center' type='image' align='middle' 
247       src='images/power.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'>&nbsp;".
248       " <input class='center' type='image' align='middle' 
249       src='images/fai_template.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'>&nbsp;".
250       "</div>";
251     
252     $filter= $_SESSION['glpiDeviceRegex'];
253     $smarty->assign("devicehead", $listhead);
254     $smarty->assign("devices", $divlist->DrawList());
255     $smarty->assign("search_image", get_template_path('images/search.png'));
256     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
257     $smarty->assign("tree_image", get_template_path('images/tree.png'));
258     $smarty->assign("infoimage", get_template_path('images/info_small.png'));
259     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
260     $smarty->assign("apply", apply_filter());
261     $smarty->assign("alphabet", generate_alphabet());
262     $smarty->assign("device_regex", $filter['device_regex']);
264     $display.= $smarty->fetch(get_template_path('glpiDeviceManagement.tpl', TRUE));
265     return($display);
266   }
268   /* Save device to glpi database 
269    * If this is a new device, create a new entry, else update this entry 
270    */
271   function save()
272   {
273     if($this->parent->handle->deviceExists($this->EditEntry)){
274       $this->parent->handle->updateDevices($this->EditEntry);
275     }else{
276       $this->parent->handle->addDevice($this->EditEntry);
277     }
279   }
281   /* this only gets all already defined devices */
282   function reload()
283   {
284     $this->devices = $this->parent->handle->getDevices();
285     ksort($this->devices);
286   }
288   /* This funtions saves all POST variables.
289      The variable must be in the array $this->EditEntry 
290   */
291   function save_object()
292   {
293     foreach($_POST as $name => $value){
295       if(preg_match("/wasOnPage_/",$name)){
296         $dN = base64_decode($value);
298         $device = $this->devices[$dN];
299         $type   = $device['device_type'];  
300             
301         if(isset($device['designation'])){
302           $str = "designation";
303         }else{
304           $str = "name";
305         }
307         if(isset($_POST['useDevice_'.$value])){
308           $this->Selected[$type][$device[$str]] = $device;
309         }else{
310           if(isset($this->Selected[$type][$device[$str]])){
311             unset($this->Selected[$type][$device[$str]]);
312           }
313         }
314       }
315     }
317     if(is_array($this->EditEntry)){
318       foreach($this->EditEntry as $name => $value){
319         if(isset($_POST[$name])){
320           $this->EditEntry[$name] = $_POST[$name];
321         }
323       }
324     }
325   }
327   /* returns the selected devices */
328   function getSelected()
329   {
330     return($this->Selected); 
331   }
333   /* This function cehck all created devices if you wan't to create device specific check 
334       use >>if($attr['device_type']=="moboard")<< to create a device type depending check
335    */
336   function check($attr)
337   {
338     $message = array();
340     if(isset($attr['designation'])){
341       $str2 = "designation";
342     }else{
343       $str2 = "name";
344     }
345     if(empty($attr[$str2])){
346       $message[]=(_("You have to specify a valid name for this device."));
347     }
349     /* Avoid same name twice */
350     $devices = ($this->parent->handle->getDevices());
351     foreach($devices as $dev){
353       /* Some devices use designation some name for name
354        */
355       if(isset($dev['designation'])){
356         $str = "designation";
357       }else{
358         $str = "name";
359       }
361       if($dev[$str]==$attr[$str2]){
362         
363         /* Entries with ['ID'] already exists, and are only edited, if ID is missing we are currently creating a new entry */
364         if(isset($attr['ID'])){
365           if(!(($dev['ID'] == $attr['ID'])&&($dev['device_type']==$attr['device_type']))){
366             $message[] = _("This device name is already in use.");
367           }
368         }else{
369           $message[] = _("This device name is already in use.");
370         }
371       }
372     }
374     return($message);
375   }
377   /* This functions displays the template for all available devices 
378    * This function is also used if we create a new device
379    */
380   function editDevice($entry)
381   {
382     $smarty = get_smarty();
384     /* Transfer given data to smarty */
385     foreach($this->EditEntry as $name => $value){
386       $smarty->assign($name,htmlentities(utf8_decode($value)));
387     }
388   
389     /* Set default select boxes, manufacturers ... */
390     $smarty->assign("device_type",$entry['device_type']);
392     $none = array(0 => _("none"));
393     $manufacturer = array_merge($none,$this->parent->handle->getEnterprises());
395     $ramtypes = $this->parent->handle->getRAMTypes();
397     $smarty->assign("RAMtypes",     $ramtypes);
398     $smarty->assign("RAMtypeKeys",  array_flip($ramtypes));
400     $deviceControlTypes = array_merge($none,$this->parent->handle->getGlpiDeviceControlTypes());
402     $smarty->assign("HDDInterfaceKeys",array_flip($deviceControlTypes));
403     $smarty->assign("HDDInterfaces"   , $deviceControlTypes);
404     
405     $gfxControlTypes = array("0"=>_("None"),"AGP"=>"AGP","PCI"=>"PCI","PCI-X"=>"PCI-X","Other"=>_("Other")); 
407     $smarty->assign("GFXInterfaceKeys",array_flip($gfxControlTypes));
408     $smarty->assign("GFXInterfaces"   , $gfxControlTypes);
409     
410     $smarty->assign("FK_glpi_enterpriseKeys",array_flip($manufacturer));
411     $smarty->assign("FK_glpi_enterprises", $manufacturer);
413     $smarty->assign("formats",array("Large","Medium","Micro"));
414     $smarty->assign("formats",array("Large","Medium","Micro"));
416     $smarty->assign("formats",array("Large","Medium","Micro"));
417     $smarty->assign("formatKeys",array('Grand','Moyen','Micro'));
418     return($smarty->fetch(get_template_path('glpi_devices.tpl', TRUE)));
419   }
421 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
422 ?>