Code

* Created "old" branch and moved stuff
[gosa.git] / branches / old / gosa-plugins / goto / addons / goto / class_target_list.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$$
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 extends MultiSelectWindow
24 {
25   var $config;
26   var $list         =array();
27   var $Targets_used =array();
29   /* Current base */
30   var $selectedBase       = "";
31   var $departments        = array();
33   /* Regex */
34   var $Regex              = "*";
35   var $IP_start           = "0.0.0.0";
36   var $IP_end             = "255.255.255.255";
38   /* CheckBoxes, to change default values modify $this->AddCheckBox */
39   var $ogroups        ;
40   var $servers        ;
41   var $workstations   ;
42   var $incoming       ;
45   /* Subsearch checkbox */
46   var $SubSearch          ;
47   var $IPMatch            ;
48   var $parent             ;
49   var $ui                 ;
50  
51  
52   function target_list(&$config,$Targets_used)
53   {
54     MultiSelectWindow::MultiSelectWindow($config, "Targetselection", array("ogroup","server","incoming","workstation","gotomasses"));
56     $this->Targets_used = $Targets_used;
57     
58     $this->SetInformation(  _("Select the target objects for your scheduled action."));
59     $this->SetTitle(        _("Available targets"));
60     $this->SetSummary(      _("Available targets"));
61     $this->SetHeadpageMode(FALSE);
62     
63     /* set Page header */
64     $chk = "<input type='checkbox' id='select_all' name='select_all'
65                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
66     $this->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
68     $this->AddHeader(array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"));
69     $this->AddHeader(array("string" => _("Object name"), "attach" => "style=''"));
71     /* Text ,Value, Name, Is selected */
72     $this->AddCheckBox("ogroups",       _("Select to see object groups"),  _("Show object groups"), true);
73     $this->AddCheckBox("servers",       _("Select to see servers")  ,   _("Show servers"), true);
74     $this->AddCheckBox("workstations",  _("Select to see workstations"),_("Show workstations"), true);
75     $this->AddCheckBox("incoming",      _("Select to see incoming objects")  , _("Show new objects"), true);
77     /* Add SubSearch checkbox */
78     $this->AddCheckBox(SEPERATOR);
79     $this->AddCheckBox("SubSearch",  msgPool::selectToView("","subsearch"), msgPool::selectToView("","subsearch_small"), false);
80     $this->AddCheckBox("IPMatch",    _("Select to search for a specific IP range only"), _("Match IP range"), false);
82     /* Name,Text,Default  , Connect with alphabet  */
83     $this->AddRegex   ("Regex",     _("Regular expression for matching group names"), "*" , true);
84     $this->AddRegex   ("IP_start",  _("IP range start"),  "0.0.0.0" , true);
85     $this->AddRegex   ("IP_end",    _("IP range end"),    "255.255.255.255" , true);
86     $this->EnableAplhabet(TRUE);
87   }
90   function GenHeader()
91   {
92     $options= "";
94     /* Get all departments within this subtree */
95     $ui= get_userinfo();
96     $first = "";
97     $found = FALSE;
98     $base = $this->config->current['BASE'];
100     /* Add base */
101     $tmp = array();
102     $tmp[] = array("dn"=>$this->config->current['BASE']);
103     $tmp=  array_merge($tmp,get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->module, $base,
104                     array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
106     $deps = array();
107     foreach($tmp as $tm){
108       $deps[$tm['dn']] = $tm['dn'];
109     }
111     /* Load possible departments */
112     $ui= get_userinfo();
113     $tdeps= $ui->get_module_departments("ogroups");
114     $ids = $this->config->idepartments;
115     $first = "";
116     $found = FALSE;
117     foreach($ids as $dep => $name){
118       if(isset($deps[$dep]) && in_array_ics($dep, $tdeps)){
120         /* Keep first base dn in mind, we could need this
121          *  info if no valid base was found
122          */
123         if(empty($first)) {
124           $first = $dep['dn'];
125         }
127         $value = $ids[$dep];
128         if ($this->selectedBase == $dep){
129           $found = TRUE;
130           $options.= "<option selected='selected' value='".$dep."'>$value</option>";
131         } else {
132           $options.= "<option value='".$dep."'>$value</option>";
133         }
134       }
135     }
137     /* The currently used base is not visible with your acl setup.
138      * Set base to first useable base.
139      */
140     if(!$found){
141       $this->selectedBase = $first;
142     }
144     /* Get copy & paste icon */
145     $acls  = $ui->get_permissions($this->selectedBase,"ogroups/ogroup");
146     $acl_all= $ui->has_complete_category_acls($this->selectedBase,"ogroups");
148     /* Add default header */
149     $listhead = MultiSelectWindow::get_default_header();
151     /* Add department selector */
152     $listhead .= _("Base")."&nbsp;<select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
153       " <input class='center' type='image' src='images/lists/submit.png' align='middle'
154       title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;";
155     $listhead .="</div>";
157     $this->SetListHeader($listhead);
158   }
161   function execute()
162   {
163     $this->ClearElementsList();
164     $this->GenHeader();
165     $this->reload();
166     $this->SetEntries($this->list);
167     return($this->Draw());
168   }
170   
171   function SetEntries($list)
172   {
173       /* Add Copy & Paste buttons if copy&paste is enabled
174      */
175     // Defining Links
176     $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
178     $ui = get_userinfo();
180     // Assigning ogroups
181     foreach($list as $key => $val){
183       if(in_array($val['cn'][0],$this->Targets_used) || 
184          isset($val['macAddress'][0]) && in_array($val['macAddress'][0],$this->Targets_used)) continue;
186       $title = "title='".preg_replace('/ /', '&nbsp;', @LDAP::fix($val['dn']))."'";
187       if(!isset($val['description'][0])){
188         $desc = "";
189       }else{
190         $desc = " - [ ".$val['description'][0]." ]";
191       }
192       if(!isset($val['ipHostNumber'][0])){
193         $desc.= "";
194       }else{
195         $desc.= " - ".$val['ipHostNumber'][0]."";
196       }
199       $img ="Hmm";
200       if(in_array("goServer",$val['objectClass'])){
201         $img = "<img src='plugins/systems/images/select_server.png' alt='"._("Server")."' ".$title.">";
202       }elseif(in_array("gotoWorkstation",$val['objectClass'])){
203         $img = "<img src='plugins/systems/images/select_workstation.png' alt='"._("Workstation")."' ".$title.">";
204       }elseif(in_array("gosaGroupOfNames",$val['objectClass'])){
205         $img = "<img src='plugins/ogroups/images/list_ogroup.png' alt='"._("Object group")."' ".$title.">";
206       }
208       /* Create each field */
209       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>" ,
210                       "attach" => "style='width:20px;'");
211       $field1 = array("string" => $img, 
212                       "attach" => "style='text-   align:center;width: 20px;'");
213       $field2 = array("string" => $val['cn'][0].$desc, 
214                       "attach" => "style='border-right:0px;' ".$title);
216       $this->AddElement(array($field0,$field1,$field2));
217     }
218   }
220   
221   function save()
222   { 
223     $ret = array();
224     $items = $this->list_get_selected_items();
225     foreach($items as $item){
226       $ret[] = $this->list[$item];
227     }
228     return($ret);
229   }
232   function save_object()
233   {
234     MultiSelectWindow::save_object();
235   }
237   
238   function reload()
239   {
240     /* Set base for all searches && initialise some vars */
241     $this->list= array();
242     $base     = $this->selectedBase;
243     $filter   = "";
244     $Regex    = $this->Regex;
245     $IP_start = $this->IP_start;
246     $IP_end   = $this->IP_end;
248     if($this->IPMatch){
249       if(!tests::is_ip($IP_start)){
250         msg_dialog::display(_("Error"), msgPool::invalid(_("IP range")), ERROR_DIALOG);
251         return;
252       }
253       if(!tests::is_ip($IP_end)){
254         msg_dialog::display(_("Error"), msgPool::invalid(_("IP range")), ERROR_DIALOG);
255         return;
256       }
257     }
258   
260     $chk = array(
261         "ogroups"       => "(&(objectClass=gosaGroupOfNames)(|(gosaGroupObjects=*S*)(gosaGroupObjects=*W*)))" ,
262         "servers"       => "(objectClass=goServer)" ,
263         "incoming"      => "(objectClass=GOhard)" ,
264         "workstations"  => "(objectClass=gotoWorkstation)");
266     /* Create filter */
267     foreach($chk as $chkBox => $FilterPart){
268       if($this->$chkBox){
269         $filter .=  $FilterPart;
270       }
271     }
272     $filter= "(&(cn=".$Regex.")(|".$filter."))";
274     if($this->SubSearch){
275       $res= get_list($filter, array("ogroups","workstations","servers"), $base, 
276             array("cn","objectClass","gosaGroupObjects","ipHostNumber","description"), GL_SIZELIMIT | GL_SUBSEARCH);
277     }else{
278       $res= get_list($filter, "ogroups", get_groups_ou().$base, 
279             array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT );
280       $res= array_merge($res,get_list($filter, "workstation",  get_ou('workstationou').$base, 
281             array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT ));
282       $res= array_merge($res,get_list($filter, "server", get_ou('serverou').$base, 
283             array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT ));
285       $deps_a = array(
286           get_ou("workstationou"),
287           get_ou("incominou"),
288           get_ou("serverou"),
289           get_ou("ogroupou"));
291       $res = get_sub_list($filter,array("server","incoming","workstation","ogroup"), 
292                   $deps_a,get_ou("systemsou").$base,array("cn","objectClass","ipHostNumber","description"),GL_SIZELIMIT);
293     }
295     $this->list= $res;
296     ksort ($this->list);
297     reset ($this->list);
298     $tmp=array();
299     foreach($this->list as $tkey => $val ){
301       if($this->IPMatch){
302         if(isset($val['ipHostNumber'][0])){
303           if(tests::is_ip_range($IP_start,$val['ipHostNumber'][0]) && tests::is_ip_range($val['ipHostNumber'][0],$IP_end)){
304             $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
305           }
306         }
307       }else{
308         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
309       }
310     }
311     ksort($tmp);
312     $this->list=array();
313     foreach($tmp as $val){
314       $this->list[]=$val;
315     }
316     reset ($this->list);
317   }
319   function list_get_selected_items()
320   {
321     $ids = array();
322     foreach($_POST as $name => $value){
323       if(preg_match("/^item_selected_[0-9]*$/",$name)){
324         $id   = preg_replace("/^item_selected_/","",$name);
325         $ids[$id] = $id;
326       }
327     }
328     return($ids);
329   }
333 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
334 ?>