Code

Apply fix for #4271
[gosa.git] / trunk / gosa-plugins / goto / addons / goto / events / class_EventTargetAddList.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 EventTargetAddList extends MultiSelectWindow
24 {
25   public $display_server = TRUE;
26   public $display_workstation = TRUE;
27   public $display_ogroup = TRUE;
28   public $filter_iprange = FALSE;
29   public $filter_mac_addr = FALSE;
30   public $filter_fai_release = FALSE;
31   public $filter_fai_class = FALSE;
33   public $regex  = "*";
34   public $ipfrom = "0.0.0.0";
35   public $ipto   = "*";
36   public $mac_addr = "*:*:*:*:*:*";
37   public $fai_release = "*";
38   public $fai_class = "*";
39   public $_target_list = array();
41   public $workstation_list = array();
42   public $server_list = array();
44   public $array_TextCheckboxes = array();
46   public $SubSearch = FALSE;
47   /* Define that Draw has to return a smarty object */
48   public $DrawReturnsSmartyObject = TRUE;
50   function __construct(&$config,$parent)
51   {
52     MultiSelectWindow::MultiSelectWindow($config, "EventTargetAddList", 
53         array("server",
54           "workstation",
55           "ogroups"));
57     $this->parent       = $parent;
58     $this->ui           = get_userinfo();
61     $this->target_divlist = new MultiSelectWindow($this->config,"EventAddTargedtList","gotomasses");
62     $this->SetSummary(_("Targets"));
63     $this->EnableCloseButton(FALSE);
64     $this->EnableSaveButton(FALSE);
66     $this->SetInformation(_("This dialog shows all available targets for the event. Check the targets you want to add and use the 'Apply' button."));
68     /* Toggle all selected / deselected */
69     $chk = "<input type='checkbox' id='select_all' name='select_all'
70       onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
72     $this->EnableAplhabet(TRUE);
74     /* set Page header */
75     $this->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
76     $this->AddHeader(array("string"=>"&nbsp;","attach"=>"style='width:20px;'"));
77     $this->AddHeader(array("string"=>_("System / Department")));
79     //$name,$string,$value,$conn,$image="images/lists/search.png")
80     $this->AddRegex("regex" ,"regex"  ,"*"                , TRUE);
82     $this->AddCheckBox("display_server","1"     ,_("Display server"),TRUE);
83     $this->AddCheckBox("display_workstation","1",_("Display workstation"),TRUE);
84     $this->AddCheckBox("display_ogroup","1"     ,_("Display object groups"),TRUE);
85     
86     /* Add SubSearch checkbox */
87     $this->AddCheckBox(SEPERATOR);
88     $this->AddCheckBox("SubSearch",  msgPool::selectToView("","subsearch"), msgPool::selectToView("","subsearch_small"), false);
90     $regexes = array();
91     $regexes[] = $this->AddRegex("ipfrom","ipfrom" ,"0.0.0.0"          , FALSE, "images/lists/search.png", TRUE);
92     $regexes[] = $this->AddRegex("ipto"  ,"ipto"   ,"255.255.255.255"  , FALSE, "images/lists/search.png", TRUE);
93     $this->AddTextCheckBox("filter_iprange","1"     ,_("Filter by IP range"),FALSE, $regexes);
95     $regexes = array();
96     $regexes[] = $this->AddRegex("mac_addr", "mac_addr", "*:*:*:*:*:*", FALSE, "images/lists/search.png", TRUE);
97     $this->AddTextCheckBox("filter_mac_addr", "1", _("Filter by MAC address"), FALSE, $regexes);
99     $regexes = array();
100     $regexes[] = $this->AddRegex("fai_release", "fai_release", "*", FALSE, "images/lists/search.png", TRUE);
101     $this->AddTextCheckBox("filter_fai_release", "1", _("Filter by FAI release"), FALSE, $regexes);
103     $regexes = array();
104     $regexes[] = $this->AddRegex("fai_class", "fai_class", "*", FALSE, "images/lists/search.png", TRUE);
105     $this->AddTextCheckBox("filter_fai_class", "1", _("Filter by FAI class"), FALSE, $regexes);
107     /* Create a list of servers
108      */
109     $tmp = get_sub_list("(&(macAddress=*)(objectClass=goServer))",
110           "server",get_ou("serverRDN"),$config->current['BASE'],
111           array("cn","objectClass","description","ipHostNumber","macAddress"),GL_SUBSEARCH);
112     foreach($tmp as $server){
113       $this->server_list[$server['dn']] = $server;
114     }
116     /* Create a list of workstations
117      */
118     $tmp = get_sub_list("(&(macAddress=*)(objectClass=gotoWorkstation))",
119           "server",get_ou("workstationRDN"),$config->current['BASE'],
120           array("cn","objectClass","description","ipHostNumber","macAddress"),GL_SUBSEARCH);
121     foreach($tmp as $server){
122       $this->workstation_list[$server['dn']] = $server;
123     }
125   }
128   function execute()
129   {
130     $this->ClearElementsList();
131     $this->AddDepartments($this->selectedBase,2,1);
132     $this->setEntries();
133     $this->GenHeader();
134   }
137   function GenHeader()
138   {
139     $modules = array("server","workstation");
141     /* Add base */
142     $tmp = array();
143     $base = $this->config->current['BASE'];
144     $tmp[] = array("dn"=>$this->config->current['BASE']);
145     $tmp=  array_merge($tmp,get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $modules, $base,
146           array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
148     $deps = array();
149     foreach($tmp as $tm){
150       $deps[$tm['dn']] = $tm['dn'];
151     }
153     $department = $departments = array();
154     $ui= get_userinfo();
155     $d = $ui->get_module_departments($modules);
156     foreach($d as $department){
157       $departments[$department] = $department;
158     }
160     /* Load possible departments */
161     $ids = $this->config->idepartments;
162     $first = "";
163     $found = FALSE;
164     $options = array();
165     foreach($ids as $dep => $name){
166       if(isset($deps[$dep]) && in_array_ics($dep, $departments)){
168         /* Keep first base dn in mind, we could need this
169          *  info if no valid base was found
170          */
171         if(empty($first)) {
172           $first = $dep['dn'];
173         }
175         $value = $ids[$dep];
176         if ($this->selectedBase == $dep){
177           $found = TRUE;
178           $options.= "<option selected='selected' value='".$dep."'>$value</option>";
179         } else {
180           $options.= "<option value='".$dep."'>$value</option>";
181         }
182       }
183     }
185     $listhead = $this->get_default_header();
187     /* Add base selection */
188     $listhead .= _("Base")."&nbsp; <select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
189       " <input class='center' type='image' src='images/lists/submit.png' align='middle'
190       title='"._("Submit department")."' name='submit_department' alt='". _("Submit")."'>&nbsp;";
192     $this->SetListHeader($listhead);
194   }
197   function get_selected_targets()
198   {
199     $a_targets = array();
200     foreach($this->list_get_selected_items() as $id){
201       if(in_array("gosaGroupOfNames",$this->_target_list[$id]['objectClass'])){
202         $object_group = $this->_target_list[$id]['cn'][0];
203         foreach($this->_target_list[$id]['member'] as $mem_dn){
204           if(isset($this->workstation_list[$mem_dn])){
205             $a_targets[] = array( "mac" => $this->workstation_list[$mem_dn]['macAddress'][0], "cn" => $this->workstation_list[$mem_dn]['cn'][0], "ogroup" => $object_group);
206           }
207           if(isset($this->server_list[$mem_dn])){
208             $a_targets[] = array( "mac" => $this->server_list[$mem_dn]['macAddress'][0], "cn" => $this->server_list[$mem_dn]['cn'][0], "ogroup" => $object_group);
209           }
210         }
211       }else{
212         if(isset($this->_target_list[$id]['macAddress'][0])){
213           $a_targets[] = array( "mac" => $this->_target_list[$id]['macAddress'][0], "cn" => $this->_target_list[$id]['cn'][0]);
214         }
215       }
216     }
217     return($a_targets);
218   }
221   function setEntries()
222   {
223     $_target_list = array();
224     if($this->display_server){
225       if (!$this->SubSearch) {
226         $_target_list = array_merge($_target_list,
227               get_list("(&(cn=".$this->regex.")(objectClass=goServer))",
228               "server",get_ou("serverRDN").$this->selectedBase,
229               array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_NONE));
230       }else {
231         $_target_list = array_merge($_target_list,
232               get_sub_list("(&(cn=".$this->regex.")(objectClass=goServer))",
233               "server",get_ou("serverRDN"),$this->selectedBase,
234               array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_SUBSEARCH));
235       }
236     }
237     if($this->display_workstation){
238       if (!$this->SubSearch) {
239         $_target_list = array_merge($_target_list,
240               get_list("(&(cn=".$this->regex.")(objectClass=gotoWorkstation))",
241               "workstation",get_ou("workstationRDN").$this->selectedBase,
242               array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_NONE));
243       }else {
244         $_target_list = array_merge($_target_list,
245               get_sub_list("(&(cn=".$this->regex.")(objectClass=gotoWorkstation))",
246               "workstation",get_ou("workstationRDN"), $this->selectedBase,
247               array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_SUBSEARCH));
248       }
249     }
250     if($this->display_ogroup){
251       if (!$this->SubSearch) {
252         $_target_list = array_merge($_target_list,
253               get_list("(&(cn=".$this->regex.")(member=*)(objectClass=gosaGroupOfNames))",
254               "ogroups",get_ou("ogroupRDN").$this->selectedBase,
255               array("cn","objectClass","description","member", "FAIclass"),GL_NONE));
256       }else {
257         $_target_list = array_merge($_target_list,
258               get_sub_list("(&(cn=".$this->regex.")(member=*)(objectClass=gosaGroupOfNames))",
259               "ogroups",get_ou("ogroupRDN"), $this->selectedBase,
260               array("cn","objectClass","description","member", "FAIclass"),GL_SUBSEARCH));
261       }
262     }
263     $this->_target_list = $_target_list;
265     $tmp = array();
266     foreach($this->_target_list as $key => $object){
267       $tmp[$key] = $object['cn'][0];
268     }
269     natcasesort($tmp);
271     foreach($tmp as $key => $obj){
273       $obj = $this->_target_list[$key];
274       $name = $obj['cn'][0];
275       if(isset($obj['description'])){
276         $name .= "&nbsp;[".$obj['description'][0]."]";
277       }
278       if(isset($obj['macAddress'])){
279         $name .= "&nbsp;- ".$obj['macAddress'][0]."";
280       }
281       if(isset($obj['ipHostNumber'])){
282         $name .= "&nbsp;- ".$obj['ipHostNumber'][0]."";
283       }
285       $img ="";
286       if(in_array("goServer",$obj['objectClass'])){
287         $img = '<img class="center" src="plugins/systems/images/select_server.png" alt="S" title="'._("Server").'">';
289         if($this->filter_iprange){
290           if(!isset($obj['ipHostNumber']) || !tests::is_in_ip_range($this->ipfrom,$this->ipto,             $obj['ipHostNumber'][0])) {
291             continue;
292           }
293         }
295         if($this->filter_mac_addr) {
296           if(!isset($obj['macAddress']) || !ldap_equality_check($obj['macAddress'][0], $this->mac_addr, "[0-9A-F]{2}", FALSE)) {
297             continue;
298           }
299         }
301         if($this->filter_fai_release) {
302           if(isset($obj['FAIclass'])) {
303             $fai_release = split(':', $obj['FAIclass'][0]);
304             if (isset($fai_release[1])) {
305               $fai_release = $fai_release[1];
306             } else {
307               continue;
308             }
310             if (!ldap_equality_check($fai_release, $this->fai_release)) {
311               continue;
312             }
313           }
314           else {
315             continue;
316           }
317         }
319         if ($this->filter_fai_class) {
320           if(isset($obj['FAIclass'])) {
321             $fai_classes = split(':', $obj['FAIclass'][0]);
322             $fai_classes = split(' ', $fai_classes[0]);
324             $found = FALSE;
325             foreach($fai_classes as $used_class) {
326               if(ldap_equality_check($used_class, $this->fai_class)){
327                 $found = TRUE;
328                 break;
329               }
330             }
331             if (!$found) {
332               continue;
333             }
334           }
335           else {
336             continue;
337           }
338         }
340         if(!isset($this->server_list[$obj['dn']])){
341           continue;
342         }
343       }elseif(in_array("gotoWorkstation",$obj['objectClass'])){
344         $img = '<img class="center" src="plugins/systems/images/select_workstation.png" alt="W" title="'._("Workstation").'">';
345         if($this->filter_iprange){
346           if(!isset($obj['ipHostNumber']) || !tests::is_in_ip_range($this->ipfrom,$this->ipto,$obj['ipHostNumber'][0])) {
347             continue;
348           }
349         }
351         if($this->filter_mac_addr){
352           if(!isset($obj['macAddress']) || !ldap_equality_check($obj['macAddress'][0], $this->mac_addr, "[0-9A-F]{2}", FALSE)) {
353             continue;
354           }
355         }
357         if($this->filter_fai_release) {
358           if (isset($obj['FAIclass'])) {
359             $fai_release = split(':', $obj['FAIclass'][0]);
360             if (isset($fai_release[1])) {
361               $fai_release = $fai_release[1];
362             } else {
363               continue;
364             }
365             if (!ldap_equality_check($fai_release, $this->fai_release)) {
366               continue;
367             }
368           }
369           else {
370             continue;
371           }
372         }
374         if ($this->filter_fai_class) {
375           if(isset($obj['FAIclass'])) {
376             $fai_classes = split(':', $obj['FAIclass'][0]);
377             $fai_classes = split(' ', $fai_classes[0]);
379             $found = FALSE;
380             foreach($fai_classes as $used_class) {
381               if(ldap_equality_check($used_class, $this->fai_class)){
382                 $found = TRUE;
383                 break;
384               }
385             }
386             if (!$found) {
387               continue;
388             }
389           }
390           else {
391             continue;
392           }
393         }
395         if(!isset($this->workstation_list[$obj['dn']])){
396           continue;
397         }
398       }elseif(in_array("gosaGroupOfNames",$obj['objectClass'])){
399         /* Do not show object groups if ip or mac filter is enabled */
400         if($this->filter_iprange || $this->filter_mac_addr) {
401           continue;
402         }
404         if($this->filter_fai_release) {
405           if (isset($obj['FAIclass'])) {
406             $fai_release = split(':', $obj['FAIclass'][0]);
407             if (isset($fai_release[1])) {
408               $fai_release = $fai_release[1];
409             } else {
410               continue;
411             }
412             if (!ldap_equality_check($fai_release, $this->fai_release)) {
413               continue;
414             }
415           }
416           else {
417             continue;
418           }
419         }
421         if ($this->filter_fai_class) {
422           if(isset($obj['FAIclass'])) {
423             $fai_classes = split(':', $obj['FAIclass'][0]);
424             $fai_classes = split(' ', $fai_classes[0]);
426             $found = FALSE;
427             foreach($fai_classes as $used_class) {
428               if(ldap_equality_check($used_class, $this->fai_class)){
429                 $found = TRUE;
430                 break;
431               }
432             }
433             if (!$found) {
434               continue;
435             }
436           }
437           else {
438             continue;
439           }
440         }
442         $img = '<img class="center" src="plugins/ogroups/images/select_ogroup.png" alt="O" title="'._("Object group").'">';
443       }
445       $field1 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>",
446                       "attach" => "style='width:20px;'");
447       $field2 = array("string" => $img,
448                       "attach" => "style='width:20px;'");
449       $field3 = array("string" => $name , "attach" => "title='".$obj['dn']."'");
450       $this->AddElement(array($field1,$field2,$field3));
451     }
452   }
454   function AddTextCheckbox($name,$value="Unset", $string="Unset", $default=false, $regexes=array())
455   {
456     $arr = array();
457     $MultiDialogFilters = session::get('MultiDialogFilters');
458     if(isset($MultiDialogFilters[$this->filterName][$name])){
459       $arr['default'] = $MultiDialogFilters[$this->filterName][$name];
460       $this->$name = $arr['default'];
461     }else {
462       $arr['default'] = $default;
463       $this->$name = $default;
464     }
465     $arr['name'] = $name;
466     $arr['string'] = $string;
467     $arr['value'] = $value;
468     $arr['enabled'] = true;
469     $arr['regexes'] = $regexes;
471     $this->array_TextCheckboxes[] = $arr;
473   }
475   function Draw() {
476     $smarty = MultiSelectWindow::Draw();
477     $special_filter = "";
478     foreach($this->array_TextCheckboxes as $textcheckbox) {
479       $special_filter .= "<table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
480       $special_filter .= $this->GetCheckbox($textcheckbox);
481       foreach($textcheckbox['regexes'] as $regex) {
482         $special_filter .= $this->GetRegex($regex, FALSE);
483       }
484       $special_filter .= "</table>";
485     }
486     $smarty->assign("special_filters"   , $special_filter);
487     $display = $smarty->fetch(get_template_path('EventTargetAddList.tpl', TRUE, dirname(__FILE__)));
488     return($display);
489   }
491   /*! \brief  Returns a set of elements selected in a MultiSelectWindow
492     @return Array[integer]=integer
493    */
494   protected  function list_get_selected_items()
495   {
496     $ids = array();
497     foreach($_POST as $name => $value){
498       if(preg_match("/^item_selected_[0-9]*$/",$name)){
499         $id   = preg_replace("/^item_selected_/","",$name);
500         $ids[$id] = $id;
501       }
502     }
503     return($ids);
504   }
506   function save_object()
507   {
508     MultiSelectWindow::save_object();
509     if(isset($_POST['MultiSelectWindow'.$this->filterName])){
510       foreach($this->array_TextCheckboxes as $key => $box) {
511           $MultiDialogFilters = session::get('MultiDialogFilters');
512           if(isset($_POST[$box['name']])){
513               $this->array_TextCheckboxes[$key]['default'] = true;
514               $this->$box['name'] = true;
515           }else{
516               $this->array_TextCheckboxes[$key]['default'] = false;
517               $this->$box['name'] = false;
518           }
519           $MultiDialogFilters[$this->filterName][$box['name']] = $this->$box['name'];
521       foreach($this->array_TextCheckboxes[$key]['regexes'] as $regex_key => $regex_box) {
522           $this->array_TextCheckboxes[$key]['regexes'][$regex_key]['value'] = $_POST[$regex_box['name']];
523           $this->$regex_box['name'] = $_POST[$regex_box['name']];
524           $MultiDialogFilters[$this->filterName][$regex_box['name']] = $this->$regex_box['name'];
525       }
526       session::set('MultiDialogFilters',$MultiDialogFilters);
527       }
528     }
529   }
531 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
532 ?>