Code

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