Code

26206cba888eaf1936c8a6c88f69a49223633e33
[gosa.git] / gosa-plugins / sudo / admin / sudo / class_target_list_systems.inc
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: class_target_list.inc 9850 2008-03-14 14:15:12Z hickert $$
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 target_list_systems extends MultiSelectWindow
24 {
25   public $config;
26   protected $list         =array();
27   private $Targets_used =array();
29   /* Current base */
30   private $departments       = array();
32   /* Regex */
33   protected $Regex           = "*";
35   /* CheckBoxes, to change default values modify $this->AddCheckBox */
36   protected $servers        ;
37   protected $workstations   ;
38   protected $terminals      ;
41   /* Subsearch checkbox */
42   protected $SubSearch       ;
43   protected $parent          ;
44   public $ui                 ;
45  
46  
47   function target_list_systems(&$config,$Targets_used)
48   {
49     MultiSelectWindow::MultiSelectWindow($config, "Targetselection", array("server","terminal","workstation"));
51     $this->Targets_used = $Targets_used;
52     
53     $this->SetInformation(  _("Select the target objects for your scheduled action."));
54     $this->SetTitle(        _("Available targets"));
55     $this->SetSummary(      _("Available targets"));
56     $this->SetHeadpageMode(FALSE);
57     
58     /* set Page header */
59     $chk = "<input type='checkbox' id='select_all' name='select_all'
60                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
61     $this->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
63     $this->AddHeader(array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"));
64     $this->AddHeader(array("string" => _("Object name"), "attach" => "style=''"));
66     /* Text ,Value, Name, Is selected */
67     $this->AddCheckBox("terminal",     _("Select to see terminals"),   _("Show terminals"), true);
68     $this->AddCheckBox("server",       _("Select to see servers")  ,   _("Show servers"), true);
69     $this->AddCheckBox("workstation",  _("Select to see workstations"),_("Show workstations"), true);
71     /* Add SubSearch checkbox */
72     $this->AddCheckBox(SEPERATOR);
73     $this->AddCheckBox("SubSearch",  msgPool::selectToView("","subsearch"), msgPool::selectToView("","subsearch_small"), false);
75     /* Name,Text,Default  , Connect with alphabet  */
76     $this->AddRegex   ("Regex",     _("Regular expression for matching group names"), "*" , true);
77     $this->EnableAplhabet(TRUE);
78   }
81   function GenHeader()
82   {
83     $options= "";
85     /* Get all departments within this subtree */
86     $ui= get_userinfo();
87     $first = "";
88     $found = FALSE;
89     $base = $this->config->current['BASE'];
91     /* Add base */
92     $tmp = array();
93     $tmp[] = array("dn"=>$this->config->current['BASE']);
94     $tmp=  array_merge($tmp,get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->module, $base,
95                     array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
97     $deps = array();
98     foreach($tmp as $tm){
99       $deps[$tm['dn']] = $tm['dn'];
100     }
102     /* Load possible departments */
103     $tdeps= array();
104     $tdeps= array_merge($tdeps,$ui->get_module_departments("server"));
105     $tdeps= array_merge($tdeps,$ui->get_module_departments("terminal"));
106     $tdeps= array_merge($tdeps,$ui->get_module_departments("workstation"));
107     $tdeps = array_unique($tdeps); 
108  
109     $ids = $this->config->idepartments;
110     $first = "";
111     $found = FALSE;
112     foreach($ids as $dep => $name){
113       if(isset($deps[$dep]) && in_array_ics($dep, $tdeps)){
115         /* Keep first base dn in mind, we could need this
116          *  info if no valid base was found
117          */
118         if(empty($first)) {
119           $first = $dep['dn'];
120         }
122         $value = $ids[$dep];
123         if ($this->selectedBase == $dep){
124           $found = TRUE;
125           $options.= "<option selected='selected' value='".$dep."'>$value</option>";
126         } else {
127           $options.= "<option value='".$dep."'>$value</option>";
128         }
129       }
130     }
132     /* The currently used base is not visible with your acl setup.
133      * Set base to first useable base.
134      */
135     if(!$found){
136       $this->selectedBase = $first;
137     }
139     /* Add default header */
140     $listhead = MultiSelectWindow::get_default_header();
142     /* Add department selector */
143     $listhead .= _("Base")."&nbsp;<select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
144       " <input class='center' type='image' src='images/lists/submit.png' align='middle'
145       title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;";
146     $listhead .="</div>";
148     $this->SetListHeader($listhead);
149   }
152   function execute()
153   {
154     $this->ClearElementsList();
155     $this->GenHeader();
156     $this->reload();
157     $this->SetEntries($this->list);
158     return($this->Draw());
159   }
161   
162   function SetEntries($list)
163   {
164       /* Add Copy & Paste buttons if copy&paste is enabled
165      */
166     // Defining Links
167     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
168     $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
170     $ui = get_userinfo();
172     // Assigning systems 
173     foreach($list as $key => $val){
175       if(in_array($val['cn'][0],$this->Targets_used) || 
176          isset($val['macAddress'][0]) && in_array($val['macAddress'][0],$this->Targets_used)) continue;
178       $title = "title='".preg_replace('/ /', '&nbsp;', @LDAP::fix($val['dn']))."'";
179       if(!isset($val['description'][0])){
180         $desc = "";
181       }else{
182         $desc = " - [ ".$val['description'][0]." ]";
183       }
185       $img ="?";
186       if(in_array("goServer",$val['objectClass'])){
187         $img = "<img src='images/select_server.png' alt='"._("Server")."' ".$title.">";
188       }elseif(in_array("gotoWorkstation",$val['objectClass'])){
189         $img = "<img src='images/select_workstation.png' alt='"._("Workstation")."' ".$title.">";
190       }elseif(in_array("gotoTerminal",$val['objectClass'])){
191         $img = "<img src='images/select_terminal.png' alt='"._("Terminal")."' ".$title.">";
192       }
194       /* Create each field */
195       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>" ,
196                       "attach" => "style='width:20px;'");
197       $field1 = array("string" => $img, 
198                       "attach" => "style='text-   align:center;width: 20px;'");
199       $field2 = array("string" => $val['cn'][0].$desc, 
200                       "attach" => "style='border-right:0px;' ".$title);
202       $this->AddElement(array($field0,$field1,$field2));
203     }
204   }
206   
207   function save()
208   { 
209     $ret = array();
210     $items = $this->list_get_selected_items();
211     foreach($items as $item){
212       $ret[] = $this->list[$item];
213     }
214     return($ret);
215   }
218   function save_object()
219   {
220     MultiSelectWindow::save_object();
221   }
223   
224   function reload()
225   {
226     /* Set base for all searches && initialise some vars */
227     $this->list= array();
228     $base     = $this->selectedBase;
229     $filter   = "";
230     $Regex    = $this->Regex;
232     $filter= "(&(cn=".$Regex.")(|".$filter."))";
234     if($this->SubSearch){
235       $res= array();
236       if($this->terminal){
237         $filter = "(objectClass=gotoTerminal)";
238         $filter= "(&(cn=".$Regex.")(|".$filter."))";
239         $res= array_merge($res,get_sub_list($filter,"terminal",get_ou('terminalou'),$base, 
240               array("cn","objectClass","description"), GL_SIZELIMIT | GL_SUBSEARCH));
241       }
242       if($this->workstation){
243         $filter = "(objectClass=gotoWorkstation)";  
244         $filter= "(&(cn=".$Regex.")(|".$filter."))";
245         $res= array_merge($res,get_sub_list($filter, "workstation",  get_ou('workstationou'),$base, 
246               array("cn","objectClass","description"), GL_SIZELIMIT | GL_SUBSEARCH));
247       }
248       if($this->server){
249         $filter = "(objectClass=goServer)";
250         $filter = "(&(cn=".$Regex.")(|".$filter."))";
251         $res= array_merge($res,get_sub_list($filter, "server", get_ou('serverou'),$base, 
252               array("cn","objectClass","description"), GL_SIZELIMIT | GL_SUBSEARCH));
253       }
254     }else{
255       $res= array();
256       if($this->terminal){
257         $filter = "(objectClass=gotoTerminal)";
258         $filter= "(&(cn=".$Regex.")(|".$filter."))";
259         $res= array_merge($res,get_list($filter,"terminal", get_ou('terminalou').$base, 
260               array("cn","objectClass","description"), GL_SIZELIMIT ));
261       }
262       if($this->workstation){
263         $filter = "(objectClass=gotoWorkstation)";  
264         $filter= "(&(cn=".$Regex.")(|".$filter."))";
265         $res= array_merge($res,get_list($filter, "workstation",  get_ou('workstationou').$base, 
266               array("cn","objectClass","description"), GL_SIZELIMIT ));
267       }
268       if($this->server){
269         $filter = "(objectClass=goServer)";
270         $filter = "(&(cn=".$Regex.")(|".$filter."))";
271         $res= array_merge($res,get_list($filter, "server", get_ou('serverou').$base, 
272               array("cn","objectClass","description"), GL_SIZELIMIT ));
273       }
274     }
276     $this->list= $res;
277     ksort ($this->list);
278     reset ($this->list);
279     $tmp=array();
280     foreach($this->list as $tkey => $val ){
281       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
282     }
283     ksort($tmp);
284     $this->list=array();
285     foreach($tmp as $val){
286       $this->list[]=$val;
287     }
288     reset ($this->list);
289   }
291   function list_get_selected_items()
292   {
293     $ids = array();
294     foreach($_POST as $name => $value){
295       if(preg_match("/^item_selected_[0-9]*$/",$name)){
296         $id   = preg_replace("/^item_selected_/","",$name);
297         $ids[$id] = $id;
298       }
299     }
300     return($ids);
301   }
305 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
306 ?>