Code

Updated trunk, introduced gosa-core
[gosa.git] / plugins / addons / gotomasses / class_target_list.inc
1 <?php
3 class target_list extends MultiSelectWindow
4 {
5   var $config;
6   var $list         =array();
7   var $Targets_used =array();
9   /* Current base */
10   var $selectedBase       = "";
11   var $departments        = array();
13   /* Regex */
14   var $Regex              = "*";
15   var $IP_start           = "0.0.0.0";
16   var $IP_end             = "255.255.255.255";
18   /* CheckBoxes, to change default values modify $this->AddCheckBox */
19   var $ogroups        ;
20   var $servers        ;
21   var $workstations   ;
24   /* Subsearch checkbox */
25   var $SubSearch          ;
26   var $IPMatch            ;
27   var $parent             ;
28   var $ui                 ;
29  
30  
31   function target_list(&$config,$Targets_used)
32   {
33     MultiSelectWindow::MultiSelectWindow($config, "Targetselection", array("ogroup","gotomasses"));
35     $this->Targets_used = $Targets_used;
36     
37     $this->SetInformation(  _("Select the target objects for your scheduled action."));
38     $this->SetTitle(        _("Available targets"));
39     $this->SetSummary(      _("Available targets"));
40     $this->SetHeadpageMode(FALSE);
41     
42     /* set Page header */
43     $chk = "<input type='checkbox' id='select_all' name='select_all'
44                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
45     $this->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
47     $this->AddHeader(array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"));
48     $this->AddHeader(array("string" => _("Object name"), "attach" => "style=''"));
50     /* Text ,Value, Name, Is selected */
51     $this->AddCheckBox("ogroups",       _("Select to see object groups"),  _("Show object groups"), true);
52     $this->AddCheckBox("servers",       _("Select to see servers")  ,   _("Show servers"), true);
53     $this->AddCheckBox("workstations",  _("Select to see workstations"),_("Show workstations"), true);
55     /* Add SubSearch checkbox */
56     $this->AddCheckBox(SEPERATOR);
57     $this->AddCheckBox("SubSearch",  _("Select to search within subtrees"), _("Search in subtrees"), false);
58     $this->AddCheckBox("IPMatch",    _("Select to search for a specific IP range only"), _("Match IP range"), false);
60     /* Name,Text,Default  , Connect with alphabet  */
61     $this->AddRegex   ("Regex",     _("Regular expression for matching group names"), "*" , true);
62     $this->AddRegex   ("IP_start",  _("IP range start"),  "0.0.0.0" , true);
63     $this->AddRegex   ("IP_end",    _("IP range end"),    "255.255.255.255" , true);
64     $this->EnableAplhabet(TRUE);
65   }
68   function GenHeader()
69   {
70     $options= "";
72     /* Get all departments within this subtree */
73     $ui= get_userinfo();
74     $first = "";
75     $found = FALSE;
76     $base = $this->config->current['BASE'];
78     /* Add base */
79     $tmp = array();
80     $tmp[] = array("dn"=>$this->config->current['BASE']);
81     $tmp=  array_merge($tmp,get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->module, $base,
82                     array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
84     $deps = array();
85     foreach($tmp as $tm){
86       $deps[$tm['dn']] = $tm['dn'];
87     }
89     /* Load possible departments */
90     $ui= get_userinfo();
91     $tdeps= $ui->get_module_departments("ogroups");
92     $ids = $this->config->idepartments;
93     $first = "";
94     $found = FALSE;
95     foreach($ids as $dep => $name){
96       if(isset($deps[$dep]) && in_array_ics($dep, $tdeps)){
98         /* Keep first base dn in mind, we could need this
99          *  info if no valid base was found
100          */
101         if(empty($first)) {
102           $first = $dep['dn'];
103         }
105         $value = $ids[$dep];
106         if ($this->selectedBase == $dep){
107           $found = TRUE;
108           $options.= "<option selected='selected' value='".$dep."'>$value</option>";
109         } else {
110           $options.= "<option value='".$dep."'>$value</option>";
111         }
112       }
113     }
115     /* The currently used base is not visible with your acl setup.
116      * Set base to first useable base.
117      */
118     if(!$found){
119       $this->selectedBase = $first;
120     }
122     /* Get copy & paste icon */
123     $acls  = $ui->get_permissions($this->selectedBase,"ogroups/ogroup");
124     $acl_all= $ui->has_complete_category_acls($this->selectedBase,"ogroups");
126     /* Add default header */
127     $listhead = MultiSelectWindow::get_default_header();
129     /* Add department selector */
130     $listhead .= _("Base")."&nbsp;<select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
131       " <input class='center' type='image' src='images/list_submit.png' align='middle'
132       title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;";
133     $listhead .="</div>";
135     $this->SetListHeader($listhead);
136   }
139   function execute()
140   {
141     $this->ClearElementsList();
142     $this->GenHeader();
143     $this->reload();
144     $this->SetEntries($this->list);
145     return($this->Draw());
146   }
148   
149   function SetEntries($list)
150   {
151       /* Add Copy & Paste buttons if copy&paste is enabled
152      */
153     // Defining Links
154     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
155     $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
157     $ui = get_userinfo();
159     // Assigning ogroups
160     foreach($list as $key => $val){
162       if(in_array($val['cn'][0],$this->Targets_used) || 
163          isset($val['macAddress'][0]) && in_array($val['macAddress'][0],$this->Targets_used)) continue;
165       $title = "title='".preg_replace('/ /', '&nbsp;', @LDAP::fix($val['dn']))."'";
166       if(!isset($val['description'][0])){
167         $desc = "";
168       }else{
169         $desc = " - [ ".$val['description'][0]." ]";
170       }
171       if(!isset($val['ipHostNumber'][0])){
172         $desc.= "";
173       }else{
174         $desc.= " - ".$val['ipHostNumber'][0]."";
175       }
178       $img ="Hmm";
179       if(in_array("goServer",$val['objectClass'])){
180         $img = "<img src='images/select_server.png' alt='"._("Server")."' ".$title.">";
181       }elseif(in_array("gotoWorkstation",$val['objectClass'])){
182         $img = "<img src='images/select_workstation.png' alt='"._("Workstation")."' ".$title.">";
183       }elseif(in_array("gosaGroupOfNames",$val['objectClass'])){
184         $img = "<img src='images/list_ogroup.png' alt='"._("Object group")."' ".$title.">";
185       }
187       /* Create each field */
188       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>" ,
189                       "attach" => "style='width:20px;'");
190       $field1 = array("string" => $img, 
191                       "attach" => "style='text-   align:center;width: 20px;'");
192       $field2 = array("string" => $val['cn'][0].$desc, 
193                       "attach" => "style='border-right:0px;' ".$title);
195       $this->AddElement(array($field0,$field1,$field2));
196     }
197   }
199   
200   function save()
201   { 
202     $ret = array();
203     $items = $this->list_get_selected_items();
204     foreach($items as $item){
205       $ret[] = $this->list[$item];
206     }
207     return($ret);
208   }
211   function save_object()
212   {
213     MultiSelectWindow::save_object();
214   }
216   
217   function reload()
218   {
219     /* Set base for all searches && initialise some vars */
220     $this->list= array();
221     $base     = $this->selectedBase;
222     $filter   = "";
223     $Regex    = $this->Regex;
224     $IP_start = $this->IP_start;
225     $IP_end   = $this->IP_end;
227     if($this->IPMatch){
228       if(!is_ip($IP_start)){
229         print_red(_("Please specify a valid IP range."));
230         return;
231       }
232       if(!is_ip($IP_end)){
233         print_red(_("Please specify a valid IP range."));
234         return;
235       }
236     }
237   
239     $chk = array(
240         "ogroups"       => "(&(objectClass=gosaGroupOfNames)(|(gosaGroupObjects=*S*)(gosaGroupObjects=*W*)))" ,
241         "servers"       => "(objectClass=goServer)" ,
242         "workstations"  => "(objectClass=gotoWorkstation)");
244     /* Create filter */
245     foreach($chk as $chkBox => $FilterPart){
246       if($this->$chkBox){
247         $filter .=  $FilterPart;
248       }
249     }
250     $filter= "(&(cn=".$Regex.")(|".$filter."))";
252     if($this->SubSearch){
253       $res= get_list($filter, array("ogroups","workstations","servers"), $base, 
254             array("cn","objectClass","gosaGroupObjects","ipHostNumber","description"), GL_SIZELIMIT | GL_SUBSEARCH);
255     }else{
256       $res= get_list($filter, "ogroups", get_groups_ou().$base, 
257             array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT );
258       $res= array_merge($res,get_list($filter, "workstation",  "ou=workstations,ou=systems,".$base, 
259             array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT ));
260       $res= array_merge($res,get_list($filter, "server", "ou=servers,ou=systems,".$base, 
261             array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT ));
262     }
264     $this->list= $res;
265     ksort ($this->list);
266     reset ($this->list);
267     $tmp=array();
268     foreach($this->list as $tkey => $val ){
270       if($this->IPMatch){
271         if(isset($val['ipHostNumber'][0])){
272           if(is_ip_range($IP_start,$val['ipHostNumber'][0]) && is_ip_range($val['ipHostNumber'][0],$IP_end)){
273             $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
274           }
275         }
276       }else{
277         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
278       }
279     }
280     ksort($tmp);
281     $this->list=array();
282     foreach($tmp as $val){
283       $this->list[]=$val;
284     }
285     reset ($this->list);
286   }
288   function list_get_selected_items()
289   {
290     $ids = array();
291     foreach($_POST as $name => $value){
292       if(preg_match("/^item_selected_[0-9]*$/",$name)){
293         $id   = preg_replace("/^item_selected_/","",$name);
294         $ids[$id] = $id;
295       }
296     }
297     return($ids);
298   }
302 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
303 ?>