Code

Updated PPD handling.
[gosa.git] / plugins / admin / systems / class_printerPPDSelectionDialog.inc
1 <?php
3 class printerPPDSelectionDialog extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account       = FALSE;
7   var $attributes           = array();
8   var $objectclasses        = array("whatever");
10   var $list                 =array();
11   var $header               =array();
13   var $Vendor               = "";
14   var $current_del_id       = "";
16   function printerPPDSelectionDialog ($config, $dn= NULL,$list=false,$headers=false,$ppd=false)
17   {
18     plugin::plugin ($config, $dn);
19     $this->list       = $list;
20     $this->header     = $headers;
21     $this->depselect  = $this->config->current['BASE'];
22     
23     if(!isset($_SESSION['printerPPDSelectionDialog'])){
24       $_SESSION['printerPPDSelectionDialog']['regex'] = "*";
25     }  
27     /* Order the manufacturers index */
28     ksort($this->header);
29   }
31   function execute()
32   {
33         /* Call parent execute */
34         plugin::execute();
36     /* Fill templating stuff */
37     $smarty= get_smarty();
38     $display= "";
39     $s_action = "none";
40   
41     $regex = $_SESSION['printerPPDSelectionDialog']['regex'];
43     if(isset($_GET['search'])){
44       $regex = $_GET['search']."*";
45     }
46   
47     if(isset($_POST['regex'])){
48       $regex= $_POST['regex'];
49     }
50     
51     $regex = str_replace("**","*",$regex);
52     $_SESSION['printerPPDSelectionDialog']['regex'] = $regex;
53   
54     /* Delete requested, check if everything is ok and display confirmation dialog */
55     if(isset($_GET['act']) && $_GET['act'] == "del"){
56         
57       /* Get id and check if id is valid */
58       $id_to_del = base64_decode($_GET['id']);
59       $found = "";
60       foreach($this->list[$this->Vendor] as $key => $data){
61         if($data['link'] == $id_to_del){
62           $found = $key;
63           break;
64         }
65       }
66       if(!empty($found)){
67         $this->current_del_id = $id_to_del;
68     
69         $smarty->assign("warning", sprintf(_("You're about to delete the ppd file '%s' at '%s'."), $found,$id_to_del));
70         return($smarty->fetch(get_template_path('remove_ppd.tpl', TRUE)));
71   
72       }else{
73         print_red(sprintf(_("Could not found specified ppd file '%s'."),$id_to_del));
74       }
75     }
77     /* Deletion confirmed */
78     if(isset($_POST['delete_ppd_confirm']) && !empty($this->current_del_id)){
80       /* check if file is removeable */
81       if(is_writeable($this->current_del_id)){
82         $is_ok = @unlink($this->current_del_id);
83    
84         /* Update ppd list */ 
85         if($is_ok){
86           foreach($this->list as $vendor => $ppds){
87             foreach($ppds as $ppd => $data){
88               if($data['link'] == $this->current_del_id){
89                 unset($this->list[$vendor][$ppd]);
90               }
91             }
92           }
93         }
94       }else{
95         $is_ok = false;
96       }
97   
98       if(!$is_ok){
99         print_red(_("Something went wrong while trying to remove the ppd file from server, possibly we have no write access."));
100       }
101     }
103     /** Added **/
104     $list = array();
105     foreach($this->list as $cat => $ppds){
106       foreach($ppds as $ppd){
107         if(preg_match("/^".str_replace("*",".*",$regex)."/i",$ppd['ppd'])){
108           if(is_readable($ppd['link'])){
109             $list[$ppd['link']] = $ppd;
110           }
111         }
112       }
113     }
116     /* Open vendor folder */ 
117     if((isset($_GET['act']))&&($_GET['act']=="open"))  {
118       if((!empty($_GET['id'])) && (!isset( $this->header[base64_decode($_GET['id'])]))){
119         print_red(sprintf(_("Selected vendor '%s' does not exists in our list of ppds."),base64_decode($_GET['id'])));      
120         $this->Vendor = ""; 
121       }else{
122         $this->Vendor = base64_decode($_GET['id']);
123       }
124     }
126     $div = new divSelectBox("printerPPDSelectionDialog");
127     $div ->SetHeight(450);
128     $div ->SetSummary(_("Printer ppd selection."));
129     
130     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>
131       <img src='images/folder.png' class='center' border=0>&nbsp;%s
132       </a>";
133     $uselink  = "<a href='?plug=".$_GET['plug']."&amp;act=use&amp;id=%s'>%s</a>";
134     $dellink  = "<a href='?plug=".$_GET['plug']."&amp;act=del&amp;id=%s'>
135       <img src='images/edittrash.png' alt='"._("delete")."' border=0>
136       </a>";
138     if(empty($this->Vendor)){
139       foreach($this-> header as $key => $entry){
140         $div ->AddEntry (array(
141               array("string"=>sprintf($linkopen,base64_encode($key),$key),"attach"=>"style='border-right:0px;'")
142               ));
143       }
144     }else{
145       $div ->AddEntry (array(
146             array("string"=>sprintf($linkopen,"",".. ["._("back")."]"),"attach"=>"style='border-right:0px;'")
147             ));
148       foreach($list as $key => $ppd){
149         if(preg_match("/^".$this->Vendor."/",$ppd['ppd'])){
151           if(is_writeable($ppd['link'])){
152             $del_str = sprintf($dellink,base64_encode($key));
153           }else{
154             $del_str = "";
155           }
156     
157           $div ->AddEntry (array(
158                                   array("string"=>sprintf($uselink,base64_encode($key),$ppd['ppd'])),
159                                   array("string"=>$del_str,"attach"=>"style='border-right:0px;'")
160                 ));
161         }
162       }
163     }
165     $smarty->assign("List",         $div -> DrawList());
166     $smarty->assign("search_image", get_template_path('images/search.png'));
167     $smarty->assign("launchimage",  get_template_path('images/small_filter.png'));
168     $smarty->assign("tree_image",   get_template_path('images/tree.png'));
169     $smarty->assign("alphabet",     generate_alphabet());
170     $smarty->assign("apply",        apply_filter());
171     $smarty->assign("regex",        $regex);
172   
173     $display.= $smarty->fetch(get_template_path('printerPPDSelectionDialog.tpl', TRUE,dirname(__FILE__)));
174     return($display);
175   }
177   function save_object()
178   {
179   } 
181   function check(){
182   }
184   /* Save to LDAP */
185   function save()
186   {
187     return $this->selectedPPD;
188   }
190 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
191 ?>