Code

Implement reinstall with offsets (#4271)
[gosa.git] / trunk / gosa-plugins / goto / addons / goto / events / class_DaemonEvent_notify.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 DaemonEvent_notify extends DaemonEvent
24 {
25   var $visible_for = USER_EVENT;
27   var $user = array();
28   var $group= array(); 
30   var $message = "";
31   var $subject = "";
32   var $from    = "";
34   var $attributes = array("from","user","group","message","subject","timestamp");
36   public function __construct($config,$data = array())
37   {
38     DaemonEvent::__construct($config,$data);
40     $ui = get_userinfo();
41     $this->from = $ui->cn; 
42  
43     $this->message = base64_decode($this->message);
44     $this->subject = base64_decode($this->subject);
46     $this->s_Menu_Name  = _("Send message");
47     $this->s_Event_Name = _("Send message");
49     $this->s_Schedule_Action = "job_send_user_msg";
50     $this->s_Trigger_Action= "gosa_send_user_msg";
51     $this->s_Queued_Action= "trigger_action_notify";
52     $this->s_Menu_Image = "plugins/goto/images/notify.png";
53     $this->s_List_Image = "plugins/goto/images/notify.png";
54     $this->a_targets = array("GOSA"); // Required to get the event send. Maybe this is a wrong value.
55   }
57   public function execute()
58   {
59     DaemonEvent::execute();
61     /* Check if target add dialog is open */
62     if($this->is_target_list_open() && $this->is_new){
63       return($this->get_target_add_list());
64     }
67     $display = $this->get_header();
68     $tmp = $this->data;
69     $smarty = get_smarty();
70     $smarty->assign("data"        , $this->data);
71     $smarty->assign("is_new"      , $this->is_new);
72     $smarty->assign("timestamp"   , $this->get_time_select());
73     $smarty->assign("time_offset"   , $this->get_time_offset_select());
74     $smarty->assign("user" , $this->user);
75     $smarty->assign("group" , $this->group);
76     
77     $smarty->assign("add_str", msgPool::addButton(_("Target")));
78     $smarty->assign("del_str", msgPool::delButton(_("Target")));
80     $smarty->assign("from",    xmlentities($this->from));
81     $smarty->assign("subject", xmlentities($this->subject));
82     $smarty->assign("message", xmlentities($this->message));
84     $display.= $smarty->fetch(get_template_path('DaemonEvent_notify.tpl', TRUE, dirname(__FILE__)));
85     $display.= $this->get_footer();
86     return($display);
87   }
90   public function check()
91   {
92     $msgs = DaemonEvent::check();
93     if(empty($this->subject)){
94       $msgs[] = msgPool::required(_("Subject"));
95     }
96     if(empty($this->message)){
97       $msgs[] = msgPool::required(_("Message"));
98     }
99     if(empty($this->from)){
100       $msgs[] = msgPool::required(_("From"));
101     }
102     if(!count($this->group) && !count($this->user)){
103       $msgs[] = msgPool::required(_("Target"));
104     }
105     return($msgs);
106   }
109   public function save_object()
110   {
111     DaemonEvent::save_object();
113     if(isset($_POST['del_any_target']) && isset($_POST['group'])){
114       foreach($_POST['group'] as $id){
115         if(isset($this->group[$id])){
116           unset($this->group[$id]);
117         }
118       }
119     }
120     if(isset($_POST['del_any_target']) && isset($_POST['user'])){
121       foreach($_POST['user'] as $id){
122         if(isset($this->user[$id])){
123           unset($this->user[$id]);
124         }
125       }
126     }
128     if(isset($_POST['subject'])){
129       $this->subject = get_post('subject');
130     }
131     if(isset($_POST['message'])){
132       $this->message = get_post('message');
133     }
134     if(isset($_POST['from'])){
135       $this->from = get_post('from');
136     }
137   }
139   public function add_users($targets)
140   {
141     $add = $targets;
142     if(!is_array($add)){
143       $add = array($add);
144     }
145     foreach($add as $target){
146       if(!in_array($target,$this->user)){
147         $this->user[] = $target;
148       }
149     }
150   }
153   public function add_groups($targets)
154   {
155     $add = $targets;
156     if(!is_array($add)){
157       $add = array($add);
158     }
159     foreach($add as $target){
160       if(!in_array($target,$this->group)){
161         $this->group[] = $target;
162       }
163     }
164   }
167   /*! \brief  Returns HTML content, displaying a dialog which allows to add new targets.
168     @return String HTML content. (EventTargetAddList)
169    */
170   public function get_target_add_list()
171   {
172     $this->target_add_list_used = TRUE;
174     if($this->target_divlist == NULL){
175       $this->target_divlist = new EventTargetAddUserList($this->config,$this);
176     }
177     $this->target_divlist->execute();
179     $smarty = get_smarty();
180     $smarty->assign("divlist",$this->target_divlist->Draw());
181     return($smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__))));
182   }
185   /*! \brief Add a target MAC address
186       @param Array A List of all target that should be added.
187    */
188   public function add_targets($targets)
189   {
190     if(isset($targets['OBJECTGROUPS'])){
191       $ogroup_targets = $this->resolve_objectgroups($targets['OBJECTGROUPS']);
192       $targets = array_merge_recursive($targets, $ogroup_targets);
193     }
195     if(isset($targets['USERS'])){
196       $this->add_users($targets['USERS']);
197     }
198     if(isset($targets['GROUPS'])){
199       $this->add_groups($targets['GROUPS']);
200     }
201   }
203   /*! \brief Resolve an object group to the contained users and groups
204       @param Array A list of all object groups
205    */
206   public function resolve_objectgroups($targets)
207   {
208     $result = array();
210     foreach($targets as $ogroup) {
211       $ldap = $this->config->get_ldap_link();
212       $ldap->cd(get_groups_ou().$this->config->current['BASE']);
213       $ldap->search("(&(cn=".$ogroup.")(member=*))", array('member'));
214       while ($entry = $ldap->fetch()) {
215         $members = $entry['member'];
216           for($i=0;$i<$members['count'];$i++) {
217             $ldap->cat($members[$i], array('uid','cn','objectClass'));
218             if ($ldap->count() > 0) {
219               $attrs = $ldap->fetch();
220               /* Determine which type the object has */
221               if (array_search('gosaAccount', $attrs['objectClass'])) {
222                 $uid = $attrs['uid'][0];
223                 $result['USERS'][] = $uid;
224               }
225               elseif (array_search('posixGroup', $attrs['objectClass'])) {
226                 $group = $attrs['cn'][0];
227                 $result['GROUPS'][] = $group;
228               }
229             }
230           }
231        }
232     }
233     return $result;
234   }
239   public function save()
240   {
241     $ret = DaemonEvent::save();
242     $ret['delivery_time'] = $ret['timestamp'];
243     $ret['user']   = array_values( $ret['user']);
244     $ret['group']  = array_values( $ret['group']);
245     $ret['subject'] = base64_encode($ret['subject']);
246     $ret['message'] = base64_encode($ret['message']);
247     return($ret);
248   }
249
250 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
251 ?>