1 <?php
3 class selectUserToPrinterDialog extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
7 var $attributes = array();
8 var $objectclasses = array("whatever");
9 var $regex = "*";
10 var $subtree = FALSE;
11 var $depselect = "/";
12 var $deplist = "/";
14 var $searchObjects = "";
15 var $searchAttrs = "";
16 var $searchAppend = "";
17 var $baseAddition = "";
18 var $type = "";
19 var $search_cat = "";
21 var $dns_used = array();
23 function selectUserToPrinterDialog (&$config, $dn= NULL,$type=false,$already_assigned )
24 {
25 plugin::plugin ($config, $dn);
26 $this->depselect = $this->config->current['BASE'];
28 switch($type){
29 case "AddUser" :
30 $this->searchObjects = "(objectClass=gosaAccount)(!(uid=*$))";
31 $this->searchAttrs = array("cn","uid");
32 $this->searchAppend = "uid";
33 $this->baseAddition = get_people_ou();
34 $this->search_cat = "users";
35 ;break;
36 case "AddGroup" :
37 $this->searchObjects = "(objectClass=posixGroup)";
38 $this->searchAttrs = array("cn","description");
39 $this->searchAppend = "cn";
40 $this->baseAddition = get_groups_ou();
41 $this->search_cat = "groups";
42 ;break;
43 case "AddAdminUser" :
44 $this->searchObjects = "(objectClass=gosaAccount)(!(uid=*$))";
45 $this->searchAttrs = array("cn","uid");
46 $this->searchAppend = "uid";
47 $this->baseAddition = get_people_ou();
48 $this->search_cat = "users";
49 ;break;
50 case "AddAdminGroup" :
51 $this->searchObjects = "(objectClass=posixGroup)";
52 $this->searchAttrs = array("cn","description");
53 $this->searchAppend = "cn";
54 $this->baseAddition = get_groups_ou();
55 $this->search_cat = "groups";
56 ;break;
57 }
58 $this->type = $type;
60 /* Collect already assigned object dns */
61 $this->dns_used = array();
62 if(isset($already_assigned[$type])){
63 foreach($already_assigned[$type] as $entry){
64 $this->dns_used[] = $entry['dn'];
65 }
66 }
67 }
69 function execute()
70 {
71 /* Call parent execute */
72 plugin::execute();
74 /* Fill templating stuff */
75 $smarty= get_smarty();
76 $display= "";
78 if(isset($_POST['dialogissubmitted'])){
79 $this->regex=$_POST['regexPrinter'];
80 $this->depselect = $_POST['depselectPrinter'];
81 }
82 $this->subtree= isset($_POST['SubSearch']);
84 if((isset($_GET['search']))&&(!empty($_GET['search']))){
85 $this->regex=$_GET['search']."*";
86 $this->regex=preg_replace("/\*\*/","*",$this->regex);
87 }
90 /* Get all departments within this subtree */
91 $base = $this->config->current['BASE'];
92 $deps_res[] = array("dn"=>$this->config->current['BASE']);
93 $deps_res= array_merge($deps_res,get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", array("users","groups"), $this->config->current['BASE'],
94 array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
96 /* Load possible departments */
97 $ui = get_userinfo();
99 /* Allowed user departments */
100 $udeps = $ui->get_module_departments("users");
102 /* Allowed groups department */
103 $gdeps = $ui->get_module_departments("groups");
105 /* Combine both arrays */
106 $tdeps = array_unique(array_merge($udeps,$gdeps));
108 /* Create usable departments array */
109 $ids = $this->config->idepartments;
110 $deps = array();
111 foreach($deps_res as $dep){
112 if(isset($ids[$dep['dn']]) && in_array_ics($dep['dn'], $tdeps)){
113 $value = $ids[$dep['dn']];
114 $deps[$dep['dn']] = $value;
115 }
116 }
117 if(!isset($deps[$this->depselect])){
118 $this->depselect = key($deps);
119 }
122 $tmp_printers= $this->getPrinter();
123 natcasesort($tmp_printers);
124 $smarty->assign("regexPrinter" , $this->regex);
125 $smarty->assign("deplistPrinter" , $deps);;//deplist);
126 $smarty->assign("depselectPrinter" , $this->depselect);
127 $smarty->assign("gotoPrinters" , array_values($tmp_printers));
128 $smarty->assign("gotoPrinterKeys" , array_keys($tmp_printers));
129 $smarty->assign("apply" , apply_filter());
130 $smarty->assign("alphabet" , generate_alphabet());
131 $smarty->assign("search_image" , get_template_path('images/lists/search.png'));
132 $smarty->assign("tree_image" , get_template_path('images/lists/search-subtree.png'));
133 $smarty->assign("infoimage" , get_template_path('images/info.png'));
134 $smarty->assign("launchimage" , get_template_path('images/lists/action.png'));
135 $smarty->assign("deplist" , $deps);
136 $smarty->assign("subtree", $this->subtree?"checked":"");
138 $display.= $smarty->fetch(get_template_path('selectUserToPrinterDialog.tpl', TRUE,dirname(__FILE__)));
139 return($display);
140 }
142 function check(){
143 /* Call common method to give check the hook */
144 $message= plugin::check();
146 if(empty($_POST['gotoPrinter'])){
147 $message[] = _("Please select a printer or press cancel.");
148 }
149 return $message;
150 }
152 /* Save to LDAP */
153 function save()
154 {
155 $a_return['type']=$this->type;
157 foreach($_POST['gotoPrinter'] as $name){
158 $data = $this->getPrinter(true);
159 $a_return[$name]= $data[$name];
160 }
161 return($a_return);
162 }
164 /* This function generates the Printerlist
165 * All printers are returned that match regex and and depselect
166 */
167 function getPrinter($detailed = false)
168 {
169 $a_return=array();
171 $filter = "(&".$this->searchObjects."(cn=".$this->regex."))";
172 $base = $this->baseAddition.$this->depselect;
173 $attrs = $this->searchAttrs;
174 $cat = $this->search_cat;
176 if ($this->subtree){
177 $res= get_list($filter,$cat,$this->depselect,$attrs, GL_SUBSEARCH);
178 } else {
179 $res= get_list($filter,$cat,$base,$attrs);
180 }
181 foreach($res as $printer){
183 /* Skip already used entries */
184 if(in_array($printer['dn'],$this->dns_used)) {
185 continue;
186 }
188 if(($detailed ==true)){
189 if(isset($printer[$this->searchAppend])){
190 $a_return[$printer[$this->searchAppend][0]] = $printer;
191 }
192 }else{
193 if(isset($printer[$this->searchAppend])){
194 if(isset($printer['description'][0])){
195 $a_return[$printer[$this->searchAppend][0]] = $printer['cn'][0]." - ".$printer['description'][0];
196 }else{
197 $a_return[$printer[$this->searchAppend][0]] = $printer['cn'][0];
198 }
199 }
200 }
201 }
202 return($a_return);
203 }
204 }
206 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
207 ?>