Index: img-assist-browser-thumbnail.tpl.php
===================================================================
RCS file: img-assist-browser-thumbnail.tpl.php
diff -N img-assist-browser-thumbnail.tpl.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ img-assist-browser-thumbnail.tpl.php	18 Jul 2008 01:44:13 -0000
@@ -0,0 +1,26 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * This template outputs individual thumbnails in Image assist's image browser.
+ * 
+ * Available variables:
+ *   view object $view
+ *   array $options
+ *   stdClass object $row
+ *   string $zebra
+ *   int $id
+ *   string $directory
+ *   boolean $is_admin
+ *   boolean $is_front
+ *   boolean $logged_in
+ *   boolean $db_is_active
+ *   stdClass object $user
+ * 
+ *   string $thumbnail
+ *   string $path
+ *   string $title
+ */
+?>
+<a href="<?php print $path; ?>" title="<?php print $title; ?>"><?php print $thumbnail; ?></a>
Index: img_assist.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/img_assist.info,v
retrieving revision 1.4
diff -u -p -r1.4 img_assist.info
--- img_assist.info	6 Apr 2008 17:32:33 -0000	1.4
+++ img_assist.info	18 Jul 2008 00:37:29 -0000
@@ -2,5 +2,6 @@
 name = Image assist
 description = This module allows users to upload and insert inline images into posts. It automatically generates an Add image link under the textarea fields of your choice.
 dependencies[] = image
+dependencies[] = views
 package = Image
 core = 6.x
Index: img_assist.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/img_assist.install,v
retrieving revision 1.6
diff -u -p -r1.6 img_assist.install
--- img_assist.install	6 Apr 2008 17:32:33 -0000	1.6
+++ img_assist.install	18 Jul 2008 02:21:45 -0000
@@ -62,4 +62,12 @@ function img_assist_uninstall() {
   variable_del('img_assist_all');
 }
 
+/**
+ * Remove obsolete img_assist_preview_count variable.
+ */
+function img_assist_update_6200() {
+  $ret = array();
+  variable_del('img_assist_preview_count');
+  return $ret;
+}
 
