Code

Removed old CLI stuff - cleanup
[gosa.git] / plugins / admin / systems / 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(!isset($_SESSION['glpiDeviceRegex'])){
29       $tmp['device_regex'] = "*";
30       $_SESSION['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($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", sprintf(_("You're about to delete the glpi device '%s'."), $tmp['name']));
109       }elseif(isset($tmp['designation'])){
110         $smarty->assign("warning", sprintf(_("You're about to delete the glpi device '%s'."), $tmp['designation']));
111       }else{
112         print_red(_("Can't detect object name."));
113       }
114       
115       return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
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($this->EditEntry))==0){
158         $this->save($this->EditEntry);
159         $this->editMode = false;
160         $this->reload();
161       }else{
162         foreach($this->check($this->EditEntry) 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->SetEntriesPerPage(0);
180     $divlist->SetHeader(array(
181           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
182           array("string" => _("devices"), "attach" => "style=''"),
183           array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
185     $edit = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'>%s</a>";
186     $editdel = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
187     $editdel.= "<a href='?plug=".$_GET['plug']."&amp;act=del_device&amp;id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
188     
189     $useDevice = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useDevice_%s' %USE%>";
191     foreach($this->devices as $key=>$user){
193       if(isset($user['designation'])){
194         $str = "designation";
195       }else{
196         $str = "name";
197       }
199       if(isset($this->Selected[$user['device_type']][$user[$str]])){
200         $use = " checked ";
201       }else{
202         $use ="";
203       }
205       /* Dawn databse struckture ....*/
206       if(empty($user['comment']) && isset($user['comments'])) {
207         $user['comment'] = $user['comments'];
208       }
210       $field1 = array("string" => preg_replace("/%s/",base64_encode($key),preg_replace("/%USE%/",$use,$useDevice)), "attach" => "style='text-align:center;width:20px;'");
211       $field2 = array("string" => sprintf($edit,base64_encode($key),$user[$str]."&nbsp;[".$user['comment']."]"), "attach" => "style=''");
212       $field3 = array("string" => sprintf($editdel,base64_encode($key),base64_encode($key)), 
213         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
214       $divlist->AddEntry(array($field1,$field2,$field3));
215       
216     }
217     
218   $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
219       " <input class='center' type='image' align='middle' 
220       src='images/monitor.png'  title='"._("New monitor")."' alt='"._("M")."' name='new_monitor'>&nbsp;".
221       " <input class='center' type='image' align='middle' 
222       src='images/mainboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'>&nbsp;".
223       " <input class='center' type='image' align='middle' 
224       src='images/processor.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'>&nbsp;".
225       " <input class='center' type='image' align='middle' 
226       src='images/server.png' title='"._("New case")."' alt='"._("C")."' name='new_case'>&nbsp;".
227       " <input class='center' type='image' align='middle' 
228       src='images/net_hardware.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'>&nbsp;".
229       " <input class='center' type='image' align='middle' 
230       src='images/memory.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'>&nbsp;".
231       " <input class='center' type='image' align='middle' 
232       src='images/fai_partitionTable.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'>&nbsp;".
233       " <input class='center' type='image' align='middle' 
234       src='images/drives.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'>&nbsp;".
235       " <input class='center' type='image' align='middle' 
236       src='images/hardware.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'>&nbsp;".
237       " <input class='center' type='image' align='middle' 
238       src='images/gfx_hardware.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'>&nbsp;".
239       " <input class='center' type='image' align='middle' 
240       src='images/snd_hardware.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'>&nbsp;".
241       " <input class='center' type='image' align='middle' 
242       src='images/power.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'>&nbsp;".
243       " <input class='center' type='image' align='middle' 
244       src='images/fai_template.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'>&nbsp;".
245       "</div>";
246     
247     $filter= $_SESSION['glpiDeviceRegex'];
248     $smarty->assign("devicehead", $listhead);
249     $smarty->assign("devices", $divlist->DrawList());
250     $smarty->assign("search_image", get_template_path('images/search.png'));
251     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
252     $smarty->assign("tree_image", get_template_path('images/tree.png'));
253     $smarty->assign("infoimage", get_template_path('images/info_small.png'));
254     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
255     $smarty->assign("apply", apply_filter());
256     $smarty->assign("alphabet", generate_alphabet());
257     $smarty->assign("device_regex", $filter['device_regex']);
259     $display.= $smarty->fetch(get_template_path('glpiDeviceManagement.tpl', TRUE));
260     return($display);
261   }
263   /* Save device to glpi database 
264    * If this is a new device, create a new entry, else update this entry 
265    */
266   function save()
267   {
268     if($this->parent->handle->deviceExists($this->EditEntry)){
269       $this->parent->handle->updateDevices($this->EditEntry);
270     }else{
271       $this->parent->handle->addDevice($this->EditEntry);
272     }
274   }
276   /* this only gets all already defined devices */
277   function reload()
278   {
279     $this->devices = $this->parent->handle->getDevices();
280     ksort($this->devices);
281   }
283   /* This funtions saves all POST variables.
284      The variable must be in the array $this->EditEntry 
285   */
286   function save_object()
287   {
288     foreach($_POST as $name => $value){
290       if(preg_match("/wasOnPage_/",$name)){
291         $dN = base64_decode($value);
293         $device = $this->devices[$dN];
294         $type   = $device['device_type'];  
295             
296         if(isset($device['designation'])){
297           $str = "designation";
298         }else{
299           $str = "name";
300         }
302         if(isset($_POST['useDevice_'.$value])){
303           $this->Selected[$type][$device[$str]] = $device;
304         }else{
305           if(isset($this->Selected[$type][$device[$str]])){
306             unset($this->Selected[$type][$device[$str]]);
307           }
308         }
309       }
310     }
312     if(is_array($this->EditEntry)){
313       foreach($this->EditEntry as $name => $value){
314         if(isset($_POST[$name])){
315           $this->EditEntry[$name] = $_POST[$name];
316         }
318       }
319     }
320   }
322   /* returns the selected devices */
323   function getSelected()
324   {
325     return($this->Selected); 
326   }
328   /* This function cehck all created devices if you wan't to create device specific check 
329       use >>if($attr['device_type']=="moboard")<< to create a device type depending check
330    */
331   function check($attr)
332   {
333     /* Call common method to give check the hook */
334     $message= plugin::check();
336     if(isset($attr['designation'])){
337       $str2 = "designation";
338     }else{
339       $str2 = "name";
340     }
341     if(empty($attr[$str2])){
342       $message[]=(_("You have to specify a valid name for this device."));
343     }
345     /* Avoid same name twice */
346     $devices = ($this->parent->handle->getDevices());
347     foreach($devices as $dev){
349       /* Some devices use designation some name for name
350        */
351       if(isset($dev['designation'])){
352         $str = "designation";
353       }else{
354         $str = "name";
355       }
357       if($dev[$str]==$attr[$str2]){
358         
359         /* Entries with ['ID'] already exists, and are only edited, if ID is missing we are currently creating a new entry */
360         if(isset($attr['ID'])){
361           if(!(($dev['ID'] == $attr['ID'])&&($dev['device_type']==$attr['device_type']))){
362             $message[] = _("This device name is already in use.");
363           }
364         }else{
365           $message[] = _("This device name is already in use.");
366         }
367       }
368     }
370     return($message);
371   }
373   /* This functions displays the template for all available devices 
374    * This function is also used if we create a new device
375    */
376   function editDevice($entry)
377   {
378     $smarty = get_smarty();
380     /* Transfer given data to smarty */
381     foreach($this->EditEntry as $name => $value){
382       $smarty->assign($name,htmlentities(utf8_decode($value)));
383     }
384   
385     /* Set default select boxes, manufacturers ... */
386     $smarty->assign("device_type",$entry['device_type']);
388     $none = array(0 => _("none"));
389     $manufacturer = array_merge($none,$this->parent->handle->getEnterprises());
391     $ramtypes = $this->parent->handle->getRAMTypes();
393     $smarty->assign("RAMtypes",     $ramtypes);
394     $smarty->assign("RAMtypeKeys",  array_flip($ramtypes));
396     $deviceControlTypes = array_merge($none,$this->parent->handle->getGlpiDeviceControlTypes());
398     $smarty->assign("HDDInterfaceKeys",array_flip($deviceControlTypes));
399     $smarty->assign("HDDInterfaces"   , $deviceControlTypes);
400     
401     $gfxControlTypes = array("0"=>_("None"),"AGP"=>"AGP","PCI"=>"PCI","PCI-X"=>"PCI-X","Other"=>_("Other")); 
403     $smarty->assign("GFXInterfaceKeys",array_flip($gfxControlTypes));
404     $smarty->assign("GFXInterfaces"   , $gfxControlTypes);
405     
406     $smarty->assign("FK_glpi_enterpriseKeys",array_flip($manufacturer));
407     $smarty->assign("FK_glpi_enterprises", $manufacturer);
409     $smarty->assign("formats",array("Large","Medium","Micro"));
410     $smarty->assign("formats",array("Large","Medium","Micro"));
412     $smarty->assign("formats",array("Large","Medium","Micro"));
413     $smarty->assign("formatKeys",array('Grand','Moyen','Micro'));
414     return($smarty->fetch(get_template_path('glpi_devices.tpl', TRUE)));
415   }
417 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
418 ?>