You are here

function notifications_content_test_template_submit in Notifications 7

Same name and namespace in other branches
  1. 6.4 notifications_content/notifications_content.pages.inc \notifications_content_test_template_submit()

Process template test

1 string reference to 'notifications_content_test_template_submit'
notifications_content_test_template_form in notifications_content/notifications_content.admin.inc
Template testing form

File

notifications_content/notifications_content.admin.inc, line 106
Subscriptions to content events

Code

function notifications_content_test_template_submit(&$form, &$form_state) {
  global $user;

  // Load node or build fake node
  if ($nid = (int) $form_state['values']['node']) {
    if (($node = node_load($nid)) && node_access('view', $node)) {
      drupal_set_message(t('Loaded node type @type', array(
        '@type' => $node->type,
      )));
    }
  }
  if (empty($node)) {
    $type = $form_state['values']['type'];
    $node = _notifications_content_test_generate_content($type);
    $node->nid = 1;
    $node->uid = $user->uid;
    $node->type = $type;
  }

  // Build fake event for node/action
  $action = $form_state['values']['action'];
  $objects['node'] = $node;

  // Add comment if needed
  if ($action == 'comment') {
    $comment = _notifications_content_test_generate_content('comment');
    $comment->cid = 1;
    $comment->uid = $user->uid;
    $comment->nid = $node->nid;
    $objects['comment'] = $comment;
  }
  $event = Notifications_Event::create(array(
    'eid' => 1,
    'module' => 'node',
    'type' => 'node',
    'action' => $action,
  ), $objects);
  if (!$event->queue) {
    drupal_set_message(t('That event type is not enabled though we try building a message for it.'), 'warning');
  }

  // Get a subscription template, that may determine the module
  $subscription = Notifications_Subscription::build_type($form_state['values']['subscription_type']);

  // Build the message and store it
  $send_method = $form_state['values']['send_method'];
  if ($destination = messaging_account_build_destination($user, $send_method)) {
    $template = new Notifications_Message();
    $template->method = $send_method;
    $template
      ->set_destination($destination);
    $template->language = $form_state['values']['language'];
    $message = Notifications_Message::build_simple_message($template, $event, array(), $subscription->module);

    // Store message and rebuild. The message will come with template information (text_parts);
    $form_state['values']['message'] = $message;
    $form_state['rebuild'] = TRUE;
  }
  else {
    drupal_set_message(t('Cannot build destination.'), 'warning');
  }
}