? filefield_insert-2009-10-07.patch
? filefield_insert-HEAD.patch
Index: filefield_insert.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filefield_insert/filefield_insert.js,v
retrieving revision 1.5
diff -u -p -r1.5 filefield_insert.js
--- filefield_insert.js	15 Jun 2009 16:18:36 -0000	1.5
+++ filefield_insert.js	7 Oct 2009 10:29:14 -0000
@@ -43,7 +43,8 @@ Drupal.behaviors.fileFieldInsert = funct
     var $preset = $fileFieldElement.find('select.filefield-insert-preset:first');
 
     var widget = $fileFieldElement.find('input.filefield-insert-widget').val();
-    var url = $fileFieldElement.find('input.filefield-insert-url').val();
+    var orgUrl = $fileFieldElement.find('input.filefield-insert-url').val();
+    var url = orgUrl;
     var width = parseInt($fileFieldElement.find('input.filefield-insert-width').val());
     var height = parseInt($fileFieldElement.find('input.filefield-insert-height').val());
     var maxWidth = parseInt($fileFieldElement.find('input.filefield-insert-max-width').val());
@@ -60,25 +61,8 @@ Drupal.behaviors.fileFieldInsert = funct
     if (preset) {
       var pieces = preset.split('|');
       url = url.replace(fileDir + '/', fileDir + '/imagecache/' + pieces[0] + '/');
-      // If the ImageCache preset specified both height and width.
-      if (pieces[1] != '0' && pieces[2] != '0') {
-        width = parseInt(pieces[1]);
-        height = parseInt(pieces[2]);
-      }
-      // If just the width, calculate the height.
-      else if (pieces[1] != '0') {
-        var newWidth = parseInt(pieces[1]);
-        var newRatio = newWidth / width;
-        width = newWidth;
-        height = Math.round(height * newRatio);
-      }
-      // And if just the height, calculate the width.
-      else if (pieces[2] != '0') {
-        var newHeight = parseInt(pieces[2]);
-        var newRatio = newHeight / height;
-        height = newHeight;
-        width = Math.round(width * newRatio);
-      }
+      width = parseInt(pieces[1]);
+      height = parseInt(pieces[2]);
     }
 
     // Check for a maximum dimension and scale down the width if necessary.
@@ -107,8 +91,14 @@ Drupal.behaviors.fileFieldInsert = funct
     // Poor man's escaping of invalid characters in the URL.
     url = encodeURI(url).replace(/([#$*(),?])/g, function(match) { return escape(match) });
 
+    // Remove width="0" and height="0"
+    if (width == 0 || height == 0) {
+      content = content.replace(/width=\"__width__\"/g, '').replace(/height=\"__height__\"/g, '');
+    }
+
     // Update replacements.
-    content = $.trim(content).replace(/__url__/g, url).replace(/__extension__/g, extension).replace(/__width__/g, width).replace(/__height__/g, height).replace(/__description__/g, description);
+    content = $.trim(content).replace(/__org__/g, orgUrl).replace(/__url__/g, url).replace(/__extension__/g, extension)
+      .replace(/__width__/g, width).replace(/__height__/g, height).replace(/__description__/g, description);
     if (Drupal.settings.fileFieldInsert.templates.image) {
       content = content.replace(/__alt__/g, alt).replace(/__title__/g, title);
     }
Index: filefield_insert.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filefield_insert/filefield_insert.module,v
retrieving revision 1.5
diff -u -p -r1.5 filefield_insert.module
--- filefield_insert.module	30 Jun 2009 22:13:22 -0000	1.5
+++ filefield_insert.module	7 Oct 2009 10:29:15 -0000
@@ -58,6 +58,8 @@ function filefield_insert_process($eleme
     return $element;
   }
 
+  $field['widget']['filefield_filepath'] = $item['filepath'];
+
   // Add base settings only once.
   if (!isset($js_added)) {
     $js_added = array();
@@ -103,11 +105,18 @@ function filefield_insert_process($eleme
       '#weight' => -1,
     );
 
+    if (module_exists('pathologic')) {
+      $insert_url = preg_replace('/^' . preg_quote($GLOBALS['base_url'] .'/', '/') . '/', '', file_create_url($item['filepath']));
+    }
+    else {
+      $insert_url = preg_replace('/^' . preg_quote($GLOBALS['base_url'] .'/', '/') . '/', base_path(), file_create_url($item['filepath']));
+    }
+
     $element['filefield_insert_url'] = array(
       '#id' => $element['#id'] . '-insert-url',
       '#name' => $element['#name'] . '[insert_url]',
       '#type' => 'hidden',
-      '#value'=> preg_replace('/^' . preg_quote($GLOBALS['base_url'] .'/', '/') . '/', base_path(), file_create_url($item['filepath'])),
+      '#value'=> $insert_url,
       '#attributes' => array('class' => 'filefield-insert-url'),
     );
     $element['filefield_insert_widget'] = array(
@@ -286,6 +295,7 @@ function filefield_insert_set_settings($
  * Right now our variables are simply place holders replaced by JavaScript.
  */
 function template_preprocess_filefield_insert(&$vars) {
+  $vars['org'] = '__org__';
   $vars['url'] = '__url__';
   $vars['description'] = '__description__';
   $vars['alt'] = '__alt__';
@@ -311,11 +321,11 @@ function template_preprocess_filefield_i
       // Determine the final width/height.
       $width = 0;
       $height = 0;
-      foreach ($preset['actions'] as $action) {
-        if (isset($action['data']['width']) || isset($action['data']['height'])) {
-          $width = !empty($action['data']['width']) ? $action['data']['width'] : 0;
-          $height = !empty($action['data']['height']) ? $action['data']['height'] : 0;
-        }
+      $filepath = $vars['field']['widget']['filefield_filepath'];
+      $imagecache_path = imagecache_create_path($preset['presetname'], $filepath);
+      if ($image = image_get_info($imagecache_path)) {
+        $width = $image['width'];
+        $height = $image['height'];
       }
 
       $vars['image_presets'][$preset['presetname'] . '|' . $width . '|' . $height] = t($preset['presetname']);
