Index: CHANGELOG.txt
===================================================================
RCS file: CHANGELOG.txt
diff -N CHANGELOG.txt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CHANGELOG.txt	28 Jan 2009 16:08:41 -0000
@@ -0,0 +1,11 @@
+// $Id: CHANGELOG.txt,v 1.1 2009/01/28 14:47:48 sun Exp $
+
+Path Image x.x-x.x, xxxx-xx-xx
+------------------------------
+
+
+Path Image 5.x-1.x, xxxx-xx-xx
+------------------------------
+#365480 by sun: Code clean-up and sync of branches.
+
+
Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/path_image/README.txt,v
retrieving revision 1.2.2.1
diff -u -p -r1.2.2.1 README.txt
--- README.txt	25 May 2007 11:07:14 -0000	1.2.2.1
+++ README.txt	28 Jan 2009 16:10:20 -0000
@@ -1,73 +1,75 @@
-1. Installation
----------------
-
-Copy the path_image.tar.gz file to your modules/ directory and unpack.
-This will create modules/path_image folder.
-
-Go to "Administer >> Site building >> Modules" and enable the "Path
-image" module.
-
-Go to "Administer >> User management >> Access control" and setup the
-roles to whom you wish to allow administration of the module.
-
-Go to "Administer >> Site configuration >> Path image" to setup the
-module (see below).
-
-Go to "Administer >> Site building >> Blocks" to setup the block on
-your page.
-
-2. Configuration
-----------------
-
-  Block title: [default 'Path image']
-    This sets the block title, blank out for no title.
-
-  Image repository: [default 'images']
-    The name of the folder in which the images are stored. Folder
-    is relative the drupal 'files' folder as specified on the File
-    system configuration page. No leading or trailing slashes.
-
-  Allowable file types in repository: [default 'gif, jpg, png']
-    A list of comma separated file extensions that will be scanned
-    for in the file repsoitory. 
-
-  Numeric parse halt: [default on]
-    When searching paths the default is not to "look beyond" a numeric
-    so for example node/243 would only search up to node/
-    However, you may have a path like taxonomy/term/24/53 and you want
-    at search all componets of the path inclusing the trailing numerics.
-    If that's the case, switch this setting off.
-
-  Default image for the homepage:
-    This allows you to select an image that is displayed on the / root
-    homepage of your site. A checkbox is provided to switch this feature
-    on or off.
-
-  Default image when no match made:
-    This allows you to select an image that is displayed when no path 
-    match is made. A checkbox is provided to switch this feature
-    on or off.	
-
-  Add a new path: Select an image:
-    Enter here a new path to associate with an image (no leading/trailing
-    slashes).
-
-  Alter a path:  Select an image:
-    An existing path can be amended here.
-
-3. Paths
---------
-
-A path match is done is reverse length order. Let's look at an example:
-
-  taxonomy/term/3
-  taxonomy
-
-Searching is currently terminating at the first full numeric component of
-the path, in this example, 3. The search would match any path that begins
-'taxonomy/term' . If no match was found then the next search would be for
-'taxonomy', etc.
-
-Switching "Numeric parse halt" off would scan for all components of the path
-including the numeric 3 in the example.
-
+// $Id$
+
+1. Installation
+---------------
+
+Copy the path_image.tar.gz file to your modules/ directory and unpack.
+This will create modules/path_image folder.
+
+Go to "Administer >> Site building >> Modules" and enable the "Path
+image" module.
+
+Go to "Administer >> User management >> Access control" and setup the
+roles to whom you wish to allow administration of the module.
+
+Go to "Administer >> Site configuration >> Path image" to setup the
+module (see below).
+
+Go to "Administer >> Site building >> Blocks" to setup the block on
+your page.
+
+2. Configuration
+----------------
+
+  Block title: [default 'Path image']
+    This sets the block title, blank out for no title.
+
+  Image repository: [default 'images']
+    The name of the folder in which the images are stored. Folder
+    is relative the drupal 'files' folder as specified on the File
+    system configuration page. No leading or trailing slashes.
+
+  Allowable file types in repository: [default 'gif, jpg, png']
+    A list of comma separated file extensions that will be scanned
+    for in the file repsoitory. 
+
+  Numeric parse halt: [default on]
+    When searching paths the default is not to "look beyond" a numeric
+    so for example node/243 would only search up to node/
+    However, you may have a path like taxonomy/term/24/53 and you want
+    at search all componets of the path inclusing the trailing numerics.
+    If that's the case, switch this setting off.
+
+  Default image for the homepage:
+    This allows you to select an image that is displayed on the / root
+    homepage of your site. A checkbox is provided to switch this feature
+    on or off.
+
+  Default image when no match made:
+    This allows you to select an image that is displayed when no path 
+    match is made. A checkbox is provided to switch this feature
+    on or off.	
+
+  Add a new path: Select an image:
+    Enter here a new path to associate with an image (no leading/trailing
+    slashes).
+
+  Alter a path:  Select an image:
+    An existing path can be amended here.
+
+3. Paths
+--------
+
+A path match is done is reverse length order. Let's look at an example:
+
+  taxonomy/term/3
+  taxonomy
+
+Searching is currently terminating at the first full numeric component of
+the path, in this example, 3. The search would match any path that begins
+'taxonomy/term' . If no match was found then the next search would be for
+'taxonomy', etc.
+
+Switching "Numeric parse halt" off would scan for all components of the path
+including the numeric 3 in the example.
+
Index: path_image.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/path_image/path_image.info,v
retrieving revision 1.1.2.2
diff -u -p -r1.1.2.2 path_image.info
--- path_image.info	25 May 2007 10:52:18 -0000	1.1.2.2
+++ path_image.info	28 Jan 2009 14:50:11 -0000
@@ -1,3 +1,3 @@
-;; $Id: path_image.info,v 1.1.2.2 2007/05/25 10:52:18 robrechtj Exp $
+; $Id: path_image.info,v 1.3 2009/01/28 14:47:48 sun Exp $
 name = Path image
 description = A module to provide an image file within a block dependant upon the Drupal path.
