Code

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