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 $this->parent->handle->removeCartridgeDropdownType($_POST['select_type_cartridge']);
115 }
117 /* Rename selected printer type to given string
118 */
119 /* Update*/
120 if((isset($_POST['rename_cartridge_type']))&&(!empty($_POST['select_type_cartridge']))&&(!empty($_POST['cartridge_type_string']))){
121 $this->parent->handle->updateCartridgeDropdownType($_POST['cartridge_type_string'],$_POST['select_type_cartridge']);
122 }
124 /* Someone wants to edit the printer types ...
125 So, lets open a new dialog which provides some buttons to edit the types
126 */
127 if($this->edit_type){
128 $smarty->assign("PrinterTypes", $this->parent->handle->getCartridgeTypes());
129 $smarty->assign("PrinterTypeKeys", array_flip($this->parent->handle->getCartridgeTypes()));
130 $display= $smarty->fetch(get_template_path('glpi_edit_cartridge_type.tpl', TRUE));
131 return($display);
132 }
141 /* Assign attributes */
142 foreach($this->attributes as $attr){
143 $smarty->assign($attr,$this->$attr);
144 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
145 }
147 /* Selecte technical responsible person
148 */
149 if(isset($_POST['SelectCartridgeTechPerson'])){
150 $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_cartridge_tech_num");
151 }
153 /* Abort user selection
154 */
155 $smarty->assign("AbortSelectUser","SelectCartridgeUserCancel");
156 $smarty->assign("SaveSelectUser" ,"user_cartridge_tech_num");
157 if(isset($_POST['SelectCartridgeUserCancel'])){
158 $this->dialog = false;
159 $this->addUser ="";
160 $this->cur_dialog = false;
161 }
163 /* Start ldap, and get all glpiUsers */
164 $ldap = $this->config->get_ldap_link();
165 $users = ($this->parent->handle->getUsers());
167 /* Technical responsible/contact person selected
168 */
169 if(isset($_GET['act'])&&($_GET['act']=="user_cartridge_tech_num")){
171 /* Get posted id */
172 $id = base64_decode($_GET['id']);
174 /* Check if user is already created in glpi database */
175 if(!in_array($id,$users)){
177 /* If this user doesn't exists in glpi db, we must create him */
178 $atr = $ldap->fetch($ldap->cat($id));
179 $tmp = array();
180 $use = array( "cn" =>"name",
181 "mail" =>"email",
182 "telephoneNumber" =>"phone");
184 /* Create array */
185 foreach($use as $gosa => $glpi){
186 if(isset($atr[$gosa])){
187 $tmp[$glpi]= $atr[$gosa][0];
188 }
189 }
191 /* Add this user */
192 $this->parent->handle->addUser($tmp,$id);
193 }
195 /* Re-read users */
196 $users = ($this->parent->handle->getUsers());
198 /* Get user */
199 $tmp = array_flip($users);
200 $id=$tmp[$id];
202 /* Close dialog and use this user */
203 $this->tech_num = $id;
204 $this->cur_dialog = false;
205 $this->dialog= false;
206 }
208 /* Execute dialog*/
209 if($this->cur_dialog){
210 $this->cur_dialog->save_object();
211 $this->dialog=true;
212 $this->cur_dialog->parent = &$this;
213 return($this->cur_dialog->execute());
214 }else{
215 $this->dialog= false;
216 }
218 /* Create matrix with all possible printer types
219 */
220 $matrix = "<table summary=''><tr>";
221 $types = $this->parent->handle->getPrinterTypes();
223 /* Specifies number of cols */
224 $spalten = 2;
226 $spalt = $spalten;
227 foreach($types as $key => $type){
228 $spalt -- ;
230 /* Create new row, if cols for this row reached $spalten */
231 if($spalt < 0) {
232 $spalt = $spalten -1;
233 $matrix .= "</tr><tr>";
234 }
236 /* Avoid deselecting every checkbox */
237 if($key == $this->printer_type){
238 $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' disabled checked>".$type."</td>";
239 }else{
241 /* Cehck checkbox */
242 $check = "";
243 if(isset($this->additional_printer[$key])){
244 $check = " checked ";
245 }
246 $matrix.= "<td><input type='checkbox' name='UsePrinterType_".$key."' ".$check." ><input type='hidden' name='wasOnPage_".$key."' ".$check.">".$type."</td>";
247 }
248 }
250 /* Complete table row, to avoid html errors */
251 if($spalt >0){
252 while($spalt >0){
253 $spalt -- ;
254 $matrix .="<td> </td>";
255 }
256 }
258 $matrix .= "</tr></table>";
260 /* Append manufacturers
261 */
262 $smarty->assign("PrinterTypeMatrix", $matrix);
264 $smarty->assign("ManufacturerKeys", array_flip($this->parent->handle->getEnterprises()));
265 $smarty->assign("Manufacturers", $this->parent->handle->getEnterprises());
266 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
268 $smarty->assign("typeKeys", array_flip($this->parent->handle->getCartridgeTypes()));
269 $smarty->assign("types", $this->parent->handle->getCartridgeTypes());
270 $smarty->assign("type", $this->type);
272 /* Handle tech person
273 Assign name ... to smarty, if set
274 */
275 if(isset($users[$this->tech_num])){
276 $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
277 $str = "";
278 if(isset($tr['givenName'][0])){ $str .= $tr['givenName'][0]." "; }
279 if(isset($tr['sn'][0])) { $str .= $tr['sn'][0]." "; }
280 if(isset($tr['uid'][0])){ $str .= "[".$tr['uid'][0]."]"; }
281 $smarty->assign("tech_num", $str);
282 }else{
283 $smarty->assign("tech_num", _("N/A"));
284 }
287 $display.= $smarty->fetch(get_template_path('glpiPrinterCartridgesEdit.tpl', TRUE,dirname(__FILE__)));
288 return($display);
289 }
291 /* Check given values */
292 function check(){
293 $message=array();
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 }
344 }
345 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
346 ?>