Index: path_image.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/path_image/path_image.install,v
retrieving revision 1.2.2.3
diff -u -p -r1.2.2.3 path_image.install
--- path_image.install	27 May 2007 10:07:15 -0000	1.2.2.3
+++ path_image.install	28 Jan 2009 15:41:51 -0000
@@ -3,7 +3,7 @@
 
 /**
  * @file
- * Installs, upgrades or uninstalls the path_image module.
+ * Module installation and update functionality for Path image.
  */
 
 /**
@@ -15,25 +15,25 @@ function path_image_install() {
   switch ($db_type) {
     case 'mysql':
     case 'mysqli':
-      db_query("CREATE TABLE IF NOT EXISTS {path_image_data} (".
-               "pid INT(10) NOT NULL,".
-               "path VARCHAR(128) NOT NULL,".
-               "image_file VARCHAR(32) NOT NULL,".
-               "PRIMARY KEY(pid),".
-               "INDEX(path(32)))");
+      db_query("CREATE TABLE {path_image_data} (
+        pid INT(10) NOT NULL,
+        path VARCHAR(128) NOT NULL,
+        image_file VARCHAR(32) NOT NULL,
+        PRIMARY KEY(pid),
+        INDEX(path(32))
+      )");
       break;
 
     case 'pgsql':
-      db_query("CREATE TABLE {path_image_data} (".
-               "pid INT(10) NOT NULL,".
-               "path VARCHAR(128) NOT NULL,".
-               "image_file VARCHAR(32) NOT NULL,".
-               "PRIMARY KEY(pid))");
+      db_query("CREATE TABLE {path_image_data} (
+        pid INT(10) NOT NULL,
+        path VARCHAR(128) NOT NULL,
+        image_file VARCHAR(32) NOT NULL,
+        PRIMARY KEY(pid)
+      )");
       db_query("CREATE INDEX {path_image_data}_path_idx ON {path_image_data} (path)");
-               
       break;
   }
-
   db_query("UPDATE {system} SET weight = 5 WHERE name = 'path_image'");
 }
 
Index: path_image.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/path_image/path_image.module,v
retrieving revision 1.4.2.12
diff -u -p -r1.4.2.12 path_image.module
--- path_image.module	28 May 2007 08:48:00 -0000	1.4.2.12
+++ path_image.module	28 Jan 2009 16:07:34 -0000
@@ -3,165 +3,97 @@
 
 /**
  * @file
- * A module that provides a block containing an image
- * depending upon the URI query path.
- *
- * Written by AjK October 2006
+ * Path image module; provides an image block depending on the path.
  */
