Index: img_assist.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/Attic/img_assist.module,v
retrieving revision 1.68.2.48
diff -u -p -r1.68.2.48 img_assist.module
--- img_assist.module	24 Feb 2008 21:46:08 -0000	1.68.2.48
+++ img_assist.module	28 Mar 2008 18:07:18 -0000
@@ -24,82 +24,100 @@ function img_assist_help($section) {
 }
 
 /**
+ * Implementation of hook_theme().
+ */
+function img_assist_theme() {
+  return array(
+    'img_assist_textarea_link' => array(
+      'arguments' => array('element' => NULL, 'link' => NULL),
+    ),
+    'img_assist_inline' => array(
+      'arguments' => array('node' => NULL, 'size' => NULL, 'attributes' => NULL),
+    ),
+    'img_assist_filter' => array(
+      'arguments' => array('text' => NULL),
+    ),
+    'img_assist_popup' => array(
+      'arguments' => array('content' => NULL, 'attributes' => NULL),
+    ),
+    'img_assist_page' => array(
+      'arguments' => array('content' => NULL, 'attributes' => NULL),
+    ),
+    'img_assist_legacy' => array(),
+  );
+}
+
+/**
  * Implementation of hook_menu().
  */
-function img_assist_menu($may_cache) {
-  $items = array();
-  
-  $access = user_access('access content');
-  
-  if ($may_cache) {
-    $items[] = array('path' => 'img_assist/cache/clear',
-      'title' => t('Empty cache'),
-      'callback' => 'img_assist_cache_clear',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'img_assist/load',
-      'title' => t('Image Assist'),
-      'callback' => 'img_assist_loader',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    // Page callbacks called internally by img_assist/load.
-    $items[] = array(
-      'path' => 'img_assist/header',
-      'title' => t('Image Assist Header'),
-      'callback' => 'img_assist_header',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'img_assist/thumbs',
-      'title' => t('Image Assist Thumbnails'),
-      'callback' => 'img_assist_thumbs',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'img_assist/upload',
-      'title' => t('Image Assist Upload'),
-      'callback' => 'img_assist_upload',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'img_assist/properties',
-      'title' => t('Image Assist Properties'),
-      'callback' => 'img_assist_properties',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    // Popup images page.
-    $items[] = array(
-      'path' => 'img_assist/popup',
-      'title' => t('Popup Image'),
-      'callback' => 'img_assist_popup',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    // Insert callback (only for inserting HTML, not filter tag).
-    $items[] = array(
-      'path' => 'img_assist/insert_html',
-      'title' => t('Insert Callback'),
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-      'callback' => 'img_assist_insert_html',
-    );
-    $items[] = array(
-      'path' => 'admin/settings/img_assist',
-      'title' => t('Image assist'),
-      'description' => t('Change settings for the Image assist module.'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => 'img_assist_admin_settings',
-      'access' => user_access('administer site configuration'),
-    );
-  }
-  else {
+function img_assist_menu() {
+  $items['img_assist/cache/clear'] = array(
+    'title' => 'Empty cache',
+    'page callback' => 'img_assist_cache_clear',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  $items['img_assist/load'] = array(
+    'title' => 'Image assist',
+    'page callback' => 'img_assist_loader',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  // Page callbacks called internally by img_assist/load.
+  $items['img_assist/header'] = array(
+    'title' => 'Image assist header',
+    'page callback' => 'img_assist_header',
+    'page arguments' => array(2),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  $items['img_assist/thumbs'] = array(
+    'title' => 'Image assist thumbnails',
+    'page callback' => 'img_assist_thumbs',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  $items['img_assist/upload'] = array(
+    'title' => 'Image assist upload',
+    'page callback' => 'img_assist_upload',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  $items['img_assist/properties'] = array(
+    'title' => 'Image assist properties',
+    'page callback' => 'img_assist_properties',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  // Popup images page.
+  $items['img_assist/popup'] = array(
+    'title' => 'Popup image',
+    'page callback' => 'img_assist_popup',
+    'access callback' => 'user_access',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  // Insert callback (only for inserting HTML, not filter tag).
+  $items['img_assist/insert_html'] = array(
+    'title' => 'Insert callback',
+    'page callback' => 'img_assist_insert_html',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  $items['admin/settings/img_assist'] = array(
+    'title' => 'Image assist',
+    'description' => 'Change settings for the Image assist module.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('img_assist_admin_settings'),
+    'access arguments' => array('administer site configuration'),
+  );
+  return $items;
+}
+
+/**
+ * Implementation of hook_init().
+ */
+function img_assist_init() {
     $path = drupal_get_path('module', 'img_assist');
     if (variable_get('img_assist_page_styling', 'yes') == 'yes') {
       drupal_add_css($path .'/img_assist.css');
@@ -107,8 +125,6 @@ function img_assist_menu($may_cache) {
     // Assign base_path to insert in image source by javascript.
     drupal_add_js('var BASE_URL = "'. base_path() .'";', 'inline');
     drupal_add_js($path .'/img_assist.js');
-  }
-  return $items;
 }
 
 /**
@@ -123,7 +139,7 @@ function img_assist_perm() {
  */
 function img_assist_elements() {
   $type['textarea'] = array(
-    '#process' => array('img_assist_textarea' => array()),
+    '#process' => 'img_assist_textarea'
   );
   return $type;
 }
@@ -372,9 +388,9 @@ function img_assist_admin_settings() {
 /**
  * Validate Image Assist settings.
  */
-function img_assist_admin_settings_validate($form_id, $form_values) {
+function img_assist_admin_settings_validate($form, &$form_state) {
   // img_assist_max_size must contain a value for width and height.
-  if (!preg_match('/\d+x\d+/', $form_values['img_assist_max_size'])) {
+  if (!preg_match('/\d+x\d+/', $form_state['values']['img_assist_max_size'])) {
     form_set_error('img_assist_max_size', t('Allowed maximum inline image size has to indicate width and height, for example %example.', array('%example' => '200x300')));
   }
 }
@@ -428,15 +444,11 @@ function img_assist_nodeapi(&$node, $op,
         // This is to make sure all image paths are up to date.
         cache_clear_all(NULL, 'cache_filter');
       }
-      // break is intentionally left out.
+      // Break is intentionally left out.
+
     case 'insert':
       // Update the image map.
       img_assist_map_save($node);
-      // Store nid globally if this node is an image uploaded with img_assist.
-      if ($node->type == 'image' && arg(0) == 'img_assist') {
-        global $_img_assist_saved_image;
-        $_img_assist_saved_image = $node->nid;
-      }
       break;
     
     case 'delete':
@@ -504,14 +516,13 @@ function img_assist_loader() {
   echo $output;
 }
 
-function img_assist_header() {
+function img_assist_header($mode) {
   // Mode may be 'uploading', 'properties' or 'browse'.
-  $mode = arg(2);
   $output = drupal_get_form('img_assist_header_form', $mode);
   echo theme('img_assist_page', $output, array('id' => 'img_assist_header', 'onload' => 'parent.initHeader();', 'class' => 'img_assist'));
 }
 
-function img_assist_header_form($mode) {
+function img_assist_header_form(&$form_state, $mode) {
   global $user;
   
   // Upload image.
@@ -560,7 +571,7 @@ function img_assist_header_form($mode) {
     if (module_exists('taxonomy')) {
       $vocabs = (array)variable_get('img_assist_vocabs', array());
       foreach ($vocabs as $vid) {
-        $vocab = taxonomy_get_vocabulary($vid);
+        $vocab = taxonomy_vocabulary_load($vid);
         $terms = taxonomy_get_tree($vid);
         if ($terms) {
           foreach ($terms as $key => $value) {
@@ -617,7 +628,8 @@ function img_assist_header_form($mode) {
  */
 function img_assist_upload() {
   global $user;
-  
+  module_load_include('inc', 'node', 'node.pages');
+
   if (module_exists('image') && user_access('create images')) {
     // On other img_assist_pages I've added the javascript using the body onload
     // attribute, but for this page will also need the collapse functions and
@@ -650,29 +662,28 @@ function img_assist_upload() {
 /**
  * Implementation of hook_form_alter().
  * 
- * Add a submit callback to image_node_form which alters the redirect.
+ * Reroute submit button callback to our own handler to be able to redirect
+ * the user after saving the node.
+ *
+ * @see img_assist_node_form_submit()
  */
-function img_assist_form_alter($form_id, &$form) {
-  switch ($form_id) {
-    case 'image_node_form':
-      if (arg(0) != 'img_assist') {
-        break;
-      }
-      if (!is_array($form['#submit'])) $form['#submit'] = array();
-      $form['#submit'] += array('img_assist_node_form_submit' => array($form));
-      break;
+function img_assist_form_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == 'image_node_form' && arg(0) == 'img_assist') {
+    $form['buttons']['submit']['#submit'] = array('img_assist_node_form_submit');
   }
 }
 
 /**
  * Submit callback for image_node_form.
- * 
- * Change the redirect from node/$nid to img_assist/properties/$nid.
  */
-function img_assist_node_form_submit($form_id, $form_values) {
-  // Get the nid of the newly created image (caught by img_assist_nodeapi).
-  global $_img_assist_saved_image;
-  drupal_goto('img_assist/properties/'. $_img_assist_saved_image);
+function img_assist_node_form_submit($form, &$form_state) {
+  // Execute regular node submit handler.
+  node_form_submit($form, $form_state);
+
+  if ($form_state['nid']) {
+    // Send to different url.
+    $form_state['redirect'] = 'img_assist/properties/'. $form_state['nid'];
+  }
 }
 
 /**
@@ -726,14 +737,16 @@ function img_assist_thumbs() {
       }
     }
     
+    $num_rows = FALSE;
     $show_amount = variable_get('img_assist_preview_count', 10);
     $result = pager_query($query, $show_amount, $element = 0, $count_query = NULL, $params);
     while ($row = db_fetch_object($result)) {
       $node    = node_load(array('nid' => $row->nid));
       $image   = img_assist_display($node, IMAGE_THUMBNAIL);
-      $output .= l($image, 'img_assist/properties/'. $node->nid, $attributes = array(), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = TRUE) ."\n";
+      $output .= l($image, 'img_assist/properties/'. $node->nid, array('html' => TRUE)) ."\n";
+      $num_rows = TRUE;
     }
-    if (!db_num_rows($result)) {
+    if (!$num_rows) {
       $output .= t('No images were found. Please upload a new image or browse images by a different category.');
     }
     
@@ -755,7 +768,7 @@ function img_assist_properties() {
   $update = (arg(3)) ? 1 : 0;
   
   if (is_numeric($nid)) {
-    $node = node_load(array('nid' => $nid));
+    $node = node_load($nid);
     
     // Setup a default caption & description.
     $node->title = str_replace("\r", ' ', strip_tags($node->title));
@@ -775,7 +788,7 @@ function img_assist_properties() {
 /**
  * Construct the image properties form.
  */
-function img_assist_properties_form($node, $update) {
+function img_assist_properties_form($form_state, $node, $update) {
   $image_info = image_get_info(file_create_path($node->images[IMAGE_ORIGINAL]));
   
   // Select (or generate) a preview image.
@@ -1021,8 +1034,8 @@ function img_assist_properties_form($nod
   return $form;
 }
 
-function img_assist_properties_form_validate($form_id, $form_values) {
-  $html = img_assist_render_image($form_values);
+function img_assist_properties_form_validate($form, &$form_state) {
+  $html = img_assist_render_image($form_state['values']);
   img_assist_set_htmlcode($html);
   drupal_goto('img_assist/insert_html');
 }
@@ -1166,16 +1179,7 @@ function img_assist_display(&$node, $siz
     _img_assist_build_derivatives($node, $size);
   }
   
-  if (empty($node->images[$label])) {
-    return;
-  }
-  
-  // Get actual size ($size is just a bounding box).
-  $info = image_get_info(file_create_path($node->images[$label]));
-  $attributes['class']  = 'image image-'. $label . (isset($attributes['class']) ? ' '. $attributes['class'] : '');
-  $attributes['width']  = $info['width'];
-  $attributes['height'] = $info['height'];
-  return theme('image_display', $node, $label, file_create_url($node->images[$label]), $attributes);
+  return image_display($node, $label);
 }
 
 /**
@@ -1236,14 +1240,15 @@ function _img_assist_build_derivatives(&
 }
 
 function _img_assist_remove($node, $size) {
-  $result = db_query("SELECT * FROM {files} WHERE nid = %d AND filename = '%s'", $node->nid, $size['key']);
+  $result = db_query("SELECT * FROM {files} f INNER JOIN {image} i WHERE f.fid = i.fid AND i.nid = %d AND f.filename = '%s'", $node->nid, $size['key']);
   while ($file = db_fetch_object($result)) {
     // Never delete original image.
     if ($file->filepath != $node->images[IMAGE_ORIGINAL]) {
       // Delete image file.
       file_delete(file_create_path($file->filepath));
       // Delete file reference in database.
-      db_query("DELETE FROM {files} WHERE nid = %d AND filename = '%s'", $node->nid, $size['key']);
+      db_query("DELETE FROM {files} WHERE fid = %d AND filename = '%s'", $file->fid, $size['key']);
+      db_query("DELETE FROM {image} WHERE nid = %d AND fid = '%d'", $node->nid, $file->fid);
     }
   }
 }
@@ -1471,7 +1476,7 @@ function img_assist_map_delete($node) {
  */
 function img_assist_map_load($nid) {
   $imagemap = array();
-  $result = db_query('SELECT * FROM {files} f INNER JOIN {img_assist_map} i ON f.nid = i.iid WHERE f.nid = %d', $nid);
+  $result = db_query('SELECT * FROM {image} f INNER JOIN {img_assist_map} i ON f.nid = i.iid WHERE f.nid = %d', $nid);
   while ($data = db_fetch_object($result)) {
     $imagemap[] = $data->nid;
   }
@@ -1605,12 +1610,13 @@ function theme_img_assist_textarea_link(
   else {
     $link = t('Add image');
   }
-  $output .= l($link, 'img_assist/load/textarea', array(
-    'class' => 'img_assist-link',
-    'id' => 'img_assist-link-'. $element['#id'],
-    'title' => t('Click here to add images'),
+  $attribs = array(
+    'class'   => 'img_assist-link',
+    'id'      => 'img_assist-link-'. $element['#id'],
+    'title'   => t('Click here to add images'),
     'onclick' => 'window.open(this.href, \'img_assist_link\', \'width=600,height=350,scrollbars=yes,status=yes,resizable=yes,toolbar=no,menubar=no\'); return false;',
-  ), 'textarea='. $element['#name'], NULL, FALSE, TRUE);
+  );
+  $output .= l($link, 'img_assist/load/textarea', array('attributes' => $attribs, 'query' => 'textarea='. $element['#name'], 'html' => TRUE));
   $output .= '</div>';
   return $output;
 }
@@ -1644,7 +1650,7 @@ function theme_img_assist_inline($node, 
   }
   
   if ($link == 'node') {
-    $output .= l($img_tag, 'node/'. $node->nid, array(), NULL, NULL, FALSE, TRUE);
+    $output .= l($img_tag, 'node/'. $node->nid, array('html' => TRUE));
   }
   elseif ($link == 'popup') {
     $popup_size = variable_get('img_assist_popup_label', IMAGE_PREVIEW);
@@ -1652,10 +1658,10 @@ function theme_img_assist_inline($node, 
     $width      = $info['width'];
     $height     = $info['height'];
     $popup_url  = file_create_url($node->images[variable_get('img_assist_popup_label', IMAGE_PREVIEW)]);
-    $output .= l($img_tag, $popup_url, array('onclick' => "launch_popup({$node->nid}, {$width}, {$height}); return false;", 'target' => '_blank'), NULL, NULL, FALSE, TRUE);
+    $output .= l($img_tag, $popup_url, array('attributes' => array('onclick' => "launch_popup({$node->nid}, {$width}, {$height}); return false;", 'target' => '_blank'), 'html' =>TRUE));
   }
   elseif ($link == 'url') {
-    $output .= l($img_tag, $url, array(), NULL, NULL, FALSE, TRUE);
+    $output .= l($img_tag, $url, array('html' => TRUE));
   }
   else {
     $output .= $img_tag;
@@ -1694,7 +1700,7 @@ function theme_img_assist_popup($content
   $output .= "</head>\n";
   $output .= '<body'. drupal_attributes($attributes) .">\n";
   $output .= "<!-- begin content -->\n";
-  $output .= l($content, '', array('onclick' => 'javascript:window.close();'), NULL, NULL, FALSE, TRUE);
+  $output .= l($content, '', array('attributes' => array('onclick' => 'javascript:window.close();'), 'html' => TRUE));
   $output .= "<!-- end content -->\n";
   $output .= '</body>';
   $output .= '</html>';
@@ -1702,6 +1708,7 @@ function theme_img_assist_popup($content
 }
 
 function theme_img_assist_page($content, $attributes = NULL) {
+  $title = drupal_get_title();
   $output .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
   $output .= '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'."\n";
   $output .= "<head>\n";
@@ -1793,7 +1800,7 @@ function img_assist_load_images($tid = N
   }
   
   if (!$image) {
-    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, r.teaser, f.* FROM {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid INNER JOIN {files} f ON n.nid = f.nid AND n.type = 'image' ". $where ." ORDER BY n.changed DESC"));
+    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, r.teaser, f.* FROM {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid INNER JOIN {image} f ON n.nid = f.nid AND n.type = 'image' ". $where ." ORDER BY n.changed DESC"));
     while ($node = db_fetch_object($result)) {
       $node->filepath = file_create_path($node->filepath);
       $dim = getimagesize($node->filepath, $info);
@@ -1816,7 +1823,7 @@ function img_assist_load_images($tid = N
     // Note: If we didn't use "LIKE 'image/%%'" here we could load other files.
     // Might be interesting to expand on this someday.
     if ($image) {
-      $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, r.teaser, f.* FROM {files} f, {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid WHERE f.nid = n.nid AND f.filemime LIKE 'image/%%' AND n.nid NOT IN (". implode(array_keys($image), ', ') .") ". $where ." ORDER BY n.changed DESC"));
+      $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, r.teaser, f.* FROM {image} f, {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid WHERE f.nid = n.nid AND f.filemime LIKE 'image/%%' AND n.nid NOT IN (". implode(array_keys($image), ', ') .") ". $where ." ORDER BY n.changed DESC"));
       while ($node = db_fetch_object($result)) {
         $node->filepath = file_create_path($node->filepath);
         $dim = getimagesize($node->filepath, $info);
@@ -1847,7 +1854,7 @@ function img_assist_load_images($tid = N
  * Load an image from the database.
  */
 function img_assist_load_image($id, $derivatives = TRUE) {
-  $node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, f.* FROM {files} f, {node} n WHERE f.nid = n.nid AND f.fid = %d'), $id));
+  $node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, f.* FROM {image} f, {node} n WHERE f.nid = n.nid AND f.fid = %d'), $id));
   $node->filepath = file_create_path($node->filepath);
   
   if (!$derivatives) {
@@ -1862,7 +1869,7 @@ function img_assist_load_image($id, $der
       foreach (image_get_sizes() as $size) {
         if ($size['label'] == $node->filename) {
           $image_module_image = TRUE;
-          $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, r.teaser, f.* FROM {files} f, {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid WHERE f.nid = n.nid AND n.nid = %d'), $node->nid);
+          $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, r.teaser, f.* FROM {image} f, {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid WHERE f.nid = n.nid AND n.nid = %d'), $node->nid);
           while ($node = db_fetch_object($result)) {
             $node->filepath = file_create_path($node->filepath);
             $dim = getimagesize($node->filepath, $info);
@@ -1895,7 +1902,7 @@ function img_assist_load_image($id, $der
 function _img_assist_get_thumbnail(&$image) {
   static $thumbs = array();
   if ($thumbs[$image->nid] === NULL) {
-    $thumbpath = file_create_path(db_result(db_query("SELECT filepath FROM {files} WHERE nid = %d AND filename = '%s'", $image->nid, IMAGE_THUMBNAIL)));
+    $thumbpath = file_create_path(db_result(db_query("SELECT filepath FROM {image} WHERE nid = %d AND filename = '%s'", $image->nid, IMAGE_THUMBNAIL)));
     // In old versions of image.module thumbs were named 'thumb_filename.ext'.
     if (!file_exists($thumbpath)) {
       $pos = strrpos($image->filepath, '/') + 1;
