1 | <?php |
||
2 | /** |
||
3 | * EGroupware - Notifications |
||
4 | * |
||
5 | * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
||
6 | * @package notifications |
||
7 | * @subpackage backends |
||
8 | * @link http://www.egroupware.org |
||
9 | * @author Cornelius Weiss <[email protected]>, Christian Binder <[email protected]> |
||
10 | * @version $Id$ |
||
11 | */ |
||
12 | |||
13 | use EGroupware\Api; |
||
14 | |||
15 | /** |
||
16 | * Instant user notification with egroupware popup. |
||
17 | * |
||
18 | * @abstract egwpopup is a two stage notification. In the first stage |
||
19 | * notification is written into self::_notification_table. |
||
20 | * In the second stage a request from the client reads |
||
21 | * out the table to look if there is a notificaton for this |
||
22 | * client. The second stage is done in class.notifications_ajax.inc.php |
||
23 | */ |
||
24 | class notifications_popup implements notifications_iface { |
||
25 | |||
26 | /** |
||
27 | * Appname |
||
28 | */ |
||
29 | const _appname = 'notifications'; |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
30 | |||
31 | /** |
||
32 | * Notification table in SQL database |
||
33 | */ |
||
34 | const _notification_table = 'egw_notificationpopup'; |
||
0 ignored issues
–
show
|
|||
35 | |||
36 | /** |
||
37 | * Notification type |
||
38 | */ |
||
39 | const _type = 'base'; |
||
0 ignored issues
–
show
|
|||
40 | |||
41 | /** |
||
42 | * holds account object for user who sends the message |
||
43 | * |
||
44 | * @var object |
||
45 | */ |
||
46 | private $sender; |
||
47 | |||
48 | /** |
||
49 | * holds account object for user to notify |
||
50 | * |
||
51 | * @var object |
||
52 | */ |
||
53 | private $recipient; |
||
54 | |||
55 | /** |
||
56 | * holds config object (sitewide application config) |
||
57 | * |
||
58 | * @var object |
||
59 | */ |
||
60 | private $config; |
||
61 | |||
62 | /** |
||
63 | * holds preferences object of user to notify |
||
64 | * |
||
65 | * @var object |
||
66 | */ |
||
67 | private $preferences; |
||
68 | |||
69 | /** |
||
70 | * holds db object of SQL database |
||
71 | * |
||
72 | * @var Api\Db |
||
73 | */ |
||
74 | private $db; |
||
75 | |||
76 | /** |
||
77 | * constructor of notifications_egwpopup |
||
78 | * |
||
79 | * @param object $_sender |
||
80 | * @param object $_recipient |
||
81 | * @param object $_config |
||
82 | * @param object $_preferences |
||
83 | */ |
||
84 | public function __construct($_sender, $_recipient, $_config = null, $_preferences = null) { |
||
85 | //error_log(__METHOD__."(".array2string($_sender).', '.array2string($_recipient).', '.array2string($config).',...)'); |
||
86 | if(!is_object($_sender)) { throw new Exception("no sender given."); } |
||
87 | if(!is_object($_recipient)) { throw new Exception("no recipient given."); } |
||
88 | $this->sender = $_sender; |
||
89 | $this->recipient = $_recipient; |
||
90 | $this->config = $_config; |
||
91 | $this->preferences = $_preferences; |
||
92 | $this->db = $GLOBALS['egw']->db; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * sends notification if user is online |
||
97 | * |
||
98 | * @param array $_messages |
||
99 | * @param string $_subject |
||
100 | * @param array $_links |
||
101 | * @param array $_attachments |
||
102 | * @param array $_data |
||
103 | */ |
||
104 | public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false) |
||
105 | { |
||
106 | unset($_attachments); // not used |
||
107 | |||
108 | $message = $this->render_infos($_subject) |
||
109 | .Api\Html::hr() |
||
110 | .(isset($_messages['popup'])&&!empty($_messages['popup'])?$_messages['popup']:$_messages['html']) |
||
111 | .$this->render_links($_links); |
||
112 | |||
113 | $this->save($message, $_data); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * saves notification into database so that the client can fetch it from there |
||
118 | * |
||
119 | * @param string $_message |
||
120 | * @param array $_user_sessions |
||
121 | * @param array $_data |
||
122 | */ |
||
123 | private function save($_message, $_data) { |
||
124 | $result = $this->db->insert( self::_notification_table, array( |
||
125 | 'account_id' => $this->recipient->account_id, |
||
126 | 'notify_message' => $_message, |
||
127 | 'notify_type' => self::_type, |
||
128 | 'notify_data' => is_array($_data) ? json_encode($_data) : NULL |
||
0 ignored issues
–
show
|
|||
129 | ), false,__LINE__,__FILE__,self::_appname); |
||
130 | if ($result === false) throw new Exception("Can't save notification into SQL table"); |
||
0 ignored issues
–
show
|
|||
131 | } |
||
132 | |||
133 | /** |
||
134 | * renders plaintext/html links from given link array |
||
135 | * should be moved to the ajax class later - like mentioned in the Todo |
||
136 | * |
||
137 | * @param array $_links |
||
138 | * @return string html rendered link(s) as complete string with jspopup or a new window |
||
139 | */ |
||
140 | private function render_links($_links = false) { |
||
141 | if(!is_array($_links) || count($_links) == 0) { return false; } |
||
142 | $newline = "<br />"; |
||
143 | |||
144 | $rendered_links = array(); |
||
145 | foreach($_links as $link) { |
||
146 | if(!$link->popup) { $link->view['no_popup'] = 1; } |
||
147 | |||
148 | // do not expose sensitive data |
||
149 | $url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','', |
||
150 | Api\Html::link('/index.php', $link->view)); |
||
151 | // extract application-icon from menuaction |
||
152 | if($link->view['menuaction']) { |
||
153 | $menuaction_arr = explode('.',$link->view['menuaction']); |
||
154 | $application = $menuaction_arr[0]; |
||
155 | $image = $application ? Api\Html::image($application,'navbar',$link->text,'align="middle" style="width: 24px; margin-right: 0.5em;"') : ''; |
||
156 | } else { |
||
157 | $image = ''; |
||
158 | } |
||
159 | if($link->popup && !$GLOBALS['egw_info']['user']['preferences']['notifications']['external_mailclient']) |
||
160 | { |
||
161 | $data = array( |
||
162 | "data-app = '{$link->app}'", |
||
163 | "data-id = '{$link->id}'", |
||
164 | "data-url = '$url'", |
||
165 | "data-popup = '{$link->popup}'" |
||
166 | ); |
||
167 | |||
168 | $rendered_links[] = Api\Html::div($image.$link->text,implode(' ',$data),'link'); |
||
169 | } else { |
||
170 | $rendered_links[] = Api\Html::div('<a href="'.$url.'" target="_blank">'.$image.$link->text.'</a>','','link'); |
||
171 | } |
||
172 | |||
173 | } |
||
174 | if(count($rendered_links) > 0) { |
||
175 | return Api\Html::hr().Api\Html::bold(Api\Translation::translate_as($this->recipient->account_id,'Linked entries:')).$newline.implode($newline,$rendered_links); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * returns javascript to open a popup window: window.open(...) |
||
181 | * |
||
182 | * @param string $link link or this.href |
||
183 | * @param string $target ='_blank' name of target or this.target |
||
184 | * @param int $width =750 width of the window |
||
185 | * @param int $height =400 height of the window |
||
186 | * @return string javascript (using single quotes) |
||
187 | */ |
||
188 | private function jspopup($link,$target='_blank',$width=750,$height=410) |
||
0 ignored issues
–
show
|
|||
189 | { |
||
190 | if($GLOBALS['egw_info']['user']['preferences']['notifications']['external_mailclient']) |
||
191 | { |
||
192 | return 'window.open('.($link == 'this.href' ? $link : "'".$link."'").','. |
||
193 | ($target == 'this.target' ? $target : "'".$target."'").",$width,$height,'yes')"; |
||
194 | } |
||
195 | else |
||
196 | { |
||
197 | return 'egw_openWindowCentered2('.($link == 'this.href' ? $link : "'".$link."'").','. |
||
198 | ($target == 'this.target' ? $target : "'".$target."'").",$width,$height,'yes')"; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * renders additional infos from sender and subject |
||
204 | * |
||
205 | * @param string $_subject |
||
206 | * @return string html rendered info as complete string |
||
207 | */ |
||
208 | private function render_infos($_subject = false) { |
||
209 | $infos = array(); |
||
210 | $newline = "<br />"; |
||
211 | |||
212 | $sender = $this->sender->account_fullname ? $this->sender->account_fullname : $this->sender_account_email; |
||
0 ignored issues
–
show
|
|||
213 | $infos[] = Api\Translation::translate_as($this->recipient->account_id, 'Message from').': '.$sender; |
||
214 | if(!empty($_subject)) { $infos[] = Api\Html::bold($_subject); } |
||
215 | return implode($newline,$infos); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Actions to take when deleting an account |
||
220 | * |
||
221 | * @param settings array with keys account_id and new_owner (new_owner is optional) |
||
222 | */ |
||
223 | public static function deleteaccount($settings) { |
||
224 | $GLOBALS['egw']->db->delete( self::_notification_table, array( |
||
225 | 'account_id' => $settings['account_id'] |
||
226 | ),__LINE__,__FILE__,self::_appname); |
||
227 | } |
||
228 | } |
||
229 |