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 }
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){
81 /* If new_ then we should create an new device */
82 if((preg_match("/^new_/",$name))&&($only_once)){
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_red(_("Can't detect object name."));
118 }
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(count($this->parent->handle->is_deviceUsed($this->delete))){
127 $tmp = $this->parent->handle->is_deviceUsed($this->delete);
129 $names = "";
130 foreach($tmp as $name){
131 $names .= ", ".$name;
132 }
133 $names = preg_replace("/^, /","",$names);
134 $names = trim($names);
135 if(count($tmp) == 3){
136 $names .= " ...";
137 }
138 print_red(sprintf(_("You can't delete this device, it is still in use by these system(s) '%s'"),$names));
139 }else{
140 $this->parent->handle->deleteDevice($this->delete);
141 $this->reload();
142 }
143 }
145 /* Open entry for editing if requested */
146 if((isset($_GET['act']))&&($_GET['act']=="edit_device")){
147 $id = base64_decode($_GET['id']);
148 $this->editMode =true;
149 $this->EditEntry = $this->devices[$id];
150 }
152 /* Abort editing this entry */
153 if(isset($_POST['AbortDeviceChanges'])){
154 $this->EditEntry = array();
155 $this->editMode = false;
156 }
158 /* Save all changes made on currently selected entry */
159 if(isset($_POST['SaveDeviceChanges'])){
161 /* First check if all changes made are allowed */
162 if(count($this->check())==0){
163 $this->save();
164 $this->editMode = false;
165 $this->reload();
166 }else{
167 foreach($this->check() as $msg){
168 print_red($msg);
169 }
170 }
172 }
174 /* Check if we are currently editing something ? */
175 if($this->editMode == true){
176 return ($this->editDevice($this->EditEntry));
177 }
179 /* ENDE : GET / POST handling
180 * Below, only output generation for headpage
181 */
183 $divlist = new divlist("glpi devices");
184 $divlist->SetPluginMode();
185 $divlist->SetEntriesPerPage(0);
186 $divlist->SetHeader(array(
187 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
188 array("string" => _("devices"), "attach" => "style=''"),
189 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
191 $edit = "<a href='?plug=".$_GET['plug']."&act=edit_device&id=%s'>%s</a>";
192 $editdel = "<a href='?plug=".$_GET['plug']."&act=edit_device&id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
193 $editdel.= "<a href='?plug=".$_GET['plug']."&act=del_device&id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
195 $useDevice = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useDevice_%s' %USE%>";
197 foreach($this->devices as $key=>$user){
199 if(isset($user['designation'])){
200 $str = "designation";
201 }else{
202 $str = "name";
203 }
205 if(isset($this->Selected[$user['device_type']][$user[$str]])){
206 $use = " checked ";
207 }else{
208 $use ="";
209 }
211 /* Dawn databse struckture ....*/
212 if(empty($user['comment']) && isset($user['comments'])) {
213 $user['comment'] = $user['comments'];
214 }
216 $field1 = array("string" => preg_replace("/%s/",base64_encode($key),preg_replace("/%USE%/",$use,$useDevice)), "attach" => "style='text-align:center;width:20px;'");
217 $field2 = array("string" => sprintf($edit,base64_encode($key),$user[$str]." [".$user['comment']."]"), "attach" => "style=''");
218 $field3 = array("string" => sprintf($editdel,base64_encode($key),base64_encode($key)),
219 "attach" => "style='width:60px;border-right:0px;text-align:right;'");
220 $divlist->AddEntry(array($field1,$field2,$field3));
222 }
224 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
225 " <input class='center' type='image' align='middle'
226 src='images/monitor.png' title='"._("New monitor")."' alt='"._("M")."' name='new_monitor'> ".
227 " <input class='center' type='image' align='middle'
228 src='images/mainboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'> ".
229 " <input class='center' type='image' align='middle'
230 src='images/processor.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'> ".
231 " <input class='center' type='image' align='middle'
232 src='images/server.png' title='"._("New case")."' alt='"._("C")."' name='new_case'> ".
233 " <input class='center' type='image' align='middle'
234 src='images/net_hardware.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'> ".
235 " <input class='center' type='image' align='middle'
236 src='images/memory.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'> ".
237 " <input class='center' type='image' align='middle'
238 src='images/fai_partitionTable.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'> ".
239 " <input class='center' type='image' align='middle'
240 src='images/drives.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'> ".
241 " <input class='center' type='image' align='middle'
242 src='images/hardware.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'> ".
243 " <input class='center' type='image' align='middle'
244 src='images/gfx_hardware.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'> ".
245 " <input class='center' type='image' align='middle'
246 src='images/snd_hardware.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'> ".
247 " <input class='center' type='image' align='middle'
248 src='images/power.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'> ".
249 " <input class='center' type='image' align='middle'
250 src='images/fai_template.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'> ".
251 "</div>";
253 $filter= $_SESSION['glpiDeviceRegex'];
254 $smarty->assign("devicehead", $listhead);
255 $smarty->assign("devices", $divlist->DrawList());
256 $smarty->assign("search_image", get_template_path('images/search.png'));
257 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
258 $smarty->assign("tree_image", get_template_path('images/tree.png'));
259 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
260 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
261 $smarty->assign("apply", apply_filter());
262 $smarty->assign("alphabet", generate_alphabet());
263 $smarty->assign("device_regex", $filter['device_regex']);
265 $display.= $smarty->fetch(get_template_path('glpiDeviceManagement.tpl', TRUE));
266 return($display);
267 }
269 /* Save device to glpi database
270 * If this is a new device, create a new entry, else update this entry
271 */
272 function save()
273 {
274 if($this->parent->handle->deviceExists($this->EditEntry)){
275 $this->parent->handle->updateDevices($this->EditEntry);
276 }else{
277 $this->parent->handle->addDevice($this->EditEntry);
278 }
280 }
282 /* this only gets all already defined devices */
283 function reload()
284 {
285 $this->devices = $this->parent->handle->getDevices();
286 ksort($this->devices);
287 }
289 /* This funtions saves all POST variables.
290 The variable must be in the array $this->EditEntry
291 */
292 function save_object()
293 {
294 foreach($_POST as $name => $value){
296 if(preg_match("/wasOnPage_/",$name)){
297 $dN = base64_decode($value);
299 $device = $this->devices[$dN];
300 $type = $device['device_type'];
302 if(isset($device['designation'])){
303 $str = "designation";
304 }else{
305 $str = "name";
306 }
308 if(isset($_POST['useDevice_'.$value])){
309 $this->Selected[$type][$device[$str]] = $device;
310 }else{
311 if(isset($this->Selected[$type][$device[$str]])){
312 unset($this->Selected[$type][$device[$str]]);
313 }
314 }
315 }
316 }
318 if(is_array($this->EditEntry)){
319 foreach($this->EditEntry as $name => $value){
320 if(isset($_POST[$name])){
321 $this->EditEntry[$name] = $_POST[$name];
322 }
324 }
325 }
326 }
328 /* returns the selected devices */
329 function getSelected()
330 {
331 return($this->Selected);
332 }
334 /* This function checks all created devices.
335 If you wan't to use device specific checks,
336 use >>if($attr['device_type']=="moboard")<< to create a device type depending check
337 */
338 function check()
339 {
340 /* Call common method to give check the hook */
341 $message= plugin::check();
343 $attr = $this->EditEntry;
345 if(isset($attr['designation'])){
346 $str2 = "designation";
347 }else{
348 $str2 = "name";
349 }
350 if(empty($attr[$str2])){
351 $message[]=(_("You have to specify a valid name for this device."));
352 }
354 /* Avoid same name twice */
355 $devices = ($this->parent->handle->getDevices());
356 foreach($devices as $dev){
358 /* Some devices use designation some name for name
359 */
360 if(isset($dev['designation'])){
361 $str = "designation";
362 }else{
363 $str = "name";
364 }
366 if($dev[$str]==$attr[$str2]){
368 /* Entries with ['ID'] already exists, and are only edited, if ID is missing we are currently creating a new entry */
369 if(isset($attr['ID'])){
370 if(!(($dev['ID'] == $attr['ID'])&&($dev['device_type']==$attr['device_type']))){
371 $message[] = _("This device name is already in use.");
372 }
373 }else{
374 $message[] = _("This device name is already in use.");
375 }
376 }
377 }
379 return($message);
380 }
382 /* This functions displays the template for all available devices
383 * This function is also used if we create a new device
384 */
385 function editDevice($entry)
386 {
387 $smarty = get_smarty();
389 /* Transfer given data to smarty */
390 foreach($this->EditEntry as $name => $value){
391 $smarty->assign($name,htmlentities(utf8_decode($value)));
392 }
394 /* Set default select boxes, manufacturers ... */
395 $smarty->assign("device_type",$entry['device_type']);
397 $none = array(0 => _("none"));
398 $manufacturer = array_merge($none,$this->parent->handle->getEnterprises());
400 $ramtypes = $this->parent->handle->getRAMTypes();
402 $smarty->assign("RAMtypes", $ramtypes);
403 $smarty->assign("RAMtypeKeys", array_flip($ramtypes));
405 $deviceControlTypes = array_merge($none,$this->parent->handle->getGlpiDeviceControlTypes());
407 $smarty->assign("HDDInterfaceKeys",array_flip($deviceControlTypes));
408 $smarty->assign("HDDInterfaces" , $deviceControlTypes);
410 $gfxControlTypes = array("0"=>_("None"),"AGP"=>"AGP","PCI"=>"PCI","PCI-X"=>"PCI-X","Other"=>_("Other"));
412 $smarty->assign("GFXInterfaceKeys",array_flip($gfxControlTypes));
413 $smarty->assign("GFXInterfaces" , $gfxControlTypes);
415 $smarty->assign("FK_glpi_enterpriseKeys",array_flip($manufacturer));
416 $smarty->assign("FK_glpi_enterprises", $manufacturer);
418 $smarty->assign("formats",array("Large","Medium","Micro"));
419 $smarty->assign("formats",array("Large","Medium","Micro"));
421 $smarty->assign("formats",array("Large","Medium","Micro"));
422 $smarty->assign("formatKeys",array('Grand','Moyen','Micro'));
423 return($smarty->fetch(get_template_path('glpi_devices.tpl', TRUE)));
424 }
425 }
426 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
427 ?>