Code

e3a5497bc2f6a429ea1a62c3d565d22c170303f9
[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($this->parent->handle->is_deviceUsed($this->delete)){
126         print_red(_("Can't delete this device, it is actually in use by some glpi accounts. Remove this relation first."));
127       }else{
128         $this->parent->handle->deleteDevice($this->delete);
129         $this->reload();
130       }
131     }
133     /* Open entry for editing if requested */
134     if((isset($_GET['act']))&&($_GET['act']=="edit_device")){
135       $id = base64_decode($_GET['id']);
136       $this->editMode =true;
137       $this->EditEntry = $this->devices[$id];
138     }
140     /* Abort editing this entry */
141     if(isset($_POST['AbortDeviceChanges'])){
142       $this->EditEntry = array();
143       $this->editMode = false;
144     }
146     /* Save all changes made on currently selected entry */
147     if(isset($_POST['SaveDeviceChanges'])){
148     
149       /* First check if all changes made are allowed */
150       if(count($this->check($this->EditEntry))==0){
151         $this->save($this->EditEntry);
152         $this->editMode = false;
153         $this->reload();
154       }else{
155         foreach($this->check($this->EditEntry) as $msg){
156           print_red($msg);
157         }
158       }
159       
160     }
162     /* Check if we are currently editing something ? */
163     if($this->editMode == true){
164       return ($this->editDevice($this->EditEntry));
165     }
167     /*  ENDE :  GET / POST handling 
168      *  Below, only output generation for headpage
169      */
171     $divlist = new divlist("glpi devices");
172     $divlist->SetEntriesPerPage(0);
173     $divlist->SetHeader(array(
174           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
175           array("string" => _("devices")." / "._("Departments"), "attach" => "style=''"),
176           array("string" => _("Use"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
178     $edit = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'>%s</a>";
179     $editdel = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
180     $editdel.= "<a href='?plug=".$_GET['plug']."&amp;act=del_device&amp;id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
181     
182     $useDevice = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useDevice_%s' %USE%>";
184     foreach($this->devices as $key=>$user){
186       if(isset($user['designation'])){
187         $str = "designation";
188       }else{
189         $str = "name";
190       }
192       if(isset($this->Selected[$user['device_type']][$user[$str]])){
193         $use = " checked ";
194       }else{
195         $use ="";
196       }
198       /* Dawn databse struckture ....*/
199       if(empty($user['comment'])) {
200         $user['comment'] = $user['comments'];
201       }
203       $field1 = array("string" => preg_replace("/%s/",base64_encode($key),preg_replace("/%USE%/",$use,$useDevice)), "attach" => "style='text-align:center;width:20px;'");
204       $field2 = array("string" => sprintf($edit,base64_encode($key),$user[$str]."&nbsp;[".$user['comment']."]"), "attach" => "style=''");
205       $field3 = array("string" => sprintf($editdel,base64_encode($key),base64_encode($key)), 
206         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
207       $divlist->AddEntry(array($field1,$field2,$field3));
208       
209     }
210     
211   $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
212       " <input class='center' type='image' align='middle' 
213       src='images/monitor.png'  title='"._("New monitor")."' alt='"._("M")."' name='new_monitor'>&nbsp;".
214       " <input class='center' type='image' align='middle' 
215       src='images/keyboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'>&nbsp;".
216       " <input class='center' type='image' align='middle' 
217       src='mages/list_back.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'>&nbsp;".
218       " <input class='center' type='image' align='middle' 
219       src='mages/list_back.png' title='"._("New case")."' alt='"._("C")."' name='new_case'>&nbsp;".
220       " <input class='center' type='image' align='middle' 
221       src='mages/list_back.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'>&nbsp;".
222       " <input class='center' type='image' align='middle' 
223       src='mages/list_back.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'>&nbsp;".
224       " <input class='center' type='image' align='middle' 
225       src='mages/list_back.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'>&nbsp;".
226       " <input class='center' type='image' align='middle' 
227       src='mages/list_back.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'>&nbsp;".
228       " <input class='center' type='image' align='middle' 
229       src='mages/list_back.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'>&nbsp;".
230       " <input class='center' type='image' align='middle' 
231       src='mages/list_back.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'>&nbsp;".
232       " <input class='center' type='image' align='middle' 
233       src='mages/list_back.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'>&nbsp;".
234       " <input class='center' type='image' align='middle' 
235       src='mages/list_back.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'>&nbsp;".
236       " <input class='center' type='image' align='middle' 
237       src='mages/list_back.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'>&nbsp;".
238       "</div>";
239     
240     $filter= $_SESSION['glpiDeviceRegex'];
241     $smarty->assign("devicehead", $listhead);
242     $smarty->assign("devices", $divlist->DrawList());
243     $smarty->assign("search_image", get_template_path('images/search.png'));
244     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
245     $smarty->assign("tree_image", get_template_path('images/tree.png'));
246     $smarty->assign("infoimage", get_template_path('images/info.png'));
247     $smarty->assign("launchimage", get_template_path('images/launch.png'));
248     $smarty->assign("apply", apply_filter());
249     $smarty->assign("alphabet", generate_alphabet());
250     $smarty->assign("device_regex", $filter['device_regex']);
252     $display.= $smarty->fetch(get_template_path('glpiDeviceManagement.tpl', TRUE));
253     return($display);
254   }
256   /* Save device to glpi database 
257    * If this is a new device, create a new entry, else update this entry 
258    */
259   function save()
260   {
261     if($this->parent->handle->deviceExists($this->EditEntry)){
262       $this->parent->handle->updateDevices($this->EditEntry);
263     }else{
264       $this->parent->handle->addDevice($this->EditEntry);
265     }
267   }
269   /* this only gets all already defined devices */
270   function reload()
271   {
272     $this->devices = $this->parent->handle->getDevices();
273     ksort($this->devices);
274   }
276   /* This funtions saves all POST variables.
277      The variable must be in the array $this->EditEntry 
278   */
279   function save_object()
280   {
281     foreach($_POST as $name => $value){
283       if(preg_match("/wasOnPage_/",$name)){
284         $dN = base64_decode($value);
286         $device = $this->devices[$dN];
287         $type   = $device['device_type'];  
288             
289         if(isset($device['designation'])){
290           $str = "designation";
291         }else{
292           $str = "name";
293         }
295         if(isset($_POST['useDevice_'.$value])){
296           $this->Selected[$type][$device[$str]] = $device;
297         }else{
298           if(isset($this->Selected[$type][$device[$str]])){
299             unset($this->Selected[$type][$device[$str]]);
300           }
301         }
302       }
303     }
305     if(is_array($this->EditEntry)){
306       foreach($this->EditEntry as $name => $value){
307         if(isset($_POST[$name])){
308           $this->EditEntry[$name] = $_POST[$name];
309         }
311       }
312     }
313   }
315   /* returns the selected devices */
316   function getSelected()
317   {
318     return($this->Selected); 
319   }
321   /* This function cehck all created devices if you wan't to create device specific check 
322       use >>if($attr['device_type']=="moboard")<< to create a device type depending check
323    */
324   function check($attr)
325   {
326     $message = array();
328     if(isset($attr['designation'])){
329       $str2 = "designation";
330     }else{
331       $str2 = "name";
332     }
333     if(empty($attr[$str2])){
334       $message[]=(_("You have to specify a valid name for this device."));
335     }
337     /* Avoid same name twice */
338     $devices = ($this->parent->handle->getDevices());
339     foreach($devices as $dev){
341       /* Some devices use designation some name for name
342        */
343       if(isset($dev['designation'])){
344         $str = "designation";
345       }else{
346         $str = "name";
347       }
349       if($dev[$str]==$attr[$str2]){
350         
351         /* Entries with ['ID'] already exists, and are only edited, if ID is missing we are currently creating a new entry */
352         if(isset($attr['ID'])){
353           if(!(($dev['ID'] == $attr['ID'])&&($dev['device_type']==$attr['device_type']))){
354             $message[] = _("This device name is already in use.");
355           }
356         }else{
357           $message[] = _("This device name is already in use.");
358         }
359       }
360     }
362     return($message);
363   }
365   /* This functions displays the template for all available devices 
366    * This function is also used if we create a new device
367    */
368   function editDevice($entry)
369   {
370     $smarty = get_smarty();
372     /* Transfer given data to smarty */
373     foreach($this->EditEntry as $name => $value){
374       $smarty->assign($name,htmlentities(utf8_decode($value)));
375     }
376   
377     /* Set default select boxes, manufacturers ... */
378     $smarty->assign("device_type",$entry['device_type']);
380     $none = array(0 => _("none"));
381     $manufacturer = array_merge($none,$this->parent->handle->getEnterprises());
383     $ramtypes = $this->parent->handle->getRAMTypes();
385     $smarty->assign("RAMtypes",     $ramtypes);
386     $smarty->assign("RAMtypeKeys",  array_flip($ramtypes));
388     $deviceControlTypes = array_merge($none,$this->parent->handle->getGlpiDeviceControlTypes());
390     $smarty->assign("HDDInterfaceKeys",array_flip($deviceControlTypes));
391     $smarty->assign("HDDInterfaces"   , $deviceControlTypes);
392     
393     $gfxControlTypes = array("0"=>_("None"),"AGP"=>"AGP","PCI"=>"PCI","PCI-X"=>"PCI-X","Other"=>_("Other")); 
395     $smarty->assign("GFXInterfaceKeys",array_flip($gfxControlTypes));
396     $smarty->assign("GFXInterfaces"   , $gfxControlTypes);
397     
398     $smarty->assign("FK_glpi_enterpriseKeys",array_flip($manufacturer));
399     $smarty->assign("FK_glpi_enterprises", $manufacturer);
401     $smarty->assign("formats",array("Large","Medium","Micro"));
402     $smarty->assign("formats",array("Large","Medium","Micro"));
404     $smarty->assign("formats",array("Large","Medium","Micro"));
405     $smarty->assign("formatKeys",array('Grand','Moyen','Micro'));
406     return($smarty->fetch(get_template_path('glpi_devices.tpl', TRUE)));
407   }
409 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
410 ?>