? x
Index: audiofield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mediafield/audiofield.module,v
retrieving revision 1.7
diff -u -r1.7 audiofield.module
--- audiofield.module	21 Feb 2007 18:04:57 -0000	1.7
+++ audiofield.module	14 Jan 2008 12:28:19 -0000
@@ -129,6 +129,55 @@
   }
 }
 
+function _audiofield_widget_prepare_form_values(&$node, $field, &$node_field) {
+  $fieldname = $field['field_name'];
+  $type = $field['type'];
+  if (!count($_POST)) {
+    _field_clear_session($field['type']);
+  }
+
+  if ($file = file_check_upload($fieldname.'_upload')) {
+    $file = (array)$file;
+
+    $ext = array_pop(explode('.', $file['filename']));
+    $allowed_extensions = array_unique(explode(' ', trim($field['widget']['file_extensions'])));
+    if (!in_array($ext, $allowed_extensions)) {
+      form_set_error($field['field_name'] .'_upload', t('Files with the extension %ext are not allowed. Please upload a file with an extension from the following list: %allowed_extensions.', array('%ext' => $ext, '%allowed_extensions' => $field['widget']['file_extensions'])));
+      return FALSE;
+    }
+
+    $file['upload_path'] = trim($field['widget']['upload_path']);
+
+    $file['fid'] = 'upload';
+    if (!$field['multiple']) {
+      if (is_array($node_field)) {
+	foreach ($node_field as $delta => $session_file) {
+	  $node_field[$delta]['flags']['delete'] = TRUE;
+	}
+      }
+      _field_clear_field_session($fieldname, $field['type']);
+    }
+
+    // Add the file to the session
+    $file['sessionid'] = count($node_field) + count($_SESSION[$type][$fieldname]);
+    $_SESSION[$type][$fieldname][$file['sessionid']] = $file;
+  }
+
+  if (is_array($_SESSION[$type][$fieldname]) && count($_SESSION[$type][$fieldname])) {
+    foreach ($_SESSION[$type][$fieldname] as $delta => $file) {
+      $node_field[] = $file;
+    }
+  }
+}
+
+function _audiofield_widget_validate(&$node, $field, &$node_field) {
+  if ($field['required']) {
+    if (!count($node_field)) {
+      form_set_error($fieldname, t('Field %name is required', array('%name' => $field['widget']['label'])));
+    }
+  }
+}
+
 /**
  * Implementation of hook_widget().
  */
@@ -138,42 +187,7 @@
   require_once(drupal_get_path('module', 'audiofield') .'/multimediafile.inc');
   switch ($op) {
     case 'prepare form values':
-      if (!count($_POST)) {
-        _field_clear_session($field['type']);
-      }
-
-      if ($file = file_check_upload($fieldname.'_upload')) {
-        $file = (array)$file;
-
-        $ext = array_pop(explode('.', $file['filename']));
-        $allowed_extensions = array_unique(explode(' ', trim($field['widget']['file_extensions'])));
-        if (!in_array($ext, $allowed_extensions)) {
-          form_set_error($field['field_name'] .'_upload', t('Files with the extension %ext are not allowed. Please upload a file with an extension from the following list: %allowed_extensions.', array('%ext' => $ext, '%allowed_extensions' => $field['widget']['file_extensions'])));
-          return FALSE;
-        }
-
-        $file['upload_path'] = trim($field['widget']['upload_path']);
-
-        $file['fid'] = 'upload';
-        if (!$field['multiple']) {
-          if (is_array($node_field)) {
-            foreach ($node_field as $delta => $session_file) {
-              $node_field[$delta]['flags']['delete'] = TRUE;
-            }
-          }
-          _field_clear_field_session($fieldname, $field['type']);
-        }
-
-        // Add the file to the session
-        $file['sessionid'] = count($node_field) + count($_SESSION[$type][$fieldname]);
-        $_SESSION[$type][$fieldname][$file['sessionid']] = $file;
-      }
-
-      if (is_array($_SESSION[$type][$fieldname]) && count($_SESSION[$type][$fieldname])) {
-        foreach ($_SESSION[$type][$fieldname] as $delta => $file) {
-          $node_field[] = $file;
-        }
-      }
+      _audiofield_widget_prepare_form_values($node, $field, $node_field);
       break;
 
     case 'form':
@@ -181,11 +195,7 @@
       return $form;
 
     case 'validate':
-      if ($field['required']) {
-        if (!count($node_field)) {
-          form_set_error($fieldname, t('Field %name is required', array('%name' => $field['widget']['label'])));
-        }
-      }
+      _audiofield_widget_validate($node, $field, $node_field);
       return;
 
     case 'process form values':
@@ -197,6 +207,9 @@
  * A helper function to build a widget form.
  */
 function _audiofield_widget_form($node, $field, &$node_field) {
+  drupal_add_js('misc/progress.js');
+  drupal_add_js('misc/upload.js');
+
   $fieldname = $field['field_name'];
   drupal_add_css(drupal_get_path('module', 'audiofield') .'/audiofield.css');
 
@@ -208,20 +221,30 @@
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
     '#tree' => TRUE,
+    '#prefix' => '<div id="'. form_clean_id($fieldname .'-attach-wrapper') .'">',
+    '#suffix' => '</div>',
     '#theme' => 'audiofield_current',
   );
-  $form[$fieldname][$fieldname .'_upload'] = array(
+
+  $form[$fieldname]['new'] = array(
+    '#tree' => FALSE,
+    '#prefix' => '<div id="'. form_clean_id($fieldname .'-attach-hide') .'">',
+    '#suffix' => '</div>',
+    '#weight' => 100,
+  );
+
+  $form[$fieldname]['new'][$fieldname .'_upload'] = array(
     '#type'  => 'file',
     '#description' => ($field['widget']['description'] ? $field['widget']['description']. '<br/>' : ''). t('Allowed extensions: %ext.', array('%ext' => $field['widget']['file_extensions'])),
     '#weight' => 9,
     '#tree' => FALSE,
   );
 
-  $form[$fieldname]['upload'] = array(
+  $form[$fieldname]['new']['upload'] = array(
     '#type' => 'button',
     '#value' => t('Upload'),
     '#name' => 'cck_audiofield_'. $fieldname .'_op',
-    '#attributes' => array('id' => $fieldname .'-attach-button'),
+    '#id' => form_clean_id($fieldname .'-attach-button'),
     '#tree' => FALSE,
     '#weight' => 10,
   );
@@ -290,6 +313,29 @@
       }
     }
   }
