You are here

function email_mail_page in Email Field 7

Same name and namespace in other branches
  1. 5 email.module \email_mail_page()
  2. 6.2 email.module \email_mail_page()
  3. 6 email.module \email_mail_page()

The contact form page.

1 string reference to 'email_mail_page'
email_menu in ./email.module
Implements hook_menu().

File

./email.module, line 278
Module file for the email module, which creates a email address field.

Code

function email_mail_page($object_type, $object_id, $field_name) {
  if (!is_numeric($object_id)) {
    return MENU_NOT_FOUND;
  }

  // Verify this is an email field.
  $field_info = field_info_field($field_name);
  if (!isset($field_info) || $field_info['type'] != 'email') {
    return MENU_NOT_FOUND;
  }

  // Check that the entity exists.
  $objects = entity_load($object_type, array(
    $object_id,
  ));
  if (!isset($objects[$object_id])) {
    return MENU_NOT_FOUND;
  }
  $object = $objects[$object_id];

  // Check that the entity has the email field.
  if (!isset($object->{$field_name})) {
    return MENU_NOT_FOUND;
  }

  // Check if the current user has access to the entity and to the field.
  if (!email_mail_page_access($object_type, $object, $field_info)) {
    return MENU_ACCESS_DENIED;
  }

  //use the first email address as receiver
  $field = array_pop($object->{$field_name});
  foreach ($field as $delta => $item) {
    if (!empty($item['email'])) {
      $email = $item['email'];
      break;
    }
  }

  //verify that the email address is not empty
  if (empty($email)) {
    return MENU_NOT_FOUND;
  }
  $entity_info = entity_extract_ids($object_type, $object);
  $settings = field_info_instance($object_type, $field_name, $entity_info[2]);
  $found = FALSE;
  foreach ($settings['display'] as $display_name => $display_data) {
    if (isset($display_data['type']) && $display_data['type'] === 'email_contact') {
      $found = TRUE;
      break;
    }
  }
  if (!$found) {
    return MENU_NOT_FOUND;
  }
  if (!flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
    return t("You cannot send more than %number messages per hour. Please try again later.", array(
      '%number' => variable_get('email_hourly_threshold', 3),
    ));
  }
  return drupal_get_form('email_mail_page_form', $object_type, $object_id, $field_name, $email);
}