- 
-/**
- * 26Oct2006 11:31 : AjK
- *  added in the use of file_directory_path() to lock the image
- *  repos into the Drupal installation file system setting
- */
-
-if (!defined("PATH_IMAGE_DEBUG")) {
-  // uncomment the following line to enable debugging mode
-  // define("PATH_IMAGE_DEBUG", "INLINE");
-}
-  
-// define our module name, this MUST be defined here
-define("PATH_IMAGE_MODULE_NAME", "path_image");
-  
-// define the block delta
-define("PATH_IMAGE_BLOCK_DELTA_DISPLAY", 0);
-  
+
 /**
  * Implementation of hook_perm().
  */
 function path_image_perm() {
-  _path_image_debug('path_image_perm()');
-  
   return array('Path image admin');
 }
 
 /**
- * Implementation of hook_menu()
+ * Implementation of hook_menu().
  */
 function path_image_menu($may_cache) {
-
   if ($may_cache) {
     $items[] = array(
       'path' => 'admin/settings/path_image',
       'title' => t('Path image'),
       'access' => user_access('Path image admin'),
       'callback' => 'drupal_get_form',
-      'callback arguments' => '_path_image_settings_form',
+      'callback arguments' => array('path_image_settings_form'),
       'description' => t('Provides an block containing an image dependent upon the URL path'),
-      'type' => MENU_NORMAL_ITEM
     );
   }
-  
   return $items;
 }
 
 /**
- * Implementation of hook_form_alter()
+ * Implementation of hook_form_alter().
  */
 function path_image_form_alter($form_id, &$form) {
-  _path_image_debug("path_image_form_alter('$form_id')");
-  
-  if ('block_admin_configure' == $form_id) {
-    if (isset($form['module']['#value']) && 'path_image' == $form['module']['#value']) {
-      $form['path_image'] = array(
-        '#type' => 'fieldset',
-        '#title' => t('Block content set-up'),
-        '#collapsible' => TRUE,
-        '#weight' => -100
-      );
-      $form['path_image']['markup_help'] = array(
-        '#type' => 'markup',
-        '#value' => t('This section allows you to control the block\'s visibility. '.
-                      'To administer the block\'s content please visit the block\'s '.
-                      l('administration section', 'admin/settings/path_image'))
-      );  
-    }
+  if ($form_id != 'block_admin_configure') {
+    return;
+  }
+  if (isset($form['module']['#value']) && 'path_image' == $form['module']['#value']) {
+    $form['path_image'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Block content set-up'),
+      '#collapsible' => TRUE,
+      '#weight' => -100,
+    );
+    $form['path_image']['markup_help'] = array(
+      '#type' => 'markup',
+      '#value' => t('This section allows you to control the visibility of the block. To administer the content please visit the <a href="@path-image-settings-url">administration section</a>.', array('@path-image-settings-url' => url('admin/settings/path_image'))),
+    );
   }
 }
 
 /**
- * Implementation of hook_block()
+ * Implementation of hook_block().
  */
 function path_image_block($op = 'list', $delta = 0, $edit = array()) {
-  _path_image_debug('path_image_block($op = "'.$op.'", $delta = "'.$delta.'")');
-
   switch ($op) {
     case 'list':
-      $blocks[PATH_IMAGE_BLOCK_DELTA_DISPLAY] = array(    
+      $blocks[0] = array(
         'info' => t('Path image'),
         'weight' => -10,
-        'enabled' => 0,
-        'region' => 'left'
+        'region' => 'left',
       );
       return $blocks;
-      break;
-      
-    case 'view':
-      switch ($delta) {
-        case PATH_IMAGE_BLOCK_DELTA_DISPLAY:
-          return _path_image_block_main();
-          break; // not really needed but be consistent
-      } // end switch()
-  } // end switch() 
-}
 
-/**
- * Implementation of hook_footer()
- */
-function path_image_footer() {
-
-  // all this is used for is to dump into the HTML page
-  // all the debugging lines. This is useful if you
-  // haven't got access to the servers error_log file
-  // where all debugging messages are normally dumped
-  // (I like to do "tail -f error_log" to watch but
-  // that's not always possible)
-  
-  if (!defined("PATH_IMAGE_DEBUG") || PATH_IMAGE_DEBUG != 'INLINE') {
-    return;
-  }
- 
-  if (is_array($GLOBALS[PATH_IMAGE_MODULE_NAME]['debug'])) {
-    print ("\n<!--\n");
-    foreach ($GLOBALS[PATH_IMAGE_MODULE_NAME]['debug'] as $line) {
-      print ("$line\n");
-    }
-    print ("\n-->\n");
+    case 'view':
+      return _path_image_block_main($delta);
   }
 }
 
-/**
- * End of hooks, helpers follow
- */
- 
 function _path_image_block_main() {
   global $base_url;
-  
-  // Flag whether to fallback to use the default image
+
+  // Flag whether to fallback to use the default image.
   $use_default_image = FALSE;
   $use_homepage_image = FALSE;
-  
-  // flag to show a match was made
+
+  // Flag to show a match was made.
   $match_made = FALSE;
-  
-  // prepare params array
+
+  // Prepare params array.
   $params = array();
-  
-  // input comes in the form of the query string,
-  // explode that into an array and scan the array
-  // for the first numeric component... REDIRECT_URL
-  // $path_parts = explode('/', drupal_get_path_alias($_GET['q']));
-  
+
+  // Input comes in the form of the query string, explode that into an array
+  // and scan the array for the first numeric component.
   $path = drupal_get_path_alias($_GET['q']);
-  
+
   if (!strlen($path) || strpos(drupal_set_header(), '403 Forbidden') || variable_get('site_frontpage', 'node') == $path) {
-      if (variable_get('path_image_homepage_use', 0)) {
-        $use_homepage_image = TRUE;
-      }  
+    if (variable_get('path_image_homepage_use', 0)) {
+      $use_homepage_image = TRUE;
+    }
   }
   else {
     $path_parts = explode('/', $path);
-  
+
     if (is_array($path_parts)) {
       foreach ($path_parts as $arg) {
         if (is_numeric($arg) && variable_get('path_image_numeric_use', 1)) {
@@ -173,53 +105,45 @@ function _path_image_block_main() {
       }
     }
     elseif ($path_parts !== FALSE) {
-      $parts[] = $path_parts; // it's a string with no '/' seperators
+      // It is a string with no '/' seperators.
+      $parts[] = $path_parts;
     }
     else {
-      // no path found, use default
+      // No path found, use default, if enabled.
       if (variable_get('path_image_default_use', 0)) {
         $use_default_image = TRUE;
       }
       else {
-        return array(); // query string was empty return empty block
+        // Query string was empty return empty block.
+        return array();
       }
     }
   }
-  
+
   if ($path_parts) {
-    // now build a series of search strings to make SQL statement(s) from
+    // Build a series of search strings to make SQL statement(s) from.
     $last_arg = FALSE;
     foreach ($parts as $arg) {
       $search_paths[] = ($last_arg) ? ($last_arg . $arg) : ($arg);
       $last_arg .= $arg . '/';
     }
-    $search_paths = array_reverse($search_paths); // ensure longest path comes first in search
- 
-    // now we can search the database for possible matches
+    // Ensure longest path comes first in search.
+    $search_paths = array_reverse($search_paths);
+
+    // Now we can search the database for possible matches.
     foreach ($search_paths as $path) {
-      $sql = 'SELECT image_file, LENGTH(path) AS path_len '.
-             'FROM {path_image_data} '.
-             'WHERE path LIKE "%s%%" '.
-             'ORDER BY path_len ASC';
-      _path_image_debug("SQL FOR PATH $path");
-      _path_image_expand_sql($sql, $path);
-    
+      $sql = 'SELECT image_file, LENGTH(path) AS path_len FROM {path_image_data} WHERE path LIKE "%s%%" ORDER BY path_len ASC';
+
       $r = db_query($sql, $path);
-      if (db_num_rows($r)) {
-        // we have a match!
-        $row = db_fetch_array($r); // get just the first match if multiple return (see ORDER BY)
+      if ($row = db_fetch_array($r)) {
         $params = array(
           'image_file' => $row['image_file'],
           'image_repos' => variable_get('path_image_repository', 'images'),
           'base_url' => $base_url,
           'file_system_folder' => file_directory_path(),
-          'path_match' => $path
+          'path_match' => $path,
         );
-        $params['full_uri'] = 
-          trim($params['base_url'], '/') . '/' . 
-          trim($params['file_system_folder']) . '/' .
-          trim($params['image_repos'], '/') . '/' . 
-          trim($params['image_file'], '/');
+        $params['full_uri'] = trim($params['base_url'], '/') . '/' . trim($params['file_system_folder']) . '/' . trim($params['image_repos'], '/') . '/' . trim($params['image_file'], '/');
         $match_made = TRUE;
         break;
       }
@@ -227,17 +151,17 @@ function _path_image_block_main() {
         if (variable_get('path_image_default_use', 0)) {
           $use_default_image = TRUE;
         }
-      }      
+      }
     }
   }
-  
+
   if (!$match_made && $use_default_image && strlen($path)) {
     $params = array(
       'image_file' => variable_get('path_image_default_image', 'none'),
       'image_repos' => variable_get('path_image_repository', 'images'),
       'base_url' => $base_url,
       'file_system_folder' => file_directory_path(),
-      'path_match' => $path
+      'path_match' => $path,
     );
   }
   elseif (!$match_made && $use_homepage_image) {
@@ -246,72 +170,60 @@ function _path_image_block_main() {
       'image_repos' => variable_get('path_image_repository', 'images'),
       'base_url' => $base_url,
       'file_system_folder' => file_directory_path(),
-      'path_match' => $path
-    );  
+      'path_match' => $path,
+    );
   }
 
   if (count($params)) {
-    $params['full_uri'] = 
-      trim($params['base_url'], '/') . '/' . 
-      trim($params['file_system_folder']) . '/' .
-      trim($params['image_repos'], '/') . '/' . 
-      trim($params['image_file'], '/');  
+    $params['full_uri'] = trim($params['base_url'], '/') . '/' . trim($params['file_system_folder']) . '/' . trim($params['image_repos'], '/') . '/' . trim($params['image_file'], '/');
     if (_path_image_file_exists($params)) {
       return array(
         'subject' => variable_get('path_image_block_subject', t('Path image')),
-        'content' => theme('path_image_block_0', $params)
+        'content' => theme('path_image_block_0', $params),
       );
     }
   }
-  
+
   return array();
 }
 
 /**
- * themeable function for display of the block
+ * Render Path image block content.
+ *
+ * @param $params
+ *   An array containing image file parameters.
  */
 function theme_path_image_block_0($params) {
-
   return '<img src="' . $params['full_uri'] . '" alt="' . $params['image_file'] . '" />';
 }
 
 /**
- *
+ * Return whether a Path image file exists.
  */
 function _path_image_file_exists($params = array()) {
-  return file_exists(file_create_path($params['image_repos'] .'/'. $params['image_file']));
+  return file_exists(file_create_path($params['image_repos'] . '/' . $params['image_file']));
 }
 
 /**
- * Implementation of hook_settings() (almost}
- *
- * We use our own menu callback for this as the form is
- * dynamic and we need to use our own validation/submit
- * methods to maintain our db table. Normal hook_settings
- * function does this automatically. Note, this will leave
- * you in good stead as it's one of the upgrade parts that
- * need doing for Drupal 5 anyway.
- */ 
-function _path_image_settings_form() {
-
+ * Form builder function for Path image settings.
+ */
+function path_image_settings_form() {
   $form = array();
   $files = _path_image_get_image_files();
-  
+
   $form['path_image']['block_subject'] = array(
     '#type' => 'textfield',
     '#title' => t('Block title'),
     '#default_value' => variable_get('path_image_block_subject', t('Path image')),
     '#size' => 40,
-    '#description' => t('Set the title of your block here or make blank for no block title')
+    '#description' => t('The title of the block. Leave blank to display no title.'),
   );
   $form['path_image']['repos'] = array(
     '#type' => 'textfield',
     '#title' => t('Image repository'),
     '#default_value' => variable_get('path_image_repository', 'images'),
     '#size' => 40,
-    '#description' => t('Set this to the location of your image files stored on the server. '.
-                        'This folder is relative to your <b>file system path</b> folder. '.
-                        '<em>Note, no leading / (slash)</em>')
+    '#description' => t('Set this to the location of your image files stored on the server. This folder is relative to your <b>file system path</b> folder. <em>Note, no leading / (slash)</em>'),
   );
 
   $form['path_image']['file_types'] = array(
@@ -319,58 +231,56 @@ function _path_image_settings_form() {
     '#title' => t('Allowable file types in repository'),
     '#default_value' => implode(', ', variable_get('path_image_file_types', array('gif', 'jpg', 'png'))),
     '#size' => 40,
-    '#description' => t('Comma seperated list of allowable file types to look for '.
-                        'within the specified image file repository')
+    '#description' => t('Comma seperated list of allowable file types to look for within the specified image file repository'),
   );
 
   $form['path_image']['numeric_use'] = array(
     '#type' => 'checkbox',
     '#title' => t('Numeric parse halt'),
     '#default_value' => variable_get('path_image_numeric_use', 1),
-    '#description' => t('When scanning a path this halts the scan at the first '.
-                       'numeric found. If you want to scan the entire path then '.
-                       'ensure this is off (default is on)')
-  );    
+    '#description' => t('When scanning a path this halts the scan at the first numeric found. If you want to scan the entire path then ensure this is off (default is on)'),
+  );
 
   $form['path_image']['homepage_image'] = array(
     '#type' => 'select',
     '#title' => t('Default image for the homepage'),
     '#default_value' => variable_get('path_image_homepage_image', reset($files)),
     '#options' => $files,
-    '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('cell' => array('valign' => 'top'))), 
+    '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('cell' => array('valign' => 'top'))),
   );
   $form['path_image']['homepage_use'] = array(
     '#type' => 'checkbox',
     '#title' => t('Use this image on the homepage'),
     '#default_value' => variable_get('path_image_homepage_use', 0),
-    '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix')            
-  );    
+    '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
+  );
 
   $form['path_image']['mid_markup'] = array(
-    '#type' => 'markup', 
-    '#value' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', // white space between table cells
-    '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))), 
-    '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix')            
+    '#type' => 'markup',
+    // Add white-space between table cells.
+    '#value' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
+    '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),
+    '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
   );
-  
+
   $form['path_image']['default_image'] = array(
     '#type' => 'select',
     '#title' => t('Default image when no match made'),
     '#default_value' => variable_get('path_image_default_image', reset($files)),
     '#options' => $files,
-    '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))), 
+    '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),
   );
   $form['path_image']['default_use'] = array(
     '#type' => 'checkbox',
     '#title' => t('Use this image when no match is made'),
     '#default_value' => variable_get('path_image_default_use', 0),
-    '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix')            
-  );    
-  
+    '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
+  );
+
   $form['path_image']['new_entry'] = array(
     '#type' => 'fieldset',
     '#title' => t('New path entry'),
-    '#tree' => TRUE,  
+    '#tree' => TRUE,
   );
   $form['path_image']['new_entry']['new_path'] = array(
     '#type' => 'textfield',
@@ -378,9 +288,9 @@ function _path_image_settings_form() {
     '#default_value' => '',
     '#size' => 50,
     '#description' => t('Enter a new path. <em>Note, no leading / (slash)</em>'),
-    '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('table' => array('border' => '0'), 'cell' => array('valign' => 'top'))), 
-    '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix'), 
-    '#disabled' => count($files) ? FALSE : TRUE
+    '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('table' => array('border' => '0'), 'cell' => array('valign' => 'top'))),
+    '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix'),
+    '#disabled' => count($files) ? FALSE : TRUE,
   );
   if (count($files)) {
     $form['path_image']['new_entry']['new_image'] = array(
@@ -388,100 +298,89 @@ function _path_image_settings_form() {
       '#title' => t('Select an image'),
       '#default_value' => '',
       '#options' => $files,
-      '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))), 
-      '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix') 
+      '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))),
+      '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
     );
   }
   else {
     $form['path_image']['new_entry']['new_image_markup'] = array(
       '#type' => 'markup',
       '#value' => t('No files available for selection'),
-      '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))), 
-      '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix') 
+      '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))),
+      '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
     );
   }
 