+  // The class triggers the js upload behaviour.
+  $form[$fieldname.'-attach-url'] = array(
+    '#type' => 'hidden',
+    '#value' => url('audiofield/js', NULL, NULL, TRUE),
+    '#attributes' => array('class' => 'upload'),
+  );
+
+  // Some useful info for our js callback.
+  $form['vid'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->vid,
+    '#tree' => FALSE,
+  );
+  $form['nid'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->nid,
+    '#tree' => FALSE,
+  );
+  $form['type'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->type,
+    '#tree' => FALSE,
+  );
   return $form;
 }
 
@@ -431,3 +477,62 @@
     return $info;
   }
 }
+
+/**
+ * Menu callback for JavaScript-based uploads.
+ */
+function audiofield_js() {
+  // Parse fieldname from submit button.
+  $matches = array();
+  foreach(array_keys($_POST) as $key) {
+    if (preg_match('/cck_audiofield_(.*)_op/', $key, $matches)) {
+      $fieldname = $matches[1];
+      break;
+    }
+  }
+
+  $node = (object)$_POST;
+  $field = content_fields($fieldname, $node->type); // load field data
+
+  // Load fids stored by content.module.
+  $node_field = array();
+  $values = content_field('load', $node, $field, $node_field, FALSE, FALSE);
+  $node_field = $values[$fieldname];
+
+  // Load additional field data.
+  audiofield_field('load', $node, $field, $node_field, FALSE, FALSE);
+
+  // Handle uploads and validation.
+  _audiofield_widget_prepare_form_values($node, $field, $node_field);
+  _audiofield_widget_validate($node, $field, $node_field);
+
+  // Get our new form baby, yeah tiger, get em!
+  $form = _audiofield_widget_form($node, $field, $node_field);
+
+  foreach (module_implements('form_alter') as $module) {
+    $function = $module .'_form_alter';
+    $function('audiofield_js', $form);
+  }
+  $form = form_builder('audiofield_js', $form);
+
+  $output = theme('status_messages') . drupal_render($form);
+
+  // Send the updated file attachments form.
+  print drupal_to_js(array('status' => TRUE, 'data' => $output));
+  exit();
+}
+    
+function audiofield_menu($may_cache) {
+  $items = array();
+
+  if (!$may_cache) {
+    $items[] = array(
+      'path' => 'audiofield/js',
+      'callback' => 'audiofield_js',
+      //'access' => user_access(),
+      'access' => TRUE,
+      'type' => MENU_CALLBACK
+    );
+  }
+  return $items;
+}
Index: videofield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mediafield/videofield.module,v
retrieving revision 1.7
diff -u -r1.7 videofield.module
--- videofield.module	21 Feb 2007 18:04:57 -0000	1.7
+++ videofield.module	14 Jan 2008 12:28:15 -0000
@@ -134,6 +134,55 @@
   }
 }
 
