Stripping CVS keywords
[project/privatemsg.git] / privatemsg_service / privatemsg_service.inc
CommitLineData
7eff8680 1<?php
7eff8680
SG
2
3/**
4 * @file
5 * Link general private message module functionalities to services module.
6 */
7
8/**
9 * Get all of the logged in user's private messages.
10 *
11 * @param $type
12 * String. Which type of messages you would like to retrieve.
13 * Possibilities are inbox or sent.
14 *
15 * @param $load_full
16 * Boolean. Whether to load the full message for each message or not.
17 *
18 * @param $offset
19 * Integer (optional). An offset integer for paging.
20 * @param $limit
21 * Integer (optional). A limit integer for paging.
22 *
23 * @return
24 * An array of messages.
25 *
26 */
27function privatemsg_service_get($type = 'inbox', $load_full = FALSE, $offset = 0, $limit = 0, $uid = 0) {
28
29 $messages = array();
30 $pms = array();
31
32 // If no type passed set default type to inbox.
33 if (!$type) {
34 $type = 'inbox';
35 }
36
37 // User needs to be authenticated to proceed.
38 global $user;
39 if (!user_is_logged_in()) {
40 return services_error(t('This user is not logged in.'), 403);
41 }
42
43 // If a user id other than the current user's ID is passed,
44 // validate that the authenticated user has the correct
45 // permissions to read another user's messages.
46 if (($uid > 0) && ($uid != $user->uid)) {
47 if (user_access("read all private messages")) {
48 $account = user_load($uid);
49 }
50 else {
51 return services_error(t('This user does not have permissions to read all messages.'), 403);
52 }
53 }
54 // Use the current user for the account.
55 else {
56 $account = $user;
57 }
58
59 // Construct the query and retrieve the correct set of messages.
60 $query = _privatemsg_assemble_query('list', $account, $type);
61
62 // Handle any paging if an offset or a limit was passed in.
63 if (empty($offset) && !empty($limit)) {
64 $result = db_query_range($query['query'], 0, $limit);
65 }
66 elseif (!empty($offset) && !empty($limit)) {
67 $result = db_query_range($query['query'], $offset, $limit);
68 }
69 elseif (!empty($offset) && empty($limit)) {
70 $rows = (int)db_result(db_query($query['count']));
71 $result = db_query_range($query['query'], $offset, $rows);
72 }
73 else {
74 // No paging, we are retrieving everything.
75 $result = db_query($query['query']);
76 }
77
78 // Loop the result object to get the messages.
79 while ($row = db_fetch_object($result)) {
80 // If the full thread should be loaded, created an array
81 // using the thread_id as the index.
82 if ($load_full) {
83 $pms[$row->thread_id] = $row;
84 }
85 // For message previews, just create a numbered array.
86 else {
87 $pms[] = $row;
88 }
89 }
90
91 // Load the full thread, if necessary.
92 if (!empty($pms) && $load_full) {
93 foreach ($pms as $thread_id => $msg) {
94 $messages[] = privatemsg_thread_load($thread_id, $account, NULL, FALSE);
95 }
96 }
97 // Otherwise, just return message previews.
98 else {
99 // Replace array of participant user IDs (only)
100 // with more information.
101 $messages = _privatemsg_service_enhance_participants($pms);
102 }
103
104 // Return messages.
105 return $messages;
106
107}
108
109/**
110 * Get the number of unread private messages of the logged-in user.
111 *
112 * @return
113 * The unread count.
114 */
115function privatemsg_service_unread_count($uid = '') {
116
117 // User needs to be authenticated to proceed.
118 global $user;
119 if (!user_is_logged_in()) {
120 return services_error(t('This user is not logged in.'), 403);
121 }
122
123 // If a user id other than the current user's ID is passed,
124 // validate that the authenticated user has the correct
125 // permissions to read another user's messages.
126 if (is_numeric($uid) && ($uid != $user->uid)) {
127 if (user_access("read all private messages")) {
128 $account = user_load($uid);
129 }
130 else {
131 return services_error(t('This user does not have permissions to use this service.'), 403);
132 }
133 }
134 // Use the current user for the account.
135 else {
136 $account = $user;
137 }
138
139 // Return unread count.
140 return privatemsg_unread_count($account);
141
142}
143
144/**
145 * Send a private message to one or more recipients.
146 *
147 * @param $recipients
148 * String. A comma separated list of usernames for recipients of the message.
149 *
150 * @param $subject
151 * String. A message subject
152 *
153 * @param $body
154 * String. A message body
155 *
156 * @return
157 * Boolean. Return TRUE if sending the message was successful.
158 */
159function privatemsg_service_send($recipients, $subject, $body = '') {
160
161 // Make sure the message author is logged in.
162 if (!user_is_logged_in()) {
163 return services_error(t('Author is not logged in.'), 403);
164 }
165
166 // Validate at least 1 recipient has been passed in.
167 if ($recipients == '') {
168 return services_error(t('There are no recipients, please enter a recipient for the message.'), 400);
169 }
170
171 // Validate that this messages has a subject.
172 if ($subject == '') {
173 return services_error(t('Please specify a subject line.'), 400);
174 }
175
176 // Load the full user object.
177 global $user;
178 $account = user_load($user->uid);
179
180 // Convert the recipients string to an array of user objects.
181 list($recipients, $invalid, $duplicates, $denieds) = _privatemsg_parse_userstring($recipients);
182
183 // Problem: At least one of the recipients could not be found.
184 if (!empty($invalid)) {
185 $invalid_usernames = array('@names' => implode(', ', $invalid));
186 return services_error(t('One or more recipients are invalid: @names', $invalid_usernames), 400);
187 }
188
189 // Problem: At least one of the recipients could not be found.
190 if (!empty($duplicates)) {
191 $duplicate_usernames = array('@names' => implode(', ', $duplicates));
192 return services_error(t('One or more recipients are not unique: @names', $duplicate_usernames), 400);
193 }
194
195 // Problem: At least one of the recipients could not be found.
196 if (!empty($denieds)) {
197 $denied_usernames = array('@names' => implode(', ', $denied_usernames));
198 return services_error(t('Not allowed to write to the following recipients: @names', $denied_usernames), 400);
199 }
200
201 // Attempt to send a new message.
202 $result = privatemsg_new_thread($recipients, $subject, $body, array('author' => $account));
203
204 // Return success status.
205 if ($result['success']) {
206 return TRUE;
207 }
208 else {
209 return services_error(implode("\n", $result['messages']['error']), 400);
210 }
211}
212
213/**
214 * Reply to an existing thread.
215 *
216 * @param $body
217 * String. A message body
218 *
219 * @param $thread_id
220 * Integer. A thread ID.
221 *
222 * @return
223 * Boolean. Return TRUE if replying to a thread was successful.
224 */
225function privatemsg_service_reply($body, $thread_id) {
226
227 // Make sure the message author is logged in.
228 if (!user_is_logged_in()) {
229 return services_error(t('Author is not logged in.'), 403);
230 }
231
232 // Make sure the body is not empty.
233 if ($body == '') {
234 return services_error(t('Please specify a body for this reply.'), 400);
235 }
236
237 // Verify that a thread id has been specified.
238 if ($thread_id == '') {
239 return services_error(t('Please specify a thread ID for this reply.'), 400);
240 }
241
242 // Load the full user object.
243 global $user;
244 $account = user_load($user->uid);
245
246 // Attempt to send a reply.
247 $result = privatemsg_reply($thread_id, $body, array('author' => $account));
248
249 // Return success status.
250 if ($result['success']) {
251 return TRUE;
252 }
253 // If $result[0] this means the thread could not be loaded.
254 elseif (!empty($result[0])) {
255 return services_error($result[0], 404);
256 }
257 // Else there was some other problem.
258 else {
259 return services_error(implode("\n", $result['messages']['error']), 400);
260 }
261
262}
263
264
265/**
266 * Get all messages in a thread.
267 *
268 * @param <int> @thread_id
269 * ID of the thread to be loaded.
270 * @param <int> $offset
271 * Optional: Message offset from the start of the thread.
272 * @return
273 * An array of messages in a thread.
274 */
275function privatemsg_service_get_thread($thread_id, $offset = 0) {
276
277 // Return if wrong paramters are passed.
278 if (!$thread_id || !is_numeric($thread_id)) {
279 return services_error(t('Invalid parameters passed'), 400);
280 }
281
282 // Make sure the user is logged in.
283 global $user;
284 $account = user_load($user->uid);
285 if (!user_is_logged_in()) {
286 return services_error(t('The user is not logged in.'), 403);
287 }
288
289 // Return the full thread.
290 $thread = privatemsg_thread_load($thread_id, $account, $offset);
291
292 // Simplify thread object and enhance messages.
293 $thread = _privatemsg_service_process_thread($thread);
294
295 // Resort the array keys for messages starting at 0.
296 $thread['messages'] = array_values($thread['messages']);
297
298 return $thread;
299
300}