? .svn
? inline.patch
? inline_cvs.patch
? po/.svn
Index: inline.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/inline/inline.module,v
retrieving revision 1.11
diff -u -F^f -r1.11 inline.module
--- inline.module	21 Nov 2005 22:30:50 -0000	1.11
+++ inline.module	16 Feb 2006 20:29:50 -0000
@@ -26,7 +26,7 @@ function inline_settings() {
     '#maxlength' => 10,
     '#required' => TRUE,
     '#default_value' => variable_get('inline_img_dim', '150,150'),
-    '#description' => t('This setting will affect the dimensions of displayed images. They will not be resized.'),
+    '#description' => t('This setting will affect the dimensions of displayed images. They will not be resized. Images larger than these dimensions will not be shown automatically.'),
   );
 
   $form['inline_link_img'] = array(
@@ -40,6 +40,20 @@ function inline_settings() {
   return $form;
 }
 
+function inline_form_alter($form_id, &$form) {
+  if (isset($form['type'])) {
+    if ($form['type']['#value'] .'_node_settings' == $form_id) {
+      $form['workflow']['upload_inline_'. $form['type']['#value']] = array(
+          '#type' => 'radios',
+          '#title' => t('Display attachments inline automatically'),
+          '#default_value' => variable_get('upload_inline_'. $form['type']['#value'], 0),
+          '#options' => array(t('Disabled'), t('Only in teaser'), t('Only in body'), t('In teaser and body')),
+          '#description' => t('Wether 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'))),
+      );
+    }
+  }
+}
+
 function inline_filter($op, $delta = 0, $format = -1, $text = '') {
   // The "list" operation provides the module an opportunity to declare both how
   // many filters it defines and a human-readable name for each filter. Note that
@@ -73,7 +87,7 @@ function inline_filter_tips($delta, $for
     <li>file1.pdf (referred as file #2)
     <li>imag2.png (referred as file #3)
     </ul>
-    
+
     <pre>[inline:1=test]  or  [inline:imag1.png=test]</pre>
 
     will be replaced by <em><code>&lt;img src=imag1.png alt=test&gt;</code></em>
@@ -87,7 +101,7 @@ function inline_filter_tips($delta, $for
     <pre>[attachment:2=test]  or  [attachment:file1.pdf=test]</pre>
 
     will be replaced by <em><code>&lt;a href=file1.pdf.png&gt;test&lt;/a&gt;</code></em>
-    
+
     ');
   }
   else {
@@ -97,17 +111,21 @@ function inline_filter_tips($delta, $for
 
 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);
+    }
   }
 }
 
-function _inline_filename(&$node, $id) {
+function _inline_fileobj(&$node, $id) {
   if (is_numeric($id)) {
     $n=1;
     foreach ($node->files as $file) {
       if ($n == $id) {
-        return array($file->filename, $file->filepath);
+        return $file;
       }
       ++$n;
     }
@@ -117,78 +135,133 @@ function _inline_filename(&$node, $id) {
   {
     foreach ($node->files as $file) {
       if ($file->filename == $id) {
-        return array($file->filename, $file->filepath);
+        //return array($file->filename, $file->filepath);
+        return $file;
       }
     }
     return NULL;
   }
 }
 
-function theme_inline_html($filename, $filepath, $title, $allow_inline_image) {
+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)))));
+}
 
