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 /* Check if we should create a new entry .... */
76 $only_once = true;
77 foreach($_POST as $name => $value){
79 /* All Post vars starts with new */
80 if((preg_match("/^new_/",$name))&&($only_once)){
82 /* do this twice */
83 $only_once = false;
85 /*extract device device_type */
86 $deviceType = preg_replace("/_.*$/","",preg_replace("/^new_/","",$name));
88 /* Check if type is allowed, and create empty entry */
89 $tmp = array();
90 if((!isset($this->DeviceAttrs[$deviceType]))||((!in_array($deviceType,$this->AllowedDevices)))){
91 print_red(sprintf(_("Internal Error can't create device of type '%s'"),$deviceType));
92 }else{
93 foreach($this->DeviceAttrs[$deviceType] as $attr){
94 $tmp[$attr] = "";
95 }
96 $tmp['device_type'] = $deviceType;
97 $this->EditEntry = $tmp;
98 $this->editMode =true;
99 }
100 }
101 }
103 /* Open entry for editing if requested */
104 if((isset($_GET['act']))&&($_GET['act']=="del_device")){
105 $id = base64_decode($_GET['id']);
106 $tmp = $this->devices[$id];
108 $this->delete = $tmp;
110 if(isset($tmp['name'])){
111 $smarty->assign("warning", sprintf(_("You're about to delete the glpi device '%s'."), $tmp['name']));
112 }elseif(isset($tmp['designation'])){
113 $smarty->assign("warning", sprintf(_("You're about to delete the glpi device '%s'."), $tmp['designation']));
114 }else{
115 print_a(_("Can't detect object name."));
116 }
118 return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
119 }
122 if(isset($_POST['delete_glpi_confirm'])){
123 $this->parent->handle->deleteDevice($this->delete);
124 $this->reload();
125 }
127 /* Open entry for editing if requested */
128 if((isset($_GET['act']))&&($_GET['act']=="edit_device")){
129 $id = base64_decode($_GET['id']);
130 $this->editMode =true;
131 $this->EditEntry = $this->devices[$id];
132 }
134 /* Abort editing this entry */
135 if(isset($_POST['AbortDeviceChanges'])){
136 $this->EditEntry = array();
137 $this->editMode = false;
138 }
140 /* Save all changes made on currently selected entry */
141 if(isset($_POST['SaveDeviceChanges'])){
143 /* First check if all changes made are allowed */
144 if(count($this->check($this->EditEntry))==0){
145 $this->save($this->EditEntry);
146 $this->editMode = false;
147 $this->reload();
148 }else{
149 foreach($this->check($this->EditEntry) as $msg){
150 print_red($msg);
151 }
152 }
154 }
156 /* Check if we are currently editing something ? */
157 if($this->editMode == true){
158 return ($this->editDevice($this->EditEntry));
159 }
161 /* ENDE : GET / POST handling
162 * Below, only output generation for headpage
163 */
165 $divlist = new divlist("glpi devices");
166 $divlist->SetEntriesPerPage(0);
167 $divlist->SetHeader(array(
168 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
169 array("string" => _("devices")." / "._("Departments"), "attach" => "style=''"),
170 array("string" => _("Use"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
172 $edit = "<a href='?plug=".$_GET['plug']."&act=edit_device&id=%s'>%s</a>";
173 $editdel = "<a href='?plug=".$_GET['plug']."&act=edit_device&id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
174 $editdel.= "<a href='?plug=".$_GET['plug']."&act=del_device&id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
176 $useDevice = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useDevice_%s' %USE%>";
178 foreach($this->devices as $key=>$user){
180 if(isset($user['designation'])){
181 $str = "designation";
182 }else{
183 $str = "name";
184 }
186 if(isset($this->Selected[$user['device_type']][$user[$str]])){
187 $use = " checked ";
188 }else{
189 $use ="";
190 }
192 /* Dawn databse struckture ....*/
193 if(empty($user['comment'])) {
194 $user['comment'] = $user['comments'];
195 }
197 $field1 = array("string" => preg_replace("/%s/",base64_encode($key),preg_replace("/%USE%/",$use,$useDevice)), "attach" => "style='text-align:center;width:20px;'");
198 $field2 = array("string" => sprintf($edit,base64_encode($key),$user[$str]." [".$user['comment']."]"), "attach" => "style=''");
199 $field3 = array("string" => sprintf($editdel,base64_encode($key),base64_encode($key)),
200 "attach" => "style='width:60px;border-right:0px;text-align:right;'");
201 $divlist->AddEntry(array($field1,$field2,$field3));
203 }
205 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
206 " <input class='center' type='image' align='middle'
207 src='images/monitor.png' title='"._("New monitor")."' alt='"._("M")."' name='new_monitor'> ".
208 " <input class='center' type='image' align='middle'
209 src='images/keyboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'> ".
210 " <input class='center' type='image' align='middle'
211 src='mages/list_back.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'> ".
212 " <input class='center' type='image' align='middle'
213 src='mages/list_back.png' title='"._("New case")."' alt='"._("C")."' name='new_case'> ".
214 " <input class='center' type='image' align='middle'
215 src='mages/list_back.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'> ".
216 " <input class='center' type='image' align='middle'
217 src='mages/list_back.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'> ".
218 " <input class='center' type='image' align='middle'
219 src='mages/list_back.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'> ".
220 " <input class='center' type='image' align='middle'
221 src='mages/list_back.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'> ".
222 " <input class='center' type='image' align='middle'
223 src='mages/list_back.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'> ".
224 " <input class='center' type='image' align='middle'
225 src='mages/list_back.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'> ".
226 " <input class='center' type='image' align='middle'
227 src='mages/list_back.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'> ".
228 " <input class='center' type='image' align='middle'
229 src='mages/list_back.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'> ".
230 " <input class='center' type='image' align='middle'
231 src='mages/list_back.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'> ".
232 "</div>";
234 $filter= $_SESSION['glpiDeviceRegex'];
235 $smarty->assign("devicehead", $listhead);
236 $smarty->assign("devices", $divlist->DrawList());
237 $smarty->assign("search_image", get_template_path('images/search.png'));
238 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
239 $smarty->assign("tree_image", get_template_path('images/tree.png'));
240 $smarty->assign("infoimage", get_template_path('images/info.png'));
241 $smarty->assign("launchimage", get_template_path('images/launch.png'));
242 $smarty->assign("apply", apply_filter());
243 $smarty->assign("alphabet", generate_alphabet());
244 $smarty->assign("device_regex", $filter['device_regex']);
246 $display.= $smarty->fetch(get_template_path('glpiDeviceManagement.tpl', TRUE));
247 return($display);
248 }
250 /* Save device to glpi database
251 * If this is a new device, create a new entry, else update this entry
252 */
253 function save()
254 {
255 if($this->parent->handle->deviceExists($this->EditEntry)){
256 $this->parent->handle->updateDevices($this->EditEntry);
257 }else{
258 $this->parent->handle->addDevice($this->EditEntry);
259 }
261 }
263 /* this only gets all already defined devices */
264 function reload()
265 {
266 $this->devices = $this->parent->handle->getDevices();
267 ksort($this->devices);
268 }
270 /* This funtions saves all POST variables.
271 The variable must be in the array $this->EditEntry
272 */
273 function save_object()
274 {
275 foreach($_POST as $name => $value){
277 if(preg_match("/wasOnPage_/",$name)){
278 $dN = base64_decode($value);
280 $device = $this->devices[$dN];
281 $type = $device['device_type'];
283 if(isset($device['designation'])){
284 $str = "designation";
285 }else{
286 $str = "name";
287 }
289 if(isset($_POST['useDevice_'.$value])){
290 $this->Selected[$type][$device[$str]] = $device;
291 }else{
292 if(isset($this->Selected[$type][$device[$str]])){
293 unset($this->Selected[$type][$device[$str]]);
294 }
295 }
296 }
297 }
299 if(is_array($this->EditEntry)){
300 foreach($this->EditEntry as $name => $value){
301 if(isset($_POST[$name])){
302 $this->EditEntry[$name] = $_POST[$name];
303 }
305 }
306 }
307 }
309 /* returns the selected devices */
310 function getSelected()
311 {
312 return($this->Selected);
313 }
315 /* This function cehck all created devices if you wan't to create device specific check
316 use >>if($attr['device_type']=="moboard")<< to create a device type depending check
317 */
318 function check($attr)
319 {
320 $message = array();
322 if(isset($attr['designation'])){
323 $str2 = "designation";
324 }else{
325 $str2 = "name";
326 }
327 if(empty($attr[$str2])){
328 $message[]=(_("You have to specify a valid name for this device."));
329 }
331 /* Avoid same name twice */
332 $devices = ($this->parent->handle->getDevices());
333 foreach($devices as $dev){
334 if(isset($dev['designation'])){
335 $str = "designation";
336 }else{
337 $str = "name";
338 }
340 if($dev[$str]==$attr[$str2]){
342 /* Entries with ['ID'] already exists, and are only edited, if ID is missing we are currently creating a new entry */
343 if(isset($attr['ID'])){
344 if(!(($dev['ID'] == $attr['ID'])&&($dev['device_type']==$attr['device_type']))){
345 $message[] = _("This device name is already in use.");
346 }
347 }else{
348 $message[] = _("This device name is already in use.");
349 }
350 }
351 }
353 return($message);
354 }
356 /* This functions displays the template for all available devices
357 * This function is also used if we create a new device
358 */
359 function editDevice($entry)
360 {
361 $smarty = get_smarty();
363 /* Transfer given data to smarty */
364 foreach($this->EditEntry as $name => $value){
365 $smarty->assign($name,htmlentities($value));
366 }
368 /* Set default select boxes, manufacturers ... */
369 $smarty->assign("device_type",$entry['device_type']);
371 $none = array(0 => _("none"));
372 $manufacturer = array_merge($none,$this->parent->handle->getEnterprises());
374 $ramtypes = $this->parent->handle->getRAMTypes();
376 $smarty->assign("RAMtypes", $ramtypes);
377 $smarty->assign("RAMtypeKeys", array_flip($ramtypes));
379 $deviceControlTypes = array_merge($none,$this->parent->handle->getGlpiDeviceControlTypes());
381 $smarty->assign("HDDInterfaceKeys",array_flip($deviceControlTypes));
382 $smarty->assign("HDDInterfaces" , $deviceControlTypes);
384 $gfxControlTypes = array("0"=>_("None"),"AGP"=>"AGP","PCI"=>"PCI","PCI-X"=>"PCI-X","Other"=>_("Other"));
386 $smarty->assign("GFXInterfaceKeys",array_flip($gfxControlTypes));
387 $smarty->assign("GFXInterfaces" , $gfxControlTypes);
389 $smarty->assign("FK_glpi_enterpriseKeys",array_flip($manufacturer));
390 $smarty->assign("FK_glpi_enterprises", $manufacturer);
392 $smarty->assign("formats",array("Large","Medium","Micro"));
393 $smarty->assign("formats",array("Large","Medium","Micro"));
395 $smarty->assign("formats",array("Large","Medium","Micro"));
396 $smarty->assign("formatKeys",array('Grand','Moyen','Micro'));
397 return($smarty->fetch(get_template_path('glpi_devices.tpl', TRUE)));
398 }
399 }
400 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
401 ?>