Index: filefield_upload_limit.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filefield_upload_limit/filefield_upload_limit.module,v
retrieving revision 1.4
diff -u -r1.4 filefield_upload_limit.module
--- filefield_upload_limit.module	5 Dec 2008 18:39:18 -0000	1.4
+++ filefield_upload_limit.module	7 Aug 2009 14:47:34 -0000
@@ -1,22 +1,28 @@
 <?php
 /**
  * Implementation of hook_form_alter().
- * 
+ *
  * Additional filesize field and corresponding default value retrieval,
- * submit and validate function calls
+ * submit and validate function calls,
+ * change the description on node edit forms.
  */
 function filefield_upload_limit_form_alter($form_id, &$form) {
+  // CCK field admin settings for filefields.
   if ($form_id == '_content_admin_field' && $form['field_type']['#value'] == 'file') {
     $form['widget']['maxsize']['#type'] = 'textfield';
     $form['widget']['maxsize']['#title'] = t('Maximum allowed filesize');
 
     $fieldname = $form['field_name']['#value'];
     $filefield_sizes = variable_get('filefield_max_size',array($fieldname => '0'));
-    
+
     $form['widget']['maxsize']['#default_value'] = $filefield_sizes[$fieldname];
     $form['widget']['maxsize']['#description'] = t('Filesize in MB for this specific field. Leave as 0 to apply standard upload module size limit.');
     $form['#validate']['filefield_upload_limit_content_admin_field_validate'] = array();
-  	$form['#submit']['filefield_upload_limit_content_admin_field_submit'] = array();
+    $form['#submit']['filefield_upload_limit_content_admin_field_submit'] = array();
+  }
+  // Node edit forms: change the description on filefield uploads.
+  if ($form['#id'] == 'node-form') {
+    filefield_upload_limit_get_filefields($form);
   }
 }
 
@@ -36,7 +42,7 @@
  */
 function filefield_upload_limit_content_admin_field_submit($form_id, $form_values) {
 
-  $vars = variable_get('filefield_max_size',array($form_values['field_name'] => '0')); 
+  $vars = variable_get('filefield_max_size',array($form_values['field_name'] => '0'));
   $vars[$form_values['field_name']] = $form_values['maxsize'];
 
   variable_set('filefield_max_size', $vars);
@@ -45,7 +51,7 @@
 /**
  * Implementation of hook_filefield()
  * from the filefield.module CCK field
- * 
+ *
  * @param $action
  *  string: action being performed on the data - options are:
  *          - file_validate
@@ -54,26 +60,26 @@
  *          - file_delete
  *          - file_prepare
  *          - file_save
- * 
+ *
  * @param $node
  *  object: the node
- * 
+ *
  * @param $field
  *  object: the CCK field
- * 
+ *
  * @param $file
  *  array: the uploaded file data
- * 
+ *
  * @param $form
  *  array: the form collection, where available
- * 
+ *
  * @return
  *  file_validate: return boolean FALSE if you want to halt the processing
  *  of the upload - all other valid return values TBD
  */
 function filefield_upload_limit_filefield($action, $node, $field, $file, $form = null) {
   global $user;
-  
+
   switch ($action) {
     case 'file_validate':
       foreach ($user->roles as $rid => $name) {
@@ -96,18 +102,18 @@
 /**
  * Check to see if there is a max filesize set for this field
  * and return it if there is
- * 
+ *
  * @param $fieldname
  *  string: the name of the field to be searched for
- * 
+ *
  * @return
  *  integer: a number denoting the stored max filesize setting in bytes.
  *  if zero is the returned number then max filesize is ignored and the
  *  upload is permitted regardless.
  */
 function filefield_upload_limit_is_set($fieldname) {
-  $vars = variable_get('filefield_max_size',array($fieldname => '0')); 
-  
+  $vars = variable_get('filefield_max_size',array($fieldname => '0'));
+
   $limit = 0;
   foreach ($vars as $key => $value) {
     if ($key == $fieldname) {
@@ -118,3 +124,73 @@
   $bytelimit = $limit * 1000000;
   return $bytelimit;
 }
+
+/**
+ * Change all filefield descriptions on a node form.
+ *
+ * Recursive helper for hook_form_alter.
+ */
+function filefield_upload_limit_get_filefields(&$elements, $element_id = NULL) {
+  // Recurse through all children.
+  foreach (element_children($elements) as $key) {
+    if (isset($elements[$key]) && $elements[$key]) {
+      filefield_upload_limit_get_filefields($elements[$key], $key);
+    }
+  }
+  // If this is an upload field, and it is part of a filefield, then alter the description.
+  if ($elements['#type'] == 'file' && substr($element_id, 0, 6) == 'field_' ) {
+    $fieldname = str_replace('_upload', '', $element_id);
+    $limit = filefield_upload_limit_get_field_upload_limit_string($fieldname);
+    $description = theme('filefield_upload_limit_field_limit', $elements['#description'], $limit, $fieldname);
+
+    $elements['#description'] = $description;
+  }
+}
+
+/**
+ * Get the upload limit for a given field name, taking into account global upload settings.
+ */
+function filefield_upload_limit_get_field_upload_limit_string($fieldname) {
+  global $user;
+
+  // Get default upload module settings.
+  $uploadsize = array();
+  $usersize = array();
+  foreach ($user->roles as $rid => $name) {
+    $uploadsize[] = variable_get("upload_uploadsize_$rid", variable_get('upload_uploadsize_default', 1)) * 1024 * 1024;
+    $usersize[]   = variable_get("upload_usersize_$rid", variable_get('upload_usersize_default', 1)) * 1024 * 1024;
+  }
+  #$message = "You have ".format_size((max($usersize) - upload_space_used($user->uid)))." of disk space remaining";
+
+  // If there is a field-specific size limit, set the size here.
+  if (format_size(filefield_upload_limit_is_set($fieldname)) > 0) {
+    $limit = filefield_upload_limit_is_set($fieldname);
+  }
+  else {
+    $limit = max($uploadsize);
+  }
+
+  return $limit;
+}
+
+/**
+ * Theme the new description on node edit form filefield upload elements.
+ *
+ * @param $original_string
+ *   The original description on the form
+ * @param $limit
+ *   The numerical limit. Run this through format_size().
+ * @param $original_string
+ *   The id of the form element. This will be the id of the field.
+ *   Use this if you want per-field descriptions.
+ * @return
+ *   The new description string.
+ */
+function theme_filefield_upload_limit_field_limit($original_string, $limit, $element_id) {
+  // Warning: this is quite delicate, as it depends on filefield module's exact string.
+  $pieces = explode('. ', $original_string);
+
+  $pieces[0] = 'Maximum file size: '. format_size($limit);
+
+  return implode('. ', $pieces);
+}
\ No newline at end of file
