Code

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