Code

Fix for #144
[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         foreach($this->_target_list[$id]['member'] as $mem_dn){
203           if(isset($this->workstation_list[$mem_dn])){
204             $a_targets[] = $this->workstation_list[$mem_dn]['macAddress'][0];
205           }
206           if(isset($this->server_list[$mem_dn])){
207             $a_targets[] = $this->server_list[$mem_dn]['macAddress'][0];
208           }
209         }
210       }else{
211         if(isset($this->_target_list[$id]['macAddress'][0])){
212           $a_targets[] = $this->_target_list[$id]['macAddress'][0];
213         }
214       }
215     }
216     return($a_targets);
217   }
220   function setEntries()
221   {
222     $_target_list = array();
223     if($this->display_server){
224       if (!$this->SubSearch) {
225         $_target_list = array_merge($_target_list,
226               get_list("(&(cn=".$this->regex.")(objectClass=goServer))",
227               "server",get_ou("serverRDN").$this->selectedBase,
228               array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_NONE));
229       }else {
230         $_target_list = array_merge($_target_list,
231               get_sub_list("(&(cn=".$this->regex.")(objectClass=goServer))",
232               "server",get_ou("serverRDN"),$this->selectedBase,
233               array("cn","objectClass","description","ipHostNumber","macAddress"),GL_SUBSEARCH));
234       }
235     }
236     if($this->display_workstation){
237       if (!$this->SubSearch) {
238         $_target_list = array_merge($_target_list,
239               get_list("(&(cn=".$this->regex.")(objectClass=gotoWorkstation))",
240               "workstation",get_ou("workstationRDN").$this->selectedBase,
241               array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_NONE));
242       }else {
243         $_target_list = array_merge($_target_list,
244               get_sub_list("(&(cn=".$this->regex.")(objectClass=gotoWorkstation))",
245               "workstation",get_ou("workstationRDN"), $this->selectedBase,
246               array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_SUBSEARCH));
247       }
248     }
249     if($this->display_ogroup){
250       if (!$this->SubSearch) {
251         $_target_list = array_merge($_target_list,
252               get_list("(&(cn=".$this->regex.")(member=*)(objectClass=gosaGroupOfNames))",
253               "ogroups",get_ou("ogroupRDN").$this->selectedBase,
254               array("cn","objectClass","description","member", "FAIclass"),GL_NONE));
255       }else {
256         $_target_list = array_merge($_target_list,
257               get_sub_list("(&(cn=".$this->regex.")(member=*)(objectClass=gosaGroupOfNames))",
258               "ogroups",get_ou("ogroupRDN"), $this->selectedBase,
259               array("cn","objectClass","description","member", "FAIclass"),GL_SUBSEARCH));
260       }
261     }
262     $this->_target_list = $_target_list;
264     $tmp = array();
265     foreach($this->_target_list as $key => $object){
266       $tmp[$key] = $object['cn'][0];
267     }
268     natcasesort($tmp);
270     foreach($tmp as $key => $obj){
272       $obj = $this->_target_list[$key];
273       $name = $obj['cn'][0];
274       if(isset($obj['description'])){
275         $name .= "&nbsp;[".$obj['description'][0]."]";
276       }
277       if(isset($obj['macAddress'])){
278         $name .= "&nbsp;- ".$obj['macAddress'][0]."";
279       }
280       if(isset($obj['ipHostNumber'])){
281         $name .= "&nbsp;- ".$obj['ipHostNumber'][0]."";
282       }
284       $img ="";
285       if(in_array("goServer",$obj['objectClass'])){
286         $img = '<img class="center" src="plugins/systems/images/select_server.png" alt="S" title="'._("Server").'">';
288         if($this->filter_iprange){
289           if(!isset($obj['ipHostNumber']) || !tests::is_in_ip_range($this->ipfrom,$this->ipto,             $obj['ipHostNumber'][0])) {
290             continue;
291           }
292         }
294         if($this->filter_mac_addr) {
295           if(!isset($obj['macAddress']) || !ldap_equality_check($obj['macAddress'][0], $this->mac_addr, "[0-9A-F]{2}", FALSE)) {
296             continue;
297           }
298         }
300         if($this->filter_fai_release) {
301           if(isset($obj['FAIclass'])) {
302             $fai_release = split(':', $obj['FAIclass'][0]);
303             if (isset($fai_release[1])) {
304               $fai_release = $fai_release[1];
305             } else {
306               continue;
307             }
309             if (!ldap_equality_check($fai_release, $this->fai_release)) {
310               continue;
311             }
312           }
313           else {
314             continue;
315           }
316         }
318         if ($this->filter_fai_class) {
319           if(isset($obj['FAIclass'])) {
320             $fai_classes = split(':', $obj['FAIclass'][0]);
321             $fai_classes = split(' ', $fai_classes[0]);
323             $found = FALSE;
324             foreach($fai_classes as $used_class) {
325               if(ldap_equality_check($used_class, $this->fai_class)){
326                 $found = TRUE;
327                 break;
328               }
329             }
330             if (!$found) {
331               continue;
332             }
333           }
334           else {
335             continue;
336           }
337         }
339         if(!isset($this->server_list[$obj['dn']])){
340           continue;
341         }
342       }elseif(in_array("gotoWorkstation",$obj['objectClass'])){
343         $img = '<img class="center" src="plugins/systems/images/select_workstation.png" alt="W" title="'._("Workstation").'">';
344         if($this->filter_iprange){
345           if(!isset($obj['ipHostNumber']) || !tests::is_in_ip_range($this->ipfrom,$this->ipto,$obj['ipHostNumber'][0])) {
346             continue;
347           }
348         }
350         if($this->filter_mac_addr){
351           if(!isset($obj['macAddress']) || !ldap_equality_check($obj['macAddress'][0], $this->mac_addr, "[0-9A-F]{2}", FALSE)) {
352             continue;
353           }
354         }
356         if($this->filter_fai_release) {
357           if (isset($obj['FAIclass'])) {
358             $fai_release = split(':', $obj['FAIclass'][0]);
359             if (isset($fai_release[1])) {
360               $fai_release = $fai_release[1];
361             } else {
362               continue;
363             }
364             if (!ldap_equality_check($fai_release, $this->fai_release)) {
365               continue;
366             }
367           }
368           else {
369             continue;
370           }
371         }
373         if ($this->filter_fai_class) {
374           if(isset($obj['FAIclass'])) {
375             $fai_classes = split(':', $obj['FAIclass'][0]);
376             $fai_classes = split(' ', $fai_classes[0]);
378             $found = FALSE;
379             foreach($fai_classes as $used_class) {
380               if(ldap_equality_check($used_class, $this->fai_class)){
381                 $found = TRUE;
382                 break;
383               }
384             }
385             if (!$found) {
386               continue;
387             }
388           }
389           else {
390             continue;
391           }
392         }
394         if(!isset($this->workstation_list[$obj['dn']])){
395           continue;
396         }
397       }elseif(in_array("gosaGroupOfNames",$obj['objectClass'])){
398         if($this->filter_fai_release) {
399           if (isset($obj['FAIclass'])) {
400             $fai_release = split(':', $obj['FAIclass'][0]);
401             if (isset($fai_release[1])) {
402               $fai_release = $fai_release[1];
403             } else {
404               continue;
405             }
406             if (!ldap_equality_check($fai_release, $this->fai_release)) {
407               continue;
408             }
409           }
410           else {
411             continue;
412           }
413         }
415         if ($this->filter_fai_class) {
416           if(isset($obj['FAIclass'])) {
417             $fai_classes = split(':', $obj['FAIclass'][0]);
418             $fai_classes = split(' ', $fai_classes[0]);
420             $found = FALSE;
421             foreach($fai_classes as $used_class) {
422               if(ldap_equality_check($used_class, $this->fai_class)){
423                 $found = TRUE;
424                 break;
425               }
426             }
427             if (!$found) {
428               continue;
429             }
430           }
431           else {
432             continue;
433           }
434         }
436         $img = '<img class="center" src="plugins/ogroups/images/select_ogroup.png" alt="O" title="'._("Object group").'">';
437       }
439       $field1 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>",
440                       "attach" => "style='width:20px;'");
441       $field2 = array("string" => $img,
442                       "attach" => "style='width:20px;'");
443       $field3 = array("string" => $name , "attach" => "title='".$obj['dn']."'");
444       $this->AddElement(array($field1,$field2,$field3));
445     }
446   }
448   function AddTextCheckbox($name,$value="Unset", $string="Unset", $default=false, $regexes=array())
449   {
450     $arr = array();
451     $MultiDialogFilters = session::get('MultiDialogFilters');
452     if(isset($MultiDialogFilters[$this->filterName][$name])){
453       $arr['default'] = $MultiDialogFilters[$this->filterName][$name];
454       $this->$name = $arr['default'];
455     }else {
456       $arr['default'] = $default;
457       $this->$name = $default;
458     }
459     $arr['name'] = $name;
460     $arr['string'] = $string;
461     $arr['value'] = $value;
462     $arr['enabled'] = true;
463     $arr['regexes'] = $regexes;
465     $this->array_TextCheckboxes[] = $arr;
467   }
469   function Draw() {
470     $smarty = MultiSelectWindow::Draw();
471     $special_filter = "";
472     foreach($this->array_TextCheckboxes as $textcheckbox) {
473       $special_filter .= "<table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
474       $special_filter .= $this->GetCheckbox($textcheckbox);
475       foreach($textcheckbox['regexes'] as $regex) {
476         $special_filter .= $this->GetRegex($regex, FALSE);
477       }
478     }
479     $special_filter .= "</table>";
480     $smarty->assign("special_filters"   , $special_filter);
481     $display = $smarty->fetch(get_template_path('EventTargetAddList.tpl', TRUE, dirname(__FILE__)));
482     return($display);
483   }
485   /*! \brief  Returns a set of elements selected in a MultiSelectWindow
486     @return Array[integer]=integer
487    */
488   protected  function list_get_selected_items()
489   {
490     $ids = array();
491     foreach($_POST as $name => $value){
492       if(preg_match("/^item_selected_[0-9]*$/",$name)){
493         $id   = preg_replace("/^item_selected_/","",$name);
494         $ids[$id] = $id;
495       }
496     }
497     return($ids);
498   }
500   function save_object()
501   {
502     MultiSelectWindow::save_object();
503     if(isset($_POST['MultiSelectWindow'.$this->filterName])){
504       foreach($this->array_TextCheckboxes as $key => $box) {
505           $MultiDialogFilters = session::get('MultiDialogFilters');
506           if(isset($_POST[$box['name']])){
507               $this->array_TextCheckboxes[$key]['default'] = true;
508               $this->$box['name'] = true;
509           }else{
510               $this->array_TextCheckboxes[$key]['default'] = false;
511               $this->$box['name'] = false;
512           }
513           $MultiDialogFilters[$this->filterName][$box['name']] = $this->$box['name'];
515       foreach($this->array_TextCheckboxes[$key]['regexes'] as $regex_key => $regex_box) {
516           $this->array_TextCheckboxes[$key]['regexes'][$regex_key]['value'] = $_POST[$regex_box['name']];
517           $this->$regex_box['name'] = $_POST[$regex_box['name']];
518           $MultiDialogFilters[$this->filterName][$regex_box['name']] = $this->$regex_box['name'];
519       }
520       session::set('MultiDialogFilters',$MultiDialogFilters);
521       }
522     }
523   }
525 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
526 ?>