--- inline.module.cvs 2006-05-04 03:03:00.000000000 +0200 +++ inline.module 2006-07-20 14:17:58.613035900 +0200 @@ -40,8 +40,13 @@ function inline_settings() { } function inline_form_alter($form_id, &$form) { - if (isset($form['type'])) { - if ($form['type']['#value'] .'_node_settings' == $form_id) { + if (!isset($form['type'])) { + return; + } + $type = $form['type']['#value']; + switch ($form_id) { + // node settings form + case $type .'_node_settings': $form['workflow']['upload_inline_'. $form['type']['#value']] = array( '#type' => 'radios', '#title' => t('Display attachments inline automatically'), @@ -49,7 +54,7 @@ function inline_form_alter($form_id, &$f '#options' => array(t('Disabled'), t('Only in teaser'), t('Only in body'), t('In teaser and body')), '#description' => t('Whether or not uploaded images should be shown inline. Make sure you set the dimensions at %settings_url', array('%settings_url' => l(t('inline settings'), 'admin/settings/inline'))), ); - } + return; } } @@ -108,14 +113,32 @@ function inline_filter_tips($delta, $for } } +/** + * Implementation of hook_nodeapi. + */ function inline_nodeapi(&$node, $op, $arg) { - if(is_array($node->files) && $op == 'view') { - //TODO: we should *really* us the filter hooks for this, not? - $node->teaser = _inline_substitute_tags($node, 'teaser'); - $node->body = _inline_substitute_tags($node, 'body'); - if (variable_get('upload_inline_'. $node->type, 0)) { - $node = _inline_auto_add($node); - } + // do nothing if no files are attached + if(!is_array($node->files) && !is_array($node->attachments)) { + return; + } + switch ($op) { + case 'view': + //TODO: we should *really* us the filter hooks for this, not? + $node->teaser = _inline_substitute_tags($node, 'teaser'); + $node->body = _inline_substitute_tags($node, 'body'); + if (variable_get('upload_inline_'. $node->type, 0)) { + $node = _inline_auto_add($node); + } + return; + case 'submit': + // don't replace numbers for attachments, since attachment file names + // are allowed to be identical. + if (is_array($node->attachments)) { + return; + } + $node->teaser = _inline_replace_numbers($node, 'teaser'); + $node->body = _inline_replace_numbers($node, 'body'); + return; } } @@ -246,6 +269,33 @@ function _inline_substitute_tags(&$node, } /** + * Replaces numeric file references with their respective file names. + * @param &$node The node object to process. + * @param $field Field of node to process. + * @return Processed $field of $node. + */ +function _inline_replace_numbers(&$node, $field) { + $tag = "/\[(inline|file|attachment):(\d+)[^=\\]]*=?([^\\]]*)?\]/i"; + // look if there are any numeric inline tags + if (preg_match_all($tag, $node->$field, $matches, PREG_SET_ORDER)) { + foreach ($matches as $match) { + // file uploads are stored with their database file ID + $filekeys = array_keys( $node->files ); + // if a corresponding file does exist, perform the replacement + if (isset( $node->files[$filekeys[$match[2]-1]]['filename'] )) { + $filename = $node->files[$filekeys[$match[2]-1]]['filename']; + $node->$field = preg_replace( $tag, '[$1:'. $filename . (!empty( $match[3] ) ? '=$3' : '') . ']', $node->$field, 1); + } + if (isset( $node->attachments[$match[2]-1]['filename'] )) { + $filename = $node->attachments[$match[2]-1]['filename']; + $node->$field = preg_replace( $tag, '[$1:'. $filename . (!empty( $match[3] ) ? '=$3' : '') . ']', $node->$field, 1); + } + } + } + return $node->$field; +} + +/** * Decides if a tag (<img>) or a link to a file should be rendered * @param $file a file object * @return TRUE in case an img tag should be generated