Index: img_assist.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/img_assist.js,v
retrieving revision 1.6
diff -u -p -r1.6 img_assist.js
--- img_assist.js	6 Apr 2008 16:43:18 -0000	1.6
+++ img_assist.js	18 Jul 2008 00:37:29 -0000
@@ -11,7 +11,7 @@ function onClickUpload() {
 }
 
 function onClickStartOver() {
-  frames['img_assist_main'].window.location.href = BASE_URL + 'index.php?q=img_assist/thumbs/myimages';
+  frames['img_assist_main'].window.location.href = BASE_URL + 'index.php?q=img_assist/thumbs/img_assist_browser';
 }
 
 function updateCaption() {
Index: img_assist.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/img_assist.module,v
retrieving revision 1.75.2.4
diff -u -p -r1.75.2.4 img_assist.module
--- img_assist.module	18 Jul 2008 00:35:21 -0000	1.75.2.4
+++ img_assist.module	18 Jul 2008 03:43:36 -0000
@@ -242,6 +242,33 @@ function img_assist_admin_settings() {
     '#options' => array('icon' => t('Show icon'), 'text' => t('Show text link'), 'none' => t('Do not show a link')),
     '#description' => t('Choose what to show under the textareas for which Image assist is enabled.'),
   );
+
+  // Prepare select options for image browser views.
+  $options = array();
+  views_include('admin');
+  $views = views_get_all_views();
+  $base_tables = views_fetch_base_tables();
+  foreach ($views as $view) {
+    // Exclude malformed views (@see views/admin.inc).
+    if (!empty($view->disabled) || empty($view->display)) {
+      continue;
+    }
+    // Exclude views considered unusable for IA.
+    if (!($view->base_table == 'node' || $view->base_table == 'files')) {
+      continue;
+    }
+    $options[$view->name] = $view->get_title();
+  }
+  $form['access']['img_assist_views'] = array(
+    '#type' => 'select',
+    '#multiple' => TRUE,
+    '#title' => t('Image browser views'),
+    '#default_value' => variable_get('img_assist_views', drupal_map_assoc(array('img_assist_browser'))),
+    '#options' => $options,
+    '#required' => TRUE,
+    '#description' => t('Select the views to use for selecting images.'),
+  );
+
   if (module_exists('taxonomy')) {
     $vocs = array(0 => '<'. t('none') .'>');
     foreach (taxonomy_get_vocabularies() as $vid => $voc) {
@@ -266,14 +293,6 @@ function img_assist_admin_settings() {
     '#collapsible' => TRUE,
     '#collapsed' => TRUE,
   );
-  $form['image']['img_assist_preview_count'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Maximum number of thumbnails per page'),
-    '#default_value' => variable_get('img_assist_preview_count', 8),
-    '#size' => 6,
-    '#maxlength' => 6,
-    '#description' => t('Enter the number of images to display in the thumbnail browser. If there are more images, next and previous links will be displayed.'),
-  );
   $form['image']['img_assist_max_size'] = array(
     '#type' => 'textfield',
     '#title' => t('Maximum inline image size allowed'),
@@ -558,45 +577,54 @@ function img_assist_header_form(&$form_s
     $form[] = array('#value' => '<div id="header-browse">');
     $form[] = array('#value' => '<strong>'. t('Browse Images: ') .'</strong>');
     
-    // Get my images and count.
-    $result = db_query("SELECT COUNT(n.nid) FROM {node} n WHERE n.type='image' AND n.uid= %d", $user->uid);
-    $count  = $result ? db_result($result, 0) : 0;
-    $options['myimages'] = t('My Images') ." ($count)";
-    
-    // Get all images and count.
-    // Count all published images and the user's unpublished images.
-    if (user_access('access all images')) {
-      $result = db_query("SELECT COUNT(n.nid) FROM {node} n WHERE n.type='image' AND (n.uid = %d OR n.status = 1)", $user->uid);
-      $count = $result ? db_result($result, 0) : 0;
-      $options['allimages'] = t('All Images') ." ($count)";
-    }
-    // Get category list.
-    if (module_exists('taxonomy')) {
-      $vocabs = (array)variable_get('img_assist_vocabs', array());
-      foreach ($vocabs as $vid) {
-        $vocab = taxonomy_vocabulary_load($vid);
-        $terms = taxonomy_get_tree($vid);
-        if ($terms) {
-          foreach ($terms as $key => $value) {
-            $tid = $value->tid;
-            $name = $value->name;
-            
-            // For this term, count all published images and the user's
-            // unpublished images.
-            if (user_access('access all images')) {
-              $result = db_query("SELECT COUNT(n.nid) FROM {node} n, {term_node} t WHERE t.nid=n.nid AND n.type='image' AND t.tid = %d AND (n.uid = %d OR n.status = 1)", $tid, $user->uid);
-            }
-            // For this term, count only the user's images.
-            else {
-              $result = db_query("SELECT COUNT(n.nid) FROM {node} n, {term_node} t WHERE t.nid=n.nid AND n.type='image' AND t.tid = %d AND n.uid = %d", $tid, $user->uid);
-            }
-            $count = $result ? db_result($result, 0) : 0;
-            if ($count) {
-              $options[$vocab->name][$tid] = $name ." ($count)";
+    $views = variable_get('img_assist_views', drupal_map_assoc(array('img_assist_browser')));
+    foreach ($views as $view_name => $view_title) {
+      if ($view_name == 'img_assist_browser') {
+        // @todo execute() performs a full query, we just need the count here.
+        // Get my images and count.
+        $myimages = views_get_view('img_assist_browser');
+        $myimages->set_arguments(array($user->uid));
+        $myimages->execute();
+        $options['img_assist_browser/' . $user->uid] = t('My Images') ." ($myimages->total_rows)";
+        
+        // Get all images and count.
+        if (user_access('access all images')) {
+          $allimages = views_get_view('img_assist_browser');
+          $allimages->set_arguments(array('all'));
+          $allimages->execute();
+          $options['img_assist_browser/all'] = t('All Images') ." ($allimages->total_rows)";
+        }
+        // Get category list.
+        if (module_exists('taxonomy')) {
+          $vocabs = (array)variable_get('img_assist_vocabs', array());
+          
+          // Get all images or only user's images depending on permissions.
+          $user_arg = (user_access('access all images')) ? 'all' : $user->uid;
+          
+          $term_images = array();
+          foreach ($vocabs as $vid) {
+            $vocab = taxonomy_vocabulary_load($vid);
+            $terms = taxonomy_get_tree($vid);
+            if ($terms) {
+              foreach ($terms as $key => $value) {
+                $tid = $value->tid;
+                $name = $value->name;
+                
+                // Get term images and count.
+                $term_images[$tid] = views_get_view('img_assist_browser');
+                $term_images[$tid]->set_arguments(array($user_arg, $tid));
+                $term_images[$tid]->execute();
+                $options[$vocab->name]["img_assist_browser/$user_arg/" . $tid] = $name ." ({$term_images[$tid]->total_rows})";
+              }
             }
           }
         }
       }
+      else {
+        $view = views_get_view($view_name);
+        $view->execute();
+        $options[$view_name] = $view->get_title() ." ($view->total_rows)";
+      }
     }
     $form['browse'] = array(
       '#type' => 'select',
@@ -692,72 +720,48 @@ function img_assist_node_form_submit($fo
 /**
  * Load the thumbnail display pane.
  *
- * Grabs all images from image.module and loads the thumbnails.
+ * Loads a View displaying the thumbnails. The view name is picked from the 
+ * third path argument and any remaining arguments are used as arguments to the
+ * view.
+ * Module developers may add options to img_assist_header_form using 
+ * hook_form_alter() if they wish to use custom views and/or arguments.
  */
 function img_assist_thumbs() {
   global $user;
   
-  if (module_exists('image')) {
-    $browse = arg(2);
-    if ($browse == 'myimages') {
-      $myimagesonly = TRUE;
-      $tid = 0;
-    }
-    elseif ($browse == 'allimages') {
-      $myimagesonly = FALSE;
-      $tid = 0;
-    }
-    else {
-      $myimagesonly = FALSE;
-      $tid = $browse;
-    }
+  if (module_exists('image') && module_exists('views')) {
+    $view_name = ((arg(2) != '') ? arg(2) : 'img_assist_browser');
     
-    $output = '<div align="center">';
+    // Get view arguments from path.
+    $args = explode('/', $_GET['q']);
+    $args = array_slice($args, 3);
     
-    // Show by term id.
-    if ($tid) {
-      // For this term, show all published images and the user's unpublished images.
-      if (user_access('access all images')) {
-        $query = "SELECT n.nid FROM {node} n, {term_node} t WHERE t.nid=n.nid AND n.type='image' AND t.tid = %d AND (n.uid = %d OR n.status = 1) ORDER BY n.sticky DESC, n.created DESC";
-        $params = array($tid, $user->uid);
+    // Check sanity and permissions for the 'img_assist_browser' view.
+    if ($view_name == 'img_assist_browser') {
+      if (empty($args[0]) || !user_access('access all images')) {
+        $args[0] = $user->uid;
       }
-      // For this term, show only the user's images.
-      else {
-        $query = "SELECT n.nid FROM {node} n, {term_node} t WHERE t.nid=n.nid AND n.type='image' AND t.tid = %d AND n.uid = %d ORDER BY n.sticky DESC, n.created DESC";
-        $params = array($tid, $user->uid);
+      if (isset($args[1])) {
+        $args[1] = (int) $args[1];
       }
     }
-    else {
-      // Show all published images and the user's unpublished images.
-      if (user_access('access all images') && (!$myimagesonly)) {
-        $query = "SELECT n.nid FROM {node} n WHERE n.type='image' AND (n.uid = %d OR n.status = 1) ORDER BY n.sticky DESC, n.created DESC";
-        $params = array($user->uid);
+    
+    $view = views_get_view($view_name);
+    if ($view) {
+      $view_output = $view->execute_display(NULL, $args);
+      if (empty($view_output)) {
+        $output = t('No images were found. Please upload a new image or browse images by a different category.');
       }
-      // Show only the user's images (default).
       else {
-        $query = "SELECT n.nid FROM {node} n WHERE n.type='image' AND n.uid= %d ORDER BY n.sticky DESC, n.created DESC";
-        $params = array($user->uid);
+        $output = $view_output;
       }
     }
-    
-    $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, array('html' => TRUE)) ."\n";
-      $num_rows = TRUE;
-    }
-    if (!$num_rows) {
-      $output .= t('No images were found. Please upload a new image or browse images by a different category.');
+    else {
+      $output = t('Error: The specified view was not found.');
     }
-    
-    $output .= theme('pager', NULL, $show_amount);
-    $output .= '</div>';
   }
   else {
-    $output = t('The image module must be enabled to use Image assist.');
+    $output = t('The Image and Views modules must be enabled to use Image assist.');
   }
   echo theme('img_assist_page', $output, array('id' => 'img_assist_thumbs', 'onload' => 'parent.initThumbs();', 'class' => 'img_assist'));
 }
Index: img_assist.views.inc
===================================================================
RCS file: img_assist.views.inc
diff -N img_assist.views.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ img_assist.views.inc	18 Jul 2008 00:48:47 -0000
@@ -0,0 +1,43 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Views integration for Image assist.
+ */
+
+
+/**
+ * Implementation of hook_views_plugins().
+ * 
+ * Define a row style plugin to display an Image assist thumbnail.
+ */
+function img_assist_views_plugins() {
+  return array(
+    // Define a row style plugin for the thumbnail browser.
+    'row' => array(
+      'img_assist_thumbnail' => array(
+        'title' => t('Image Assist thumbnail'),
+        // 'help' => t(''),
+        'handler' => 'views_plugin_row',
+        'theme' => 'img_assist_browser_thumbnail',
+        'uses options' => FALSE,
+        'type' => 'normal',
+      ),
+    ),
+  );
+}
+
+/**
+ * Setup variables for the browser thumbnail template.
+ * 
+ * @param array $vars
+ */
+function template_preprocess_img_assist_browser_thumbnail(&$vars) {
+  $node = node_load($vars['row']->nid);
+
+  $vars['thumbnail'] = img_assist_display($node, IMAGE_THUMBNAIL);
+  $vars['path'] = url('img_assist/properties/'. $node->nid); 
+  $vars['title'] = $node->title; 
+}
+
Index: img_assist.views_default.inc
===================================================================
RCS file: img_assist.views_default.inc
diff -N img_assist.views_default.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ img_assist.views_default.inc	18 Jul 2008 03:44:09 -0000
@@ -0,0 +1,174 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Default views for Image assist module.
+ */
+
+
+/**
+ * Implementation of hook_views_default_views().
+ */
+function img_assist_views_default_views() {
+  $view = new view;
+  $view->name = 'img_assist_browser';
+  $view->description = 'This view is used by Image assist for its thumbnail browser.';
+  $view->tag = '';
+  $view->view_php = '';
+  $view->base_table = 'node';
+  $view->is_cacheable = FALSE;
+  $view->api_version = 2;
+  $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
+  $view->hide_admin_links = TRUE;
+  $handler = $view->new_display('default', 'Defaults', 'default');
+  $handler->override_option('title', 'Image assist thumbnail browser');
+  $handler->override_option('fields', array(
+    'title' => array(
+      'label' => '',
+      'link_to_node' => 1,
+      'exclude' => 0,
+      'id' => 'title',
+      'table' => 'node',
+      'field' => 'title',
+      'relationship' => 'none',
+    ),
+  ));
+  $handler->override_option('sorts', array(
+    'created' => array(
+      'order' => 'DESC',
+      'granularity' => 'second',
+      'id' => 'created',
+      'table' => 'node',
+      'field' => 'created',
+      'relationship' => 'none',
+    ),
+  ));
+  $handler->override_option('arguments', array(
+    'uid' => array(
+      'default_action' => 'ignore',
+      'style_plugin' => 'default_summary',
+      'style_options' => array(),
+      'wildcard' => 'all',
+      'wildcard_substitution' => 'All',
+      'title' => '',
+      'default_argument_type' => 'fixed',
+      'default_argument' => '',
+      'validate_type' => 'none',
+      'validate_fail' => 'not found',
+      'break_phrase' => 0,
+      'not' => 0,
+      'id' => 'uid',
+      'table' => 'users',
+      'field' => 'uid',
+      'relationship' => 'none',
+      'default_options_div_prefix' => '',
+      'default_argument_user' => 0,
+      'default_argument_fixed' => '',
+      'default_argument_php' => '',
+      'validate_argument_node_type' => array(
+        'image' => 0,
+        'page' => 0,
+        'story' => 0,
+      ),
+      'validate_argument_node_access' => 0,
+      'validate_argument_nid_type' => 'nid',
+      'validate_argument_vocabulary' => array(
+        '4' => 0,
+        '2' => 0,
+        '1' => 0,
+        '3' => 0,
+      ),
+      'validate_argument_type' => 'tid',
+      'validate_argument_php' => '',
+    ),
+    'tid' => array(
+      'default_action' => 'ignore',
+      'style_plugin' => 'default_summary',
+      'style_options' => array(),
+      'wildcard' => 'all',
+      'wildcard_substitution' => 'All',
+      'title' => '',
+      'default_argument_type' => 'fixed',
+      'default_argument' => '',
+      'validate_type' => 'numeric',
+      'validate_fail' => 'ignore',
+      'break_phrase' => 0,
+      'add_table' => 0,
+      'require_value' => 0,
+      'reduce_duplicates' => 0,
+      'set_breadcrumb' => 0,
+      'id' => 'tid',
+      'table' => 'term_node',
+      'field' => 'tid',
+      'relationship' => 'none',
+      'default_options_div_prefix' => '',
+      'default_argument_user' => 0,
+      'default_argument_fixed' => '',
+      'default_argument_php' => '',
+      'validate_argument_node_type' => array(
+        'image' => 0,
+        'page' => 0,
+        'story' => 0,
+      ),
+      'validate_argument_node_access' => 0,
+      'validate_argument_nid_type' => 'nid',
+      'validate_argument_vocabulary' => array(
+        '4' => 0,
+        '2' => 0,
+        '1' => 0,
+        '3' => 0,
+      ),
+      'validate_argument_type' => 'tid',
+      'validate_argument_php' => '',
+    ),
+  ));
+  $handler->override_option('filters', array(
+    'type' => array(
+      'operator' => 'in',
+      'value' => array(
+        'image' => 'image',
+      ),
+      'group' => '0',
+      'exposed' => FALSE,
+      'expose' => array(
+        'operator' => FALSE,
+        'label' => '',
+      ),
+      'id' => 'type',
+      'table' => 'node',
+      'field' => 'type',
+      'relationship' => 'none',
+    ),
+    'status' => array(
+      'operator' => '=',
+      'value' => 1,
+      'group' => '0',
+      'exposed' => FALSE,
+      'expose' => array(
+        'operator' => FALSE,
+        'label' => '',
+      ),
+      'id' => 'status',
+      'table' => 'node',
+      'field' => 'status',
+      'relationship' => 'none',
+    ),
+  ));
+  $handler->override_option('access', array(
+    'type' => 'none',
+    'role' => array(),
+    'perm' => '',
+  ));
+  $handler->override_option('items_per_page', 9);
+  $handler->override_option('style_plugin', 'grid');
+  $handler->override_option('style_options', array(
+    'columns' => '3',
+    'alignment' => 'horizontal',
+  ));
+  $handler->override_option('row_plugin', 'img_assist_thumbnail');
+
+  $views[$view->name] = $view;
+
+  return $views;
+}
Index: img_assist_textarea.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/img_assist_textarea.js,v
retrieving revision 1.4
diff -u -p -r1.4 img_assist_textarea.js
--- img_assist_textarea.js	6 Apr 2008 16:43:18 -0000	1.4
+++ img_assist_textarea.js	18 Jul 2008 00:37:29 -0000
@@ -38,7 +38,7 @@ function initLoader() {
       }
     }
   }
-  frames['img_assist_main'].window.location.href = BASE_URL + 'index.php?q=img_assist/thumbs/myimages';
+  frames['img_assist_main'].window.location.href = BASE_URL + 'index.php?q=img_assist/thumbs/img_assist_browser';
 }
 
 function initProperties() {
Index: img_assist_tinymce.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/img_assist_tinymce.js,v
retrieving revision 1.3
diff -u -p -r1.3 img_assist_tinymce.js
--- img_assist_tinymce.js	6 Apr 2008 16:43:18 -0000	1.3
+++ img_assist_tinymce.js	18 Jul 2008 00:37:29 -0000
@@ -37,7 +37,7 @@ function initLoader() {
     frames['img_assist_main'].window.location.href = BASE_URL + 'index.php?q=img_assist/properties/' + nid + '/update';
   }
   else {
-    frames['img_assist_main'].window.location.href = BASE_URL + 'index.php?q=img_assist/thumbs/myimages';
+    frames['img_assist_main'].window.location.href = BASE_URL + 'index.php?q=img_assist/thumbs/img_assist_browser';
   }
 }
 
