You are here

function forward_form_submit in Forward 5

Same name and namespace in other branches
  1. 6 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 582

Code

function forward_form_submit($form_id, $edit) {
  global $base_url, $user;
  $dynamic_content = "";

  // Prepare the sender:
  $from = $edit['mail'];

  // 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':
      $dynamic_content = '<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':
      $dynamic_content = '<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':
      $dynamic_content = '<h3>' . t('Recent comments') . '</h3>';
      $query = 'SELECT c.nid, c.cid, c.subject FROM {comments} c WHERE c.status = 0 ORDER BY c.timestamp DESC';
      $dynamic_content .= forward_top5_list($query, $base_url, 'comment');
      break;
    case 'popular':
      $dynamic_content = '<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;
  }
  if ($edit['path'] == 'epostcard') {
    $emailtype = 'postcard';
    $returnurl = '';
  }
  else {
    $emailtype = 'email';
    $path = explode('/', $edit['path']);
    $returnurl = $edit['path'];
    if ($path[0] == 'node' && !empty($path[1]) && is_numeric($path[1])) {
      $nid = $path[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($content->teaser, $content->format, false);
    }
    else {
      $_GET['q'] = $edit['path'];
      _menu_append_contextual_items();
      menu_set_active_item($edit['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 = '';
    }
  }
  global $theme_key;
  $theme_key = variable_get('theme_default', '');
  $vars = array(
    'logo' => variable_get('forward_header_image', '') == '' ? theme_get_setting('logo') : variable_get('forward_header_image', ''),
    'site_name' => check_plain(variable_get('site_name', 'Drupal')),
    'yemail' => $edit['yemail'],
    'forward_message' => t(variable_get('forward_' . $emailtype . 'message', '!name thought you would like to see the !site web site.'), array(
      '!name' => l($edit['yname'], 'mailto:' . $edit['yemail'], null, null, null, true),
      '!site' => variable_get('site_name', 'drupal'),
    )),
    'message' => variable_get('forward_filter_html', false) ? nl2br(filter_xss($edit['message'], explode(',', variable_get('forward_filter_tags', 'p,br,em,strong,cite,code,ul,ol,li,dl,dt,dd')))) : nl2br(check_plain($edit['message'])),
    'base_url' => $base_url,
    'content' => $content,
    'path' => $edit['path'] . $edit['path_cid'],
    'dynamic_content' => $dynamic_content,
    'forward_ad_footer' => variable_get('forward_ad_footer', ''),
    'forward_footer' => variable_get('forward_footer', ''),
  );
  $body = theme('forward_' . $emailtype, $vars);
  $subject = t(variable_get('forward_' . $emailtype . 'subject', '!name has sent you a message from !site'), array(
    '!name' => $edit['yname'],
    '!site' => variable_get('site_name', 'drupal'),
  ));
  $from = variable_get('forward_sender_address', '') != '' ? variable_get('forward_sender_address', '') : $edit['yemail'];
  $headers['MIME-Version'] = '1.0';
  $headers['Content-Type'] = 'text/html; charset=utf-8';
  $recipients = trim($edit['recipients']);
  $recipients = str_replace(array(
    "\r\n",
    "\n",
    "\r",
  ), ',', $recipients);
  $recipients = explode(',', $recipients);
  foreach ($recipients as $to) {
    $from = $edit['yname'] . ' <' . variable_get('site_mail', '') . '>';
    $headers['Reply-To'] = $edit['yname'] . ' <' . $edit['yemail'] . '>';
    drupal_mail('forward', trim($to), $subject, $body, trim($from), $headers);

    // 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) VALUES ('%s', '%s', %d)", $edit['path'], 'SENT', time());

  // 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(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');

  //print $returnurl; exit;

  // 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'] = $edit['yemail'];
      $names = explode(' ', $edit['yname']);
      $contact['first_name'] = $names[0];
      $contact['last_name'] = isset($names[2]) ? $names[2] : $names[1];
      $contact['mail_name'] = $edit['yname'];
      $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, null, null, true), 0, 100),
    );
    crmapi_activity_save($activity_params);
  }
  return $returnurl != '' ? $returnurl : variable_get('forward_epostcard_return', '');
}