-  $sql = 'SELECT * FROM {path_image_data} ORDER BY path ASC';
-  $result = db_query($sql);
-  $num_of_rows = db_num_rows($result);
-  if ($num_of_rows > 0) {
-    $form['path_image']['existing_entries'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Existing path %x', array('%x' => format_plural($num_of_rows, 'entry', 'entries'))),
-      '#tree' => TRUE,  
-    );
-    $row_counter = 0;
-    while ($row = db_fetch_object($result)) {
-      if (count($files)) {
-        $form['path_image']['existing_entries'][$row_counter]['pid'] = array(
-          '#type' => 'hidden',
-          '#default_value' => $row->pid,
-        );
-        $form['path_image']['existing_entries'][$row_counter]['path'] = array(
-          '#type' => 'textfield',
-          '#title' => t('Alter a path'),
-          '#default_value' => $row->path,
-          '#size' => 50,
-          '#description' => t('Amend the path. <em>Note, no leading / (slash)</em>'),
-          '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('cell' => array('valign' => 'top'))), 
-          '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix')         
-        );
-        $form['path_image']['existing_entries'][$row_counter]['image_file'] = array(
-          '#type' => 'select',
-          '#title' => t('Select an image'),
-          '#default_value' => $row->image_file,
-          '#options' => $files,
-          '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),
-          '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix')        
-        );    
-        $form['path_image']['existing_entries'][$row_counter]['delete'] = array(
-          '#type' => 'checkbox',
-          '#title' => t('Delete'),
-          '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'middle'))), 
-          '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix')            
-        );    
-      }
-      else {
-        $form['path_image']['existing_entries'][$row_counter]['path_markup'] = array(
-          '#type' => 'markup',
-          '#value' => check_plain($row->path),
-          '#suffix' => '&nbsp; -- &nbsp;'    
-        );      
-        $form['path_image']['existing_entries'][$row_counter]['image_file_markup'] = array(
-          '#type' => 'markup',
-          '#value' => t('No files available for selection'),
-          '#suffix' => theme('path_image_form_wrapper', 'new_line')
-        );
-      }
-      $row_counter++;
+  $result = db_query("SELECT * FROM {path_image_data} ORDER BY path ASC");
+  $row_counter = 0;
+  while ($row = db_fetch_object($result)) {
+    if (count($files)) {
+      $form['path_image']['existing_entries'][$row_counter]['pid'] = array(
+        '#type' => 'hidden',
+        '#default_value' => $row->pid,
+      );
+      $form['path_image']['existing_entries'][$row_counter]['path'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Alter a path'),
+        '#default_value' => $row->path,
+        '#size' => 50,
+        '#description' => t('Amend the path. <em>Note, no leading / (slash)</em>'),
+        '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('cell' => array('valign' => 'top'))),
+        '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix'),
+      );
+      $form['path_image']['existing_entries'][$row_counter]['image_file'] = array(
+        '#type' => 'select',
+        '#title' => t('Select an image'),
+        '#default_value' => $row->image_file,
+        '#options' => $files,
+        '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),
+        '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
+      );
+      $form['path_image']['existing_entries'][$row_counter]['delete'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Delete'),
+        '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'middle'))),
+        '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
+      );
+    }
+    else {
+      $form['path_image']['existing_entries'][$row_counter]['path_markup'] = array(
+        '#type' => 'markup',
+        '#value' => check_plain($row->path),
+        '#suffix' => '&nbsp; -- &nbsp;',
+      );
+      $form['path_image']['existing_entries'][$row_counter]['image_file_markup'] = array(
+        '#type' => 'markup',
+        '#value' => t('No files available for selection'),
+        '#suffix' => theme('path_image_form_wrapper', 'new_line'),
+      );
     }
