function forward_form_submit in Forward 7
Same name and namespace in other branches
- 5 forward.module \forward_form_submit()
- 6 forward.module \forward_form_submit()
- 7.3 forward.module \forward_form_submit()
- 7.2 forward.module \forward_form_submit()
@todo Please document this function.
See also
File
- ./
forward.module, line 799
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 = drupal_save_session();
drupal_save_session(FALSE);
if ($access_control == 'recipient') {
$account = user_load_by_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 = db_select('node', 'n');
$query
->fields('n', array(
'nid',
'title',
));
$query
->condition('n.type', 'blog');
$query
->condition('n.status', 1);
$query
->orderBy('n.created', 'DESC');
if (variable_get('forward_block_access_control', 'recipient') != 'none') {
$query
->addTag('node_access');
}
$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 = db_select('users', 'u');
$query
->fields('u', array(
'uid',
'name',
));
$query
->condition('u.status', 0, '<>');
$query
->orderBy('u.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 = db_select('comment', 'c');
$query
->fields('c', array(
'nid',
'cid',
'subject',
));
$query
->condition('c.status', 1);
$query
->orderBy('c.created', 'DESC');
if (variable_get('forward_block_access_control', 'recipient') != 'none') {
$query
->addTag('node_access');
}
$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 = db_select('node_counter', 's');
$query
->join('node', 'n', 's.nid = n.nid');
$query
->fields('n', array(
'nid',
'title',
));
$query
->condition('s.totalcount', 0, '>');
$query
->condition('n.status', 1);
$query
->orderBy('s.totalcount', 'DESC');
if (variable_get('forward_block_access_control', 'recipient') != 'none') {
$query
->addTag('node_access');
}
$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;
drupal_save_session($old_state);
}
// Send email of appropruate type based on module configuration
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();
}
//dsm($content);
$node = $content;
$node->content = array();
$langcode = $GLOBALS['language_content']->language;
if (variable_get('forward_custom_viewmode', FALSE)) {
$view_mode = 'forward';
field_attach_prepare_view('node', array(
$node->nid => $node,
), $view_mode);
entity_prepare_view('node', array(
$node->nid => $node,
));
$node->content += field_attach_view('node', $node, $view_mode, $langcode);
$content->teaser = render($node->content);
}
if (empty($content->teaser)) {
$view_mode = variable_get('forward_full_body', FALSE) ? 'full' : 'teaser';
field_attach_prepare_view('node', array(
$node->nid => $node,
), $view_mode);
entity_prepare_view('node', array(
$node->nid => $node,
));
$node->content += field_attach_view('node', $node, $view_mode, $langcode);
$content->teaser = render($node->content);
}
}
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(NULL, FALSE);
$content->title = menu_get_active_title();
// It may happen that a drupal_not_found is called in the above call
$headers = drupal_get_http_header();
if ($headers) {
foreach ($headers as $header) {
if (preg_match('/404 Not Found/', $header) == 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 = '';
$content->type = '';
}
}
if (variable_get('forward_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' => array(
'path' => '<front>',
),
)),
'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' => array(
'path' => $returnurl,
),
)) : FALSE,
'submitted' => $emailtype == 'email' && variable_get('node_submitted_' . $content->type) ? !empty($content->name) ? t('by %author', array(
'%author' => $content->name,
)) : t('by %author', array(
'%author' => variable_get('anonymous', 'Anonymous'),
)) : FALSE,
'node' => $emailtype == 'email' ? $content->teaser : FALSE,
'link' => $emailtype == 'email' ? l(t('Click here to read more on our site'), 'forward/emailref', array(
'absolute' => TRUE,
'query' => array(
'path' => $returnurl . $form_state['values']['path_cid'],
),
)) : FALSE,
);
if (variable_get('forward_theme_template', 0)) {
// New forward.tpl.php
$params['body'] = theme('forward', array(
'vars' => $vars,
));
}
else {
// Old forward_*_theme functions
$params['body'] = theme('forward_' . $emailtype, array(
'vars' => $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
$id = db_insert('forward_log')
->fields(array(
'path' => $form_state['values']['path'],
'type' => 'SENT',
'timestamp' => REQUEST_TIME,
'uid' => $user->uid,
'hostname' => ip_address(),
))
->execute();
// update node forward statistics
if (!empty($nid)) {
db_update('forward_statistics')
->fields(array(
'last_forward_timestamp' => REQUEST_TIME,
))
->expression('forward_count', 'forward_count + 1')
->condition('nid', $nid)
->execute();
}
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' => $from,
'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']),
)),
);
//dsm('here');
$mail = drupal_mail('forward', 'forward_thankyou', trim($form_state['values']['email']), language_default(), $thankyou_params, $thankyou_params['from']);
//dsm($mail);
}
$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);
}
// Rules invoke event fires if available
if (module_exists('rules')) {
rules_invoke_event('node_forward', $user, $node);
}
}