1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class EventTargetAddList extends MultiSelectWindow
24 {
25 public $display_server = TRUE;
26 public $display_workstation = TRUE;
27 public $display_ogroup = TRUE;
28 public $filter_iprange = FALSE;
30 public $regex = "*";
31 public $ipfrom = "0.0.0.0";
32 public $ipto = "*";
33 public $_target_list = array();
35 public $workstation_list = array();
36 public $server_list = array();
39 function __construct(&$config,$parent)
40 {
41 MultiSelectWindow::MultiSelectWindow($config, "EventTargetAddList",
42 array("server",
43 "workstation",
44 "ogroups"));
46 $this->parent = $parent;
47 $this->ui = get_userinfo();
50 $this->target_divlist = new MultiSelectWindow($this->config,"EventAddTargedtList","gotomasses");
51 $this->SetSummary(_("Targets"));
52 $this->EnableCloseButton(FALSE);
53 $this->EnableSaveButton(FALSE);
55 $this->SetInformation(_("This dialog shows all available targets for the event. Check the targets you want to add and use the 'Apply' button."));
57 /* Toggle all selected / deselected */
58 $chk = "<input type='checkbox' id='select_all' name='select_all'
59 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
61 $this->EnableAplhabet(TRUE);
63 /* set Page header */
64 $this->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
65 $this->AddHeader(array("string"=>" ","attach"=>"style='width:20px;'"));
66 $this->AddHeader(array("string"=>_("System / Department")));
68 //$name,$string,$value,$conn,$image="images/lists/search.png")
69 $this->AddRegex("regex" ,"regex" ,"*" , TRUE);
70 $this->AddRegex("ipfrom","ipfrom" ,"0.0.0.0" , FALSE);
71 $this->AddRegex("ipto" ,"ipto" ,"255.255.255.255" , FALSE);
73 $this->AddCheckBox("display_server","1" ,_("Display server"),TRUE);
74 $this->AddCheckBox("display_workstation","1",_("Display workstation"),TRUE);
75 $this->AddCheckBox("display_ogroup","1" ,_("Display object groups"),TRUE);
76 $this->AddCheckBox("filter_iprange","1" ,_("Filter by IP range"),FALSE);
80 /* Create a list of servers
81 */
82 $tmp = get_sub_list("(&(macAddress=*)(objectClass=goServer))",
83 "server",get_ou("serverRDN"),$config->current['BASE'],
84 array("cn","objectClass","description","ipHostNumber","macAddress"),GL_SUBSEARCH);
85 foreach($tmp as $server){
86 $this->server_list[$server['dn']] = $server;
87 }
89 /* Create a list of workstations
90 */
91 $tmp = get_sub_list("(&(macAddress=*)(objectClass=gotoWorkstation))",
92 "server",get_ou("workstationRDN"),$config->current['BASE'],
93 array("cn","objectClass","description","ipHostNumber","macAddress"),GL_SUBSEARCH);
94 foreach($tmp as $server){
95 $this->workstation_list[$server['dn']] = $server;
96 }
98 }
101 function execute()
102 {
103 $this->ClearElementsList();
104 $this->AddDepartments($this->selectedBase,2,1);
105 $this->setEntries();
106 $this->GenHeader();
107 }
110 function GenHeader()
111 {
112 $modules = array("server","workstation");
114 /* Add base */
115 $tmp = array();
116 $base = $this->config->current['BASE'];
117 $tmp[] = array("dn"=>$this->config->current['BASE']);
118 $tmp= array_merge($tmp,get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $modules, $base,
119 array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
121 $deps = array();
122 foreach($tmp as $tm){
123 $deps[$tm['dn']] = $tm['dn'];
124 }
126 $department = $departments = array();
127 $ui= get_userinfo();
128 $d = $ui->get_module_departments($modules);
129 foreach($d as $department){
130 $departments[$department] = $department;
131 }
133 /* Load possible departments */
134 $ids = $this->config->idepartments;
135 $first = "";
136 $found = FALSE;
137 $options = array();
138 foreach($ids as $dep => $name){
139 if(isset($deps[$dep]) && in_array_ics($dep, $departments)){
141 /* Keep first base dn in mind, we could need this
142 * info if no valid base was found
143 */
144 if(empty($first)) {
145 $first = $dep['dn'];
146 }
148 $value = $ids[$dep];
149 if ($this->selectedBase == $dep){
150 $found = TRUE;
151 $options.= "<option selected='selected' value='".$dep."'>$value</option>";
152 } else {
153 $options.= "<option value='".$dep."'>$value</option>";
154 }
155 }
156 }
158 $listhead = $this->get_default_header();
160 /* Add base selection */
161 $listhead .= _("Base")." <select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
162 " <input class='center' type='image' src='images/lists/submit.png' align='middle'
163 title='"._("Submit department")."' name='submit_department' alt='". _("Submit")."'> ";
165 $this->SetListHeader($listhead);
167 }
170 function get_selected_targets()
171 {
172 $a_targets = array();
173 foreach($this->list_get_selected_items() as $id){
174 if(in_array("gosaGroupOfNames",$this->_target_list[$id]['objectClass'])){
175 foreach($this->_target_list[$id]['member'] as $mem_dn){
176 if(isset($this->workstation_list[$mem_dn])){
177 $a_targets[] = $this->workstation_list[$mem_dn]['macAddress'][0];
178 }
179 if(isset($this->server_list[$mem_dn])){
180 $a_targets[] = $this->server_list[$mem_dn]['macAddress'][0];
181 }
182 }
183 }else{
184 if(isset($this->_target_list[$id]['macAddress'][0])){
185 $a_targets[] = $this->_target_list[$id]['macAddress'][0];
186 }
187 }
188 }
189 return($a_targets);
190 }
193 function setEntries()
194 {
195 $_target_list = array();
196 if($this->display_server){
197 $_target_list = array_merge($_target_list,
198 get_list("(&(cn=".$this->regex.")(objectClass=goServer))",
199 "server",get_ou("serverRDN").$this->selectedBase,
200 array("cn","objectClass","description","ipHostNumber","macAddress"),GL_NONE));
201 }
202 if($this->display_workstation){
203 $_target_list = array_merge($_target_list,
204 get_list("(&(cn=".$this->regex.")(objectClass=gotoWorkstation))",
205 "workstation",get_ou("workstationRDN").$this->selectedBase,
206 array("cn","objectClass","description","ipHostNumber","macAddress"),GL_NONE));
207 }
208 if($this->display_ogroup){
209 $_target_list = array_merge($_target_list,
210 get_list("(&(cn=".$this->regex.")(member=*)(objectClass=gosaGroupOfNames))",
211 "ogroups",get_ou("ogroupRDN").$this->selectedBase,
212 array("cn","objectClass","description","member"),GL_NONE));
213 }
214 $this->_target_list = $_target_list;
216 $tmp = array();
217 foreach($this->_target_list as $key => $object){
218 $tmp[$key] = $object['cn'][0];
219 }
220 natcasesort($tmp);
222 foreach($tmp as $key => $obj){
224 $obj = $this->_target_list[$key];
225 $name = $obj['cn'][0];
226 if(isset($obj['description'])){
227 $name .= " [".$obj['description'][0]."]";
228 }
229 if(isset($obj['macAddress'])){
230 $name .= " - ".$obj['macAddress'][0]."";
231 }
232 if(isset($obj['ipHostNumber'])){
233 $name .= " - ".$obj['ipHostNumber'][0]."";
234 }
236 $img ="";
237 if(in_array("goServer",$obj['objectClass'])){
238 $img = '<img class="center" src="plugins/systems/images/select_server.png" alt="S" title="'._("Server").'">';
240 if($this->filter_iprange){
241 if(!isset($obj['ipHostNumber']) || !tests::is_in_ip_range($this->ipfrom,$this->ipto, $obj['ipHostNumber'][0])) {
242 continue;
243 }
244 }
245 if(!isset($this->server_list[$obj['dn']])){
246 continue;
247 }
248 }elseif(in_array("gotoWorkstation",$obj['objectClass'])){
249 $img = '<img class="center" src="plugins/systems/images/select_workstation.png" alt="W" title="'._("Workstation").'">';
250 if($this->filter_iprange){
251 if(!isset($obj['ipHostNumber']) || !tests::is_in_ip_range($this->ipfrom,$this->ipto,$obj['ipHostNumber'][0])) {
252 continue;
253 }
254 }
255 if(!isset($this->workstation_list[$obj['dn']])){
256 continue;
257 }
258 }elseif(in_array("gosaGroupOfNames",$obj['objectClass'])){
259 $img = '<img class="center" src="plugins/ogroups/images/select_ogroup.png" alt="O" title="'._("Object group").'">';
260 }
262 $field1 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>",
263 "attach" => "style='width:20px;'");
264 $field2 = array("string" => $img,
265 "attach" => "style='width:20px;'");
266 $field3 = array("string" => $name , "attach" => "title='".$obj['dn']."'");
267 $this->AddElement(array($field1,$field2,$field3));
268 }
269 }
272 /*! \brief Returns a set of elements selected in a MultiSelectWindow
273 @return Array[integer]=integer
274 */
275 protected function list_get_selected_items()
276 {
277 $ids = array();
278 foreach($_POST as $name => $value){
279 if(preg_match("/^item_selected_[0-9]*$/",$name)){
280 $id = preg_replace("/^item_selected_/","",$name);
281 $ids[$id] = $id;
282 }
283 }
284 return($ids);
285 }
286 }
287 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
288 ?>