Code

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