Code

a5153c05f64bb0847e9986011b7bae7a2f5f3ad1
[gosa.git] / gosa-plugins / glpi / admin / systems / services / glpi / class_glpiDeviceManagement.inc
1 <?php
3 class glpiDeviceManagement extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes= array();
8   var $objectclasses= array("whatever");
10   var $devices = array();
11   var $ui;
13   var $parent   ;
14   var $EditEntry;
15   var $editMode =false;
17   var $DeviceAttrs = array();
18   var $AllowedDevices = array();
20   var $Selected = array();
22   var $delete = array();
24   function glpiDeviceManagement (&$config, $dn= NULL,$used=NULL)
25   {
26     plugin::plugin ($config, $dn);
28     if(!session::is_set('glpiDeviceRegex')){
29       $tmp['device_regex'] = "*";
30       session::set('glpiDeviceRegex',$tmp);
31     }
32     $this->ui = get_userinfo();  
34     if(isset($used)){
35     $this->Selected = $used;
36     }
37  
38     /* Specify which vars are allowed for a sepcific type of device */ 
39     $this->DeviceAttrs['case']      = array("designation","format","comment","FK_glpi_enterprise");
40     $this->DeviceAttrs['moboard']   = array("designation","chipset","comment","FK_glpi_enterprise");
41     $this->DeviceAttrs['processor'] = array("designation","frequence","comment","FK_glpi_enterprise","specif_default");
42     $this->DeviceAttrs['iface']     = array("designation","bandwidth","comment","FK_glpi_enterprise","specif_default");
43     $this->DeviceAttrs['ram']       = array("designation","frequence","comment","FK_glpi_enterprise","specif_default","type");
44     $this->DeviceAttrs['hdd']       = array("designation","rpm","interface","cache","comment","FK_glpi_enterprise","specif_default");
45     $this->DeviceAttrs['drive']     = array("designation","speed","interface","is_writer","comment","FK_glpi_enterprise","specif_default");
46     $this->DeviceAttrs['control']   = array("designation","interface","raid","comment","FK_glpi_enterprise");
47     $this->DeviceAttrs['gfxcard']   = array("designation","ram","interface","comment","FK_glpi_enterprise","specif_default");
48     $this->DeviceAttrs['sndcard']   = array("designation","type","comment","FK_glpi_enterprise","specif_default");
49     $this->DeviceAttrs['power']     = array("designation","power","comment","FK_glpi_enterprise","atx");
50     $this->DeviceAttrs['pci']       = array("designation","comment","FK_glpi_enterprise","specif_default");
51     $this->DeviceAttrs['monitor']   = array("name","comments","serial","otherserial","size",
52                                             "flags_micro","flags_speaker","flags_subd","flags_bnc",
53                                             "location","type","FK_glpi_enterprise","is_global","deleted","is_template","tplname");
55     $this->AllowedDevices=array("case","moboard","sndcard","processor","iface","ram","hdd","drive","control","gfxcard","power","pci","monitor");
56   }
58   function execute()
59   {
60     /* Call parent execute */
61     plugin::execute();
63     /* Get all defined devices */
64     $this->reload();
66     /* Fill templating stuff */
67     $smarty= get_smarty();
68     $display= "";
70     /* this var is used to ensure that every post is only performed once */
71     $only_once = true;
73     /* Check Post for some actions */
74     foreach($_POST as $name => $value){
75       
76       /* If new_ then we should create an new device */
77       if((preg_match("/^new_/",$name))&&($only_once)){
78     
79         /* don't do this twice */
80         $only_once = false;
82         /*extract device device_type */
83         $deviceType = preg_replace("/_.*$/","",preg_replace("/^new_/","",$name));
85         /* Check if type is allowed, and create empty entry */
86         $tmp = array();
87         if((!isset($this->DeviceAttrs[$deviceType]))||((!in_array_strict($deviceType,$this->AllowedDevices)))){
88           print_red(sprintf(_("Internal Error can't create device of type '%s'"),$deviceType));
89         }else{
90           foreach($this->DeviceAttrs[$deviceType] as $attr){
91             $tmp[$attr] = "";
92           }
93           $tmp['device_type'] = $deviceType;
94           $this->EditEntry = $tmp;
95           $this->editMode =true;
96         }
97       }
98     }
100     /* delete was requested ... show dialog   */
101     if((isset($_GET['act']))&&($_GET['act']=="del_device")){
102       $id = base64_decode($_GET['id']);
103       $tmp = $this->devices[$id];
105       $this->delete = $tmp;
107       if(isset($tmp['name'])){
108         $smarty->assign("warning",msgPool::deleteInfo($tmp['name'],_("glpi device")));
109       }elseif(isset($tmp['designation'])){
110         $smarty->assign("warning",msgPool::deleteInfo($tmp['designation'],_("glpi device")));
111       }else{
112         print_red(_("Can't detect object name."));
113       }
114       
115       return($smarty->fetch(get_template_path('remove_glpi.tpl',TRUE,dirname(__FILE__))));
116     }
118     /* Delete entry, but check if this device is in currently in use */
119     if(isset($_POST['delete_glpi_confirm'])){
120       if(count($this->parent->handle->is_deviceUsed($this->delete))){
122         $tmp = $this->parent->handle->is_deviceUsed($this->delete);    
123   
124         $names = "";
125         foreach($tmp as $name){
126           $names .= ", ".$name;
127         }
128         $names = preg_replace("/^, /","",$names);
129         $names = trim($names);
130         if(count($tmp) == 3){
131           $names .= " ...";
132         }
133         print_red(sprintf(_("You can't delete this device, it is still in use by these system(s) '%s'"),$names));
134       }else{
135         $this->parent->handle->deleteDevice($this->delete);
136         $this->reload();
137       }
138     }
140     /* Open entry for editing if requested */
141     if((isset($_GET['act']))&&($_GET['act']=="edit_device")){
142       $id = base64_decode($_GET['id']);
143       $this->editMode =true;
144       $this->EditEntry = $this->devices[$id];
145     }
147     /* Abort editing this entry */
148     if(isset($_POST['AbortDeviceChanges'])){
149       $this->EditEntry = array();
150       $this->editMode = false;
151     }
153     /* Save all changes made on currently selected entry */
154     if(isset($_POST['SaveDeviceChanges'])){
155     
156       /* First check if all changes made are allowed */
157       if(count($this->check())==0){
158         $this->save();
159         $this->editMode = false;
160         $this->reload();
161       }else{
162         foreach($this->check() as $msg){
163           print_red($msg);
164         }
165       }
166       
167     }
169     /* Check if we are currently editing something ? */
170     if($this->editMode == true){
171       return ($this->editDevice($this->EditEntry));
172     }
174     /*  ENDE :  GET / POST handling 
175      *  Below, only output generation for headpage
176      */
178     $divlist = new divlist("glpi devices");
179     $divlist->SetPluginMode();
180     $divlist->SetEntriesPerPage(0);
181     $divlist->SetHeader(array(
182           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
183           array("string" => _("devices"), "attach" => "style=''"),
184           array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
186     $edit = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'>%s</a>";
187     $editdel = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'><img src='plugins/glpi/images/lists/edit.png' alt='"._("Edit")."' border=0></a>";
188     $editdel.= "<a href='?plug=".$_GET['plug']."&amp;act=del_device&amp;id=%s'><img src='plugins/glpi/images/lists/trash.png' alt='"._("Delete")."' border=0></a>";
189     
190     $useDevice = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useDevice_%s' %USE%>";
192     foreach($this->devices as $key=>$user){
194       if(isset($user['designation'])){
195         $str = "designation";
196       }else{
197         $str = "name";
198       }
200       if(isset($this->Selected[$user['device_type']][$user[$str]])){
201         $use = " checked ";
202       }else{
203         $use ="";
204       }
206       /* Dawn databse struckture ....*/
207       if(empty($user['comment']) && isset($user['comments'])) {
208         $user['comment'] = $user['comments'];
209       }
211       $field1 = array("string" => preg_replace("/%s/",base64_encode($key),preg_replace("/%USE%/",$use,$useDevice)), "attach" => "style='text-align:center;width:20px;'");
212       $field2 = array("string" => sprintf($edit,base64_encode($key),$user[$str]."&nbsp;[".$user['comment']."]"), "attach" => "style=''");
213       $field3 = array("string" => sprintf($editdel,base64_encode($key),base64_encode($key)), 
214         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
215       $divlist->AddEntry(array($field1,$field2,$field3));
216       
217     }
218     
219   $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
220       " <input class='center' type='image' align='middle' 
221       src='plugins/glpi/images/monitor.png'  title='"._("New monitor")."' alt='"._("M")."' name='new_monitor'>&nbsp;".
222       " <input class='center' type='image' align='middle' 
223       src='plugins/glpi/images/mainboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'>&nbsp;".
224       " <input class='center' type='image' align='middle' 
225       src='plugins/glpi/images/processor.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'>&nbsp;".
226       " <input class='center' type='image' align='middle' 
227       src='plugins/glpi/images/server.png' title='"._("New case")."' alt='"._("C")."' name='new_case'>&nbsp;".
228       " <input class='center' type='image' align='middle' 
229       src='plugins/glpi/images/net_hardware.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'>&nbsp;".
230       " <input class='center' type='image' align='middle' 
231       src='plugins/glpi/images/memory.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'>&nbsp;".
232       " <input class='center' type='image' align='middle' 
233       src='plugins/glpi/images/harddisk.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'>&nbsp;".
234       " <input class='center' type='image' align='middle' 
235       src='plugins/glpi/images/drives.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'>&nbsp;".
236       " <input class='center' type='image' align='middle' 
237       src='plugins/glpi/images/hardware.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'>&nbsp;".
238       " <input class='center' type='image' align='middle' 
239       src='plugins/glpi/images/gfx_hardware.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'>&nbsp;".
240       " <input class='center' type='image' align='middle' 
241       src='plugins/glpi/images/snd_hardware.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'>&nbsp;".
242       " <input class='center' type='image' align='middle' 
243       src='plugins/glpi/images/power.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'>&nbsp;".
244       " <input class='center' type='image' align='middle' 
245       src='plugins/glpi/images/glpi_device.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'>&nbsp;".
246       "</div>";
247     
248     $filter = session::get('glpiDeviceRegex');
249     $smarty->assign("devicehead", $listhead);
250     $smarty->assign("devices", $divlist->DrawList());
251     $smarty->assign("search_image", get_template_path('images/lists/search.png'));
252     $smarty->assign("searchu_image", get_template_path('images/lists/search-user.png'));
253     $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
254     $smarty->assign("infoimage", get_template_path('images/info_small.png'));
255     $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
256     $smarty->assign("apply", apply_filter());
257     $smarty->assign("alphabet", generate_alphabet());
258     $smarty->assign("device_regex", $filter['device_regex']);
260     $display.= $smarty->fetch(get_template_path('glpiDeviceManagement.tpl',TRUE,dirname(__FILE__)));
261     return($display);
262   }
264   /* Save device to glpi database 
265    * If this is a new device, create a new entry, else update this entry 
266    */
267   function save()
268   {
269     if($this->parent->handle->deviceExists($this->EditEntry)){
270       $this->parent->handle->updateDevices($this->EditEntry);
271     }else{
272       $this->parent->handle->addDevice($this->EditEntry);
273     }
275   }
277   /* this only gets all already defined devices */
278   function reload()
279   {
280     $this->devices = $this->parent->handle->getDevices();
281     ksort($this->devices);
282   }
284   /* This funtions saves all POST variables.
285      The variable must be in the array $this->EditEntry 
286   */
287   function save_object()
288   {
289     foreach($_POST as $name => $value){
291       if(preg_match("/wasOnPage_/",$name)){
292         $dN = base64_decode($value);
294         $device = $this->devices[$dN];
295         $type   = $device['device_type'];  
296             
297         if(isset($device['designation'])){
298           $str = "designation";
299         }else{
300           $str = "name";
301         }
303         if(isset($_POST['useDevice_'.$value])){
304           $this->Selected[$type][$device[$str]] = $device;
305         }else{
306           if(isset($this->Selected[$type][$device[$str]])){
307             unset($this->Selected[$type][$device[$str]]);
308           }
309         }
310       }
311     }
313     if(is_array($this->EditEntry)){
314       foreach($this->EditEntry as $name => $value){
315         if(isset($_POST[$name])){
316           $this->EditEntry[$name] = $_POST[$name];
317         }
319       }
320     }
321   }
323   /* returns the selected devices */
324   function getSelected()
325   {
326     return($this->Selected); 
327   }
329   /* This function checks all created devices.
330      If you wan't to use device specific checks, 
331         use >>if($attr['device_type']=="moboard")<< to create a device type depending check
332    */
333   function check()
334   {
335     /* Call common method to give check the hook */
336     $message= plugin::check();
338     $attr = $this->EditEntry;
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,dirname(__FILE__))));
419   }
421 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
422 ?>