Index: cck_redirection.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck_redirection/cck_redirection.module,v
retrieving revision 1.1
diff -u -r1.1 cck_redirection.module
--- cck_redirection.module	2 Aug 2007 04:37:15 -0000	1.1
+++ cck_redirection.module	16 Nov 2007 21:21:33 -0000
@@ -1,306 +1,307 @@
-<?php
-// $Id: cck_redirection.module,v 1.1 2007/08/02 04:37:15 morrissinger Exp $
-
-/**
- * @file
- *  Provides a CCK field and widget for providing a URL to which to redirect the user.
- */
-
-/*********************************************************************
- * Drupal Hooks
- */
-
-/**
- * Implementation of hook_perm().
- */
-function cck_redirection_perm() {
-  return array('bypass redirection'); 
-}
-
-/**
- * Implementation of hook_nodeapi().
- *
- * This hook is implemented to do redirection when the node is viewed as a page.
- */
-function cck_redirection_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
-  switch ($op) {
-    case 'view':
-      $fields = _cck_redirection_get_redirect_fields($node->type);
-      if(!empty($fields)) {
-        $field = array_shift($fields);
-        $data = $node->$field['field_name'];
-        $url = check_plain($data[0]['value']);
-        if (!empty($url)) {
-          if(user_access('bypass redirection')) {
-            drupal_set_message(t('This node is redirected to a !r', array('!r' => l(t('remote source'), $url))));
-          } else {
-            drupal_goto($url);
-          }
-        }
-      }
-      break;
-  }
-}
-
-/**
- * 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_id, &$form) {
-  if (in_array($form_id, array('_content_admin_field_add_existing', '_content_admin_field_add_new'))) {
-    $form['#validate']['cck_redirection_add_field_validate'] = array();
-  }
-}
-
-/*********************************************************************
- * CCK Hooks: Fields
- */
- 
-/**
- * Implementation of hook_field_info().
- */
-function cck_redirection_field_info() {
-  return array(
-    'cck_redirection' => array('label' => 'Redirection'),
-  );
-}
-
-/**
- * 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' => TRUE,
-          'default' => "''",
-          'sortable' => TRUE,
-        ),
-      );
-      return $columns;
-    case 'filters':
-      return array();
-  }
-}
-
-/**
- * Implementation of hook_field().
- */
-function cck_redirection_field($op, &$node, $field, &$items, $teaser, $page) {
-  switch ($op) {
-    case 'load':
-      return array();
-    case 'view':
-      foreach($items as $delta => $item) {
-        $items[$delta]['view'] = content_format($field, $item, 'default', $node); 
-      }
-      return theme('field', $node, $field, $items, $teaser, $page);
-    case 'validate':
-      break;
-    case 'submit':
-      break;
-    case 'insert':
-      break;
-    case 'update':
-      break;
-    case 'delete':
-      break;
-  } 
-}
-
-/*********************************************************************
- * CCK Hooks: Field Formatters
- */
-
-/**
- * Implementation of hook_field_formatter_info().
- */
-function cck_redirection_field_formatter_info() {
-  return array(
-    'default' => array(
-      'label' => t('Default: 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'),
-    ),
-  ); 
-}
-
-/**
- * Implementation of hook_field_formatter().
- */
-function cck_redirection_field_formatter($field, $item, $formatter, $node) {
-  $text = $item['value'];  
-  switch ($formatter) {
-    case 'link':
-      return l(check_plain($text), $text);
-    case 'plain':
-      return check_plain($text);
-    default:
-      return NULL;
-    }
-}
-
-/*********************************************************************
- * CCK Hooks: Widgets
- */
-
-/**
- * Implementation of hook_widget_info().
- */
-function cck_redirection_widget_info() {
-  return array(
-    'cck_redirection' => array(
-      'label' => 'Text Field with URI validation',
-      'field types' => array('cck_redirection'),
-    ),
-  );
-}
-
-
-/**
- * Implementation of hook_widget_settings().
- */
-function cck_redirection_widget_settings($op, $widget) {
-  switch ($op) {
-    case 'form':
-      return array();
-    case 'validate':
-      break;
-    case 'submit': 
-      break;
-  } 
-}
-
-
-/**
- * Implementation of hook_widget().
- */
-function cck_redirection_widget($op, &$node, $field, &$items) {
-  switch ($op) {
-    case 'prepare form values':
-      break;
-    case 'form':
-      $form = array();
-      $form[$field['field_name']]['#tree'] = TRUE;
-      $form[$field['field_name']][0]['value'] = array(
-        '#type' => 'textfield',
-        '#title' => t($field['widget']['label']),
-        '#default_value' => $items[0]['value'],
-        '#required' => $field['required'],
-        '#description' => t('Enter a valid URI (!i).', array('!i' => 'i.e. http://www.example.com')),
-      );
-      return $form;
-    case 'validate':
-      $value = $items[0]['value'];
-      $okay = _cck_redirection_validate_uri($value);
-      if(!$okay && !empty($value)) {
-        form_set_error($field['field_name'], t('You have specified an invalid URI.'));
-      }
-      break;
-    case 'process form values':
-      break;
-    case 'submit':
-      break;
-  }
-}
-
-/*********************************************************************
- * Form Callbacks
- */
- 
-/**
- * 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_add_field_validate($form_id, $form) {
-  switch ($form_id) {
-    case '_content_admin_field_add_existing':
-      $added_field = $form['field_name'];
-      $added_field = content_fields($added_field);
-      
-      $type = $form['type_name'];
-      $fields = _cck_redirection_get_redirect_fields($type);
-      if(!empty($fields) && $added_field['type'] == 'cck_redirection') {
-        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.')); 
-      }
-      break;
-    case '_content_admin_field_add_new':
-      $widgets = cck_redirection_field_info();
-      $added_widget = substr($form['field_widget_type'], 16);
-      $type = $form['type_name'];
-      $fields = _cck_redirection_get_redirect_fields($type);
-      if(!empty($fields) && array_key_exists($added_widget, $widgets)) {
-        form_set_error('field_widget_type', t('A Redirection field already exists for this node type. You may only redirect the browser to one site at a time.')); 
-      }
-      break;
-  }
-  
-  
-}
-
-/*********************************************************************
- * 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 = $info['content types'][$type]['fields'];
-  
-  $cck_redirection_fields = array();
-  
-  foreach($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;
-}
\ No newline at end of file
+<?php
+// $Id: cck_redirection.module,v 1.1 2007/08/02 04:37:15 morrissinger Exp $
+
+/**
+ * @file
+ *  Provides a CCK field and widget for providing a URL to which to redirect the user.
+ */
+
+/*********************************************************************
+ * Drupal Hooks
+ */
+
+/**
+ * Implementation of hook_perm().
+ */
+function cck_redirection_perm() {
+  return array('bypass redirection'); 
+}
+
+/**
+ * Implementation of hook_menu().
+ *
+ * This hook is implemented to do redirection when the node is viewed as a page.
+ */
+function cck_redirection_menu($may_cache) {
+  if (!$may_cache && arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
+    $node = node_load(arg(1));
+    $fields = _cck_redirection_get_redirect_fields($node->type);
+    if(!empty($fields)) {
+      $field = array_shift($fields);
+      $data = $node->$field['field_name'];
+      $url = check_plain($data[0]['value']);
+      if (!empty($url)) {
+        if(user_access('bypass redirection')) {
+          drupal_set_message(t('This node is redirected to a !r', array('!r' => l(t('remote source'), $url))));
+        } else {
+          drupal_goto($url);
+        }
+      }
+    }
+  }
+}
+
+/**
+ * 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_id, &$form) {
+  if (in_array($form_id, array('_content_admin_field_add_existing', '_content_admin_field_add_new'))) {
+    $form['#validate']['cck_redirection_add_field_validate'] = array();
+  }
+}
+
+/*********************************************************************
+ * CCK Hooks: Fields
+ */
+ 
+/**
+ * Implementation of hook_field_info().
+ */
+function cck_redirection_field_info() {
+  return array(
+    'cck_redirection' => array('label' => 'Redirection'),
+  );
+}
+
+/**
+ * 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' => TRUE,
+          'default' => "''",
+          'sortable' => TRUE,
+        ),
+      );
+      return $columns;
+    case 'filters':
+      return array();
+  }
+}
+
+/**
+ * Implementation of hook_field().
+ */
+function cck_redirection_field($op, &$node, $field, &$items, $teaser, $page) {
+  switch ($op) {
+    case 'load':
+      return array();
+    case 'view':
+      foreach($items as $delta => $item) {
+        $items[$delta]['view'] = content_format($field, $item, 'default', $node); 
+      }
+      return theme('field', $node, $field, $items, $teaser, $page);
+    case 'validate':
+      break;
+    case 'submit':
+      break;
+    case 'insert':
+      break;
+    case 'update':
+      break;
+    case 'delete':
+      break;
+  } 
+}
+
+/*********************************************************************
+ * CCK Hooks: Field Formatters
+ */
+
+/**
+ * Implementation of hook_field_formatter_info().
+ */
+function cck_redirection_field_formatter_info() {
+  return array(
+    'default' => array(
+      'label' => t('Default: 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'),
+    ),
+  ); 
+}
+
+/**
+ * Implementation of hook_field_formatter().
+ */
+function cck_redirection_field_formatter($field, $item, $formatter, $node) {
+  $text = $item['value'];  
+  switch ($formatter) {
+    case 'link':
+      return l(check_plain($text), $text);
+    case 'plain':
+      return check_plain($text);
+    default:
+      return NULL;
+    }
+}
+
+/*********************************************************************
+ * CCK Hooks: Widgets
+ */
+
+/**
+ * Implementation of hook_widget_info().
+ */
+function cck_redirection_widget_info() {
+  return array(
+    'cck_redirection' => array(
+      'label' => 'Text Field with URI validation',
+      'field types' => array('cck_redirection'),
+    ),
+  );
+}
+
+
+/**
+ * Implementation of hook_widget_settings().
+ */
+function cck_redirection_widget_settings($op, $widget) {
+  switch ($op) {
+    case 'form':
+      return array();
+    case 'validate':
+      break;
+    case 'submit': 
+      break;
+  } 
+}
+
+
+/**
+ * Implementation of hook_widget().
+ */
+function cck_redirection_widget($op, &$node, $field, &$items) {
+  switch ($op) {
+    case 'prepare form values':
+      break;
+    case 'form':
+      $form = array();
+      $form[$field['field_name']]['#tree'] = TRUE;
+      $form[$field['field_name']][0]['value'] = array(
+        '#type' => 'textfield',
+        '#title' => t($field['widget']['label']),
+        '#default_value' => $items[0]['value'],
+        '#required' => $field['required'],
+        '#description' => t('Enter a valid URI (!i).', array('!i' => 'i.e. http://www.example.com')),
+      );
+      return $form;
+    case 'validate':
+      $value = $items[0]['value'];
+      $okay = _cck_redirection_validate_uri($value);
+      if(!$okay && !empty($value)) {
+        form_set_error($field['field_name'], t('You have specified an invalid URI.'));
+      }
+      break;
+    case 'process form values':
+      break;
+    case 'submit':
+      break;
+  }
+}
+
+/*********************************************************************
+ * Form Callbacks
+ */
+ 
+/**
+ * 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_add_field_validate($form_id, $form) {
+  switch ($form_id) {
+    case '_content_admin_field_add_existing':
+      $added_field = $form['field_name'];
+      $added_field = content_fields($added_field);
+      
+      $type = $form['type_name'];
+      $fields = _cck_redirection_get_redirect_fields($type);
+      if(!empty($fields) && $added_field['type'] == 'cck_redirection') {
+        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.')); 
+      }
+      break;
+    case '_content_admin_field_add_new':
+      $widgets = cck_redirection_field_info();
+      $added_widget = substr($form['field_widget_type'], 16);
+      $type = $form['type_name'];
+      $fields = _cck_redirection_get_redirect_fields($type);
+      if(!empty($fields) && array_key_exists($added_widget, $widgets)) {
+        form_set_error('field_widget_type', t('A Redirection field already exists for this node type. You may only redirect the browser to one site at a time.')); 
+      }
+      break;
+  }
+  
+  
+}
+
+/*********************************************************************
+ * 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 = $info['content types'][$type]['fields'];
+  
+  $cck_redirection_fields = array();
+  
+  foreach($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;
+}
+
+?>
\ No newline at end of file
