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 $atr = $ldap->fetch($ldap->cat($id));
184 $tmp = array();
185 $use = array( "cn" =>"name",
186 "mail" =>"email",
187 "telephoneNumber" =>"phone");
189 /* Create array */
190 foreach($use as $gosa => $glpi){
191 if(isset($atr[$gosa])){
192 $tmp[$glpi]= $atr[$gosa][0];
193 }
194 }
196 /* Add this user */
197 $this->parent->handle->addUser($tmp,$id);
198 }
200 /* Re-read users */
201 $users = ($this->parent->handle->getUsers());
203 /* Get user */
204 $tmp = array_flip($users);
205 $id=$tmp[$id];
207 /* Close dialog and use this user */
208 $this->tech_num = $id;
209 $this->cur_dialog = false;
210 $this->dialog= false;
211 }
213 /* Execute dialog*/
214 if($this->cur_dialog){
215 $this->cur_dialog->save_object();
216 $this->dialog=true;
217 $this->cur_dialog->parent = &$this;
218 return($this->cur_dialog->execute());
219 }else{
220 $this->dialog= false;
221 }
223 /* Create matrix with all possible printer types
224 */
225 $matrix = "<table summary=''><tr>";
226 $types = $this->parent->handle->getPrinterTypes();
228 /* Specifies number of cols */
229 $spalten = 2;
231 $spalt = $spalten;
232 foreach($types as $key => $type){
233 $spalt -- ;
235 /* Create new row, if cols for this row reached $spalten */
236 if($spalt < 0) {
237 $spalt = $spalten -1;
238 $matrix .= "</tr><tr>";
239 }
241 /* Avoid deselecting every checkbox */
242 if($key == $this->printer_type){
243 $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' disabled checked>".$type."</td>";
244 }else{
246 /* Cehck checkbox */
247 $check = "";
248 if(isset($this->additional_printer[$key])){
249 $check = " checked ";
250 }
251 $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' ".$check." ><input type='hidden' name='wasOnPage_".$key."' ".$check.">".$type."</td>";
252 }
253 }
255 /* Complete table row, to avoid html errors */
256 if($spalt >0){
257 while($spalt >0){
258 $spalt -- ;
259 $matrix .="<td> </td>";
260 }
261 }
263 $matrix .= "</tr></table>";
265 /* Append manufacturers
266 */
267 $smarty->assign("PrinterTypeMatrix", $matrix);
269 $smarty->assign("ManufacturerKeys", array_flip($this->parent->handle->getEnterprises()));
270 $smarty->assign("Manufacturers", $this->parent->handle->getEnterprises());
271 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
273 $smarty->assign("typeKeys", array_flip($this->parent->handle->getCartridgeTypes()));
274 $smarty->assign("types", $this->parent->handle->getCartridgeTypes());
275 $smarty->assign("type", $this->type);
277 /* Handle tech person
278 Assign name ... to smarty, if set
279 */
280 if(isset($users[$this->tech_num])){
281 $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
282 $str = "";
283 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
284 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
285 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
286 $smarty->assign("tech_num", $str);
287 }else{
288 $smarty->assign("tech_num", _("N/A"));
289 }
292 $display.= $smarty->fetch(get_template_path('glpiPrinterCartridgesEdit.tpl', TRUE,dirname(__FILE__)));
293 return($display);
294 }
296 /* Check given values */
297 function check(){
298 /* Call common method to give check the hook */
299 $message= plugin::check();
301 /* Avoid choosing an already used name */
302 $types = $this->parent->handle->getCartridgeTypeInformations();
303 foreach($types as $type){
304 if($type['name'] == $this->name){
305 if($type['ID'] != $this->ID){
306 $message[]= _("The selected name is already in use.");
307 }
308 }
309 }
310 return $message;
311 }
313 /* Save Post data */
314 function save_object()
315 {
316 foreach($this->attributes as $attr){
317 if(isset($_POST[$attr])){
318 $this->$attr= $_POST[$attr];
319 }
320 }
322 /* Get selected checkboxes */
323 foreach($_POST as $name => $value){
324 if(preg_match("/wasOnPage_/",$name)){
325 $id = preg_replace("/wasOnPage_/","",$name);
326 if(isset($_POST['UsePrinterType_'.$id])){
327 $this->additional_printer[$id]=$id;
328 }else{
329 if(isset($this->additional_printer[$id])){
330 unset($this->additional_printer[$id]);
331 }
332 }
333 }
334 }
335 }
337 /* Create / Update database with this new infos */
338 function save()
339 {
340 $tmp = array();
341 foreach($this->attributes as $attr){
342 $tmp[$attr] = $this-> $attr;
343 }
344 $types = array();
345 $types = $this->additional_printer;
346 $types[$this->printer_type]=$this->printer_type;
347 $this->parent->handle->Add_UpdateCatrigdeType($tmp,$types);
348 }
350 }
351 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
352 ?>