+    $row_counter++;
+  }
+  if ($row_counter) {
+    $form['path_image']['existing_entries']['#type'] = 'fieldset';
+    $form['path_image']['existing_entries']['#title'] = format_plural($row_counter, '1 existing path entry', '@count existing path entries');
+    $form['path_image']['existing_entries']['#tree'] = TRUE;
   }
 
-  $form['path_image']['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save configuration')
-  );
-  
+  $form['path_image']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
+
   return $form;
 }
 
 /**
- * Form validation function for the _settings page
+ * Form validation function for Path image settings form.
  */
-function _path_image_settings_form_validate($form_id, $form_values) {
-  
+function path_image_settings_form_validate($form_id, $form_values) {
   if ($form_values['default_image'] == 'no image selected') {
-    form_set_error(
-      'default_image', 
-      t('You must set this even if you do not intend to use it.'));
-  }  
+    form_set_error('default_image', t('You must set this even if you do not intend to use it'));
+  }
 }
 
 /**
- * Form submit function for the _settings page
+ * Form submit function for Path image settings form.
  */
-function _path_image_settings_form_submit($form_id, $form_values) {
-
+function path_image_settings_form_submit($form_id, $form_values) {
   variable_set('path_image_block_subject', $form_values['block_subject']);
   variable_set('path_image_repository', $form_values['repos']);
   variable_set('path_image_default_image', $form_values['default_image']);
@@ -489,7 +388,7 @@ function _path_image_settings_form_submi
   variable_set('path_image_homepage_image', $form_values['homepage_image']);
   variable_set('path_image_homepage_use', $form_values['homepage_use']);
   variable_set('path_image_numeric_use', $form_values['numeric_use']);
-    
+
   $file_types = explode(',', $form_values['file_types']);
   if (is_array($file_types)) {
     foreach ($file_types as $k => $v) {
@@ -497,104 +396,100 @@ function _path_image_settings_form_submi
     }
     variable_set('path_image_file_types', $file_types);
   }
-  
-  if (strlen($form_values['new_entry']['new_path']) &&
-      strlen($form_values['new_entry']['new_image'])) {
-    
+
+  if (strlen($form_values['new_entry']['new_path']) && strlen($form_values['new_entry']['new_image'])) {
     $pid = db_next_id('{path_image_data}');  
-    $sql = 'INSERT INTO {path_image_data} SET ' .
-           ' pid = %d, '.
-           ' path = "%s", '.
-           ' image_file = "%s"';
-    db_query($sql, $pid, $form_values['new_entry']['new_path'], $form_values['new_entry']['new_image']);
+    db_query("INSERT INTO {path_image_data} SET pid = %d, path = '%s', image_file = '%s'", $pid, $form_values['new_entry']['new_path'], $form_values['new_entry']['new_image']);
   }
-  
+
   if (isset($form_values['existing_entries']) && is_array($form_values['existing_entries'])) {
     foreach ($form_values['existing_entries'] as $entry) {
       if (isset($entry['delete']) && (int)$entry['delete']) {
-        $sql = 'DELETE FROM {path_image_data} WHERE pid = %d';
-        _path_image_expand_sql($sql, $entry['pid']);
-        db_query($sql, $entry['pid']);
+        db_query("DELETE FROM {path_image_data} WHERE pid = %d", $entry['pid']);
       }
       else {
         if (isset($entry['pid']) && is_numeric($entry['pid']) && strlen($entry['image_file'])) {
-          $sql ='UPDATE {path_image_data} SET path = "%s", image_file = "%s" WHERE pid = %d';
-          db_query($sql, $entry['path'], $entry['image_file'], $entry['pid']);
+          db_query("UPDATE {path_image_data} SET path = '%s', image_file = '%s' WHERE pid = %d", $entry['path'], $entry['image_file'], $entry['pid']);
         }
       }
     }
   }
-  
-  drupal_set_message(t('Your settings have been saved'));  
+
+  drupal_set_message(t('Your settings have been saved'));
 }
 
 /**
- * Theme function for form element wrappers
+ * Theme function for form element wrappers.
  */
 function theme_path_image_form_wrapper($arg, $params = array()) {
-
   $table_attribs = '';
   if (isset($params['table']) && is_array($params['table'])) {
     foreach ($params['table'] as $key => $attrib) {
-      $table_attribs .= $key . '="' . $attrib . '" '; 
+      $table_attribs .= $key . '="' . $attrib . '" ';
     }
   }
 
   $row_attribs = '';
   if (isset($params['row']) && is_array($params['row'])) {
     foreach ($params['row'] as $key => $attrib) {
-      $row_attribs .= $key . '="' . $attrib . '" '; 
+      $row_attribs .= $key . '="' . $attrib . '" ';
     }
   }
 
   $cell_attribs = '';
   if (isset($params['cell']) && is_array($params['cell'])) {
     foreach ($params['cell'] as $key => $attrib) {
-      $cell_attribs .= $key . '="' . $attrib . '" '; 
+      $cell_attribs .= $key . '="' . $attrib . '" ';
     }
   }
-  
+
   switch ($arg) {
     case 'cell_first_prefix':
       return "<table $table_attribs ><tr $row_attribs ><td $cell_attribs >";
+
     case 'cell_first_suffix':
-      return "</td>";      
+      return "</td>";
+
     case 'cell_middle_prefix':
       return "<td $cell_attribs>&nbsp;</td><td $cell_attribs >";
+
     case 'cell_middle_suffix':
-      return "</td>";      
+      return "</td>";
+
     case 'cell_last_prefix':
       return "<td $cell_attribs >&nbsp;</td><td $cell_attribs >";
+
     case 'cell_last_suffix':
-      return "</td></tr></table>";      
+      return "</td></tr></table>";
+
     case 'new_line':
       return "<br $class />";
+
     case 'new_para':
       return "<p $class />";
   }
 }
 
 /**
- * Get an array list of files.  The return type is suitable to
- * pass directly to the '#options' of a "select" forms API element
+ * Retrieve a list of available image files.
+ *
+ * @return
+ *   An array suitable to use as Form API '#options' value.
  */
 function _path_image_get_image_files() {
-  static $files = FALSE;
-  
-  // if cache exists use it to save hitting the disk system
-  if ($files !== FALSE) {
+  static $files;
+
+  if (isset($files)) {
     return $files;
   }
-  
+
   $files = array();
-  
   $file_types = variable_get('path_image_file_types', array('gif', 'jpg', 'png'));
-  
   $dir = file_create_path(variable_get('path_image_repository', 'images'));
-         
+
   if (is_dir($dir)) {
     if ($dh = opendir($dir)) {
-      while (($file = readdir($dh)) !== false) {
+      while (($file = readdir($dh)) !== FALSE) {
         $file_info = pathinfo($file);
         if (in_array($file_info['extension'], $file_types)) {
           $files[$file] = $file;
@@ -603,49 +498,7 @@ function _path_image_get_image_files() {
       closedir($dh);
     }
   }
-  
-  return $files;
-}
 
-/**
- * internal debug function 
- */
-function _path_image_debug($x) {
-   
-  if (!defined("PATH_IMAGE_DEBUG")) {
-    return;
-  }
- 
-  if (is_array($x) || is_object($x)) {
-    $t = split("\n", print_r($x, TRUE));
-    foreach($t as $s) {
-      $GLOBALS[PATH_IMAGE_MODULE_NAME]['debug'][] = $s;
-      error_log(" $s");      
-    }
-  }
-  else {
-    $t = split("\n", $x);
-    foreach($t as $s) {
-      $GLOBALS[PATH_IMAGE_MODULE_NAME]['debug'][] = $s;
-      error_log(" $s");      
-    }
-  }
+  return $files;
 }
 
-/**
- * useful if you want to see how an SQL
- * statement will be expanded by Drupal
- */
-function _path_image_expand_sql($query) {
-  _path_image_debug("_path_image_expand_sql()");
-  
-  $args = func_get_args();
-  array_shift($args);
-  $query = db_prefix_tables($query);
-  if (isset($args[0]) && is_array($args[0])) {
-    $args = $args[0];
-  }
-  _db_query_callback($args, TRUE);
-  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
-  _path_image_debug($query);
-}