-  //get file size
-  $f_size=format_size(filesize($filepath));
+function theme_inline_img($file) {
+  if (variable_get('inline_link_img', '1') == '1') {
+    $html .= '<a href="'. file_create_url($file->filepath) . '" title="'.t("View").': '. $file->name .'">';
+  }
 
-  //make a list with allowed image-tags
-  $extensions = 'jpg jpeg gif png';
-  $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
-  if (preg_match($regex, $filepath) && $allow_inline_image) {
-    $f_dim=getimagesize($filepath);
-    $f_width=$f_dim[0];
-    $f_height=$f_dim[1];
-    $ratio = 0; if ($f_height) $ratio=($f_width*100)/$f_height;
-    // read settings
-    $dim=explode(',',variable_get('inline_img_dim', '150,150'));
-    $width=$dim[0];
-    $height=$dim[0];
-    // maintain aspect ration
-    if ($f_width > $width)   {
-       $f_width=$width;
-       $f_height=intval($f_width*100/$ratio);
-    }
-    $html = ''; if (variable_get('inline_link_img', '1') == '1')    $html = '<a href="'. file_create_url($filepath) . '" title="'.t("View").': '. $filename .'">';
+  $title = ( $file->description ? $file->description : $file->name );
 
-    if ($title != "") {
-      $html .= '<img style="width: '.$f_width.'px; height: '.$f_height.'px;" src="'. file_create_url($filepath). '" class="inline" alt="'. $title .'" title="'.$title.'" />';
+  $html .= '<img style="width: '.$file->width.'px; height:'.$file->height.'px;" src="'. file_create_url($file->filepath). '" class="inline" alt="'. $title .'" title="'. $title.'" />';
 
-    }
-    else {
-      $html .= '<img style="width: '.$f_width.'px; height: '.$f_height.'px;" src="'. file_create_url($filepath). '" class="inline" alt="'. $filename .'" />';
-    }
-    if (variable_get('inline_link_img', '1') == '1')    $html .= '</a>';
+  if (variable_get('inline_link_img', '1') == '1') {
+    $html .= '</a>';
   }
-  else {
-    if ($title != "") {
-      $html .= '<a href="'. file_create_url($filepath) . '" title="'.t('Download').': '. $filename .' | '. $f_size .'">'. $title .'</a>';
-    }
-    else {
-      $html .= '<a href="'. file_create_url($filepath) . '" title="'.t('Download').': '. $filename .' | '. $f_size .'">'. $filename .'</a>';
-    }
-  }
-
   return $html;
 }
 
+function theme_inline_add_to_teaser($node, $file) {
+   return theme('inline_img', $file) . $node->teaser;
+}
+
+function theme_inline_add_to_body($node, $file) {
+   return theme('inline_img', $file) . $node->body;
+}
+
+function _inline_auto_add($node) {
+  $inline_file = _inline_fileobj($node, $value);
+  //0 Disabled
+  //1 Only in teaser
+  //2 Only in body
+  //3 In teaser and body
+  switch (variable_get('upload_inline_'. $node->type, 0)) {
+    case 1:
+      foreach ($node->files as $fid => $file) {
+        if (_inline_decide_img_tag($file)) {
+          $node->files[$fid]->inline = TRUE;
+          $node->teaser = theme('inline_add_to_teaser', $node, $file);
+        }
+        else {
+          $node->files[$fid]->inline = FALSE;
+        }
+      }
+    break;
+    case 2:
+      foreach ($node->files as $fid => $file) {
+        if (_inline_decide_img_tag($file)) {
+          $node->files[$fid]->inline = TRUE;
+          $node->body = theme('inline_add_to_body', $node, $file);
+        }
+        else {
+          $node->files[$fid]->inline = FALSE;
+        }
+      }
+    break;
+    case 3:
+      foreach ($node->files as $fid => $file) {
+        if (_inline_decide_img_tag($file)) {
+          $node->files[$fid]->inline = TRUE;
+          $node->teaser = theme('inline_add_to_teaser', $node, $file);
+          $node->body = theme('inline_add_to_body', $node, $file);
+        }
+        else {
+          $node->files[$fid]->inline = FALSE;
+        }
+      }
+    break;
+  }
+  return $node;
+}
+
 function _inline_substitute_tags(&$node, $field) {
-    if (preg_match_all("/\[(inline|file|attachment):([^=\\]]+)=?([^\\]]*)?\]/i", $node->$field, $match)) {
-       foreach ($match[2] as $key => $value) {
-         $map[$value] = $key;
-         $titl = $match[3][$key];
-         $mytype = $match[1][$key];
-         $inline_file = _inline_filename($node, $value);
-         $replace = "";
-         if ($inline_file != NULL) {
-           $replace = theme('inline_html', $inline_file[0], $inline_file[1], $titl, $mytype == "inline");
-         }
-         else {
-           $replace = "<span style=\"color:red; font-weight:bold\">NOT FOUND: $value</span>";
-         }
-         $mtch[] = $match[0][$key];
-         $repl[] = $replace;
-       }
-       return str_replace($mtch, $repl, $node->$field);
+  if (preg_match_all("/\[(inline|file|attachment):([^=\\]]+)=?([^\\]]*)?\]/i", $node->$field, $match)) {
+    foreach ($match[2] as $key => $value) {
+      $map[$value] = $key;
+      $titl = $match[3][$key];
+      $ytype = $match[1][$key];
+      $file = _inline_fileobj($node, $value);
+      $replace = "";
+      if ($file->fid != NULL) {
+        //decide if we should show a link or an img tag
+        if (_inline_decide_img_tag($file)) {
+          $replace = theme('inline_img', $file);
+        }
+        else {
+          $replace = theme('inline_as_link', $file);
+        }
+      }
+      else {
+        $replace = "<span style=\"color:red; font-weight:bold\">NOT FOUND: $value</span>";
+      }
+      $mtch[] = $match[0][$key];
+      $repl[] = $replace;
     }
-    return $node->$field;
+    return str_replace($mtch, $repl, $node->$field);
+  }
+  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
+ */
+function _inline_decide_img_tag($file) {
+  $inlined = array('jpg', 'jpeg', 'gif', 'png');
+  $mime = array_pop(explode('/', $file->filemime));
+  if (in_array($mime, $inlined)) {
+    // read settings
+    list($maxwidth, $maxheight) = explode(',',variable_get('inline_img_dim', '150,150'));
+    list($width, $height) = getimagesize($file->filepath);
+    if (($width && $height) && ($width < $maxwidth && $height < $maxheight)) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
 ?>
