Issues (4868)

notifications/inc/class.notifications_ajax.inc.php (7 issues)

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 ajaxpopup
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
 * Ajax methods for notifications
17
 */
18
class notifications_ajax {
19
	/**
20
	 * Appname
21
	 */
22
	const _appname = 'notifications';
0 ignored issues
show
This class constant is not uppercase (expected _APPNAME).
Loading history...
23
24
	/**
25
	 * Mailappname
26
	 */
27
	const _mailappname = 'mail';
0 ignored issues
show
This class constant is not uppercase (expected _MAILAPPNAME).
Loading history...
28
29
	/**
30
	 * Notification table in SQL database
31
	 */
32
	const _notification_table = 'egw_notificationpopup';
0 ignored issues
show
This class constant is not uppercase (expected _NOTIFICATION_TABLE).
Loading history...
33
34
	/**
35
	 * Notification type
36
	 */
37
	const _type = 'base';
0 ignored issues
show
This class constant is not uppercase (expected _TYPE).
Loading history...
38
39
	/**
40
	 * holds account object for user to notify
41
	 *
42
	 * @var object
43
	 */
44
	private $recipient;
45
46
	/**
47
	 * holds config object (sitewide application config)
48
	 *
49
	 * @var object
50
	 */
51
	private $config;
52
53
	/**
54
	 * holds preferences array of user to notify
55
	 *
56
	 * @var array
57
	 */
58
	private $preferences;
59
60
	/**
61
	 * reference to global db object
62
	 *
63
	 * @var Api\Db
64
	 */
65
	private $db;
66
67
	/**
68
	 * holds the users session data
69
	 *
70
	 * @var array
71
	 */
72
	var $session_data;
73
74
	/**
75
	 * holds the users session data defaults
76
	 *
77
	 * @var array
78
	 */
79
	var $session_data_defaults = array(
80
		'notified_mail_uids'	=> array(),
81
	);
82
83
	/**
84
	 * the xml response object
85
	 *
86
	 * @var Api\Json\Response
87
	 */
88
	private $response;
89
90
	/**
91
	 * constructor
92
	 *
93
	 */
94
	public function __construct() {
95
		$this->response = Api\Json\Response::get();
96
		$this->recipient = (object)$GLOBALS['egw']->accounts->read($GLOBALS['egw_info']['user']['account_id']);
97
98
		$this->config = (object)Api\Config::read(self::_appname);
99
100
		$prefs = new Api\Preferences($this->recipient->account_id);
101
		$this->preferences = $prefs->read();
102
103
		$this->db = $GLOBALS['egw']->db;
104
	}
105
106
	/**
107
	 * public AJAX trigger function to be called by the JavaScript client
108
	 *
109
	 * this function calls all other recurring AJAX notifications methods
110
	 * to have ONE single recurring AJAX call per user
111
	 *
112
	 * @return xajax response
0 ignored issues
show
The type xajax was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
113
	 */
114
	public function get_notifications($browserNotify = false)
115
	{
116
		// close session now, to not block other user actions, as specially mail checks can be time consuming
117
		$GLOBALS['egw']->session->commit_session();
118
119
		// call a hook for notifications on new mail
120
		//if ($GLOBALS['egw_info']['user']['apps']['mail'])  $this->check_mailbox();
121
		Api\Hooks::process('check_notify');
122
123
		// update currentusers
124
		if ($GLOBALS['egw_info']['user']['apps']['admin'] &&
125
			$GLOBALS['egw_info']['user']['preferences']['common']['show_currentusers'])
126
		{
127
			$this->response->jquery('#currentusers', 'text', array((string)$GLOBALS['egw']->session->session_count()));
128
		}
129
130
		$this->get_egwpopup($browserNotify);
131
	}
132
133
	/**
134
	 * Remove given notification id(s) from the table
135
	 *
136
	 * @param array|int $notify_ids one or multiple notify_id(s)
137
	 */
138
	public function delete_message($notify_ids)
139
	{
140
		if ($notify_ids)
141
		{
142
			$this->db->delete(self::_notification_table,array(
143
				'notify_id' => $notify_ids,
144
				'account_id' => $this->recipient->account_id,
145
				'notify_type' => self::_type
146
			),__LINE__,__FILE__,self::_appname);
147
		}
148
	}
149
150
	/**
151
	 * Method to update message(s) status
152
	 *
153
	 * @param int|array $notify_ids one or more notify_id(s)
154
	 * @param string $status = SEEN, status of message:
155
	 *  - SEEN: message has been seen
156
	 *	- UNSEEN: message has not been seen
157
	 *	- DISPLAYED: message has been shown but no further status applied yet
158
	 *				 this status has been used more specifically for browser type
159
	 *				 of notifications.
160
	 */
161
	public function update_status($notify_ids,$status = "SEEN")
162
	{
163
		if ($notify_ids)
164
		{
165
			$this->db->update(self::_notification_table,array('notify_status' => $status),array(
166
				'notify_id' => $notify_ids,
167
				'account_id' => $this->recipient->account_id,
168
				'notify_type' => self::_type
169
			),__LINE__,__FILE__,self::_appname);
170
		}
171
	}
172
173
	/**
174
	 * gets all egwpopup notifications for calling user
175
	 *
176
	 * @return boolean true or false
177
	 */
178
	private function get_egwpopup($browserNotify = false) {
179
		$rs = $this->db->select(self::_notification_table, '*', array(
180
				'account_id' => $this->recipient->account_id,
181
				'notify_type' => self::_type
182
			),
183
			__LINE__,__FILE__,0 ,'ORDER BY notify_id DESC',self::_appname, 100);
184
		$result = array();
185
		if ($rs->NumRows() > 0)	{
186
			foreach ($rs as $notification) {
187
				$actions = null;
188
				$data = json_decode($notification['notify_data'], true);
189
				if ($data['appname'] && $data['data'])
190
				{
191
					$_actions = Api\Hooks::process (array(
192
						'location' => 'notifications_actions',
193
						'data' => $data['data']
194
						), $data['appname'], true);
195
					$actions = $_actions[$data['appname']];
196
				}
197
				$result[] = array(
198
					'id'		=> $notification['notify_id'],
199
					'message'	=> $notification['notify_message'],
200
					'status'	=> $notification['notify_status'],
201
					'created'	=> Api\DateTime::to($notification['notify_created']),
202
					'current'		=> new DateTime(),
203
					'actions'	=> is_array($actions)?$actions:NULL,
204
					'extra_data'		=> ($data['data'] ? $data['data'] : array())
205
				);
206
207
			}
208
			$this->response->apply('app.notifications.append', array($result, $browserNotify));
209
		}
210
		return true;
211
	}
212
213
	/**
214
	 * restores the users session data for notifications
215
	 *
216
	 * @return boolean true
217
	 */
218
	private function restore_session_data() {
0 ignored issues
show
The method restore_session_data() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
219
		$session_data = Api\Cache::getSession(self::_appname, 'session_data');
220
		if(is_array($session_data)) {
221
			$this->session_data = $session_data;
222
		} else {
223
			$this->session_data = $this->session_data_defaults;
224
		}
225
226
		return true;
227
	}
228
229
	/**
230
	 * saves the users session data for notifications
231
	 *
232
	 * @return boolean true
233
	 */
234
	private function save_session_data() {
0 ignored issues
show
The method save_session_data() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
235
		Api\Cache::setSession(self::_appname, 'session_data', $this->session_data);
236
		return true;
237
	}
238
}
239