Code

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