eff3a80f6139caed4b2428759440af7f3627c565
1 <?php
3 class glpiPrinterCartridgesEdit extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
7 var $attributes = array("ID","name","ref","location","type","FK_glpi_enterprise","tech_num","deleted","comments","alarm");
8 var $objectclasses = array("whatever");
10 var $printer_type = 0;
12 var $additional_printer= array();
13 var $needToCatch = true;
15 var $parent = NULL;
17 var $ID = "";
18 var $name = "";
19 var $ref = "";
20 var $location = "";
21 var $type = "";
22 var $FK_glpi_enterprise = "";
23 var $tech_num = "";
24 var $deleted = "";
25 var $comments = "";
26 var $alarm = "";
28 var $cur_dialog = false;
29 var $edit_type =false;
32 function glpiPrinterCartridgesEdit ($config, $dn,$printer_type,$values = NULL )
33 {
34 plugin::plugin ($config, $dn);
36 $ui= get_userinfo();
37 $acl= get_permissions ($this->dn, $ui->subtreeACL);
38 $this->acl= get_module_permission($acl, "component", $this->dn);
41 foreach($this->attributes as $val){
42 if(isset($values[$val])){
43 $this->$val = $values[$val];
44 }else{
45 $this->$val = "";
46 }
47 }
49 $this->needToCatch = true;
51 $this->printer_type = $printer_type;
53 }
55 function remove_from_parent()
56 {
57 if($this->ID > 0){
58 $this->parent->handle->removeCartridgeDropdownType($this->ID);
59 }
60 }
62 function execute()
63 {
64 /* Call parent execute */
65 plugin::execute();
67 /* Fill templating stuff */
68 $smarty= get_smarty();
69 $display= "";
71 /* Get all currently used/supported printer types for this cartridge type
72 */
73 if($this->needToCatch){
74 $this->needToCatch = false;
75 if(!empty($this->type)){
76 $this->additional_printer= $this->parent->handle->getSupportedPrinterTypeIDsForCartridge($this->ID);
77 }
78 }
85 /* Printer type management
86 */
87 if(isset($_POST['edit_type_cartridge'])){
88 $this->dialog = true;
89 $this->edit_type=true;
90 }
92 /* This closes the printer type editing dialog
93 */
94 if(isset($_POST['close_edit_type_cartridge'])){
95 $this->edit_type=false;
96 $this->dialog = false;
97 }
99 /* This appends a new printer to our sytem types
100 */
101 if((isset($_POST['add_cartridge_type']))&&(!empty($_POST['cartridge_type_string']))){
102 print "Add";
103 $this->parent->handle->addCartridgeDropdownType($_POST['cartridge_type_string']);
104 }
106 /* Remove selected type from our printer types list
107 */
108 if((isset($_POST['del_cartridge_type']))&&(!empty($_POST['select_type_cartridge']))){
109 $used = $this->parent->handle->getUsedDropdownTypes($_POST['select_type_cartridge']);
110 if(!count($used)){
111 $this->parent->handle->removeCartridgeDropdownType($_POST['select_type_cartridge']);
112 }else{
113 print_red(_("Can't delete this entry, it is still in use."));
114 }
115 }
117 /* Rename selected printer type to given string
118 */
119 /* Update*/
120 if((isset($_POST['rename_cartridge_type']))&&(!empty($_POST['select_type_cartridge']))&&(!empty($_POST['cartridge_type_string']))){
121 $this->parent->handle->updateCartridgeDropdownType($_POST['cartridge_type_string'],$_POST['select_type_cartridge']);
122 }
124 /* Someone wants to edit the printer types ...
125 So, lets open a new dialog which provides some buttons to edit the types
126 */
127 if($this->edit_type){
128 $smarty->assign("PrinterTypes", $this->parent->handle->getCartridgeTypes());
129 $smarty->assign("PrinterTypeKeys", array_flip($this->parent->handle->getCartridgeTypes()));
130 $display= $smarty->fetch(get_template_path('glpi_edit_cartridge_type.tpl', TRUE));
131 return($display);
132 }
141 /* Assign attributes */
142 foreach($this->attributes as $attr){
143 $smarty->assign($attr,$this->$attr);
144 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
145 }
147 /* Selecte technical responsible person
148 */
149 if(isset($_POST['SelectCartridgeTechPerson'])){
150 $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_cartridge_tech_num");
151 }
153 /* Abort user selection
154 */
155 $smarty->assign("AbortSelectUser","SelectCartridgeUserCancel");
156 $smarty->assign("SaveSelectUser" ,"user_cartridge_tech_num");
157 if(isset($_POST['SelectCartridgeUserCancel'])){
158 $this->dialog = false;
159 $this->addUser ="";
160 $this->cur_dialog = false;
161 }
163 /* Start ldap, and get all glpiUsers */
164 $ldap = $this->config->get_ldap_link();
165 $users = ($this->parent->handle->getUsers());
167 /* Technical responsible/contact person selected
168 */
169 if(isset($_GET['act'])&&($_GET['act']=="user_cartridge_tech_num")){
171 /* Get posted id */
172 $id = base64_decode($_GET['id']);
174 /* Check if user is already created in glpi database */
175 if(!in_array($id,$users)){
177 /* If this user doesn't exists in glpi db, we must create him */
178 $ldap->cat($id, array('cn', 'mail', 'telephoneNumber'));
179 $atr = $ldap->fetch();
180 $tmp = array();
181 $use = array( "cn" =>"name",
182 "mail" =>"email",
183 "telephoneNumber" =>"phone");
185 /* Create array */
186 foreach($use as $gosa => $glpi){
187 if(isset($atr[$gosa])){
188 $tmp[$glpi]= $atr[$gosa][0];
189 }
190 }
192 /* Add this user */
193 $this->parent->handle->addUser($tmp,$id);
194 }
196 /* Re-read users */
197 $users = ($this->parent->handle->getUsers());
199 /* Get user */
200 $tmp = array_flip($users);
201 $id=$tmp[$id];
203 /* Close dialog and use this user */
204 $this->tech_num = $id;
205 $this->cur_dialog = false;
206 $this->dialog= false;
207 }
209 /* Execute dialog*/
210 if($this->cur_dialog){
211 $this->cur_dialog->save_object();
212 $this->dialog=true;
213 $this->cur_dialog->parent = &$this;
214 return($this->cur_dialog->execute());
215 }else{
216 $this->dialog= false;
217 }
219 /* Create matrix with all possible printer types
220 */
221 $matrix = "<table summary=''><tr>";
222 $types = $this->parent->handle->getPrinterTypes();
224 /* Specifies number of cols */
225 $spalten = 2;
227 $spalt = $spalten;
228 foreach($types as $key => $type){
229 $spalt -- ;
231 /* Create new row, if cols for this row reached $spalten */
232 if($spalt < 0) {
233 $spalt = $spalten -1;
234 $matrix .= "</tr><tr>";
235 }
237 /* Avoid deselecting every checkbox */
238 if($key == $this->printer_type){
239 $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' disabled checked>".$type."</td>";
240 }else{
242 /* Cehck checkbox */
243 $check = "";
244 if(isset($this->additional_printer[$key])){
245 $check = " checked ";
246 }
247 $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' ".$check." ><input type='hidden' name='wasOnPage_".$key."' ".$check.">".$type."</td>";
248 }
249 }
251 /* Complete table row, to avoid html errors */
252 if($spalt >0){
253 while($spalt >0){
254 $spalt -- ;
255 $matrix .="<td> </td>";
256 }
257 }
259 $matrix .= "</tr></table>";
261 /* Append manufacturers
262 */
263 $smarty->assign("PrinterTypeMatrix", $matrix);
265 $smarty->assign("ManufacturerKeys", array_flip($this->parent->handle->getEnterprises()));
266 $smarty->assign("Manufacturers", $this->parent->handle->getEnterprises());
267 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
269 $smarty->assign("typeKeys", array_flip($this->parent->handle->getCartridgeTypes()));
270 $smarty->assign("types", $this->parent->handle->getCartridgeTypes());
271 $smarty->assign("type", $this->type);
273 /* Handle tech person
274 Assign name ... to smarty, if set
275 */
276 if(isset($users[$this->tech_num])){
277 $ldap->cat($users[$this->tech_num], array('givenName', 'sn', 'uid'));
278 $tr = $ldap->fetch();
279 $str = "";
280 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
281 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
282 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
283 $smarty->assign("tech_num", $str);
284 }else{
285 $smarty->assign("tech_num", _("N/A"));
286 }
289 $display.= $smarty->fetch(get_template_path('glpiPrinterCartridgesEdit.tpl', TRUE,dirname(__FILE__)));
290 return($display);
291 }
293 /* Check given values */
294 function check(){
295 /* Call common method to give check the hook */
296 $message= plugin::check();
298 /* Avoid choosing an already used name */
299 $types = $this->parent->handle->getCartridgeTypeInformations();
300 foreach($types as $type){
301 if($type['name'] == $this->name){
302 if($type['ID'] != $this->ID){
303 $message[]= _("The selected name is already in use.");
304 }
305 }
306 }
307 return $message;
308 }
310 /* Save Post data */
311 function save_object()
312 {
313 foreach($this->attributes as $attr){
314 if(isset($_POST[$attr])){
315 $this->$attr= $_POST[$attr];
316 }
317 }
319 /* Get selected checkboxes */
320 foreach($_POST as $name => $value){
321 if(preg_match("/wasOnPage_/",$name)){
322 $id = preg_replace("/wasOnPage_/","",$name);
323 if(isset($_POST['UsePrinterType_'.$id])){
324 $this->additional_printer[$id]=$id;
325 }else{
326 if(isset($this->additional_printer[$id])){
327 unset($this->additional_printer[$id]);
328 }
329 }
330 }
331 }
332 }
334 /* Create / Update database with this new infos */
335 function save()
336 {
337 $tmp = array();
338 foreach($this->attributes as $attr){
339 $tmp[$attr] = $this-> $attr;
340 }
341 $types = array();
342 $types = $this->additional_printer;
343 $types[$this->printer_type]=$this->printer_type;
344 $this->parent->handle->Add_UpdateCatrigdeType($tmp,$types);
345 }
347 }
348 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
349 ?>