Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 372 lines (346 sloc) 14.956 kb
8f24f10 6.4 version
Jose Reyero authored
1 <?php
2 // $Id: notifications.admin.inc,v 1.5.2.6.2.19.2.12.2.15 2010/07/22 11:57:49 jareyero Exp $
3
4 // Library for managing subscriptions, pages and forms
5 require_once drupal_get_path('module', 'notifications') . '/notifications.manage.inc';
6
7 /**
8 * Implementation of hook_help()
9 *
10 * This file will be included only for Notifications admin pages
11 */
12 function notifications_help($path, $arg) {
13 $pages = array(
14 '@admin-events' => url( 'admin/messaging/notifications/events'),
15 '@admin-intervals' => url( 'admin/messaging/notifications/interval'),
16 '@admin-subscriptions' => url( 'admin/messaging/notifications/subscriptions'),
17 '@admin-messaging' => url('admin/messaging/settings'),
18 '@admin-messaging' => url('admin/messaging/settings/method'),
19 );
20 switch ($path) {
21 case 'admin/messaging/notifications':
22 // Try to clarify some concepts, tell the long story short.
23 $output = '<p>' . t('Users can subscribe to different objects (nodes, users, tags) by creating <strong><a href="@admin-subscriptions">Subscriptions</a></strong> for that objects. The subscription types available and the options to display will depend on additional modules enabled.', $pages) . '</p>';
24 $output .= '<p>' . t('When an <a href="@admin-events">Event</a> happens (node update, comment) and it matches an existing subscription, that triggers a <strong>Notification</strong> which is a <strong>Message</strong> that will be sent to the user through one of the available <a href="@admin-messaging-methods">Sending Methods</a>.', $pages) . '</p>';
25 $output .= '<p>' . t('These <strong>Notifications</strong> can be sent right away or queued to be processed on <i>cron</i>. Optionally Notifications can be digested and sent out every some <a href="@admin-intervals">time interval</a>.', $pages) . '</p>';
26 return $output;
27 case 'admin/messaging/notifications/subscriptions':
28 $output = '<p>' . t('On this page you can define which subscription types are enabled or disabled. <strong>Disabled subscription types will not be available for users</strong>.') . '</p>';
29 $output .= '<p>' . t('<strong>Existing subscriptions will be updated accordingly</strong>. They\'ll be disabled when their type is disabled or re-enabled when they were disabled and the type is enabled.') . '</p>';
30 return $output;
31
32 }
33 }
34
35 /**
36 * Menu callback: subscriptions administration.
37 */
f613683 Some admin form
Jose Reyero authored
38 function notifications_admin_manage_subscriptions($form, &$form_state) {
39 module_load_include('manage.inc', 'notifications');
8f24f10 6.4 version
Jose Reyero authored
40 if (isset($form_state['values']['operation']) && $form_state['values']['operation'] == 'delete') {
f613683 Some admin form
Jose Reyero authored
41 return notifications_manage_multiple_delete_confirm($form_state, array_filter($form_state['values']['subscriptions']));
8f24f10 6.4 version
Jose Reyero authored
42 }
f613683 Some admin form
Jose Reyero authored
43 //$form['filter'] = notifications_manage_subscriptions_filter_form(TRUE);
44 $form['#submit'][] = 'notifications_manage_subscriptions_filter_form_submit';
45 //$form['#theme'] = 'notifications_subscriptions_filter_form';
8f24f10 6.4 version
Jose Reyero authored
46 $form['admin'] = notifications_manage_subscriptions_form();
47
48 return $form;
49 }
50
51 /**
52 * Current subscriptions page
53 */
54 function notifications_admin_status_page() {
55 $output = '';
56
57 // Subscriptions summary by type
58 $header = array(t('Type'), t('Number'));
bb90f87 Drafted new queue schema
Jose Reyero authored
59 $result = db_query("SELECT type, count(*) AS count FROM {notifications_subscription} GROUP BY type");
8f24f10 6.4 version
Jose Reyero authored
60 $count = 0;
5571f03 Notifications_Subscription::delete_multiple()
Jose Reyero authored
61 $types = notifications_subscription_type();
8f24f10 6.4 version
Jose Reyero authored
62 $rows = array();
63 while ($stype = db_fetch_object($result)) {
64 $rows[] = array(
65 !empty($types[$stype->type]['title']) ? $types[$stype->type]['title'] : '<strong>' . $stype->type . '</strong>' ,
66 $stype->count);
67 $count += $stype->count;
68 }
4074c99 Upgrading
Jose Reyero authored
69 $summary = theme('table', array('header' => $header, 'rows' =>$rows));
8f24f10 6.4 version
Jose Reyero authored
70 $summary .= t('Total: %number', array('%number' => $count));
71 $output .= theme('box', t('Subscriptions by type'), $summary);
72
73 // Summary by sending method
74 $header = array(t('Method'), t('Number'));
bb90f87 Drafted new queue schema
Jose Reyero authored
75 $result = db_query("SELECT send_method, count(*) AS count FROM {notifications_subscription} GROUP BY send_method");
8f24f10 6.4 version
Jose Reyero authored
76 $rows = array();
77 while ($subs = db_fetch_object($result)) {
78 $name = messaging_method_info($subs->send_method, 'title');
79 $rows[] = array(
80 $name ? $name : '<strong>' . $subs->send_method . '</strong>' ,
81 $subs->count);
82 }
4074c99 Upgrading
Jose Reyero authored
83 $summary = theme('table', array('header' => $header, 'rows' =>$rows));
8f24f10 6.4 version
Jose Reyero authored
84 $output .= theme('box', t('Subscriptions by send method'), $summary);
85
86 return $output;
87 }
88
89 /**
90 * Admin settings
91 */
92 function notifications_settings_form() {
93 $form['general'] = array(
94 '#type' => 'fieldset',
95 '#title' => t('General settings'),
96 '#weight' => -10,
97 );
98 $form['general']['notifications_sendself'] = array(
99 '#type' => 'checkbox',
100 '#title' => t('Notify poster of own posts'),
101 '#default_value' => variable_get('notifications_sendself', 1),
0ab9a2f Moved send intervals to notifications_queue
Jose Reyero authored
102 '#description' => t("Notifies a node poster about their own posts. Useful principally during testing."),
8f24f10 6.4 version
Jose Reyero authored
103 );
0ab9a2f Moved send intervals to notifications_queue
Jose Reyero authored
104 $form['general']['notifications_event_queue'] = array(
105 '#title' => t('Queue notifications'),
8f24f10 6.4 version
Jose Reyero authored
106 '#type' => 'checkbox',
0ab9a2f Moved send intervals to notifications_queue
Jose Reyero authored
107 '#default_value' => variable_get('notifications_event_queue', 0),
108 '#description' => t('Queue notifications to be sent on cron process later. Recommended for sites with a large number of users.'),
8f24f10 6.4 version
Jose Reyero authored
109 );
110 $form['general']['notifications_sender'] = array(
111 '#title' => t('Notifications Sender'),
112 '#type' => 'radios',
113 '#options' => array(
114 t('No one (All notifications will appear as coming from the web site)'),
115 t('User name, site data (Only the user name will be used)'),
116 t('Full user data (User name and available user information)'),
117 ),
118 '#default_value' => variable_get('notifications_sender', 0),
119 '#description' => t('Use the site information as the sender for notification messages or use account data from the user causing the event. WARNING: Selecting the last option (Full user data) may disclose private information to subscribers like the user e-mail address.'),
120 );
121
122 // Default options
123 $form['defaults'] = array(
124 '#type' => 'fieldset',
125 '#title' => t('Default settings'),
126 );
127 $form['defaults']['notifications_default_send_interval'] = array(
128 '#type' => 'select',
129 '#title' => t('Default send interval'),
0ab9a2f Moved send intervals to notifications_queue
Jose Reyero authored
130 '#options' => notifications_send_intervals(),
8f24f10 6.4 version
Jose Reyero authored
131 '#default_value' => variable_get('notifications_default_send_interval', 0),
132 );
133
134 return system_settings_form($form);
135 }
136
137
138 /**
139 * Subscription settings
140 */
141 function notifications_admin_subscriptions_settings() {
f68fd46 Add subscription form
Jose Reyero authored
142 // Enable / disable subscription types
143 $type_list = array_map('notifications_format_title_description', notifications_subscription_type());
8f24f10 6.4 version
Jose Reyero authored
144 $form['subscriptions'] = array(
145 '#title' => t('Enabled Subscription types'),
146 '#type' => 'fieldset',
147 '#collapsible' => TRUE,
148 );
f68fd46 Add subscription form
Jose Reyero authored
149 $form['subscriptions']['enabled'] = array(
8f24f10 6.4 version
Jose Reyero authored
150 '#type' => 'checkboxes',
f68fd46 Add subscription form
Jose Reyero authored
151 '#options' => $type_list,
152 '#default_value' => notifications_subscription_type_enabled(),
153 '#return_value' => TRUE,
8f24f10 6.4 version
Jose Reyero authored
154 '#description' => t('Check the available subscription types that will be enabled globally'),
155 );
156 // Link options. These were UI settings that will become global now, so other modules can use them without Notifications UI
157 $form['links'] = array(
158 '#title' => t('Subscribe / unsubscribe links options'),
159 '#type' => 'fieldset',
160 '#collapsible' => TRUE,
161 );
162 $options = array(
163 t('<strong>Confirmation form</strong>. Links take the user through a confirmation form with some more options.'),
164 t('<strong>Direct operation</strong>. Links create/delete a subscription without confirmation using default settings.'),
165 );
62db0da Updating
Jose Reyero authored
166 $form['links']['notifications_option_subscribe_links'] = array(
8f24f10 6.4 version
Jose Reyero authored
167 '#title' => t('Subscribe links'),
168 '#type' => 'radios',
169 '#options' => $options,
62db0da Updating
Jose Reyero authored
170 '#default_value' => variable_get('notifications_option_subscribe_links', 0),
8f24f10 6.4 version
Jose Reyero authored
171 );
62db0da Updating
Jose Reyero authored
172 $form['links']['notifications_option_unsubscribe_links'] = array(
8f24f10 6.4 version
Jose Reyero authored
173 '#title' => t('Unsubscribe links'),
174 '#type' => 'radios',
175 '#options' => $options,
62db0da Updating
Jose Reyero authored
176 '#default_value' => variable_get('notifications_option_unsubscribe_links', 0),
8f24f10 6.4 version
Jose Reyero authored
177 );
178
179 $form = system_settings_form($form);
180 // We have our own processing to do after system settings
181 $form['#submit'][] = 'notifications_admin_subscriptions_settings_submit';
182
183 return $form;
184 }
185
186 /**
187 * Subscription settings submit, disable all subscriptions not allowed
62db0da Updating
Jose Reyero authored
188 *
189 * @todo d7update
8f24f10 6.4 version
Jose Reyero authored
190 */
191 function notifications_admin_subscriptions_settings_submit($form, &$form_state) {
f68fd46 Add subscription form
Jose Reyero authored
192 $types_enabled = $form_state['values']['enabled'];
193 // Convert type name values to 1 for storage (instead of type name)
194 $enabled = array_filter($types_enabled);
195 $types_enabled = array_merge($types_enabled, array_combine($enabled, array_fill(0, count($enabled), 1)));
196 notifications_option_array_set('subscription_types', 'enabled', $types_enabled);
197 unset($form_state['values']['enabled']);
8f24f10 6.4 version
Jose Reyero authored
198 // Submission may have been Save or Reset to defaults, so we cannot really trust submitted values
199 // Instead we do a cache refresh and re-check enabled / disabled types
4074c99 Upgrading
Jose Reyero authored
200 drupal_static_reset('notifications_subscription_type_enabled');
8f24f10 6.4 version
Jose Reyero authored
201 $types = notifications_subscription_type_enabled();
bb90f87 Drafted new queue schema
Jose Reyero authored
202 $sql_update = 'UPDATE {notifications_subscription} SET status = %d WHERE status = %d';
8f24f10 6.4 version
Jose Reyero authored
203 $enabled = $disabled = 0;
62db0da Updating
Jose Reyero authored
204 /*
8f24f10 6.4 version
Jose Reyero authored
205 if ($types) {
206 // Enable subscriptions of these types that were disabled before
207 $placeholders = db_placeholders($types, 'varchar');
208 $sql_where = ' AND type IN (' . $placeholders . ')';
209 $params = array_merge(array(Notifications_Subscription::STATUS_ACTIVE, Notifications_Subscription::STATUS_DISABLED), $types);
210 db_query($sql_update . $sql_where, $params);
211 $enabled = db_affected_rows();
212
213 $params = array_merge(array(Notifications_Subscription::STATUS_DISABLED, Notifications_Subscription::STATUS_ACTIVE), $types);
214 $sql_where = ' AND type NOT IN (' . $placeholders . ')';
215 }
216 else {
217 $params = array(Notifications_Subscription::STATUS_DISABLED, Notifications_Subscription::STATUS_ACTIVE);
218 $sql_where = '';
219 }
220 db_query($sql_update . $sql_where, $params);
221 $disabled = db_affected_rows();
222
223 if ($enabled) {
224 drupal_set_message(t('@count existing subscriptions have been enabled.', array('@count' => $enabled)), 'warning');
225 }
226 if ($disabled) {
227 drupal_set_message(t('@count existing subscriptions have been disabled.', array('@count' => $disabled)), 'warning');
228 // Delete rows from queue and do some clean up.
229 notifications_queue()->queue_clean(FALSE);
230 }
62db0da Updating
Jose Reyero authored
231 */
8f24f10 6.4 version
Jose Reyero authored
232 }
233
234 /**
235 * Event configuration administration
236 *
237 * Will allow to define which events trigger a notification and which ones not
238 */
96e83bd Built skeleton for Notifications_Template
Jose Reyero authored
239 function notifications_admin_events_form($form, &$form_state) {
8f24f10 6.4 version
Jose Reyero authored
240 // Compile array from plug-ins and settings
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
241 $event_list = notifications_event_type();
242 $template_list = notifications_info('message templates', NULL, 'title');
243
244 $header = array(
96e83bd Built skeleton for Notifications_Template
Jose Reyero authored
245 'name' => t('Event type'),
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
246 'template' => t('Template'),
247 'triggers' => t('Triggers'),
248 );
249 $form['events'] = array(
96e83bd Built skeleton for Notifications_Template
Jose Reyero authored
250 '#tree' => TRUE,
251 '#type' => 'fieldset',
252 '#theme' => 'notifications_admin_table_form',
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
253 '#header' => $header,
254 '#empty' => t('You need to enable some plug-ins to provide notifications events.'),
255 );
96e83bd Built skeleton for Notifications_Template
Jose Reyero authored
256 foreach (notifications_event_type() as $type => $event) {
257 $form['events']['name'][$type]['#markup'] = $event['title'];
258 $form['events']['template'][$type] = array(
259 '#type' => 'select',
260 '#options' => $template_list,
261 '#default_value' => $event['template'],
262 );
263 $form['events']['triggers'][$type]['#markup'] = implode(', ', notifications_admin_event_triggers($type));
264 }
265 $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
266 return $form;
8f24f10 6.4 version
Jose Reyero authored
267 }
268
269 /**
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
270 * Get assigned triggers for event
271 *
272 * @todo Better coding, get from db?
273 */
274 function notifications_admin_event_triggers($type) {
275 static $action_info, $trigger_info;
276 if (!isset($action_info)) {
277 $action_info = module_invoke_all('action_info');
278 // We need to index trigger info by actual trigger key
96e83bd Built skeleton for Notifications_Template
Jose Reyero authored
279 foreach (module_invoke_all('trigger_info') as $trigger_type => $type_info) {
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
280 foreach ($type_info as $key => $key_info) {
281 $trigger_info[$key] = $key_info;
282 }
283 }
284 }
96e83bd Built skeleton for Notifications_Template
Jose Reyero authored
285 $event_triggers = array();
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
286 $event_actions = notifications_event_type($type, 'actions', array());
287 foreach ($event_actions as $action) {
288 foreach ($action_info[$action]['triggers'] as $trigger) {
289 foreach (trigger_get_assigned_actions($trigger) as $trigger_action) {
290 if (in_array($trigger_action['aid'], $event_actions)) {
291 $event_triggers[$trigger] = $trigger_info[$trigger]['label'];
292 }
293 }
294 }
295 }
296 return $event_triggers;
297 }
298 /**
8f24f10 6.4 version
Jose Reyero authored
299 * Build event tree
300 */
301 function notifications_admin_events_build_tree($events) {
302 $tree = array();
303 foreach ($events as $key => &$event) {
304 if (empty($event['parent'])) {
305 $tree[$key] = &$event;
306 }
307 else {
308 $events[$event['parent']]['children'][$key] = &$event;
309 }
310 }
311 return $tree;
312 }
313
314 /**
315 * Build tree of event types
316 */
317 function notifications_admin_events_tree_form($tree, &$form, $depth = 0) {
318 foreach ($tree as $key => $event) {
319 $form['name'][$key] = array('#value' => theme('indentation', $depth) . $event['description']);
320 $form['notifications_event_enabled'][$key] = array(
321 '#type' => 'checkbox',
322 '#default_value' => notifications_event_enabled($key),
323 );
324 $form['notifications_event_template'][$key] = array(
325 '#type' => 'select',
326 '#default_value' => Notifications_Event::template_map($key),
327 '#options' => notifications_template_list(),
328 );
329 if (!empty($event['children'])) {
330 notifications_admin_events_tree_form($event['children'], $form, $depth +1);
331 }
332 }
333 }
334
335 /**
336 * Plain list of template names
337 */
338 function notifications_template_list() {
4074c99 Upgrading
Jose Reyero authored
339 $list = &drupal_static(__FUNCTION__);
8f24f10 6.4 version
Jose Reyero authored
340 if (!isset($list)) {
341 notifications_include('templates.inc');
342 foreach (notifications_get_templates() as $key => $template) {
343 $list[$key] = $template['name'];
344 }
345 }
346 return $list;
347 }
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
348
8f24f10 6.4 version
Jose Reyero authored
349 /**
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
350 * Theme administration form/fieldset as table
8f24f10 6.4 version
Jose Reyero authored
351 */
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
352 function theme_notifications_admin_table_form($variables) {
353 $form = $variables['form'];
354 $default = language_default();
355 $header = $form['#header'];
356 $index = key($header);
357 $rows = array();
358 foreach ($form[$index] as $key => $element) {
359 // Do not take form control structures.
360 if (is_array($element) && element_child($key)) {
8f24f10 6.4 version
Jose Reyero authored
361 $row = array();
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
362 foreach (array_keys($header) as $field) {
363 $row[] = drupal_render($form[$field][$key]);
8f24f10 6.4 version
Jose Reyero authored
364 }
365 $rows[] = $row;
366 }
367 }
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
368
369 $output = theme('table', array('header' => $header, 'rows' => $rows, 'empty' => $form['#empty']));
370 $output .= drupal_render_children($form);
8f24f10 6.4 version
Jose Reyero authored
371 return $output;
4d58caa Fixed bug in theme hook, updated event and intervals admin tab
Jose Reyero authored
372 }
Something went wrong with that request. Please try again.