/[drupal]/contributions/modules/notifications/notifications.admin.inc
ViewVC logotype

Contents of /contributions/modules/notifications/notifications.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Thu Feb 14 16:54:32 2008 UTC (21 months, 2 weeks ago) by jareyero
Branch: MAIN
CVS Tags: DRUPAL-5--1-0-ALPHA1, HEAD
Branch point for: DRUPAL-5
Changes since 1.4: +28 -34 lines
File MIME type: text/x-php
Left files in latest commit
1 <?php
2 // $Id: notifications.admin.inc,v 1.1.2.6 2007/09/06 18:25:15 chx Exp $
3
4 /**
5 * Admin settings
6 */
7 function notifications_settings_form() {
8 $form['sub_settings'] = array(
9 '#type' => 'fieldset',
10 '#title' => t('General settings'),
11 '#weight' => -10,
12 );
13 $form['sub_settings']['notifications_sendself'] = array(
14 '#type' => 'checkbox',
15 '#title' => t('Notify poster of own posts'),
16 '#default_value' => variable_get('notifications_sendself', 0),
17 '#description' => t("Notifies a node poster about their own posts. Useful principally during testing. Default is OFF."),
18 );
19 $form['sub_settings']['notifications_watchgood'] = array(
20 '#type' => 'checkbox',
21 '#title' => t('Display watchdog entries for successful mailings'),
22 '#default_value' => variable_get('notifications_watchgood', 1),
23 '#description' => t('Inserts notification of successful mailings in the watchdog log. Default is ON.'),
24 );
25 $form['sub_settings']['notifications_testpost'] = array(
26 '#type' => 'checkbox',
27 '#title' => t('Test held posts prior to sending'),
28 '#default_value' => variable_get('notifications_testpost', 0),
29 '#description' => t('Tests to see if a post about to be sent by cron is still active. Adds a small amount of overhead. Default is OFF.'),
30 );
31 $form['sub_settings']['notifications_send_immediate'] = array(
32 '#title' => t('Immediate sending'),
33 '#type' => 'checkbox',
34 '#size' => 10,
35 '#default_value' => variable_get('notifications_send_immediate', 0),
36 '#description' => t('Do not queue notifications for immediate sending. This will produce more timely notifications for sites with a small number of users.'),
37 );
38 // Default options
39 $form['defaults'] = array(
40 '#type' => 'fieldset',
41 '#title' => t('Default settings'),
42 );
43 $form['defaults']['notifications_default_send_interval'] = array(
44 '#type' => 'select',
45 '#title' => t('Default send interval'),
46 '#options' => _notifications_send_intervals(),
47 '#default_value' => variable_get('notifications_default_send_interval', 0),
48 );
49 $form['defaults']['notifications_default_send_method'] = array(
50 '#type' => 'select',
51 '#title' => t('Default send method'),
52 '#options' => _notifications_send_methods(),
53 '#default_value' => variable_get('notifications_default_send_method', ''),
54 );
55
56 // Processing limits
57 $limit = variable_get('notifications_process_limit', array('row' => 0, 'message' => 0, 'percent' => 0, 'time' => 0));
58 $form['notifications_process_limit'] = array(
59 '#type' => 'fieldset',
60 '#title' => t('Limits for queue processing'),
61 '#tree' => TRUE,
62 '#description' => t('These are the limits for each cron run on queue processing. The process will stop when it first meets any of them. Set to 0 for no limit.'),
63 );
64 $form['notifications_process_limit']['row'] = array(
65 '#title' => t('Number of rows'),
66 '#type' => 'textfield',
67 '#size' => 10,
68 '#default_value' => $limit['row'],
69 );
70 $form['notifications_process_limit']['message'] = array(
71 '#title' => t('Number of messages sent'),
72 '#type' => 'textfield',
73 '#size' => 10,
74 '#default_value' => $limit['message'],
75 );
76 $form['notifications_process_limit']['time'] = array(
77 '#title' => t('Time (seconds)'),
78 '#type' => 'textfield',
79 '#size' => 10,
80 '#default_value' => $limit['time'],
81 );
82 $form['notifications_process_limit']['percent'] = array(
83 '#title' => t('Time (% of cron time)'),
84 '#type' => 'textfield',
85 '#size' => 10,
86 '#default_value' => $limit['percent'],
87 '#description' => t('Maximum percentage of cron time the process may use.'),
88 );
89 return system_settings_form($form);
90 }
91
92
93 /* ******************************************************* */
94 /* user screens: display, edit functions */
95 /* ******************************************************* */
96
97 /**
98 * Theme subscriptions list
99 */
100 function theme_notifications_form_table($element) {
101 $output = '';
102 if ($fields = element_children($element)) {
103 $header = $element['#header'];
104 $rows = array();
105 // The first element determines the number of columns
106 foreach (element_children($element[$fields[key($fields)]]) as $index) {
107 $row = array();
108 foreach ($fields as $key) {
109 $row[] = isset($element[$key][$index]) ? drupal_render($element[$key][$index]) : '';
110 }
111 $rows[] = $row;
112 }
113 $output .= theme('table', $header, $rows);
114 }
115 $output .= drupal_render($element);
116 return $output;
117 }
118
119 /**
120 * Menu callback. Overview page for user subscriptions.
121 */
122 function notifications_page_user_overview($account) {
123 // Build summary
124 $count = array();
125 // Query all subscriptions for this user
126 $result = db_query("SELECT s.type, count(s.sid) as number FROM {notifications} s WHERE s.uid = %d GROUP BY s.type", $account->uid);
127 while ($subs = db_fetch_object($result)) {
128 $count[$subs->type] = $subs->number;
129 }
130 $header = array(t('Type'), t('Number'));
131 $rows = array();
132
133 // List types and count for each type
134 foreach (notifications_subscription_types() as $type => $info) {
135 $access = user_access('administer notifications') || !empty($info['access']) && user_access($info['access']);
136 // If no access and no count, skip this type.
137 // But if no access and there are some we show the type and number without link.
138 if ($access || !empty($count[$type])) {
139 $rows[] = array(
140 $access ? l($info['title'], "user/$account->uid/notifications/$type") : $info['title'],
141 isset($count[$type]) ? $count[$type] : 0,
142 );
143 }
144 }
145 if ($rows) {
146 $output .= theme('table', $header, $rows);
147 } else {
148 $output .= t('No existing or allowed subscriptions');
149 }
150 return $output;
151 }
152
153 /** Administration pages **/
154
155 /**
156 * Admin overview page
157 */
158 function notifications_admin_overview_page() {
159 $menu = menu_get_item(NULL, 'admin/notifications');
160 $content = system_admin_menu_block($menu);
161 $output = theme('admin_block_content', $content);
162 return $output;
163 }
164
165 /**
166 * Current subscriptions page
167 */
168 function notifications_admin_status_page() {
169 $output = '';
170 // Subscriptions summary
171 $header = array(t('Type'), t('Number'));
172 $result = db_query("SELECT type, count(*) AS count FROM {notifications} GROUP BY type");
173 $count = 0;
174 while ($stype = db_fetch_object($result)) {
175 $rows[] = array($stype->type, $stype->count);
176 $count += $stype->count;
177 }
178 $summary = theme('table', $header, $rows);
179 $summary .= t('Total: %number', array('%number' => $count));
180 $output .= theme('box', t('Current subscriptions'), $summary);
181 $output .= notifications_admin_queue_summary();
182 return $output;
183 }
184
185 /**
186 * Summary of queued notifications
187 */
188 function notifications_admin_queue_summary() {
189 $output = '';
190 // Queue status
191 $send_intervals = _notifications_send_intervals();
192 $header = array(t('Send interval'), t('Number'));
193 $count = 0;
194 $result = db_query("SELECT send_interval, count(*) AS count FROM {notifications_queue} GROUP BY send_interval");
195 while ($stype = db_fetch_object($result)) {
196 $rows[] = array($send_intervals[$stype->send_interval], $stype->count);
197 $count += $stype->count;
198 }
199 $output .= theme('table', $header, $rows);
200 $output .= t('Total: %number', array('%number' => $count));
201 return theme('box', t('Messages in queue'), $output);
202 }
203
204 /**
205 * Admin queue management
206 *
207 * @ TO DO Add confirmation before queue reset
208 */
209 function notifications_admin_queue($op = 'status', $param = NULL) {
210 $base = 'admin/notifications/status/queue';
211 $output = '';
212 switch ($op) {
213 case 'run':
214 $out = notifications_admin_queue_process($param);
215 if ($out) {
216 $output .= theme('box', t('Output'), $out);
217 }
218 break;
219 case 'reset':
220 db_query("DELETE FROM {notifications_queue}");
221 db_query("DELETE FROM {notifications_event}");
222 drupal_set_message(t('The queue has been reset.'));
223 drupal_goto($base);
224 break;
225 default:
226 }
227 // Add operations
228 $list[] = l(t('Run queue process'), "$base/run");
229 $list[] = l(t('Process immediate sending'), "$base/run/immediate");
230 $list[] = l(t('Reset queue. Delete all notifications.'), "$base/reset");
231
232 $output .= theme('box', t('Operations'), theme('item_list', $list));
233 // Summary
234 $output .= notifications_admin_queue_summary();
235 return $output;
236 }
237
238 /**
239 * Admin manual queue processing
240 */
241 function notifications_admin_queue_process($param) {
242 include_once drupal_get_path('module', 'notifications') .'/notifications.cron.inc';
243 // Set some running parameters
244 switch ($param) {
245 case 'immediate':
246 notifications_process_rows(array('cron' => 1, 'send_interval' => 0));
247 break;
248 case 'debug':
249 notifications_process('option', 'debug', TRUE);
250 notifications_process('option', 'output', TRUE);
251 notifications_process_run(FALSE);
252 break;
253 default:
254 notifications_process_run(FALSE);
255 }
256 // Go for it, not cron run
257
258 if ($logs = notifications_log()) {
259 return theme('item_list', $logs);
260 }
261 }
262
263 /**
264 * Menu callback add subscription
265 *
266 * Presents confirmation page or not depending on confirm parameter
267 */
268 function notifications_page_subscribe($uid, $type, $fields, $values, $send_interval = NULL, $send_method = NULL) {
269 global $user;
270
271 // Access checking
272 if ($uid && ($uid == $user->uid || user_access('administer notifications'))) {
273 if (($account = user_load(array('uid' => $uid)))) {
274 // Build subscriptions object
275 $subscription = (object)array(
276 'uid' => $uid,
277 'type' => $type,
278 'fields' => notifications_field_args($fields, $values),
279 'send_interval' => $send_interval ? $send_interval : notifications_user_setting('send_interval', $account),
280 'send_method' => $send_method ? $send_method : notifications_user_setting('send_method', $account),
281 'event_type' => notifications_subscription_types($type, 'event_type'),
282 );
283 if (notifications_user_allowed('subscription', $account, $subscription)) {
284 // Display subscription information
285 if (empty($_GET['confirm'])) {
286 // Subscribe, no confirmation
287 notifications_save_subscription($subscription);
288 drupal_goto();
289 } else {
290 // Ask for confirmation
291 drupal_set_title(t('Confirm your subscription'));
292 return drupal_get_form('notifications_form_confirm', $subscription);
293 }
294 } else {
295 drupal_set_message(t('Subscription type or parameters not allowed'), 'error');
296 drupal_goto();
297 }
298 }
299
300 }
301 drupal_access_denied();
302 }
303
304 /**
305 * Form for subscription confirmation
306 */
307 function notifications_form_confirm($subscription) {
308
309 // Pass on simple values
310 foreach (array('sid', 'uid', 'type', 'fields', 'event_type') as $field) {
311 $form[$field] = array('#type' => 'value', '#value' => $subscription->$field);
312 }
313
314 // The names will be added here
315 notifications_module_invoke('names', $subscription);
316
317 $form['info'] = array(
318 '#type' => 'item',
319 '#title' => t('!type subscription to', array('!type' => $subscription->type_name)),
320 '#value' => theme('item_list', $subscription->names),
321 );
322
323 // Additional parameters
324 $form['send_interval'] = array(
325 '#type' => 'select',
326 '#title' => t('Send interval'),
327 '#options' => _notifications_send_intervals(),
328 '#default_value' => $subscription->send_interval,
329 );
330 $form['send_method'] = array(
331 '#type' => 'select',
332 '#title' => t('Send method'),
333 '#options' => _notifications_send_methods(),
334 '#default_value' => $subscription->send_method,
335 );
336
337 $form['confirm'] = array('#type' => 'submit', '#value' => t('Subscribe'));
338 $form['cancel'] = array('#type' => 'submit', '#value' => t('Cancel'));
339 return $form;
340 }
341
342 /**
343 * Process form submission
344 */
345 function notifications_form_confirm_submit($form_id, $form_values) {
346 $subscription = (object)$form_values;
347 switch($form_values['op']) {
348 case t('Subscribe'):
349 notifications_save_subscription($subscription);
350 drupal_set_message(t('Your subscription was activated.'));
351 break;
352 case t('Cancel'):
353 drupal_set_message(t('Your subscription was cancelled'));
354 break;
355 }
356 return 'user/'.$form_values['uid'].'/notifications';
357 }
358
359 /**
360 * Process arguments and return an array of field/value pairs
361 */
362 function notifications_field_args($fields, $values) {
363 $names = explode(',', $fields);
364 $params = explode(',', $values);
365 return array_combine($names, $params);
366 }
367
368 /**
369 * Menu callback add subscription
370 *
371 * This just admits one field/value pair
372 */
373 function notifications_page_unsubscribe($sid) {
374 global $user;
375
376 if (is_numeric($sid) && ($subscription = notifications_load_subscription($sid)) ) {
377 if (user_access('administer notifications') ||
378 ($user->uid && $user->uid == $subscription->uid) ||
379 (!empty($_GET['signature']) && ($_GET['signature'] == _notifications_signature(array('unsubscribe', $sid))))
380 ) {
381 notifications_delete_subscription($sid);
382 drupal_set_message(t('Your subscription has been removed.'));
383 drupal_goto();
384 return;
385 }
386 }
387 drupal_access_denied();
388 }
389
390 /**
391 * Generic subscriptions content form
392 *
393 * Builds a form for a user to manage its own subscriptions with
394 * some generic parameters
395 *
396 * Currently it only manages simple condition fields
397 * @param $account
398 * User account
399 * @param $type
400 * Subscription type
401 * @param $subscriptions
402 * Current subscriptions of this type. If null they'll be loaded
403 * @param $list
404 * Array with available subscriptions indexed by field value
405 * @param $defaults
406 * Default value for subscriptions
407 * @param $options
408 * Optional, array of aditional options for the form
409 */
410 function notifications_user_form($account, $type, $subscriptions, $list, $defaults, $options = array()) {
411 // Complete defaults
412 $info = notifications_subscription_types($type);
413 $field = $info['fields'][0];
414 $field_title = !empty($options['title']) ? $options['title'] : '';
415 if (is_null($subscriptions)) {
416 // Fetch subscriptions with given parameters
417 $subscriptions = notifications_get_subscriptions(array('type' => $type, 'event_type' => $info['event_type'], 'uid' => $account->uid), TRUE, 'value');
418 }
419 $defaults += array(
420 'sid' => 0,
421 'type' => $type,
422 'event_type' => $info['event_type'],
423 );
424 $defaults += _notifications_subscription_defaults($account);
425 $form['defaults'] = array('#type' => 'value', '#value' => $defaults);
426 $form['account'] = array('#type' => 'value', '#value' => $account);
427 $form['current'] = array('#type' => 'value', '#value' => $subscriptions);
428 $form['subscription_fields'] = array('#type' => 'value', '#value' => array());
429 $form['subscriptions'] = array(
430 '#tree' => TRUE,
431 '#theme' => 'notifications_form_table',
432 '#header' => array('', $field_title, t('Send interval'), t('Send method'))
433 );
434 foreach ($list as $key => $title) {
435 $rowdefaults = isset($subscriptions[$key]) ? (array)($subscriptions[$key]) : $defaults;
436 $rowdefaults += $rowdefaults;
437 $form['subscriptions']['checkbox'][$key] = array(
438 '#type' => 'checkbox',
439 '#default_value' => $rowdefaults['sid'],
440 );
441 $form['subscriptions']['title'][$key] = array(
442 '#value' => $title,
443 );
444 $form['subscriptions']['send_interval'][$key] = array(
445 '#type' => 'select',
446 '#options' => _notifications_send_intervals(),
447 '#default_value' => $rowdefaults['send_interval'],
448 );
449 $form['subscriptions']['send_method'][$key] = array(
450 '#type' => 'select',
451 '#options' => _notifications_send_methods(),
452 '#default_value' => $rowdefaults['send_method'],
453 );
454 // Pass on the fields for processing
455 $form['subscription_fields']['#value'][$key] = array($field => $key);
456
457 }
458 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
459
460 return $form;
461 }
462
463 /**
464 * Process generic form submission
465 */
466 function notifications_user_form_submit($form_id, $form_values) {
467 $account = $form_values['account'];
468 $current = $form_values['current'];
469 $defaults = $form_values['defaults'];
470 $defaults += array('uid' => $account->uid);
471 $fields = $form_values['subscription_fields'];
472 $values = $form_values['subscriptions'];
473 $check = 'checkbox';
474
475 foreach ($values[$check] as $index => $value) {
476 $subscription = NULL;
477 if ($value) {
478 // Checked, save only if new or changed
479 if (!isset($current[$index])) {
480 $subscription = $defaults;
481 } elseif ($current[$index]->send_interval != $values['send_interval'][$index] || $current[$index]->send_method != $values['send_method'][$index]) {
482 $subscription = (array)($current[$index]);
483 }
484 // Complete and save
485 if ($subscription) {
486 $subscription['send_interval'] = $values['send_interval'][$index];
487 $subscription['send_method'] = $values['send_method'][$index];
488 $subscription['fields'] = $fields[$index];
489 notifications_save_subscription($subscription);
490 }
491 } elseif(isset($current[$index])) {
492 notifications_delete_subscription($current[$index]->sid);
493 }
494 }
495 }

  ViewVC Help
Powered by ViewVC 1.1.2