Commit 86aaf782 authored by Matthias Hutterer's avatar Matthias Hutterer
Browse files

first working copy of the 7.x version

parent 67b2306c
......@@ -4,13 +4,7 @@
DESCRIPTION:
------------
This module provides an email field type for CCK.
REQUIREMENTS:
-------------
The email.module requires the content.module to be installed.
optional modules: invisimail, spamspan, token, views
This module provides an email field type.
INSTALLATION:
......
; $Id$
name = Email
description = Defines an email field type for cck
dependencies[] = content
package = CCK
core = 6.x
description = Defines an email field type.
core = 7.x
files[] = email.module
files[] = email.install
<?php
/**
* Implementation of hook_install().
*/
function email_install() {
drupal_load('module', 'content');
content_notify('install', 'email');
}
/**
* Implementation of hook_uninstall().
*/
function email_uninstall() {
drupal_load('module', 'content');
content_notify('uninstall', 'email');
}
/**
* Implementation of hook_enable().
*
* Notify content module when this module is enabled.
*/
function email_enable() {
drupal_load('module', 'content');
content_notify('enable', 'email');
}
/**
* Implementation of hook_disable().
*
* Notify content module when this module is disabled.
*/
function email_disable() {
drupal_load('module', 'content');
content_notify('disable', 'email');
}
/**
* Implemenation of hook_update_N();
*
* Update from 5.x to 6.x
*
* Set all columns to accept NULL values and set empty string values in the
* database to NULL.
*/
function email_update_6001() {
if ($abort = content_check_update('email')) {
return $abort;
}
drupal_load('module', 'content');
$ret = array();
$ret[] = update_sql("UPDATE {". content_instance_tablename() ."} SET widget_type = 'email_textfield' WHERE widget_type = 'email'");
content_associate_fields('email');
content_clear_type_cache(TRUE);
include_once(drupal_get_path('module', 'content') .'/content.install');
$types = content_types_install();
foreach ($types as $type_name => $fields) {
foreach ($fields as $field) {
switch ($field['type']) {
case 'email':
$db_info = content_database_info($field);
$table = $db_info['table'];
foreach ($db_info['columns'] as $column => $attributes) {
$attributes['not null'] = FALSE;
$column = $attributes['column'];
db_change_field($ret, $table, $column, $column, $attributes);
db_field_set_no_default($ret, $table, $column);
$ret[] = update_sql("UPDATE {". $table ."} SET ". $column ." = NULL WHERE ". $column ." = ''");
}
break;
}
}
}
return $ret;
}
......@@ -2,54 +2,52 @@
// $Id$
/**
* Implementation of hook_theme().
* Implements hook_field_info().
*/
function email_theme() {
function email_field_info() {
return array(
'email_textfield' => array(
'arguments' => array('element' => NULL),
),
'email_formatter_default' => array(
'arguments' => array('element' => NULL),
),
'email_formatter_spamspan' => array(
'arguments' => array('element' => NULL),
),
'email_formatter_contact' => array(
'arguments' => array('element' => NULL),
'email' => array(
'label' => 'Email',
'description' => t('This field stores and renderes email addresses.'),
'default_widget' => 'text_textfield',
'default_formatter' => 'default',
),
);
}
/**
* Implementation of hook_field_info().
* Implements hook_field_schema().
*/
function email_field_info() {
function email_field_schema($field) {
return array(
'email' => array(
'label' => 'Email',
'callbacks' => array(
'tables' => CONTENT_CALLBACK_DEFAULT,
'arguments' => CONTENT_CALLBACK_DEFAULT,
'columns' => array(
'email' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
),
);
}
/**
* Implementation of hook_field_settings().
* Implements hook_field_validate().
*
* Possible error codes:
* - 'email_invalid': The email address is not valid
*/
function email_field_settings($op, $field) {
switch ($op) {
case 'database columns':
$columns['email'] = array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'sortable' => TRUE);
return $columns;
function email_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if ($item['email'] != '' && !valid_email_address(trim($item['email']))) {
$message = t('"%mail" is not a valid email address', array('%mail' => $item['email']));
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => "email_invalid",
'message' => $message,
);
}
}
}
/**
* Implementation of hook_field().
*/
......@@ -74,9 +72,9 @@ function email_field($op, &$node, $field, &$items, $teaser, $page) {
}
/**
* Implementation of hook_content_is_empty().
* Implements of hook_content_is_empty().
*/
function email_content_is_empty($item, $field) {
function email_field_is_empty($item, $field) {
if (empty($item['email'])) {
return TRUE;
}
......@@ -84,7 +82,7 @@ function email_content_is_empty($item, $field) {
}
/**
* Implementation of hook_field_formatter_info().
* Implements hook_field_formatter_info().
*
*/
function email_field_formatter_info() {
......@@ -92,12 +90,10 @@ function email_field_formatter_info() {
'default' => array(
'label' => t('Default email link'),
'field types' => array('email'),
'multiple values' => CONTENT_HANDLE_CORE,
),
'contact' => array(
'label' => t('Email contact form'),
'field types' => array('email'),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
if (module_exists('spamspan')) {
......@@ -105,7 +101,6 @@ function email_field_formatter_info() {
'spamspan' => array(
'label' => t('Email SpamSpan'),
'field types' => array('email'),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
......@@ -113,92 +108,87 @@ function email_field_formatter_info() {
}
/**
* Theme function for 'default' email field formatter.
*/
function theme_email_formatter_default($element) {
return !empty($element['#item']['safe']) ? '<a href="mailto:'. $element['#item']['email'] .'">'. $element['#item']['safe'] .'</a>' : '';
}
/**
* Theme function for 'spamspan' email field formatter.
* Implements hook_field_formatter_view().
*/
function theme_email_formatter_spamspan($element) {
if (empty($element['#item']['safe'])) {
return '';
}
if (module_exists('spamspan')) {
return spamspan($element['#item']['email']);
}
else {
return '<a href="mailto:'. $element['#item']['email'] .'">'. $element['#item']['safe'] .'</a>';
function email_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'default':
foreach ($items as $delta => $item) {
$output = l($item['email'], 'mailto:'. $item['email']);
$element[$delta] = array('#markup' => $output);
}
break;
case 'contact':
foreach ($items as $delta => $item) {
//TODO at the moment only nodes as object type
$element[$delta] = l(t('Email contact form'), 'email/'. $object->nid .'/'. $instance['field_name']);
}
break;
case 'spamspan':
foreach ($items as $delta => $item) {
if (module_exists('spamspan')) {
$element[$delta] = spamspan($item['email']);
}
else {
$output = l($item['email'], 'mailto:'. $item['email']);
$element[$delta] = array('#markup' => $output);
}
}
break;
}
}
/**
* Theme function for 'contact' email field formatter.
*/
function theme_email_formatter_contact($element) {
return !empty($element['#item']['safe']) ? l(t('Email contact form'), 'email/'. $element['#node']->nid .'/'. $element['#field_name']) : '';
return $element;
}
/**
* Implementation of hook_widget_info().
* Implements hook_field_widget_info().
*/
function email_widget_info() {
function email_field_widget_info() {
return array(
'email_textfield' => array(
'label' => t('Text field'),
'field types' => array('email'),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
'settings' => array('size' => 60),
),
);
}
/**
* Implementation of FAPI hook_elements().
* Implements hook_field_widget_settings_form().
*/
function email_elements() {
return array(
'email_textfield' => array(
'#input' => TRUE,
'#columns' => array('email'),
'#delta' => 0,
'#process' => array('email_textfield_process'),
),
function email_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $settings['size'],
'#required' => TRUE,
'#element_validate' => array('_element_validate_integer_positive'),
);
return $form;
}
/**
* Implementation of hook_widget_settings().
* Implements hook_field_widget_form().
*/
function email_widget_settings($op, $widget) {
switch ($op) {
case 'form':
$size = (isset($widget['size']) && is_numeric($widget['size'])) ? $widget['size'] : 60;
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $size,
'#element_validate' => array('_email_widget_settings_size_validate'),
'#required' => TRUE,
);
return $form;
case 'save':
return array('size');
}
function email_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
$element = $base;
$element['email'] = $base + array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['email']) ? $items[$delta]['email'] : NULL,
'#size' => $instance['widget']['settings']['size'],
'#prefix' => '<div class="text-full-wrapper">',
'#suffix' => '</div>',
);
return $element;
}
function _email_widget_settings_size_validate($element, &$form_state) {
$value = $form_state['values']['size'];
if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
form_error($element, t('"Size" must be a positive integer.'));
}
}
/**
* Implementation of hook_widget().
......@@ -231,13 +221,6 @@ function email_textfield_process($element, $edit, $form_state, $form) {
return $element;
}
/**
* FAPI theme for an individual text elements.
*/
function theme_email_textfield($element) {
return $element['#children'];
}
/**
* Implementation of hook_menu().
*/
......@@ -250,8 +233,8 @@ function email_menu() {
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['admin/settings/email'] = array(
'title' => 'CCK Email Contact Form Settings',
$items['admin/config/content/email'] = array(
'title' => 'Email Contact Form Settings',
'description' => 'Administer flood control settings for email contact forms',
'page callback' => 'drupal_get_form',
'page arguments' => array('email_admin_settings'),
......@@ -280,9 +263,11 @@ function email_mail_page($nid, $field_name) {
drupal_not_found();
return;
}
$field = $node->$field_name;
$field = array_pop($node->$field_name);
$email = $field[0]['email'];
$types = content_types($node->type);
//TODO
/*$types = content_types($node->type);
// Validate field name
$types = content_types($node->type);
if (empty($email) ||
......@@ -292,7 +277,7 @@ function email_mail_page($nid, $field_name) {
$types['fields'][$field_name]['display_settings']['full']['format'] != 'contact')) {
drupal_not_found();
return;
}
}*/
if (!flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
......@@ -308,12 +293,16 @@ function email_mail_page($nid, $field_name) {
/**
* Contact form
*/
function email_mail_page_form($form_state, $node, $field_name, $email) {
function email_mail_page_form($form_state, $args) {
global $user;
$form['node'] = array(
$node = $args['build_info']['args'][0];
$field_name = $args['build_info']['args'][1];
$email = $args['build_info']['args'][2];
$form['nid'] = array(
'#type' => 'value',
'#value' => $node,
'#value' => $node->nid,
);
$form['field_name'] = array(
'#type' => 'value',
......@@ -374,7 +363,7 @@ function email_mail_page_form_validate($form, &$form_state) {
* Process the site-wide contact page form submission.
*/
function email_mail_page_form_submit($form, &$form_state) {
$node = $form_state['values']['node'];
$node = node_load($form_state['values']['nid']);
$field_name = $form_state['values']['field_name'];
$email = $form_state['values']['email'];
......@@ -412,7 +401,7 @@ function email_mail($key, &$message, $params) {
case 'contact':
$node = $params['node'];
// Compose the body:
$msg[] = t('@name sent a message using the contact form at @node.', array('@name' => $params['name'], '@node' => $params['url']), $language->language);
$msg[] = t('@name sent a message using the contact form at @node.', array('@name' => $params['name'], '@node' => $params['url']), array('langcode' =>$language->language));
$msg[] = $params['message'];
// Tidy up the body:
......@@ -423,15 +412,17 @@ function email_mail($key, &$message, $params) {
// Prepare the body:
$message['body'] = implode("\n\n", $msg);
$message['subject'] = t('[@title] @subject', array('@title' => preg_replace("/\r|\n/",'', $node->title), '@subject' => $params['subject']), $language->language);
$message['subject'] = t('[@title] @subject', array('@title' => preg_replace("/\r|\n/",'', $node->title), '@subject' => $params['subject']), array('langcode' =>$language->language));
break;
}
}
//TODO Token support
/**
* Implementation of hook token_list
*/
* Implements of hook_token_list()
*
function email_token_list($type = 'all') {
if ($type == 'field' || $type == 'all') {
$tokens['email']['raw'] = t('Raw email address');
......@@ -441,8 +432,8 @@ function email_token_list($type = 'all') {
}
/**
* Implementation of hook token_values
*/
* Implements of hook token_values().
*
function email_token_values($type, $object = NULL, $options = array()) {
if ($type == 'field') {
$item = $object[0];
......@@ -451,7 +442,7 @@ function email_token_values($type, $object = NULL, $options = array()) {
return $tokens;
}
}
*/
/**
* Settings for contact form
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment