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