Code

Added some comments
[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   function glpiDeviceManagement ($config, $dn= NULL,$used=NULL)
28   {
29     plugin::plugin ($config, $dn);
31     if(!isset($_SESSION['glpiDeviceRegex'])){
32       $tmp['device_regex'] = "*";
33       $_SESSION['glpiDeviceRegex'] = $tmp;
34     }
35     $this->ui = get_userinfo();  
37     if(isset($used)){
38     $this->Selected = $used;
39     }
40  
41     /* Specify which vars are allowed for a sepcific type of device */ 
42     $this->DeviceAttrs['case']      = array("designation","format","comment","FK_glpi_enterprise");
43     $this->DeviceAttrs['moboard']   = array("designation","chipset","comment","FK_glpi_enterprise");
44     $this->DeviceAttrs['processor'] = array("designation","frequence","comment","FK_glpi_enterprise","specif_default");
45     $this->DeviceAttrs['iface']     = array("designation","bandwidth","comment","FK_glpi_enterprise","specif_default");
46     $this->DeviceAttrs['ram']       = array("designation","frequence","comment","FK_glpi_enterprise","specif_default","type");
47     $this->DeviceAttrs['hdd']       = array("designation","rpm","interface","cache","comment","FK_glpi_enterprise","specif_default");
48     $this->DeviceAttrs['drive']     = array("designation","speed","interface","is_writer","comment","FK_glpi_enterprise","specif_default");
49     $this->DeviceAttrs['control']   = array("designation","interface","raid","comment","FK_glpi_enterprise");
50     $this->DeviceAttrs['gfxcard']   = array("designation","ram","interface","comment","FK_glpi_enterprise","specif_default");
51     $this->DeviceAttrs['sndcard']   = array("designation","type","comment","FK_glpi_enterprise","specif_default");
52     $this->DeviceAttrs['power']     = array("designation","power","comment","FK_glpi_enterprise","atx");
53     $this->DeviceAttrs['pci']       = array("designation","comment","FK_glpi_enterprise","specif_default");
54     $this->DeviceAttrs['monitor']   = array("name","comments","serial","otherserial","size",
55                                             "flags_micro","flags_speaker","flags_subd","flags_bnc",
56                                             "location","type","FK_glpi_enterprise","is_global","deleted","is_template","tplname");
58     $this->AllowedDevices=array("case","moboard","sndcard","processor","iface","ram","hdd","drive","control","gfxcard","power","pci","monitor");
59   }
61   function execute()
62   {
63     /* Call parent execute */
64     plugin::execute();
66     /* Get all defined devices */
67     $this->reload();
69     /* Fill templating stuff */
70     $smarty= get_smarty();
71     $display= "";
73     /* Check if we should create a new entry .... */
74     $only_once = true;
75     foreach($_POST as $name => $value){
76       
77       /* All Post vars starts with new */
78       if((preg_match("/^new_/",$name))&&($only_once)){
79     
80         /* do this twice */
81         $only_once = false;
83         /*extract device device_type */
84         $deviceType = preg_replace("/_.*$/","",preg_replace("/^new_/","",$name));
86         /* Check if type is allowed, and create empty entry */
87         $tmp = array();
88         if((!isset($this->DeviceAttrs[$deviceType]))||((!in_array($deviceType,$this->AllowedDevices)))){
89           print_red(sprintf(_("Internal Error can't create device of type '%s'"),$deviceType));
90         }else{
91           foreach($this->DeviceAttrs[$deviceType] as $attr){
92             $tmp[$attr] = "";
93           }
94           $tmp['device_type'] = $deviceType;
95           $this->EditEntry = $tmp;
96           $this->editMode =true;
97         }
98       }
99     }
101     /* Open entry for editing if requested */
102     if((isset($_GET['act']))&&($_GET['act']=="del_device")){
103       $id = base64_decode($_GET['id']);
104       $tmp = $this->devices[$id];
105       $this->parent->handle->deleteDevice($tmp);
106       $this->reload();
107     }
109     /* Open entry for editing if requested */
110     if((isset($_GET['act']))&&($_GET['act']=="edit_device")){
111       $id = base64_decode($_GET['id']);
112       $this->editMode =true;
113       $this->EditEntry = $this->devices[$id];
114     }
116     /* Abort editing this entry */
117     if(isset($_POST['AbortDeviceChanges'])){
118       $this->EditEntry = array();
119       $this->editMode = false;
120     }
122     /* Save all changes made on currently selected entry */
123     if(isset($_POST['SaveDeviceChanges'])){
124     
125       /* First check if all changes made are allowed */
126       if(count($this->check($this->EditEntry))==0){
127         $this->save($this->EditEntry);
128         $this->editMode = false;
129         $this->reload();
130       }else{
131         foreach($this->check($this->EditEntry) as $msg){
132           print_red($msg);
133         }
134       }
135       
136     }
138     /* Check if we are currently editing something ? */
139     if($this->editMode == true){
140       return ($this->editDevice($this->EditEntry));
141     }
143     /*  ENDE :  GET / POST handling 
144      *  Below, only output generation for headpage
145      */
147     $divlist = new divlist("glpi devices");
148     $divlist->SetEntriesPerPage(0);
149     $divlist->SetHeader(array(
150           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
151           array("string" => _("devices")." / "._("Departments"), "attach" => "style=''"),
152           array("string" => _("Use"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
154     $edit = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'>%s</a>";
155     $editdel = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
156     $editdel.= "<a href='?plug=".$_GET['plug']."&amp;act=del_device&amp;id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
157     
158     $useDevice = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useDevice_%s' %USE%>";
160     foreach($this->devices as $key=>$user){
162       if(isset($user['designation'])){
163         $str = "designation";
164       }else{
165         $str = "name";
166       }
168       if(isset($this->Selected[$user['device_type']][$user[$str]])){
169         $use = " checked ";
170       }else{
171         $use ="";
172       }
174       /* Dawn databse struckture ....*/
175       if(empty($user['comment'])) {
176         $user['comment'] = $user['comments'];
177       }
179       $field1 = array("string" => preg_replace("/%s/",base64_encode($key),preg_replace("/%USE%/",$use,$useDevice)), "attach" => "style='text-align:center;width:20px;'");
180       $field2 = array("string" => sprintf($edit,base64_encode($key),$user[$str]."&nbsp;[".$user['comment']."]"), "attach" => "style=''");
181       $field3 = array("string" => sprintf($editdel,base64_encode($key),base64_encode($key)), 
182         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
183       $divlist->AddEntry(array($field1,$field2,$field3));
184       
185     }
186     
187   $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
188       " <input class='center' type='image' align='middle' 
189       src='images/monitor.png'  title='"._("New monitor")."' alt='"._("M")."' name='new_monitor'>&nbsp;".
190       " <input class='center' type='image' align='middle' 
191       src='images/keyboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'>&nbsp;".
192       " <input class='center' type='image' align='middle' 
193       src='mages/list_back.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'>&nbsp;".
194       " <input class='center' type='image' align='middle' 
195       src='mages/list_back.png' title='"._("New case")."' alt='"._("C")."' name='new_case'>&nbsp;".
196       " <input class='center' type='image' align='middle' 
197       src='mages/list_back.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'>&nbsp;".
198       " <input class='center' type='image' align='middle' 
199       src='mages/list_back.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'>&nbsp;".
200       " <input class='center' type='image' align='middle' 
201       src='mages/list_back.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'>&nbsp;".
202       " <input class='center' type='image' align='middle' 
203       src='mages/list_back.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'>&nbsp;".
204       " <input class='center' type='image' align='middle' 
205       src='mages/list_back.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'>&nbsp;".
206       " <input class='center' type='image' align='middle' 
207       src='mages/list_back.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'>&nbsp;".
208       " <input class='center' type='image' align='middle' 
209       src='mages/list_back.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'>&nbsp;".
210       " <input class='center' type='image' align='middle' 
211       src='mages/list_back.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'>&nbsp;".
212       " <input class='center' type='image' align='middle' 
213       src='mages/list_back.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'>&nbsp;".
214       "</div>";
215     
216     $filter= $_SESSION['glpiDeviceRegex'];
217     $smarty->assign("devicehead", $listhead);
218     $smarty->assign("devices", $divlist->DrawList());
219     $smarty->assign("search_image", get_template_path('images/search.png'));
220     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
221     $smarty->assign("tree_image", get_template_path('images/tree.png'));
222     $smarty->assign("infoimage", get_template_path('images/info.png'));
223     $smarty->assign("launchimage", get_template_path('images/launch.png'));
224     $smarty->assign("apply", apply_filter());
225     $smarty->assign("alphabet", generate_alphabet());
226     $smarty->assign("device_regex", $filter['device_regex']);
228     $display.= $smarty->fetch(get_template_path('glpiDeviceManagement.tpl', TRUE));
229     return($display);
230   }
232   /* Save device to glpi database 
233    * If this is a new device, create a new entry, else update this entry 
234    */
235   function save()
236   {
237     if($this->parent->handle->deviceExists($this->EditEntry)){
238       $this->parent->handle->updateDevices($this->EditEntry);
239     }else{
240       $this->parent->handle->addDevice($this->EditEntry);
241     }
243   }
245   /* this only gets all already defined devices */
246   function reload()
247   {
248     $this->devices = $this->parent->handle->getDevices();
249     ksort($this->devices);
250   }
252   /* This funtions saves all POST variables.
253      The variable must be in the array $this->EditEntry 
254   */
255   function save_object()
256   {
257     foreach($_POST as $name => $value){
259       if(preg_match("/wasOnPage_/",$name)){
260         $dN = base64_decode($value);
262         $device = $this->devices[$dN];
263         $type   = $device['device_type'];  
264             
265         if(isset($device['designation'])){
266           $str = "designation";
267         }else{
268           $str = "name";
269         }
271         if(isset($_POST['useDevice_'.$value])){
272           $this->Selected[$type][$device[$str]] = $device;
273         }else{
274           if(isset($this->Selected[$type][$device[$str]])){
275             unset($this->Selected[$type][$device[$str]]);
276           }
277         }
278       }
279     }
281     if(is_array($this->EditEntry)){
282       foreach($this->EditEntry as $name => $value){
283         if(isset($_POST[$name])){
284           $this->EditEntry[$name] = $_POST[$name];
285         }
287       }
288     }
289   }
291   /* returns the selected devices */
292   function getSelected()
293   {
294     return($this->Selected); 
295   }
297   /* This function cehck all created devices if you wan't to create device specific check 
298       use >>if($attr['device_type']=="moboard")<< to create a device type depending check
299   */
300   function check($attr)
301   {
302     $message = array();
303     
304     if(isset($attr['designation'])){
305       $str2 = "designation";
306     }else{
307       $str2 = "name";
308     }
309     if(empty($attr[$str2])){
310       $message[]=(_("You have to specify a valid name for this device."));
311     }
313     /* Avoid same name twice */
314     $devices = ($this->parent->handle->getDevices());
315     foreach($devices as $dev){
316       if(isset($dev['designation'])){
317         $str = "designation";
318       }else{
319         $str = "name";
320       }
322       if($dev[$str]==$attr[$str2]){
324         /* Entries with ['ID'] already exists, and are only edited, if ID is missing we are currently creating a new entry */
325         if(isset($attr['ID'])){
326           if(!(($dev['ID'] == $attr['ID'])&&($dev['device_type']==$attr['device_type']))){
327             $message[] = _("This device name is already in use.");
328           }
329         }else{
330           $message[] = _("This device name is already in use.");
331         }
332       }
333     }
335     return($message);
336   }
338   /* This functions displays the template for all available devices 
339    * This function is also used if we create a new device
340    */
341   function editDevice($entry)
342   {
343     $smarty = get_smarty();
345     /* Transfer given data to smarty */
346     foreach($this->EditEntry as $name => $value){
347       $smarty->assign($name,$value);
348     }
349   
350     /* Set default select boxes, manufacturers ... */
351     $smarty->assign("device_type",$entry['device_type']);
353     $none = array(0 => _("none"));
354     $manufacturer = array_merge($none,$this->parent->handle->getEnterprises());
356     $ramtypes = $this->parent->handle->getRAMTypes();
358     $smarty->assign("RAMtypes",     $ramtypes);
359     $smarty->assign("RAMtypeKeys",  array_flip($ramtypes));
361     $deviceControlTypes = array_merge($none,$this->parent->handle->getGlpiDeviceControlTypes());
363     $smarty->assign("HDDInterfaceKeys",array_flip($deviceControlTypes));
364     $smarty->assign("HDDInterfaces"   , $deviceControlTypes);
365     
366     $gfxControlTypes = array("0"=>_("None"),"AGP"=>"AGP","PCI"=>"PCI","PCI-X"=>"PCI-X","Other"=>_("Other")); 
368     $smarty->assign("GFXInterfaceKeys",array_flip($gfxControlTypes));
369     $smarty->assign("GFXInterfaces"   , $gfxControlTypes);
370     
371     $smarty->assign("FK_glpi_enterpriseKeys",array_flip($manufacturer));
372     $smarty->assign("FK_glpi_enterprises", $manufacturer);
374     $smarty->assign("formats",array("Large","Medium","Micro"));
375     $smarty->assign("formats",array("Large","Medium","Micro"));
377     $smarty->assign("formats",array("Large","Medium","Micro"));
378     $smarty->assign("formatKeys",array('Grand','Moyen','Micro'));
379     return($smarty->fetch(get_template_path('glpi_devices.tpl', TRUE)));
380   }
382 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
383 ?>