+function _videofield_widget_prepare_form_values(&$node, $field, &$node_field) {
+  $fieldname = $field['field_name'];
+  $type = $field['type'];
+	if (!count($_POST)) {
+		_field_clear_session($type);
+	}
+
+	if ($file = file_check_upload($fieldname .'_upload')) {
+		$file = (array)$file;
+
+		$ext = array_pop(explode('.', $file['filename']));
+		$allowed_extensions = array_unique(explode(' ', trim($field['widget']['file_extensions'])));
+		if (!in_array($ext, $allowed_extensions)) {
+			form_set_error($field['field_name'] .'_upload', t('Files with the extension %ext are not allowed. Please upload a file with an extension from the following list: %allowed_extensions.', array('%ext' => $ext, '%allowed_extensions' => $field['widget']['file_extensions'])));
+			return FALSE;
+		}
+
+		$file['upload_path'] = trim($field['widget']['upload_path']);
+
+		$file['fid'] = 'upload';
+		if (!$field['multiple']) {
+			if (is_array($node_field)) {
+				foreach ($node_field as $delta => $session_file) {
+					$node_field[$delta]['flags']['delete'] = TRUE;
+				}
+			}
+			_field_clear_field_session($fieldname, $field['type']);
+		}
+
+		// Add the file to the session
+		$file['sessionid'] = count($node_field) + count($_SESSION[$type][$fieldname]);
+		$_SESSION[$type][$fieldname][$file['sessionid']] = $file;
+	}
+
+	if (is_array($_SESSION[$type][$fieldname]) && count($_SESSION[$type][$fieldname])) {
+		foreach ($_SESSION[$type][$fieldname] as $delta => $file) {
+			$node_field[] = $file;
+		}
+	}
+}
+
+function _videofield_widget_validate(&$node, $field, &$node_field) {
+	if ($field['required']) {
+		if (!count($node_field)) {
+			form_set_error($fieldname, $field['widget']['label'] .' is required.');
+		}
+	}
+}
+
 /**
  * Implementation of hook_widget().
  */
@@ -143,42 +192,7 @@
   $type = $field['type'];
   switch ($op) {
     case 'prepare form values':
-      if (!count($_POST)) {
-        _field_clear_session($type);
-      }
-
-      if ($file = file_check_upload($fieldname .'_upload')) {
-        $file = (array)$file;
-
-        $ext = array_pop(explode('.', $file['filename']));
-        $allowed_extensions = array_unique(explode(' ', trim($field['widget']['file_extensions'])));
-        if (!in_array($ext, $allowed_extensions)) {
-          form_set_error($field['field_name'] .'_upload', t('Files with the extension %ext are not allowed. Please upload a file with an extension from the following list: %allowed_extensions.', array('%ext' => $ext, '%allowed_extensions' => $field['widget']['file_extensions'])));
-          return FALSE;
-        }
-
-        $file['upload_path'] = trim($field['widget']['upload_path']);
-
-        $file['fid'] = 'upload';
-        if (!$field['multiple']) {
-          if (is_array($node_field)) {
-            foreach ($node_field as $delta => $session_file) {
-              $node_field[$delta]['flags']['delete'] = TRUE;
-            }
-          }
-          _field_clear_field_session($fieldname, $field['type']);
-        }
-
-        // Add the file to the session
-        $file['sessionid'] = count($node_field) + count($_SESSION[$type][$fieldname]);
-        $_SESSION[$type][$fieldname][$file['sessionid']] = $file;
-      }
-
-      if (is_array($_SESSION[$type][$fieldname]) && count($_SESSION[$type][$fieldname])) {
-        foreach ($_SESSION[$type][$fieldname] as $delta => $file) {
-          $node_field[] = $file;
-        }
-      }
+      _videofield_widget_prepare_form_values($node, $field, $node_field);
       break;
 
     case 'form':
@@ -186,11 +200,7 @@
       return $form;
 
     case 'validate':
-      if ($field['required']) {
-        if (!count($node_field)) {
-          form_set_error($fieldname, $field['widget']['label'] .' is required.');
-        }
-      }
+      _videofield_widget_validate($node, $field, $node_field);
       return;
 
     case 'process form values':
@@ -199,6 +209,9 @@
 }
 
 function _videofield_widget_form($node, $field, &$node_field) {
+  drupal_add_js('misc/progress.js');
+  drupal_add_js('misc/upload.js');
+
   $fieldname = $field['field_name'];
   drupal_add_css(drupal_get_path('module', 'videofield') .'/videofield.css');
 
@@ -211,20 +224,29 @@
     '#collapsed' => FALSE,
     '#tree' => TRUE,
     '#theme' => 'videofield_current',
+    '#prefix' => '<div id="'. form_clean_id($fieldname .'-attach-wrapper') .'">',
+    '#suffix' => '</div>',
+  );
+
+  $form[$fieldname]['new'] = array(
+    '#tree' => FALSE,
+    '#prefix' => '<div id="'. form_clean_id($fieldname .'-attach-hide') .'">',
+    '#suffix' => '</div>',
+    '#weight' => 100,
   );
 
