Index: cck_redirection.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck_redirection/cck_redirection.module,v
retrieving revision 1.1.4.2
diff -u -p -r1.1.4.2 cck_redirection.module
--- cck_redirection.module 31 Oct 2008 21:51:55 -0000 1.1.4.2
+++ cck_redirection.module 24 Jun 2009 05:51:30 -0000
@@ -1,607 +1,606 @@
- 'cck_redirection_js',
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK,
- );
-
- // Frameset Redirection
- $items['redirect'] = array(
- 'page callback' => 'cck_redirection_frameset',
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK,
- );
-
- // Header page in frameset redirection
- $items['cck_redirection/header'] = array(
- 'page callback' => 'cck_redirection_frameset_header',
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK,
- );
- return $items;
-}
-
-/**
- * Implementation of hook_theme().
- */
-function cck_redirection_theme() {
+ 'cck_redirection_js',
+ 'access arguments' => array('access content'),
+ 'type' => MENU_CALLBACK,
+ );
+
+ // Frameset Redirection
+ $items['redirect'] = array(
+ 'page callback' => 'cck_redirection_frameset',
+ 'access arguments' => array('access content'),
+ 'type' => MENU_CALLBACK,
+ );
+
+ // Header page in frameset redirection
+ $items['cck_redirection/header'] = array(
+ 'page callback' => 'cck_redirection_frameset_header',
+ 'access arguments' => array('access content'),
+ 'type' => MENU_CALLBACK,
+ );
+ return $items;
+}
+
+/**
+ * Implementation of hook_theme().
+ */
+function cck_redirection_theme() {
return array(
'cck_redirection' => array(
'arguments' => array('element' => NULL),
- ),
- 'cck_redirection_frameset' => array( // Show the new page in the bottom half of a frameset (think Google Images)
- 'template' => 'cck-redirection-frameset',
- 'arguments' => array('uri' => NULL, 'element' => NULL),
- ),
- 'cck_redirection_frameset_header' => array( // Theme the header on a frameset
- 'template' => 'cck-redirection-frameset-header',
- ),
- 'cck_redirection_delay_msg' => array( // Themes a JS Delay message (i.e. "This node will redirect to ___ in five seconds...")
- 'arguments' => array('element' => NULL)
- ),
- 'cck_redirection_formatter_default' => array( // Suppress
- 'arguments' => array('element' => NULL),
- ),
- 'cck_redirection_formatter_link' => array( // Show as link
- 'arguments' => array('element' => NULL),
- ),
- 'cck_redirection_formatter_plain' => array( // Show as plain text
- 'arguments' => array('element' => NULL),
- ),
- );
-}
-
-/**
- * Implementation of hook_perm().
- */
-function cck_redirection_perm() {
- return array('bypass redirection');
-}
-
-/**
- * Implementation of hook_nodeapi().
- */
-function cck_redirection_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
- if ($op == 'view') {
- $GLOBALS['cck_redirection_teaser'] = $a3;
- $GLOBALS['cck_redirection_page'] = $a4;
- }
-}
-
-/**
- * Implementation of hook_form_alter().
- *
- * This hook is used to add custom validation to the addition of new or
- * existing fields to a node type. This is to make sure that a user
- * does not add two or more cck_redirection fields to a given node type.
- * While this does not present any inherent danger, this should be
- * prevented because the cck_redirection field module will only look at
- * one cck_redirection field anyway. After all, we will never have the
- * opportunity to redirect the same user to two different sites
- * at the same time in the same browser window.
- */
-function cck_redirection_form_alter(&$form, $form_state, $form_id) {
- if ($form_id == 'content_admin_field_main') {
- $form['#validate'][] = 'cck_redirection_content_admin_field_main_validate';
- }
-
- // We don't want the user to be able to have this field work with multiple values, so we will
- // hide this option from the admin when he is configuring the field.
- if ($form_id == '_content_admin_field' && $form['main']['type']['#value'] == 'cck_redirection') {
- $form['field']['multiple']['#type'] = 'hidden';
- $form['field']['multiple']['#value'] = $form['field']['multiple']['#default_value'];
- }
-}
-
-/*********************************************************************
- * FAPI Hooks
- */
-
-/**
- * Implementation of FAPI hook_elements().
- */
+ ),
+ 'cck_redirection_frameset' => array( // Show the new page in the bottom half of a frameset (think Google Images)
+ 'template' => 'cck-redirection-frameset',
+ 'arguments' => array('uri' => NULL, 'element' => NULL),
+ ),
+ 'cck_redirection_frameset_header' => array( // Theme the header on a frameset
+ 'template' => 'cck-redirection-frameset-header',
+ ),
+ 'cck_redirection_delay_msg' => array( // Themes a JS Delay message (i.e. "This node will redirect to ___ in five seconds...")
+ 'arguments' => array('element' => NULL)
+ ),
+ 'cck_redirection_formatter_default' => array( // Suppress
+ 'arguments' => array('element' => NULL),
+ ),
+ 'cck_redirection_formatter_link' => array( // Show as link
+ 'arguments' => array('element' => NULL),
+ ),
+ 'cck_redirection_formatter_plain' => array( // Show as plain text
+ 'arguments' => array('element' => NULL),
+ ),
+ );
+}
+
+/**
+ * Implementation of hook_perm().
+ */
+function cck_redirection_perm() {
+ return array('bypass redirection');
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ */
+function cck_redirection_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
+ if ($op == 'view') {
+ $GLOBALS['cck_redirection_teaser'] = $a3;
+ $GLOBALS['cck_redirection_page'] = $a4;
+ }
+}
+
+/**
+ * Implementation of hook_form_alter().
+ *
+ * This hook is used to add custom validation to the addition of new or
+ * existing fields to a node type. This is to make sure that a user
+ * does not add two or more cck_redirection fields to a given node type.
+ * While this does not present any inherent danger, this should be
+ * prevented because the cck_redirection field module will only look at
+ * one cck_redirection field anyway. After all, we will never have the
+ * opportunity to redirect the same user to two different sites
+ * at the same time in the same browser window.
+ */
+function cck_redirection_form_alter(&$form, $form_state, $form_id) {
+ if ($form_id == 'content_admin_field_main') {
+ $form['#validate'][] = 'cck_redirection_content_admin_field_main_validate';
+ }
+
+ // We don't want the user to be able to have this field work with multiple values, so we will
+ // hide this option from the admin when he is configuring the field.
+ if ($form_id == '_content_admin_field' && $form['main']['type']['#value'] == 'cck_redirection') {
+ $form['field']['multiple']['#type'] = 'hidden';
+ $form['field']['multiple']['#value'] = $form['field']['multiple']['#default_value'];
+ }
+}
+
+/*********************************************************************
+ * FAPI Hooks
+ */
+
+/**
+ * Implementation of FAPI hook_elements().
+ */
function cck_redirection_elements() {
return array(
'cck_redirection' => array(
'#input' => TRUE,
- '#columns' => array('value'),
+ '#columns' => array('value'),
'#delta' => 0,
'#process' => array('cck_redirection_process'),
'#autocomplete_path' => FALSE,
),
- );
-
-}
-
-/*********************************************************************
- * CCK Hooks: Fields
- */
-
-/**
- * Implementation of hook_field_info().
- */
-function cck_redirection_field_info() {
- return array(
- 'cck_redirection' => array(
- 'label' => 'Redirection',
- 'description' => t('Store a URI in the database (up to 255 characters).'),
- ),
- );
-}
-
-/**
- * Implementation of hook_field_settings().
- */
-function cck_redirection_field_settings($op, $field) {
- switch ($op) {
- case 'form':
- return array();
- case 'validate':
- break;
- case 'save':
- return array();
- case 'database columns':
- $columns = array(
- 'value' => array(
- 'type' => 'varchar',
- 'length' => 255,
- 'not null' => FALSE,
- 'sortable' => TRUE,
- 'default value' => NULL,
- ),
- );
- return $columns;
- case 'filters':
- return array();
- }
-}
-
-/**
- * Implementation of hook_field().
- */
-function cck_redirection_field($op, &$node, $field, &$items, $teaser, $page) {
- switch ($op) {
- case 'validate':
- // TODO: Do URI validation here.
- break;
- }
-}
-
-/*********************************************************************
- * CCK Hooks: Field Formatters
- */
-
-/**
- * Implementation of hook_field_formatter_info().
- */
-function cck_redirection_field_formatter_info() {
- return array(
- 'default' => array(
- 'label' => t('Suppress'),
- 'field types' => array('cck_redirection'),
- ),
- 'link' => array(
- 'label' => t('Show as Link'),
- 'field types' => array('cck_redirection'),
- ),
- 'plain' => array(
- 'label' => t('Show as Plain Text'),
- 'field types' => array('cck_redirection'),
- ),
- );
-}
-
-/*********************************************************************
- * CCK Hooks: Widgets
- */
-
-/**
- * Implementation of hook_widget_info().
- */
-function cck_redirection_widget_info() {
- return array(
- 'cck_redirection' => array(
- 'label' => 'Text field',
- 'field types' => array('cck_redirection'),
- 'multiple values' => CONTENT_HANDLE_CORE,
- 'callbacks' => array(
- 'default value' => CONTENT_CALLBACK_DEFAULT,
- ),
- ),
- );
-}
-
-/**
- * Implementation of hook_widget_settings().
- */
-function cck_redirection_widget_settings($op, $widget) {
- switch ($op) {
- case 'form':
- $form = array();
-
- $description = NULL;
- $description .= t('Select the manner in which redirection should take place. Options are:') . '
';
- $description .= '- ' . t('Divert: Show the target URI page, instead of the node.') . '
';
- $description .= '- ' . t('Delay: Show the node, and redirect after five seconds.') . ' ' . t('Note: You should display this field in %d, to give the user some warning of redirection.', array('%d' => t('Display fields'))) . '
';
- $description .= '- ' . t('Frameset: Show a frameset with some basic site information at the top, and the target URI page in the main frame.') . '
';
- $description .= '- ' . t('None: Do not redirect. This may be useful for theming or other development purposes.') . '
';
-
-
- $form['redirect_type'] = array(
- '#type' => 'select',
- '#title' => t('Redirection type'),
- '#default_value' => is_numeric($widget['redirect_type']) ? $widget['redirect_type'] : CCK_REDIRECTION_DIVERT,
- '#description' => $description,
- '#options' => _cck_redirection_redirect_types(),
- );
-
- return $form;
- case 'validate':
- break;
- case 'save':
- return array('redirect_type');
- }
-}
-
-
-/**
- * Implementation of hook_widget().
- */
-function cck_redirection_widget(&$form, &$form_state, $field, $items, $delta = 0) {
- $element = array(
- '#type' => 'cck_redirection',
- '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
- );
-
- return $element;
-}
-
-/*********************************************************************
- * CCK Hooks: Misc.
- */
-
-/**
-* Implementation of hook_content_is_empty().
-*/
-function cck_redirection_content_is_empty($item, $field) {
- if (empty($item['value'])) {
- return TRUE;
- }
- return FALSE;
-}
-
-/*********************************************************************
- * CCK Hooks: Installation, etc.
- */
-
-/**
-* Implementation of hook_install().
-*/
-function cck_redirection_install() {
- content_notify('install', 'cck_redirection');
-}
-
-/**
-* Implementation of hook_uninstall().
-*/
-function cck_redirection_uninstall() {
- content_notify('uninstall', 'cck_redirection');
-}
-
-/**
-* Implementation of hook_enable().
-*/
-function cck_redirection_enable() {
- content_notify('enable', 'cck_redirection');
-}
-
-/**
-* Implementation of hook_disable().
-*/
-function cck_redirection_disable() {
- content_notify('disable', 'cck_redirection');
-}
-
-/*********************************************************************
- * Page Callbacks
- */
-
-/**
- * CCK Redirection Javascript
- *
- * @param $uri
- * A valid URI to redirect to,
- */
-function cck_redirection_js() {
- $uri = $_SESSION['cck_redirection']['#item']['value'];
- $field_name = $_SESSION['cck_redirection']['#field_name'];
- $element = $_SESSION['cck_redirection'];
-
- $id = str_replace("_", "-", "field-$field_name");
-
- unset($_SESSION['cck_redirection']);
-
- echo '
-$(document).ready(function(){
- $(".' . $id . ' .field-items .field-item").text("' . theme('cck_redirection_delay_msg', $element) . '");
- setTimeout("cck_redirection_redirect()", 5000);
-});
-
-function cck_redirection_redirect() {
- window.location=\'' . $uri . '\';
-}
- ';
-}
-
-/**
- * CCK Redirection with Frameset
- */
-function cck_redirection_frameset() {
- $uri = urldecode(check_plain($_GET['uri']));
- $element = $_SESSION['cck_redirection'];
- unset($_SESSION['cck_redirection']);
-
- print theme('cck_redirection_frameset', $uri, $element);
- exit();
-}
-
-/**
- * Header for CCK Redirection with Frameset
- */
-function cck_redirection_frameset_header() {
- print theme('cck_redirection_frameset_header');
- exit();
-}
-
-/*********************************************************************
- * Form Callbacks
- */
-
-/**
- * Process an individual element.
- *
- * Build the form element. When creating a form using FAPI #process,
- * note that $element['#value'] is already set.
- *
- * The $fields array is in $form['#field_info'][$element['#field_name']].
- */
+ );
+
+}
+
+/*********************************************************************
+ * CCK Hooks: Fields
+ */
+
+/**
+ * Implementation of hook_field_info().
+ */
+function cck_redirection_field_info() {
+ return array(
+ 'cck_redirection' => array(
+ 'label' => 'Redirection',
+ 'description' => t('Store a URI in the database (up to 255 characters).'),
+ ),
+ );
+}
+
+/**
+ * Implementation of hook_field_settings().
+ */
+function cck_redirection_field_settings($op, $field) {
+ switch ($op) {
+ case 'form':
+ return array();
+ case 'validate':
+ break;
+ case 'save':
+ return array();
+ case 'database columns':
+ $columns = array(
+ 'value' => array(
+ 'type' => 'varchar',
+ 'length' => 255,
+ 'not null' => FALSE,
+ 'sortable' => TRUE,
+ 'default value' => NULL,
+ ),
+ );
+ return $columns;
+ case 'filters':
+ return array();
+ }
+}
+
+/**
+ * Implementation of hook_field().
+ */
+function cck_redirection_field($op, &$node, $field, &$items, $teaser, $page) {
+ switch ($op) {
+ case 'validate':
+ // TODO: Do URI validation here.
+ break;
+ }
+}
+
+/*********************************************************************
+ * CCK Hooks: Field Formatters
+ */
+
+/**
+ * Implementation of hook_field_formatter_info().
+ */
+function cck_redirection_field_formatter_info() {
+ return array(
+ 'default' => array(
+ 'label' => t('Suppress'),
+ 'field types' => array('cck_redirection'),
+ ),
+ 'link' => array(
+ 'label' => t('Show as Link'),
+ 'field types' => array('cck_redirection'),
+ ),
+ 'plain' => array(
+ 'label' => t('Show as Plain Text'),
+ 'field types' => array('cck_redirection'),
+ ),
+ );
+}
+
+/*********************************************************************
+ * CCK Hooks: Widgets
+ */
+
+/**
+ * Implementation of hook_widget_info().
+ */
+function cck_redirection_widget_info() {
+ return array(
+ 'cck_redirection' => array(
+ 'label' => 'Text field',
+ 'field types' => array('cck_redirection'),
+ 'multiple values' => CONTENT_HANDLE_CORE,
+ 'callbacks' => array(
+ 'default value' => CONTENT_CALLBACK_DEFAULT,
+ ),
+ ),
+ );
+}
+
+/**
+ * Implementation of hook_widget_settings().
+ */
+function cck_redirection_widget_settings($op, $widget) {
+ switch ($op) {
+ case 'form':
+ $form = array();
+
+ $description = NULL;
+ $description .= t('Select the manner in which redirection should take place. Options are:') . '';
+ $description .= '- ' . t('Divert: Show the target URI page, instead of the node.') . '
';
+ $description .= '- ' . t('Delay: Show the node, and redirect after five seconds.') . ' ' . t('Note: You should display this field in %d, to give the user some warning of redirection.', array('%d' => t('Display fields'))) . '
';
+ $description .= '- ' . t('Frameset: Show a frameset with some basic site information at the top, and the target URI page in the main frame.') . '
';
+ $description .= '- ' . t('None: Do not redirect. This may be useful for theming or other development purposes.') . '
';
+
+
+ $form['redirect_type'] = array(
+ '#type' => 'select',
+ '#title' => t('Redirection type'),
+ '#default_value' => is_numeric($widget['redirect_type']) ? $widget['redirect_type'] : CCK_REDIRECTION_DIVERT,
+ '#description' => $description,
+ '#options' => _cck_redirection_redirect_types(),
+ );
+
+ return $form;
+ case 'validate':
+ break;
+ case 'save':
+ return array('redirect_type');
+ }
+}
+
+
+/**
+ * Implementation of hook_widget().
+ */
+function cck_redirection_widget(&$form, &$form_state, $field, $items, $delta = 0) {
+ $element = array(
+ '#type' => 'cck_redirection',
+ '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
+ );
+
+ return $element;
+}
+
+/*********************************************************************
+ * CCK Hooks: Misc.
+ */
+
+/**
+* Implementation of hook_content_is_empty().
+*/
+function cck_redirection_content_is_empty($item, $field) {
+ if (empty($item['value'])) {
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/*********************************************************************
+ * CCK Hooks: Installation, etc.
+ */
+
+/**
+* Implementation of hook_install().
+*/
+function cck_redirection_install() {
+ content_notify('install', 'cck_redirection');
+}
+
+/**
+* Implementation of hook_uninstall().
+*/
+function cck_redirection_uninstall() {
+ content_notify('uninstall', 'cck_redirection');
+}
+
+/**
+* Implementation of hook_enable().
+*/
+function cck_redirection_enable() {
+ content_notify('enable', 'cck_redirection');
+}
+
+/**
+* Implementation of hook_disable().
+*/
+function cck_redirection_disable() {
+ content_notify('disable', 'cck_redirection');
+}
+
+/*********************************************************************
+ * Page Callbacks
+ */
+
+/**
+ * CCK Redirection Javascript
+ *
+ * @param $uri
+ * A valid URI to redirect to,
+ */
+function cck_redirection_js() {
+ $uri = $_SESSION['cck_redirection']['#item']['value'];
+ $field_name = $_SESSION['cck_redirection']['#field_name'];
+ $element = $_SESSION['cck_redirection'];
+
+ $id = str_replace("_", "-", "field-$field_name");
+
+ unset($_SESSION['cck_redirection']);
+
+ echo '
+$(document).ready(function(){
+ $(".' . $id . ' .field-items .field-item").text("' . theme('cck_redirection_delay_msg', $element) . '");
+ setTimeout("cck_redirection_redirect()", 5000);
+});
+
+function cck_redirection_redirect() {
+ window.location=\'' . $uri . '\';
+}
+ ';
+}
+
+/**
+ * CCK Redirection with Frameset
+ */
+function cck_redirection_frameset() {
+ $uri = urldecode(check_plain($_GET['uri']));
+ $element = $_SESSION['cck_redirection'];
+ unset($_SESSION['cck_redirection']);
+
+ print theme('cck_redirection_frameset', $uri, $element);
+ exit();
+}
+
+/**
+ * Header for CCK Redirection with Frameset
+ */
+function cck_redirection_frameset_header() {
+ print theme('cck_redirection_frameset_header');
+ exit();
+}
+
+/*********************************************************************
+ * Form Callbacks
+ */
+
+/**
+ * Process an individual element.
+ *
+ * Build the form element. When creating a form using FAPI #process,
+ * note that $element['#value'] is already set.
+ *
+ * The $fields array is in $form['#field_info'][$element['#field_name']].
+ */
function cck_redirection_process($element, $edit, $form_state, $form) {
$field = $form['#field_info'][$element['#field_name']];
- $field_key = $element['#columns'][0];
+ $field_key = $element['#columns'][0];
$delta = $element['#delta'];
- $element[$field_key] = array(
+ $element[$field_key] = array(
'#type' => 'textfield',
- '#title' => $element['#title'],
- '#description' => $element['#description'],
+ '#title' => $element['#title'],
+ '#description' => $element['#description'],
'#required' => $element['#required'],
- '#default_value' => isset($element['#value']) ? $element['#value'] : NULL,
- '#autocomplete_path' => $element['#autocomplete_path'],
- );
+ '#default_value' => isset($element['#value']) ? $element['#value'] : NULL,
+ '#autocomplete_path' => $element['#autocomplete_path'],
+ );
- if (!empty($field['max_length'])) {
+ if (!empty($field['max_length'])) {
$element[$field_key]['#maxlength'] = $field['max_length'];
}
- if (!empty($field['text_processing'])) {
- $filter_key = $element['#columns'][1];
- $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
- $parents = array_merge($element['#parents'] , array($filter_key));
- $element[$filter_key] = filter_form($format, 1, $parents);
- }
-
- return $element;
-}
-
-/**
- * Validates the addition of a new or existing field to a content type.
- *
- * Ensures that only one cck_redirection field exists per node type.
- */
-function cck_redirection_content_admin_field_main_validate($form, $form_state) {
- $values = $form_state['values'];
- $fields = _cck_redirection_get_redirect_fields($values['type_name']); // $values['type_name'] is the node type.
-
- if(!empty($fields) && $values['type'] == 'cck_redirection') { //$values['type'] is the field type.
- form_set_error('field_name', t('A Redirection field already exists for this node type. You may only redirect the browser to one site at a time.'));
- }
-}
-
-/*********************************************************************
- * Theme Functions
- */
-
-/**
- * Theme a cck redirection widget.
- */
-function theme_cck_redirection($element) {
- return $element['#children'];
-}
-
-/**
- * Theme a message to display to the user when using the "delay" redirect method.
- */
-function theme_cck_redirection_delay_msg($element) {
- return t('This page will redirect to !url in five seconds...', array('!url' => $element['#item']['value']));
-}
-
-/**
- * Theme a frameset.
- */
-function template_preprocess_cck_redirection_frameset(&$vars) {
- global $base_url;
- $element = $vars['element'];
- $vars['node'] = $element['#node'];
- $vars['header_uri'] = $base_url . '/cck_redirection/header';
-}
-
-/**
- * Theme a frameset header.
- */
-function template_preprocess_cck_redirection_frameset_header(&$vars) {
- template_preprocess_page($vars);
-}
-
-/**
- * Theme function for 'default' CCK_Redirection field formatter.
- */
-function theme_cck_redirection_formatter_default($element) {
- $method = _cck_redirection_redirect($element);
-
- return NULL;
-}
-
-/**
- * Theme function for 'link' CCK_Redirection field formatter.
- */
-function theme_cck_redirection_formatter_link($element) {
- $method = _cck_redirection_redirect($element);
-
- $text = $element['#item']['value'];
- return l(check_plain($text), $text);
-}
-
-/**
- * Theme function for 'plain' CCK_Redirection field formatter.
- */
-function theme_cck_redirection_formatter_plain($element) {
- $method = _cck_redirection_redirect($element);
-
- $text = $element['#item']['value'];
- return check_plain($text);
-}
-
-/*********************************************************************
- * Helper Functions
- */
-
-/**
- * Returns all redirect fields for a given node type.
- *
- * @param $type
- * A node type.
- * @ return
- * An array of fields, as returned by _content_type_info, keyed
- * by field name.
- */
-function _cck_redirection_get_redirect_fields($type) {
- $info = _content_type_info();
- $fields = array();
- $fields = $info['content types'][$type]['fields'];
-
- $cck_redirection_fields = array();
-
- foreach((array)$fields as $key => $field) {
- if ($field['type'] == 'cck_redirection') {
- $cck_redirection_fields[$key] = $field;
- }
- }
-
- return $cck_redirection_fields;
-}
-
-/**
- * Returns whether or not $value is a valid URI.
- *
- * @param $value
- * A value to check.
- * @return
- * TRUE if a valid URI. FALSE if not.
- */
-function _cck_redirection_validate_uri($value) {
- $pattern = "/https?:\/\/[-\w.]+(:\d+)?(\/([\w\/_.]*)?)?/";
- $works = preg_match($pattern, $value);
- return $works;
-}
-
-/**
- * Return an array of CCK Redirection types.
- */
-function _cck_redirection_redirect_types() {
- return array(
- CCK_REDIRECTION_DIVERT => t('Divert'),
- CCK_REDIRECTION_DELAY => t('Delay'),
- CCK_REDIRECTION_FRAMESET => t('Frameset'),
- CCK_REDIRECTION_NONE => t('None'),
- );
-}
-
-/**
- * Accomplish redirection using the widget settings.
- */
-function _cck_redirection_redirect(&$element) {
- $teaser = $GLOBALS['cck_redirection_teaser'];
- $page = $GLOBALS['cck_redirection_page'];
-
- $method = NULL;
-
- if ($page) {
- $node = $element['#node'];
- $field = content_fields($element['#field_name'], $node->type);
- $widget = $field['widget'];
-
- $method = is_numeric($widget['redirect_type']) ? $widget['redirect_type'] : NULL;
-
- if (user_access('bypass redirection')) {
- if (!empty($element['#item']['value'])) {
- drupal_set_message(t('This node is redirected to a !r.', array('!r' => l(t('remote URI'), $element['#item']['value']))));
+ if (!empty($field['text_processing'])) {
+ $filter_key = $element['#columns'][1];
+ $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
+ $parents = array_merge($element['#parents'] , array($filter_key));
+ $element[$filter_key] = filter_form($format, 1, $parents);
+ }
+
+ return $element;
+}
+
+/**
+ * Validates the addition of a new or existing field to a content type.
+ *
+ * Ensures that only one cck_redirection field exists per node type.
+ */
+function cck_redirection_content_admin_field_main_validate($form, $form_state) {
+ $values = $form_state['values'];
+ $fields = _cck_redirection_get_redirect_fields($values['type_name']); // $values['type_name'] is the node type.
+
+ if(!empty($fields) && $values['type'] == 'cck_redirection') { //$values['type'] is the field type.
+ form_set_error('field_name', t('A Redirection field already exists for this node type. You may only redirect the browser to one site at a time.'));
+ }
+}
+
+/*********************************************************************
+ * Theme Functions
+ */
+
+/**
+ * Theme a cck redirection widget.
+ */
+function theme_cck_redirection($element) {
+ return $element['#children'];
+}
+
+/**
+ * Theme a message to display to the user when using the "delay" redirect method.
+ */
+function theme_cck_redirection_delay_msg($element) {
+ return t('This page will redirect to !url in five seconds...', array('!url' => $element['#item']['value']));
+}
+
+/**
+ * Theme a frameset.
+ */
+function template_preprocess_cck_redirection_frameset(&$vars) {
+ global $base_url;
+ $element = $vars['element'];
+ $vars['node'] = $element['#node'];
+ $vars['header_uri'] = $base_url . '/cck_redirection/header';
+}
+
+/**
+ * Theme a frameset header.
+ */
+function template_preprocess_cck_redirection_frameset_header(&$vars) {
+ template_preprocess_page($vars);
+}
+
+/**
+ * Theme function for 'default' CCK_Redirection field formatter.
+ */
+function theme_cck_redirection_formatter_default($element) {
+ $method = _cck_redirection_redirect($element);
+
+ return NULL;
+}
+
+/**
+ * Theme function for 'link' CCK_Redirection field formatter.
+ */
+function theme_cck_redirection_formatter_link($element) {
+ $method = _cck_redirection_redirect($element);
+
+ $text = $element['#item']['value'];
+ return l(check_plain($text), $text);
+}
+
+/**
+ * Theme function for 'plain' CCK_Redirection field formatter.
+ */
+function theme_cck_redirection_formatter_plain($element) {
+ $method = _cck_redirection_redirect($element);
+
+ $text = $element['#item']['value'];
+ return check_plain($text);
+}
+
+/*********************************************************************
+ * Helper Functions
+ */
+
+/**
+ * Returns all redirect fields for a given node type.
+ *
+ * @param $type
+ * A node type.
+ * @ return
+ * An array of fields, as returned by _content_type_info, keyed
+ * by field name.
+ */
+function _cck_redirection_get_redirect_fields($type) {
+ $info = _content_type_info();
+ $fields = array();
+ $fields = $info['content types'][$type]['fields'];
+
+ $cck_redirection_fields = array();
+
+ foreach((array)$fields as $key => $field) {
+ if ($field['type'] == 'cck_redirection') {
+ $cck_redirection_fields[$key] = $field;
+ }
+ }
+
+ return $cck_redirection_fields;
+}
+
+/**
+ * Returns whether or not $value is a valid URI.
+ *
+ * @param $value
+ * A value to check.
+ * @return
+ * TRUE if a valid URI. FALSE if not.
+ */
+function _cck_redirection_validate_uri($value) {
+ $pattern = "/https?:\/\/[-\w.]+(:\d+)?(\/([\w\/_.]*)?)?/";
+ $works = preg_match($pattern, $value);
+ return $works;
+}
+
+/**
+ * Return an array of CCK Redirection types.
+ */
+function _cck_redirection_redirect_types() {
+ return array(
+ CCK_REDIRECTION_DIVERT => t('Divert'),
+ CCK_REDIRECTION_DELAY => t('Delay'),
+ CCK_REDIRECTION_FRAMESET => t('Frameset'),
+ CCK_REDIRECTION_NONE => t('None'),
+ );
+}
+
+/**
+ * Accomplish redirection using the widget settings.
+ */
+function _cck_redirection_redirect(&$element) {
+ $teaser = $GLOBALS['cck_redirection_teaser'];
+ $page = $GLOBALS['cck_redirection_page'];
+
+ $method = NULL;
+
+ // Only proceed if we're viewing a full node page and the URL is not empty.
+ if ($page && $element['#item']['value']) {
+ $node = $element['#node'];
+ $field = content_fields($element['#field_name'], $node->type);
+ $widget = $field['widget'];
+
+ $method = is_numeric($widget['redirect_type']) ? $widget['redirect_type'] : NULL;
+
+ if (user_access('bypass redirection')) {
+ drupal_set_message(t('This node is redirected to a !r.', array('!r' => l(t('remote URI'), $element['#item']['value']))));
+ } else {
+ switch ($widget['redirect_type']) {
+ case CCK_REDIRECTION_DIVERT:
+ _cck_redirection_divert($element);
+ break;
+ case CCK_REDIRECTION_DELAY:
+ _cck_redirection_delay($element);
+ break;
+ case CCK_REDIRECTION_FRAMESET:
+ _cck_redirection_frameset($element);
+ break;
+ default:
+ // This takes care of CCK_REDIRECTION_NONE by doing nothing
+ break;
}
- } else {
- switch ($widget['redirect_type']) {
- case CCK_REDIRECTION_DIVERT:
- _cck_redirection_divert($element);
- break;
- case CCK_REDIRECTION_DELAY:
- _cck_redirection_delay($element);
- break;
- case CCK_REDIRECTION_FRAMESET:
- _cck_redirection_frameset($element);
- break;
- default:
- // This takes care of CCK_REDIRECTION_NONE by doing nothing
- break;
- }
- }
- }
-
- return $method;
-}
-
-/**
- * Diverts the browser to the target URI page.
- */
-function _cck_redirection_divert($element) {
+ }
+ }
+
+ return $method;
+}
+
+/**
+ * Diverts the browser to the target URI page.
+ */
+function _cck_redirection_divert($element) {
if (!empty($element['#item']['value'])) {
drupal_goto($element['#item']['value']);
}
-}
-
-/**
- * Uses JS to delay redirection by five seconds, and then diverts the browser to the target URI page.
- */
+}
+
+/**
+ * Uses JS to delay redirection by five seconds, and then diverts the browser to the target URI page.
+ */
function _cck_redirection_delay($element) {
if (!empty($element['#item']['value'])) {
$_SESSION['cck_redirection'] = $element;
@@ -609,10 +608,10 @@ function _cck_redirection_delay($element
}
}
-/**
- * Displays the target URI inside of a frameset.
- */
-function _cck_redirection_frameset($element) {
+/**
+ * Displays the target URI inside of a frameset.
+ */
+function _cck_redirection_frameset($element) {
if (!empty($element['#item']['value'])) {
$_SESSION['cck_redirection'] = $element;
drupal_goto('redirect', 'uri=' . drupal_urlencode($element['#item']['value']));