--- uploadpath.module 2007-06-16 10:44:39.000000000 +1200 +++ uploadpath.module.patch 2007-12-12 10:38:19.000000000 +1300 @@ -35,9 +35,46 @@ function uploadpath_admin_settings() { $form['uploadpath_prefix'] = array( '#type' => 'textfield', - '#title' => t('Pattern for the file prefix'), + '#title' => t('Default pattern for the file path prefix'), '#description' => t('Specify the pattern to prefix to file names uploaded with the upload module. It will be appended after the site files directory (e.g., files) but before the file name itself. Do not include a leading or trailing slash. Spaces will be converted to underscores to avoid file system issues.'), '#default_value' => variable_get('uploadpath_prefix', ''), + '#weight' => -9 + ); + + $form['misc'] = array( + '#title' => t('Miscellaneous'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Other settings'), + '#weight' => -6 + ); + + $form['misc']['uploadpath_bugfix1'] = array( + '#type' => 'checkbox', + '#title' => t('Set node created and updated time, if not set'), + '#return_value' => 1, + '#default_value' => variable_get('uploadpath_bugfix1', 0), + '#description' => t('This is a bugfix for this issue http://drupal.org/node/153737 where [yyyy] etc isn\'t evaluated properly and is only useful if you\'re using date tokens in your paths.'), + '#weight' => 0, + ); + + $form['node_types_exclude'] = array( + '#title' => t('Excluded node types'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Select the node types to exclude from upload path processing'), + '#weight' => -8 + ); + + $form['node_types'] = array( + '#title' => t('Patterns for each node type'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Patterns for node types. If empty, the default pattern will be used.'), + '#weight' => -7 ); $form['token_help'] = array( @@ -46,11 +83,38 @@ function uploadpath_admin_settings() { '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'), + '#weight' => -6 ); $form['token_help']['help'] = array( '#value' => theme('token_help', 'node'), ); - + + //node type settings + $node_types = node_get_types(); + foreach($node_types as $type){ + $types[$type->type] = $type->name; + } + //set excluded node types + $form['node_types_exclude']['uploadpath_excluded_node_types'] = array( + '#type' => 'select', + '#multiple' => true, + '#title' => t('Excluded Node Types'), + '#default_value' => variable_get('uploadpath_excluded_node_types', array()), + '#description' => t('Select the node types to exclude from uploadpath processing.'), + '#options' => $types, + ); + + foreach($node_types as $type){ + //only show settings if not excluded + if(!in_array($type->type, variable_get('uploadpath_excluded_node_types',array()))){ + $form['node_types']['uploadpath_prefix_'.$type->type] = array( + '#type' => 'textfield', + '#title' => t('Path pattern for '.$type->name), + '#description' => t('Specify the path pattern to prefix for '.$type->name.' type nodes'), + '#default_value' => variable_get('uploadpath_prefix_'.$type->type, ''), + ); + } + } return system_settings_form($form); } @@ -60,23 +124,40 @@ function uploadpath_admin_settings() { function uploadpath_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { switch ($op) { case 'submit': - if (isset($node->files)) { - foreach ($node->files as $key => $file) { - if (0 === strpos($key, 'upload_')) { // Only rewrite the name when adding the file, not when updating it - // Get the new, prefixed file name - $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace(variable_get('uploadpath_prefix', '') . '/', 'node', $node)) . $node->files[$key]['filename']; - - // Create the directory if it doesn't exist yet. - $dirs = explode('/', dirname($file_name)); - $directory = file_directory_path(); - while (count($dirs)) { - $directory .= '/' . array_shift($dirs); - file_check_directory($directory, FILE_CREATE_DIRECTORY); - } - // Change where the file will be saved to the specified directory. - $node->files[$key]['filename'] = $file_name; - } - } + if(!in_array($node->type, variable_get('uploadpath_excluded_node_types',array()))){ + if (isset($node->files)) { + foreach ($node->files as $key => $file) { + if (0 === strpos($key, 'upload_')) { // Only rewrite the name when adding the file, not when updating it + + //optional + if(variable_get('uploadpath_bugfix1', false)){ + //bugfix: set node created and updated timestamp if not set for token replace + //see http://drupal.org/node/153737 + if(!$node->created){$node->created = (int)time();} + if(!$node->changed){$node->changed = (int)$node->created;} + } + + //get the token path pattern + $pattern = variable_get('uploadpath_prefix_'.$node->type,false); + if(!$pattern){ + //default pattern + $pattern = variable_get('uploadpath_prefix', ''); + } + // Get the new, prefixed file name + $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace($pattern . '/', 'node', $node)) . $node->files[$key]['filename']; + + // Create the directory if it doesn't exist yet. + $dirs = explode('/', dirname($file_name)); + $directory = file_directory_path(); + while (count($dirs)) { + $directory .= '/' . array_shift($dirs); + file_check_directory($directory, FILE_CREATE_DIRECTORY); + } + // Change where the file will be saved to the specified directory. + $node->files[$key]['filename'] = $file_name; + } + } + } } break; }