Code

765f2e96f511749d459519a4989bba6539a2412e
[gosa.git] / gosa-plugins / glpi / admin / systems / services / glpi / class_glpiPrinterCartridgesEdit.inc
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);
35     foreach($this->attributes as $val){
36       if(isset($values[$val])){
37         $this->$val = $values[$val];
38       }else{
39         $this->$val = "";
40       }
41     }
42     $this->needToCatch = true;
43     $this->printer_type = $printer_type;
44   }
46   function remove_from_parent()
47   {
48     if($this->ID > 0){
49       $this->parent->handle->removeCartridgeDropdownType($this->ID);
50     }
51   }
53   function execute()
54   {
55     /* Call parent execute */
56     plugin::execute();
58     /* Fill templating stuff */
59     $smarty= get_smarty();
60     $display= "";
62     /* Get all currently used/supported printer types for this cartridge type
63      */
64     if($this->needToCatch){
65       $this->needToCatch = false;
66       if(!empty($this->type)){
67         $this->additional_printer= $this->parent->handle->getSupportedPrinterTypeIDsForCartridge($this->ID);
68       }
69     }
75   
76     /* Printer type management
77      */
78     if(isset($_POST['edit_type_cartridge'])){
79       $this->dialog = true;
80       $this->edit_type=true;
81     }
83     /* This closes the printer type editing dialog
84      */
85     if(isset($_POST['close_edit_type_cartridge'])){
86       $this->edit_type=false;
87       $this->dialog = false;
88     }
90     /* This appends a new printer to our sytem types
91      */
92     if((isset($_POST['add_cartridge_type']))&&(!empty($_POST['cartridge_type_string']))){
93       print "Add";
94       $this->parent->handle->addCartridgeDropdownType($_POST['cartridge_type_string']);
95     }
97     /* Remove selected type from our printer types list
98      */
99     if((isset($_POST['del_cartridge_type']))&&(!empty($_POST['select_type_cartridge']))){
100       $used = $this->parent->handle->getUsedDropdownTypes($_POST['select_type_cartridge']);
101       if(!count($used)){
102         $this->parent->handle->removeCartridgeDropdownType($_POST['select_type_cartridge']);
103       }else{
104         print_red(_("Can't delete this entry, it is still in use."));
105       }
106     }
108     /* Rename selected printer type to given string
109      */
110     /* Update*/
111     if((isset($_POST['rename_cartridge_type']))&&(!empty($_POST['select_type_cartridge']))&&(!empty($_POST['cartridge_type_string']))){
112       $this->parent->handle->updateCartridgeDropdownType($_POST['cartridge_type_string'],$_POST['select_type_cartridge']);
113     }
115     /* Someone wants to edit the printer types ...
116        So, lets open a new dialog which provides some buttons to edit the types
117      */
118     if($this->edit_type){
119       $smarty->assign("PrinterTypes",            $this->parent->handle->getCartridgeTypes());
120       $smarty->assign("PrinterTypeKeys",         array_flip($this->parent->handle->getCartridgeTypes()));
121       $display= $smarty->fetch(get_template_path('glpi_edit_cartridge_type.tpl',TRUE,dirname(__FILE__)));
122       return($display);
123     }
125     /* Assign attributes */ 
126     foreach($this->attributes as $attr){
127       $smarty->assign($attr,$this->$attr);
128     }
130     /* Selecte technical responsible person
131      */
132     if(isset($_POST['SelectCartridgeTechPerson'])){
133       $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_cartridge_tech_num");
134     }
136     /* Abort user selection
137      */
138     $smarty->assign("AbortSelectUser","SelectCartridgeUserCancel");
139     $smarty->assign("SaveSelectUser" ,"user_cartridge_tech_num");
140     if(isset($_POST['SelectCartridgeUserCancel'])){
141       $this->dialog = false;
142       $this->addUser ="";
143       $this->cur_dialog = false;
144     }
146     /* Start ldap, and get all glpiUsers */
147     $ldap = $this->config->get_ldap_link();
148     $users = ($this->parent->handle->getUsers());
150     /* Technical responsible/contact person selected 
151     */
152     if(isset($_GET['act'])&&($_GET['act']=="user_cartridge_tech_num")){
154       /* Get posted id */
155       $id = base64_decode($_GET['id']);
157       /* Check if user is already created in glpi database */
158       if(!in_array($id,$users)){
160         /* If this user doesn't exists in glpi db, we must create him */
161         $ldap->cat($id, array('cn', 'mail', 'telephoneNumber'));
162         $atr = $ldap->fetch();
163         $tmp = array();
164         $use = array( "cn"              =>"name",
165             "mail"            =>"email",
166             "telephoneNumber" =>"phone");
168         /* Create array */
169         foreach($use as $gosa => $glpi){
170           if(isset($atr[$gosa])){
171             $tmp[$glpi]= $atr[$gosa][0];
172           }
173         }
175         /* Add this user */
176         $this->parent->handle->addUser($tmp,$id);
177       }
179       /* Re-read users */
180       $users = ($this->parent->handle->getUsers());
182       /* Get user */
183       $tmp = array_flip($users);
184       $id=$tmp[$id];
185     
186       /* Close dialog and use this user */
187       $this->tech_num = $id;
188       $this->cur_dialog   = false;
189       $this->dialog= false;
190     }
192     /* Execute dialog*/
193     if($this->cur_dialog){
194       $this->cur_dialog->save_object();
195       $this->dialog=true;
196       $this->cur_dialog->parent = &$this;
197       return($this->cur_dialog->execute());
198     }else{
199       $this->dialog= false;
200     }
202     /* Create matrix with all possible printer types 
203      */
204     $matrix = "<table summary=''><tr>";
205     $types = $this->parent->handle->getPrinterTypes();
206    
207     /* Specifies number of cols */ 
208     $spalten = 2;
209     
210     $spalt = $spalten; 
211     foreach($types as $key => $type){
212       $spalt -- ; 
214       /* Create new row, if cols for this row reached $spalten */
215       if($spalt < 0) {
216         $spalt = $spalten -1;
217         $matrix .= "</tr><tr>";
218       }
220       /* Avoid deselecting every checkbox */
221       if($key == $this->printer_type){
222         $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' disabled checked>".$type."</td>";
223       }else{
224       
225         /* Cehck checkbox */
226         $check = "";
227         if(isset($this->additional_printer[$key])){
228           $check = " checked "; 
229         }
230         $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' ".$check." ><input type='hidden' name='wasOnPage_".$key."' ".$check.">".$type."</td>";
231       }
232     }
233     
234     /* Complete table row, to avoid html errors */
235     if($spalt >0){
236       while($spalt >0){
237         $spalt -- ;
238         $matrix .="<td>&nbsp;</td>";
239       }
240     }
241     
242     $matrix .= "</tr></table>";
244      /* Append manufacturers
245      */
246     $smarty->assign("PrinterTypeMatrix",      $matrix);
247     
248     $smarty->assign("ManufacturerKeys",       array_flip($this->parent->handle->getEnterprises()));
249     $smarty->assign("Manufacturers",          $this->parent->handle->getEnterprises());
250     $smarty->assign("FK_glpi_enterprise",     $this->FK_glpi_enterprise);
252     $smarty->assign("typeKeys",               array_flip($this->parent->handle->getCartridgeTypes()));
253     $smarty->assign("types",                  $this->parent->handle->getCartridgeTypes());
254     $smarty->assign("type",                   $this->type);
256      /* Handle tech person
257        Assign name ... to smarty, if set
258      */
259     if(isset($users[$this->tech_num])){
260       $ldap->cat($users[$this->tech_num], array('givenName', 'sn', 'uid'));
261       $tr = $ldap->fetch();
262       $str = "";
263       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
264       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
265       if(isset($tr['uid'][0])){         $str .= "[".$tr['uid'][0]."]";        }
266       $smarty->assign("tech_num",               $str);
267     }else{
268       $smarty->assign("tech_num",               _("N/A"));
269     }
272     $display.= $smarty->fetch(get_template_path('glpiPrinterCartridgesEdit.tpl', TRUE,dirname(__FILE__)));
273     return($display);
274   }
276   /* Check given values */
277   function check(){
278     /* Call common method to give check the hook */
279     $message= plugin::check();
280   
281     /* Avoid choosing an already used name */
282     $types = $this->parent->handle->getCartridgeTypeInformations();
283     foreach($types as $type){
284       if($type['name'] == $this->name){
285         if($type['ID'] != $this->ID){
286           $message[]= _("The selected name is already in use.");
287         }        
288       }
289     }  
290     return $message;
291   }
293   /* Save Post data */
294   function save_object()
295   {
296     foreach($this->attributes as $attr){
297       if(isset($_POST[$attr])){
298         $this->$attr= $_POST[$attr];
299       }
300     }
302     /* Get selected checkboxes */
303     foreach($_POST as $name => $value){
304       if(preg_match("/wasOnPage_/",$name)){
305         $id = preg_replace("/wasOnPage_/","",$name);
306         if(isset($_POST['UsePrinterType_'.$id])){
307           $this->additional_printer[$id]=$id;
308         }else{
309           if(isset($this->additional_printer[$id])){
310             unset($this->additional_printer[$id]);
311           }
312         }
313       }
314     }
315   }
317   /* Create / Update database with this new infos */
318   function save()
319   {
320     $tmp  = array();
321     foreach($this->attributes as $attr){
322       $tmp[$attr] = $this-> $attr;
323     }
324     $types = array();
325     $types = $this->additional_printer;
326     $types[$this->printer_type]=$this->printer_type;
327     $this->parent->handle->Add_UpdateCatrigdeType($tmp,$types);
328   }
331 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
332 ?>