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);
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;
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 }
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 }
146 /* Assign attributes */
147 foreach($this->attributes as $attr){
148 $smarty->assign($attr,$this->$attr);
149 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
150 }
152 /* Selecte technical responsible person
153 */
154 if(isset($_POST['SelectCartridgeTechPerson'])){
155 $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_cartridge_tech_num");
156 }
158 /* Abort user selection
159 */
160 $smarty->assign("AbortSelectUser","SelectCartridgeUserCancel");
161 $smarty->assign("SaveSelectUser" ,"user_cartridge_tech_num");
162 if(isset($_POST['SelectCartridgeUserCancel'])){
163 $this->dialog = false;
164 $this->addUser ="";
165 $this->cur_dialog = false;
166 }
168 /* Start ldap, and get all glpiUsers */
169 $ldap = $this->config->get_ldap_link();
170 $users = ($this->parent->handle->getUsers());
172 /* Technical responsible/contact person selected
173 */
174 if(isset($_GET['act'])&&($_GET['act']=="user_cartridge_tech_num")){
176 /* Get posted id */
177 $id = base64_decode($_GET['id']);
179 /* Check if user is already created in glpi database */
180 if(!in_array($id,$users)){
182 /* If this user doesn't exists in glpi db, we must create him */
183 $ldap->cat($id, array('cn', 'mail', 'telephoneNumber'));
184 $atr = $ldap->fetch();
185 $tmp = array();
186 $use = array( "cn" =>"name",
187 "mail" =>"email",
188 "telephoneNumber" =>"phone");
190 /* Create array */
191 foreach($use as $gosa => $glpi){
192 if(isset($atr[$gosa])){
193 $tmp[$glpi]= $atr[$gosa][0];
194 }
195 }
197 /* Add this user */
198 $this->parent->handle->addUser($tmp,$id);
199 }
201 /* Re-read users */
202 $users = ($this->parent->handle->getUsers());
204 /* Get user */
205 $tmp = array_flip($users);
206 $id=$tmp[$id];
208 /* Close dialog and use this user */
209 $this->tech_num = $id;
210 $this->cur_dialog = false;
211 $this->dialog= false;
212 }
214 /* Execute dialog*/
215 if($this->cur_dialog){
216 $this->cur_dialog->save_object();
217 $this->dialog=true;
218 $this->cur_dialog->parent = &$this;
219 return($this->cur_dialog->execute());
220 }else{
221 $this->dialog= false;
222 }
224 /* Create matrix with all possible printer types
225 */
226 $matrix = "<table summary=''><tr>";
227 $types = $this->parent->handle->getPrinterTypes();
229 /* Specifies number of cols */
230 $spalten = 2;
232 $spalt = $spalten;
233 foreach($types as $key => $type){
234 $spalt -- ;
236 /* Create new row, if cols for this row reached $spalten */
237 if($spalt < 0) {
238 $spalt = $spalten -1;
239 $matrix .= "</tr><tr>";
240 }
242 /* Avoid deselecting every checkbox */
243 if($key == $this->printer_type){
244 $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' disabled checked>".$type."</td>";
245 }else{
247 /* Cehck checkbox */
248 $check = "";
249 if(isset($this->additional_printer[$key])){
250 $check = " checked ";
251 }
252 $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' ".$check." ><input type='hidden' name='wasOnPage_".$key."' ".$check.">".$type."</td>";
253 }
254 }
256 /* Complete table row, to avoid html errors */
257 if($spalt >0){
258 while($spalt >0){
259 $spalt -- ;
260 $matrix .="<td> </td>";
261 }
262 }
264 $matrix .= "</tr></table>";
266 /* Append manufacturers
267 */
268 $smarty->assign("PrinterTypeMatrix", $matrix);
270 $smarty->assign("ManufacturerKeys", array_flip($this->parent->handle->getEnterprises()));
271 $smarty->assign("Manufacturers", $this->parent->handle->getEnterprises());
272 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
274 $smarty->assign("typeKeys", array_flip($this->parent->handle->getCartridgeTypes()));
275 $smarty->assign("types", $this->parent->handle->getCartridgeTypes());
276 $smarty->assign("type", $this->type);
278 /* Handle tech person
279 Assign name ... to smarty, if set
280 */
281 if(isset($users[$this->tech_num])){
282 $ldap->cat($users[$this->tech_num], array('givenName', 'sn', 'uid'));
283 $tr = $ldap->fetch();
284 $str = "";
285 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
286 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
287 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
288 $smarty->assign("tech_num", $str);
289 }else{
290 $smarty->assign("tech_num", _("N/A"));
291 }
294 $display.= $smarty->fetch(get_template_path('glpiPrinterCartridgesEdit.tpl', TRUE,dirname(__FILE__)));
295 return($display);
296 }
298 /* Check given values */
299 function check(){
300 /* Call common method to give check the hook */
301 $message= plugin::check();
303 /* Avoid choosing an already used name */
304 $types = $this->parent->handle->getCartridgeTypeInformations();
305 foreach($types as $type){
306 if($type['name'] == $this->name){
307 if($type['ID'] != $this->ID){
308 $message[]= _("The selected name is already in use.");
309 }
310 }
311 }
312 return $message;
313 }
315 /* Save Post data */
316 function save_object()
317 {
318 foreach($this->attributes as $attr){
319 if(isset($_POST[$attr])){
320 $this->$attr= $_POST[$attr];
321 }
322 }
324 /* Get selected checkboxes */
325 foreach($_POST as $name => $value){
326 if(preg_match("/wasOnPage_/",$name)){
327 $id = preg_replace("/wasOnPage_/","",$name);
328 if(isset($_POST['UsePrinterType_'.$id])){
329 $this->additional_printer[$id]=$id;
330 }else{
331 if(isset($this->additional_printer[$id])){
332 unset($this->additional_printer[$id]);
333 }
334 }
335 }
336 }
337 }
339 /* Create / Update database with this new infos */
340 function save()
341 {
342 $tmp = array();
343 foreach($this->attributes as $attr){
344 $tmp[$attr] = $this-> $attr;
345 }
346 $types = array();
347 $types = $this->additional_printer;
348 $types[$this->printer_type]=$this->printer_type;
349 $this->parent->handle->Add_UpdateCatrigdeType($tmp,$types);
350 }
352 }
353 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
354 ?>