--- inline.module.cvs	2006-05-04 03:03:00.000000000 +0200
+++ inline.module	2006-05-23 18:07:30.483131400 +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,45 +116,132 @@ 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':
+      $node->teaser = _inline_replace_numbers($node, 'teaser');
+      $node->body = _inline_replace_numbers($node, 'body');
+      return;
   }
 }
 
+/**
+ * Create $file object for further use.
+ * For use with attachment module we're fetching some variables
+ * from filemanager module to accompany with regular file object
+ * provided by the upload module.
+ * 
+ * @param object $node The currently processed node.
+ * @param mixed $id Either an image id or filename to display inline.
+ * @return object $file An object holding all necessary information
+ *   about the attached file.
+ */
 function _inline_fileobj(&$node, $id) {
   if (is_numeric($id)) {
-    $n=1;
-    foreach ($node->files as $file) {
-      if ($n == $id) {
-        return $file;
+    // numeric attachment parameter
+    // upload module
+    if (isset( $node->files )) {
+      $n=1;
+      foreach ($node->files as $file) {
+        if ($n == $id) {
+          $file['url'] = file_create_url($file['filepath']);
+          return (object)$file;
+        }
+        ++$n;
+      }
+    }
+    // attachment module
+    if (isset( $node->attachments ) && function_exists('filemanager_url')) {
+      $n=1;
+      foreach ($node->attachments as $file) {
+        if ($n == $id) {
+          $attachment = filemanager_get_file_info($file['fid']);
+          $file['url'] = filemanager_url($attachment);
+          $file['filepath'] = filemanager_create_path($attachment);
+          $file['filesize'] = $file['size'];
+          $file['filemime'] = $attachment->mimetype;
+          return (object)$file;
+        }
+        ++$n;
       }
-      ++$n;
     }
     return NULL;
   }
   else
   {
-    foreach ($node->files as $file) {
-      if ($file->filename == $id) {
-        //return array($file->filename, $file->filepath);
-        return $file;
+    // named attachment parameter
+    // upload module
+    if (isset( $node->files )) {
+      foreach ($node->files as $file) {
+        if ($file['filename'] == $id) {
+          $file['url'] = file_create_url($file['filepath']);
+          return (object)$file;
+        }
+      }
+    }
+    // attachment module
+    if (isset( $node->attachments ) && function_exists('filemanager_get_file_info')) {
+      foreach ($node->attachments as $file) {
+        if ($file['filename'] == $id) {
+          $attachment = filemanager_get_file_info($file['fid']);
+          $file['url'] = filemanager_url($attachment);
+          $file['filepath'] = filemanager_create_path($attachment);
+          $file['filesize'] = $file['size'];
+          $file['filemime'] = $attachment->mimetype;
+          return (object)$file;
+        }
       }
     }
     return NULL;
   }
 }
 
+/**
+ * Theme $file as inline link.
+ * Displays file type icon if exists in ./themes/<template>/mime/<mime-type>.png.
+ * Mime types are converted to simple extensions, i.e. 'application/zip' becomes
+ * 'zip', 'image/jpeg' => 'jpeg'. If no corresponding mime file type icon is present,
+ * double quotes are preprended.
+ * 
+ * @param object $file The file object holding file information.
+ * @return Themed inline file link.
+ */
 function theme_inline_as_link($file) {
-  return l(($file->description ? $file->description : $file->name),
-    file_create_url($file->filepath),
-    array('title' => t('Download: %name (%size)',
-      array('%name' => $file->filename, '%size' => format_size($file->filesize)))));
+  // preprend file type icon or marker
+  $mimeimg = base_path() . path_to_theme() .'/mime/'. array_pop(explode('/', $file->filemime)) .'.png';
+  if (file_exists( '.'. $mimeimg )) {
+    $linktext = '<img src="'. $mimeimg .'" alt="'. $file->filemime .'" style="vertical-align: bottom;" />';
+  } else {
+    $linktext = '&raquo;';
+  }
+  // prepare link text with title or filename
+  // $file->title for attachment module support.
+  $linktext.= '&nbsp;'. ($file->title ? $file->title : $file->filename);
+  
+  // prepare link
+  $link = l($linktext, $file->url, array('title' => t('Download: %name (%size)', array('%name' => $file->filename, '%size' => format_size($file->filesize)))), NULL, NULL, TRUE, TRUE);
+  
+  // generate output
+  $output = '<dl class="inline">';
+  $output.= '<dt>'. $link .' <small>('. format_size($file->filesize) .')</small></dt>';
+  $output.= ($file->description ? '<dd><small>'. $file->description .'</small></dd>' : '');
+  $output.= '</dl>';
+  return $output;
 }
 
 function theme_inline_img($file) {
@@ -221,9 +316,12 @@ function _inline_substitute_tags(&$node,
   if (preg_match_all("/\[(inline|file|attachment):([^=\\]]+)=?([^\\]]*)?\]/i", $node->$field, $match)) {
     foreach ($match[2] as $key => $value) {
       $map[$value] = $key;
-      $titl = $match[3][$key];
+      $title = $match[3][$key];
       $ytype = $match[1][$key];
       $file = _inline_fileobj($node, $value);
+      if (!empty( $title )) {
+        $file->title = $title;
+      }
       $replace = "";
       if ($file->fid != NULL) {
         //decide if we should show a link or an img tag
@@ -246,6 +344,31 @@ 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)) {
+    // if a corresponding file does exist, perform the replacement
+    foreach ($matches as $match) {
+      if (isset( $node->files[$match[2]-1]['filename'] )) {
+        $filename = $node->files[$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 (&lt;img&gt;) or a link to a file should be rendered
  * @param $file a file object
  * @return TRUE in case an img tag should be generated
