You are here

function forward_form_submit in Forward 6

Same name and namespace in other branches
  1. 5 forward.module \forward_form_submit()
  2. 7.3 forward.module \forward_form_submit()
  3. 7 forward.module \forward_form_submit()
  4. 7.2 forward.module \forward_form_submit()

File

./forward.module, line 769

Code

function forward_form_submit($form, &$form_state) {
  global $base_url, $user;
  $dynamic_content = '';

  // Access control:
  // Possibly impersonate another user depending on dynamic block configuration settings
  $access_control = variable_get('forward_block_access_control', 'recipient');
  $switch_user = $access_control == 'recipient' || $access_control == 'anonymous';
  $impersonate_user = variable_get('forward_dynamic_block', 'none') != 'none' && $switch_user;
  if ($impersonate_user) {
    $original_user = $user;
    $old_state = session_save_session();
    session_save_session(FALSE);
    if ($access_control == 'recipient') {
      $account = user_load(array(
        'mail' => trim($form_state['values']['recipients']),
      ));

      // Fall back to anonymous user if recipient is not a valid account
      $user = isset($account->status) && $account->status == 1 ? $account : drupal_anonymous_user();
    }
    else {
      $user = drupal_anonymous_user();
    }
  }

  // Compose the body:
  // Note how the form values are accessed the same way they were accessed in the validate function

  //If selected assemble dynamic footer block.
  switch (variable_get('forward_dynamic_block', '')) {
    case 'node':
      if (module_exists('blog')) {
        $dynamic_content_header = '<h3>' . t('Recent blog posts') . '</h3>';
        $query = "SELECT n.nid, n.title FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC";
        $dynamic_content = forward_top5_list($query, $base_url, 'blog');
      }
      break;
    case 'user':
      if (variable_get('forward_block_access_control', 'recipient') != 'none' && user_access('access user profiles')) {
        $dynamic_content_header = '<h3>' . t("Who's new") . '</h3>';
        $query = 'SELECT u.uid, u.name FROM {users} u WHERE status <> 0 ORDER BY uid DESC';
        $dynamic_content = forward_top5_list($query, $base_url, 'user');
      }
      break;
    case 'comment':
      if (module_exists('comment')) {
        $dynamic_content_header = '<h3>' . t('Recent comments') . '</h3>';
        $query = 'SELECT c.nid, c.cid, c.subject FROM {comments} c INNER JOIN {node} n on c.nid = n.nid WHERE c.status = 0 ORDER BY c.timestamp DESC';
        $dynamic_content = forward_top5_list($query, $base_url, 'comment');
      }
      break;
    case 'popular':
      if (module_exists('statistics')) {
        $dynamic_content_header = '<h3>' . t('Most Popular Content') . '</h3>';
        $query = "SELECT n.nid, n.title FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid WHERE s.timestamp <> '0' AND n.status = 1 ORDER BY s.timestamp DESC";
        $dynamic_content = forward_top5_list($query, $base_url, 'blog');
      }
      break;
  }

  // Only include header for non-empty dynamic block
  if ($dynamic_content) {
    $dynamic_content = $dynamic_content_header . $dynamic_content;
  }

  // Restore user if impersonating someone else during dynamic block build
  if ($impersonate_user) {
    $user = $original_user;
    session_save_session($old_state);
  }
  if (!$form_state['values']['path'] || $form_state['values']['path'] == 'epostcard') {
    $emailtype = 'epostcard';
    $content = '';
    $returnurl = '';
  }
  else {
    $emailtype = 'email';
    $returnurl = $form_state['values']['path'];
    $path_array = explode('/', $form_state['values']['path']);
    if ($path_array[0] == 'node' && !empty($path_array[1]) && is_numeric($path_array[1])) {
      $nid = $path_array[1];

      // we have a node
      $content = node_load($nid);
      if (!node_access('view', $content)) {

        // Access is denied
        return drupal_access_denied();
      }
      $content->teaser = check_markup(variable_get('forward_full_body', FALSE) ? $content->body : $content->teaser, $content->format, FALSE);
    }
    else {
      $_GET['q'] = $form_state['values']['path'];

      //_menu_append_contextual_items();
      menu_set_active_item($form_state['values']['path']);

      // Adapted from index.php.
      $content = new stdClass();
      $content->body = menu_execute_active_handler();
      $content->title = menu_get_active_title();

      // It may happen that a drupal_not_found is called in the above call
      if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
        return;
      }
      switch ($content->body) {
        case MENU_NOT_FOUND:
          return drupal_not_found();
          break;
        case MENU_ACCESS_DENIED:
          return drupal_access_denied();
          break;
      }
      $content->teaser = '';
      $content->body = '';
    }
  }
  if (variable_get('forward_allow_message', TRUE)) {
    $message = variable_get('forward_filter_html', FALSE) ? nl2br(filter_xss($form_state['values']['message'], explode(',', variable_get('forward_filter_tags', 'p,br,em,strong,cite,code,ul,ol,li,dl,dt,dd')))) : nl2br(check_plain($form_state['values']['message']));
  }
  else {
    $message = FALSE;
  }
  global $theme_key;
  $theme_key = variable_get('theme_default', '');
  $logo = variable_get('forward_header_image', '') == '' ? theme_get_setting('logo') : variable_get('forward_header_image', '');
  $vars = array(
    'type' => $emailtype,
    'site_name' => check_plain(variable_get('site_name', 'Drupal')),
    'name' => check_plain($form_state['values']['name']),
    'email' => check_plain($form_state['values']['email']),
    'forward_message' => t(variable_get('forward_' . $emailtype . '_message', '!name thought you would like to see the !site web site.'), array(
      '!name' => l($form_state['values']['name'], 'mailto:' . $form_state['values']['email'], array(
        'absolute' => TRUE,
      )),
      '!site' => variable_get('site_name', 'drupal'),
    )),
    'message' => $message,
    'base_url' => $base_url,
    'content' => $content,
    'path' => $returnurl . $form_state['values']['path_cid'],
    'dynamic_content' => $dynamic_content,
    'forward_ad_footer' => variable_get('forward_ad_footer', ''),
    'forward_footer' => variable_get('forward_footer', ''),
    // New values for forward.tpl.php
    'site_url' => url('forward/emailref', array(
      'absolute' => TRUE,
      'query' => 'path=' . $returnurl . $form_state['values']['path_cid'],
    )),
    'width' => variable_get('forward_width', 400),
    'logo' => !empty($logo) ? '<img src="' . url($logo, array(
      'absolute' => TRUE,
    )) . '" alt="" />' : '',
    'title' => $emailtype == 'email' ? l($content->title, 'forward/emailref', array(
      'absolute' => TRUE,
      'query' => 'path=' . $returnurl,
    )) : FALSE,
    'submitted' => theme_get_setting('toggle_node_info_' . $content->type) ? !empty($content->name) ? t('by %author', array(
      '%author' => $content->name,
    )) : t('by %author', array(
      '%author' => variable_get('anonymous', 'Anonymous'),
    )) : FALSE,
    'teaser' => $emailtype == 'email' ? $content->teaser : FALSE,
    'node' => $emailtype == 'email' ? $content : FALSE,
    'link' => $emailtype == 'email' ? l(t('Click here to read more on our site'), 'forward/emailref', array(
      'absolute' => TRUE,
      'query' => 'path=' . $returnurl . $form_state['values']['path_cid'],
    )) : FALSE,
  );
  if (variable_get('forward_theme_template', 0)) {

    // New forward.tpl.php
    $params['body'] = theme('forward', $vars);
  }
  else {

    // Old forward_*_theme functions
    $hook = $emailtype == 'epostcard' ? 'postcard' : $emailtype;
    $params['body'] = theme('forward_' . $hook, $vars);
  }
  $params['subject'] = t(variable_get('forward_' . $emailtype . '_subject', '!name has sent you a message from !site'), array(
    '!name' => $form_state['values']['name'],
    '!site' => variable_get('site_name', 'drupal'),
  ));
  $from = variable_get('forward_sender_address', '');
  if (empty($from)) {
    $from = variable_get('site_mail', '');
  }
  $params['from'] = trim(mime_header_encode($form_state['values']['name']) . ' <' . $from . '>');
  $params['headers']['Reply-To'] = trim(mime_header_encode($form_state['values']['name']) . ' <' . $form_state['values']['email'] . '>');
  $recipients = trim($form_state['values']['recipients']);
  $recipients = preg_replace('/,,+/', ',', trim($recipients, ','));
  $recipients = str_replace(array(
    "\r\n",
    "\n",
    "\r",
  ), ',', $recipients);
  $recipients = explode(',', $recipients);
  foreach ($recipients as $to) {
    drupal_mail('forward', 'forward_page', trim($to), language_default(), $params, $params['from']);

    // Ensure that we register a flood event for each e-mail.
    flood_register_event('forward');
  }

  // insert record into db to record nid, type and timestamp of send
  db_query("INSERT INTO {forward_log} (path, type, timestamp, uid, hostname) VALUES ('%s', '%s', %d, %d, '%s')", $form_state['values']['path'], 'SENT', time(), $user->uid, ip_address());

  // update node forward statistics
  if (!empty($nid)) {
    db_query("UPDATE {forward_statistics} SET forward_count = forward_count+1, last_forward_timestamp = %d WHERE nid = %d", time(), $nid);
  }
  variable_set('forward_total', variable_get('forward_total', 0) + 1);
  variable_set('forward_recipients', variable_get('forward_recipients', 0) + count($recipients));
  drupal_set_message(check_plain(t(variable_get('forward_thankyou', 'Thank you for your help in spreading the word about !site. We appreciate your help.'), array(
    '!site' => variable_get('site_name', 'drupal'),
  ))), 'status');
  if (variable_get('forward_thankyou_send', FALSE)) {
    $thankyou_params = array(
      'from' => variable_get('forward_sender_address', variable_get('site_mail', '')),
      'subject' => t(variable_get('forward_thankyou_subject', 'Thank you for spreading the word about !site'), array(
        '!site' => variable_get('site_name', 'drupal'),
      )),
      'body' => t(variable_get('forward_thankyou_text', "Dear !name,\n\nThank you for your help in spreading the word about !site.  We appreciate your help."), array(
        '!site' => variable_get('site_name', 'drupal'),
        '!name' => check_plain($form_state['values']['name']),
      )),
    );
    drupal_mail('forward', 'forward_thankyou', trim($form_state['values']['email']), language_default(), $thankyou_params, $thankyou_params['from']);
  }
  $form_state['redirect'] = $returnurl != '' ? $returnurl : variable_get('forward_epostcard_return', '');

  // CRMAPI hook - saves data to default enabled CRM
  if (module_exists('crmapi')) {
    if (!empty($user->crmapi_contact) && is_numeric($user->crmapi_contact)) {
      $contact = crmapi_contact_load('', $user->crmapi_contact);
      $contact_id = $user->crmapi_contact;
    }
    else {
      $contact['email'] = $form_state['values']['email'];
      $names = explode(' ', $form_state['values']['name']);
      $contact['first_name'] = $names[0];
      $contact['last_name'] = isset($names[2]) ? $names[2] : $names[1];
      $contact['mail_name'] = $form_state['values']['name'];
      $contact_id = crmapi_contact_save($contact);
    }
    $activity_params = array(
      'contact_id' => $contact_id,
      'activity_id' => 'OLForward',
      'activity_type' => 'F',
      'level' => '',
      'flag' => '',
      'description' => substr(url($returnurl, array(
        'absolute' => TRUE,
      )), 0, 100),
    );
    crmapi_activity_save($activity_params);
  }
}