-  $form[$fieldname][$fieldname .'_upload'] = array(
+  $form[$fieldname]['new'][$fieldname .'_upload'] = array(
     '#type'  => 'file',
     '#description' => ($field['widget']['description'] ? $field['widget']['description']. '<br/>' : ''). t('Allowed extensions: %ext.', array('%ext' => $field['widget']['file_extensions'])),
     '#weight' => 9,
     '#tree' => FALSE,
   );
 
-  $form[$fieldname]['upload'] = array(
+  $form[$fieldname]['new']['upload'] = array(
     '#type' => 'button',
     '#value' => t('Upload'),
     '#name' => 'cck_videofield_'. $fieldname .'_op',
-    '#attributes' => array('id' => $fieldname .'-attach-button'),
+    '#id' => form_clean_id($fieldname .'-attach-button'),
     '#tree' => FALSE,
     '#weight' => 10,
   );
@@ -305,6 +327,29 @@
       }
     }
   }
+  // The class triggers the js upload behaviour.
+  $form[$fieldname.'-attach-url'] = array(
+    '#type' => 'hidden',
+    '#value' => url('videofield/js', NULL, NULL, TRUE),
+    '#attributes' => array('class' => 'upload'),
+  );
+
+  // Some useful info for our js callback.
+  $form['vid'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->vid,
+    '#tree' => FALSE,
+  );
+  $form['nid'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->nid,
+    '#tree' => FALSE,
+  );
+  $form['type'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->type,
+    '#tree' => FALSE,
+  );
   return $form;
 }
 
@@ -460,3 +505,62 @@
     return theme('table', array(), $rows);
   }
 }
+
+/**
+ * Menu callback for JavaScript-based uploads.
+ */
+function videofield_js() {
+  // Parse fieldname from submit button.
+  $matches = array();
+  foreach(array_keys($_POST) as $key) {
+    if (preg_match('/cck_videofield_(.*)_op/', $key, $matches)) {
+      $fieldname = $matches[1];
+      break;
+    }
+  }
+
+  $node = (object)$_POST;
+  $field = content_fields($fieldname, $node->type); // load field data
+
+  // Load fids stored by content.module.
+  $node_field = array();
+  $values = content_field('load', $node, $field, $node_field, FALSE, FALSE);
+  $node_field = $values[$fieldname];
+
+  // Load additional field data.
+  videofield_field('load', $node, $field, $node_field, FALSE, FALSE);
+
+  // Handle uploads and validation.
+  _videofield_widget_prepare_form_values($node, $field, $node_field);
+  _videofield_widget_validate($node, $field, $node_field);
+
+  // Get our new form baby, yeah tiger, get em!
+  $form = _videofield_widget_form($node, $field, $node_field);
+
+  foreach (module_implements('form_alter') as $module) {
+    $function = $module .'_form_alter';
+    $function('videofield_js', $form);
+  }
+  $form = form_builder('videofield_js', $form);
+
+  $output = theme('status_messages') . drupal_render($form);
+
+  // Send the updated file attachments form.
+  print drupal_to_js(array('status' => TRUE, 'data' => $output));
+  exit();
+}
+    
+function videofield_menu($may_cache) {
+  $items = array();
+
+  if (!$may_cache) {
+    $items[] = array(
+      'path' => 'videofield/js',
+      'callback' => 'videofield_js',
+      //'access' => user_access(),
+      'access' => TRUE,
+      'type' => MENU_CALLBACK
+    );
+  }
+  return $items;
+}
