'admin/notifications/settings/ui', 'title' => t('User Interface'), 'description' => t('Enables site settings for user subscriptions.'), 'callback' => 'drupal_get_form', 'callback arguments' => 'notifications_ui_settings_form', 'access' => user_access('administer site configuration'), 'type' => MENU_LOCAL_TASK, ); } return $items; } /** * Implementation of hook_notifications. */ function notifications_ui_notifications($op, $arg0, $arg1 = NULL, $arg2 = NULL) { if ($op == 'event trigger') { $event = $arg0; if ($event->type == 'node' && isset($event->node->subscriptions)) { if ($event->action == 'insert') { // On insert some field information will be missing, we need to recreate it. foreach ($event->node->subscriptions['params'] as $i => $subscriptions) { foreach ($subscriptions['fields'] as $key => $value) { if (!$value && isset($event->params[$key])) { $event->node->subscriptions['params'][$i]['fields'][$key] = $event->params[$key]; } } } } notifications_ui_form_submit('', array('subscriptions' => $event->node->subscriptions)); } elseif ($event->type == 'node' && isset($event->comment->subscriptions)) { notifications_ui_form_submit('', array('subscriptions' => $event->comment->subscriptions)); } } } /** * Site-wide settings form. */ function notifications_ui_settings_form() { $form['notifications_ui_types'] = array( '#type' => 'checkboxes', '#title' => t('Enabled subscription types'), '#options' => notifications_subscription_types(NULL, 'title'), '#default_value' => variable_get('notifications_ui_types', array()), ); return system_settings_form($form); } /** * Implementation of hook_form_alter() * */ function notifications_ui_form_alter($form_id, &$form) { global $user; // Content type settings if ($form_id == 'node_type_form' && isset($form['identity']['type'])) { // Just in case we want to add more settings here $form['workflow']['notifications_node_ui'] = array( '#type' => 'radios', '#title' => t('Subscriptions UI'), '#default_value' => variable_get('notifications_node_ui_'.$form['#node_type']->type, ''), '#options' => array('' => t('None'), 'form' => t('As form below the node content'), 'links' => t('As node links')), ); } elseif (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && $form['#node']->comment == COMMENT_NODE_READ_WRITE) { // Add node forms. $node = $form['#node']; $form[] = notifications_ui_form($node); } elseif ($form_id == "comment_form") { // Add to comment forms. $node = node_load($form['nid']['#value']); $form[] = notifications_ui_form($node); } } /** * Form for node subscriptions * @ TODO: offer the same form in a block to be put in the contents region. * * @param $node * a node object * * @return * Partial subscription form, just missing submit button. */ function notifications_ui_form($node) { global $user; $form = array(); $node_options = notifications_ui_user_node($user, $node); if (count($node_options)) { // Process all options building the array of indexed params for each $options = $params = $defaults = array(); $index = 1; // If we start with zero, get some value sent as 0 => 0 $number = 0; // Number of subscriptions foreach ($node_options as $option) { $options[$index] = $option['name']; // Check wether user is subscribed if (!empty($option['subscription'])) { $params[$index] = (array)$option['subscription']; $defaults[] = $index; $number++; } else { $params[$index] = $option; } $index++; } // Now we have compiled the data, build the form $form['subscriptions'] = array( '#type' => 'fieldset', '#title' => t('Subscriptions (%number)', array('%number' => $number)), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, ); $form['subscriptions']['params'] = array('#type' => 'value', '#value' => $params); $form['subscriptions']['options'] = array( '#type' => 'checkboxes', '#default_value' => $defaults, '#options' => $options, ); $form['subscriptions']['account'] = array('#type' => 'value', '#value' => $user); } return $form; } /** * Form submission, node subscriptions form */ function notifications_ui_form_submit($form_id, $form_values) { $uid = $form_values['subscriptions']['account']->uid; foreach ($form_values['subscriptions']['options'] as $index => $value) { $subscription = $form_values['subscriptions']['params'][$index] + array('uid' => $uid, 'event_type' => 'node'); if ($value) { notifications_save_subscription($subscription); } elseif(!empty($subscription['sid'])) { notifications_delete_subscription($subscription['sid']); } } } /** * Implementation of hook_link() * * Add subscriptions links to nodes */ function notifications_ui_link($type, $node = NULL, $teaser = FALSE) { global $user; $links = array(); if ($type == 'node' && !$teaser && $user->uid && variable_get('notifications_node_ui_'.$node->type, 'links') == 'links') { // Now we have the array of allowed options ready, build links foreach (notifications_ui_user_node($user, $node) as $index => $option) { if (!empty($option['subscription'])) { // Unsubscribe link $title = t('Unsubscribe to: !name', array('!name' => $option['name'])); $props = notifications_get_link('unsubscribe', array('sid' => $option['subscription']->sid)); } else { // Subscribe link $title = t('Subscribe to: !name', array('!name' => $option['name'])); $props = notifications_get_link('subscribe', array('type' => $option['type'], 'confirm' => TRUE, 'fields' => $option['fields'])); } $links['notifications_'.$index] = array( 'title' => $title, 'html' => TRUE, ) + $props; } } return $links; } /** * Get list of allowed subscriptions types * * Checks permissions and settings * * @return * Subscription types allowed for this user */ function notifications_ui_allowed_types() { $enabled = variable_get('notifications_ui_types', array()); $allowed = array(); foreach (notifications_subscription_types() as $type => $info) { if (isset($enabled[$type]) && $enabled[$type] && !empty($info['access']) && user_access($info['access'])) { $allowed[$type] = $info; } } return $allowed; } /** * Get list of possible and existing subscriptions for user/node * * @return * Array of subscription options * The enabled ones will have a 'subscriptions' element loaded */ function notifications_ui_user_node($account, $node) { // Get allowed node options and current subscriptions $node_options = notifications_module_info('node options', $account, $node); $allowed_types = notifications_ui_allowed_types(); $allowed_options = array(); // We also keep track of event types for each subscription type $event_types = array('node' => TRUE); foreach ($node_options as $index => $option) { if (isset($allowed_types[$option['type']])) { $allowed_options[] = $option; // We add the event type to our list $event_types[$allowed_types[$option['type']]['event_type']] = TRUE; } } // Check if user is subscribed to show 'subscribe'/'unsubscribe' links $subscriptions = array(); foreach (array_keys($event_types) as $type) { if ($more = notifications_user_get_subscriptions($account->uid, $type, $node->nid, $node)) { $subscriptions += $more; } } foreach ($allowed_options as $index => $option) { foreach ($subscriptions as $sub) { if ($sub->type == $option['type'] && !array_diff_key($option['fields'], $sub->fields)) { $allowed_options[$index]['subscription'] = $sub; } } } return $allowed_options; }