You are here

function messaging_notify_admin_settings_submit in Messaging 6

Submit admin settings

File

messaging_notify/messaging_notify.module, line 99
Subscriptions to messaging events

Code

function messaging_notify_admin_settings_submit(&$form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';

  //dsm($form_state['values']);

  // Update existing accounts
  if ($op == t('Update user accounts')) {
    $count = 0;
    $update = array_filter($form_state['values']['update_methods']);
    foreach (array_keys($update) as $method) {

      // Delete all existing subscriptions for this method
      notifications_delete_subscriptions(array(
        'type' => 'messaging',
      ), array(
        'method' => $method,
      ));
      if ($send_method = $form_state['values']['messaging_notify_default_' . $method]['method']) {
        $interval = $form_state['values']['messaging_notify_default_' . $method]['interval'];

        // Create a new subscription for each user, speed it up with some query tricks
        db_query("INSERT INTO {notifications} (uid, type, event_type, module, status, conditions, send_method, send_interval) SELECT u.uid, 'messaging', 'message', 'messaging', 7, 1, '%s', %d FROM {users} u WHERE u.status = 1", $send_method, $interval);
        db_query("INSERT INTO {notifications_fields} (sid, field, value) SELECT n.sid, 'method', '%s' FROM {notifications} n WHERE n.status = 7", $method);
      }
      $count += db_result(db_query("SELECT COUNT(*) FROM {notifications} WHERE status = 7"));
    }
    if ($count) {
      db_query("UPDATE {notifications} SET status = 1 WHERE status = 7");
      drupal_set_message(t('Updated or created %count subscriptions', array(
        '%count' => $count,
      )));
    }
  }

  // Remove update values and post to settings submission
  unset($form_state['values']['update_permissions']);
  unset($form_state['values']['update_methods']);
  return system_settings_form_submit($form, $form_state);
}