/[drupal]/contributions/modules/path_image/path_image.module
ViewVC logotype

Diff of /contributions/modules/path_image/path_image.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.10, Wed Jan 28 14:47:48 2009 UTC revision 1.11, Sun Feb 1 01:18:01 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: path_image.module,v 1.9 2007/01/08 23:05:01 karpuz Exp $  // $Id: path_image.module,v 1.10 2009/01/28 14:47:48 sun Exp $
3    
4  /**  /**
5   * @file   * @file
6   * A module that provides a block containing an image   * Path image module; provides an image block depending on the path.
  * depending upon the URI query path.  
  *  
  * Written by AjK October 2006  
  */  
   
 /**  
  * 26Oct2006 11:31 : AjK  
  *  added in the use of file_directory_path() to lock the image  
  *  repos into the Drupal installation file system setting  
  */  
   
 /**  
  * Implementation of hook_init()  
7   */   */
 function path_image_init() {  
   
   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);  
   
   _path_image_debug("path_image_init()");  
 }  
8    
9  /**  /**
10   * Implementation of hook_perm().   * Implementation of hook_perm().
11   */   */
12  function path_image_perm() {  function path_image_perm() {
   _path_image_debug('path_image_perm()');  
   
13    return array('Path image admin');    return array('Path image admin');
14  }  }
15    
# Line 58  function path_image_theme() { Line 28  function path_image_theme() {
28  }  }
29    
30  /**  /**
31   * Implementation of hook_menu()   * Implementation of hook_menu().
32   */   */
33  function path_image_menu() {  function path_image_menu() {
34    $items['admin/settings/path_image'] = array(    $items['admin/settings/path_image'] = array(
35      'title' => 'Path image',      'title' => 'Path image',
36      'access arguments' => array('Path image admin'),      'access arguments' => array('Path image admin'),
37      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
38      'page arguments' => array('_path_image_settings_form'),      'page arguments' => array('path_image_settings_form'),
39      'description' => 'Provides an block containing an image dependent upon the URL path',      'description' => 'Provides an block containing an image dependent upon the URL path',
40    );    );
41    return $items;    return $items;
# Line 75  function path_image_menu() { Line 45  function path_image_menu() {
45   * Implementation of hook_form_FORM_ID_alter().   * Implementation of hook_form_FORM_ID_alter().
46   */   */
47  function path_image_form_block_admin_configure_alter(&$form, $form_state, $form_id) {  function path_image_form_block_admin_configure_alter(&$form, $form_state, $form_id) {
   _path_image_debug("path_image_form_alter('$form_id')");  
   
48    if (isset($form['module']['#value']) && 'path_image' == $form['module']['#value']) {    if (isset($form['module']['#value']) && 'path_image' == $form['module']['#value']) {
49      $form['path_image'] = array(      $form['path_image'] = array(
50        '#type' => 'fieldset',        '#type' => 'fieldset',
51        '#title' => t('Block content set-up'),        '#title' => t('Block content set-up'),
52        '#collapsible' => TRUE,        '#collapsible' => TRUE,
53        '#weight' => -100        '#weight' => -100,
54      );      );
55      $form['path_image']['markup_help'] = array(      $form['path_image']['markup_help'] = array(
56        '#type' => 'markup',        '#type' => 'markup',
# Line 92  function path_image_form_block_admin_con Line 60  function path_image_form_block_admin_con
60  }  }
61    
62  /**  /**
63   * Implementation of hook_block()   * Implementation of hook_block().
64   */   */
65  function path_image_block($op = 'list', $delta = 0, $edit = array()) {  function path_image_block($op = 'list', $delta = 0, $edit = array()) {
   _path_image_debug('path_image_block($op = "'.$op.'", $delta = "'.$delta.'")');  
   
66    switch ($op) {    switch ($op) {
67      case 'list':      case 'list':
68        $blocks[PATH_IMAGE_BLOCK_DELTA_DISPLAY] = array(        $blocks[0] = array(
69          'info' => t('Path to image display block'),          'info' => t('Path image'),
70          'weight' => -10,          'weight' => -10,
71          'enabled' => 0,          'region' => 'left',
         'region' => 'left'  
72        );        );
73        return $blocks;        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()  
 }  
74    
75  /**      case 'view':
76   * Implementation of hook_footer()        return _path_image_block_main($delta);
  */  
 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");  
77    }    }
78  }  }
79    
 /**  
  * End of hooks, helpers follow  
  */  
   
80  function _path_image_block_main() {  function _path_image_block_main() {
81    global $base_url;    global $base_url;
82    
83    // Flag whether to fallback to use the default image    // Flag whether to fallback to use the default image.
84    $use_default_image = FALSE;    $use_default_image = FALSE;
85    $use_homepage_image = FALSE;    $use_homepage_image = FALSE;
86    
87    // flag to show a match was made    // Flag to show a match was made.
88    $match_made = FALSE;    $match_made = FALSE;
89    
90    // prepare params array    // Prepare params array.
91    $params = array();    $params = array();
92    
93    // input comes in the form of the query string,    // Input comes in the form of the query string, explode that into an array
94    // explode that into an array and scan the array    // and scan the array for the first numeric component.
95    // for the first numeric component... REDIRECT_URL    $path = drupal_get_path_alias($_GET['q']);
96    // $path_parts = explode('/', drupal_get_path_alias($_GET['q']));  
97      if (!strlen($path) || strpos(drupal_set_header(), '403 Forbidden') || variable_get('site_frontpage', 'node') == $path) {
98    $path = trim($_SERVER['REDIRECT_URL'], '/');      if (variable_get('path_image_homepage_use', 0)) {
99    $headers = drupal_set_header();        $use_homepage_image = TRUE;
100        }
   if (!strlen($path) || strpos($headers, '403 Forbidden')) {  
       if (variable_get('path_image_homepage_use', 0)) {  
         $use_homepage_image = TRUE;  
       }  
101    }    }
102    else {    else {
103      $path_parts = explode('/', $path);      $path_parts = explode('/', $path);
104    
105      if (is_array($path_parts)) {      if (is_array($path_parts)) {
106        foreach($path_parts as $arg) {        foreach ($path_parts as $arg) {
107          if (is_numeric($arg) && variable_get('path_image_numeric_use', 1)) {          if (is_numeric($arg) && variable_get('path_image_numeric_use', 1)) {
108            break;            break;
109          }          }
# Line 186  function _path_image_block_main() { Line 113  function _path_image_block_main() {
113        }        }
114      }      }
115      elseif ($path_parts !== FALSE) {      elseif ($path_parts !== FALSE) {
116        $parts[] = $path_parts; // it's a string with no '/' seperators        // It is a string with no '/' seperators.
117          $parts[] = $path_parts;
118      }      }
119      else {      else {
120        // no path found, use default        // No path found, use default, if enabled.
121        if (variable_get('path_image_default_use', 0)) {        if (variable_get('path_image_default_use', 0)) {
122          $use_default_image = TRUE;          $use_default_image = TRUE;
123        }        }
124        else {        else {
125          return array(); // query string was empty return empty block          // Query string was empty return empty block.
126            return array();
127        }        }
128      }      }
129    }    }
130    
131    if ($path_parts) {    if ($path_parts) {
132      // 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.
133      $last_arg = FALSE;      $last_arg = FALSE;
134      foreach ($parts as $arg) {      foreach ($parts as $arg) {
135        $search_paths[] = ($last_arg) ? ($last_arg . $arg) : ($arg);        $search_paths[] = ($last_arg) ? ($last_arg . $arg) : ($arg);
136        $last_arg .= $arg . '/';        $last_arg .= $arg . '/';
137      }      }
138      $search_paths = array_reverse($search_paths); // ensure longest path comes first in search      // Ensure longest path comes first in search.
139        $search_paths = array_reverse($search_paths);
140      // now we can search the database for possible matches  
141        // Now we can search the database for possible matches.
142      foreach ($search_paths as $path) {      foreach ($search_paths as $path) {
143        $sql = 'SELECT image_file, LENGTH(path) AS path_len '.        $sql = 'SELECT image_file, LENGTH(path) AS path_len FROM {path_image_data} WHERE path LIKE "%s%%" ORDER BY path_len ASC';
144               'FROM {path_image_data} '.  
              'WHERE path LIKE "%s%%" '.  
              'ORDER BY path_len ASC';  
   
145        $r = db_query($sql, $path);        $r = db_query($sql, $path);
146        if ($row = db_fetch_array($r)) {        if ($row = db_fetch_array($r)) {
         // we have a match!  
147          $params = array(          $params = array(
148            'image_file' => $row['image_file'],            'image_file' => $row['image_file'],
149            'image_repos' => variable_get('path_image_repository', 'files/images'),            'image_repos' => variable_get('path_image_repository', 'images'),
150            'base_url' => $base_url,            'base_url' => $base_url,
151            'file_system_folder' => file_directory_path(),            'file_system_folder' => file_directory_path(),
152            'path_match' => $path            'path_match' => $path,
153          );          );
154          $params['full_uri'] =          $params['full_uri'] = trim($params['base_url'], '/') . '/' . trim($params['file_system_folder']) . '/' . trim($params['image_repos'], '/') . '/' . trim($params['image_file'], '/');
           trim($params['base_url'], '/') . '/' .  
           trim($params['file_system_folder']) . '/' .  
           trim($params['image_repos'], '/') . '/' .  
           trim($params['image_file'], '/');  
155          $match_made = TRUE;          $match_made = TRUE;
156          break;          break;
157        }        }
# Line 237  function _path_image_block_main() { Line 159  function _path_image_block_main() {
159          if (variable_get('path_image_default_use', 0)) {          if (variable_get('path_image_default_use', 0)) {
160            $use_default_image = TRUE;            $use_default_image = TRUE;
161          }          }
162        }        }
163      }      }
164    }    }
165    
166    if (!$match_made && $use_default_image && strlen($path)) {    if (!$match_made && $use_default_image && strlen($path)) {
167      $params = array(      $params = array(
168        'image_file' => variable_get('path_image_default_image', 'none'),        'image_file' => variable_get('path_image_default_image', 'none'),
169        'image_repos' => variable_get('path_image_repository', 'images'),        'image_repos' => variable_get('path_image_repository', 'images'),
170        'base_url' => $base_url,        'base_url' => $base_url,
171        'file_system_folder' => file_directory_path(),        'file_system_folder' => file_directory_path(),
172        'path_match' => $path        'path_match' => $path,
173      );      );
174    }    }
175    elseif (!$match_made && $use_homepage_image) {    elseif (!$match_made && $use_homepage_image) {
# Line 256  function _path_image_block_main() { Line 178  function _path_image_block_main() {
178        'image_repos' => variable_get('path_image_repository', 'images'),        'image_repos' => variable_get('path_image_repository', 'images'),
179        'base_url' => $base_url,        'base_url' => $base_url,
180        'file_system_folder' => file_directory_path(),        'file_system_folder' => file_directory_path(),
181        'path_match' => $path        'path_match' => $path,
182      );      );
183    }    }
184    
185    if (count($params)) {    if (count($params)) {
186      $params['full_uri'] =      $params['full_uri'] = trim($params['base_url'], '/') . '/' . trim($params['file_system_folder']) . '/' . trim($params['image_repos'], '/') . '/' . trim($params['image_file'], '/');
       trim($params['base_url'], '/') . '/' .  
       trim($params['file_system_folder']) . '/' .  
       trim($params['image_repos'], '/') . '/' .  
       trim($params['image_file'], '/');  
187      if (_path_image_file_exists($params)) {      if (_path_image_file_exists($params)) {
188        return array(        return array(
189          'subject' => variable_get('path_image_block_subject', t('Path Image')),          'subject' => variable_get('path_image_block_subject', t('Path image')),
190          'content' => theme('path_image_block_0', $params)          'content' => theme('path_image_block_0', $params),
191        );        );
192      }      }
193    }    }
   
   return array();  
 }  
   
 /**  
  * helper function to create the themeable function parameters  
  */  
 function _path_image_block_main_helper() {  
194    
195      return array();
196  }  }
197    
198  /**  /**
199   * themeable function for display of the block   * Render Path image block content.
200     *
201     * @param $params
202     *   An array containing image file parameters.
203   */   */
204  function theme_path_image_block_0($params) {  function theme_path_image_block_0($params) {
   
205    return '<img src="' . $params['full_uri'] . '" alt="' . $params['image_file'] . '" />';    return '<img src="' . $params['full_uri'] . '" alt="' . $params['image_file'] . '" />';
206  }  }
207    
208  /**  /**
209   *   * Return whether a Path image file exists.
210   */   */
211  function _path_image_file_exists($params = array()) {  function _path_image_file_exists($params = array()) {
212      return file_exists(file_create_path($params['image_repos'] . '/' . $params['image_file']));
   return file_exists(  
     $_SERVER['DOCUMENT_ROOT'] . '/' .  
     file_directory_path() . '/' .  
     $params['image_repos'] . '/' .  
     $params['image_file']  
   );  
213  }  }
214    
215  /**  /**
216   * Implementation of hook_settings() (almost}   * Form builder function for Path image settings.
217   *   */
218   * We use our own menu callback for this as the form is  function path_image_settings_form() {
  * 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() {  
   
219    $form = array();    $form = array();
220    $files = _path_image_get_image_files();    $files = _path_image_get_image_files();
221    
222    $form['path_image']['block_subject'] = array(    $form['path_image']['block_subject'] = array(
223      '#type' => 'textfield',      '#type' => 'textfield',
224      '#title' => t('Block title'),      '#title' => t('Block title'),
225      '#default_value' => variable_get('path_image_block_subject', t('Path Image')),      '#default_value' => variable_get('path_image_block_subject', t('Path image')),
226      '#size' => 40,      '#size' => 40,
227      '#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.'),
228    );    );
229    $form['path_image']['repos'] = array(    $form['path_image']['repos'] = array(
230      '#type' => 'textfield',      '#type' => 'textfield',
231      '#title' => t('Image repository'),      '#title' => t('Image repository'),
232      '#default_value' => variable_get('path_image_repository', 'images'),      '#default_value' => variable_get('path_image_repository', 'images'),
233      '#size' => 40,      '#size' => 40,
234      '#description' => t('Set this to the location of your image files stored on the server. '.      '#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>'),
                         'This folder is relative to your <b>file system path</b> folder. '.  
                         '<em>Note, no leading / (slash)</em>')  
235    );    );
236    
237    $form['path_image']['file_types'] = array(    $form['path_image']['file_types'] = array(
# Line 342  function _path_image_settings_form() { Line 239  function _path_image_settings_form() {
239      '#title' => t('Allowable file types in repository'),      '#title' => t('Allowable file types in repository'),
240      '#default_value' => implode(', ', variable_get('path_image_file_types', array('gif', 'jpg', 'png'))),      '#default_value' => implode(', ', variable_get('path_image_file_types', array('gif', 'jpg', 'png'))),
241      '#size' => 40,      '#size' => 40,
242      '#description' => t('Comma seperated list of allowable file types to look for '.      '#description' => t('Comma seperated list of allowable file types to look for within the specified image file repository'),
                         'within the specified image file repository')  
243    );    );
244    
245    $form['path_image']['numeric_use'] = array(    $form['path_image']['numeric_use'] = array(
246      '#type' => 'checkbox',      '#type' => 'checkbox',
247      '#title' => t('Numeric parse halt'),      '#title' => t('Numeric parse halt'),
248      '#default_value' => variable_get('path_image_numeric_use', 1),      '#default_value' => variable_get('path_image_numeric_use', 1),
249      '#description' => t('When scanning a path this halts the scan at the first '.      '#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)'),
250                         'numeric found. If you want to scan the entire path then '.    );
                        'ensure this is off (default is on)')  
   );  
251    
252    $form['path_image']['homepage_image'] = array(    $form['path_image']['homepage_image'] = array(
253      '#type' => 'select',      '#type' => 'select',
254      '#title' => t('Default image for the homepage'),      '#title' => t('Default image for the homepage'),
255      '#default_value' => variable_get('path_image_homepage_image', reset($files)),      '#default_value' => variable_get('path_image_homepage_image', reset($files)),
256      '#options' => $files,      '#options' => $files,
257      '#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'))),
258    );    );
259    $form['path_image']['homepage_use'] = array(    $form['path_image']['homepage_use'] = array(
260      '#type' => 'checkbox',      '#type' => 'checkbox',
261      '#title' => t('Use this image on the homepage'),      '#title' => t('Use this image on the homepage'),
262      '#default_value' => variable_get('path_image_homepage_use', 0),      '#default_value' => variable_get('path_image_homepage_use', 0),
263      '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix')      '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
264    );    );
265    
266    $form['path_image']['mid_markup'] = array(    $form['path_image']['mid_markup'] = array(
267      '#type' => 'markup',      '#type' => 'markup',
268      '#value' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', // white space between table cells      // Add white-space between table cells.
269      '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),      '#value' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
270      '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix')      '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),
271        '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
272    );    );
273    
274    $form['path_image']['default_image'] = array(    $form['path_image']['default_image'] = array(
275      '#type' => 'select',      '#type' => 'select',
276      '#title' => t('Default image when no match made'),      '#title' => t('Default image when no match made'),
277      '#default_value' => variable_get('path_image_default_image', reset($files)),      '#default_value' => variable_get('path_image_default_image', reset($files)),
278      '#options' => $files,      '#options' => $files,
279      '#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'))),
280    );    );
281    $form['path_image']['default_use'] = array(    $form['path_image']['default_use'] = array(
282      '#type' => 'checkbox',      '#type' => 'checkbox',
283      '#title' => t('Use this image when no match is made'),      '#title' => t('Use this image when no match is made'),
284      '#default_value' => variable_get('path_image_default_use', 0),      '#default_value' => variable_get('path_image_default_use', 0),
285      '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix')      '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
286    );    );
287    
288    $form['path_image']['new_entry'] = array(    $form['path_image']['new_entry'] = array(
289      '#type' => 'fieldset',      '#type' => 'fieldset',
290      '#title' => t('New path entry'),      '#title' => t('New path entry'),
291      '#tree' => TRUE,      '#tree' => TRUE,
292    );    );
293    $form['path_image']['new_entry']['new_path'] = array(    $form['path_image']['new_entry']['new_path'] = array(
294      '#type' => 'textfield',      '#type' => 'textfield',
# Line 401  function _path_image_settings_form() { Line 296  function _path_image_settings_form() {
296      '#default_value' => '',      '#default_value' => '',
297      '#size' => 50,      '#size' => 50,
298      '#description' => t('Enter a new path. <em>Note, no leading / (slash)</em>'),      '#description' => t('Enter a new path. <em>Note, no leading / (slash)</em>'),
299      '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('table' => array('border' => '0'), 'cell' => array('valign' => 'top'))),      '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('table' => array('border' => '0'), 'cell' => array('valign' => 'top'))),
300      '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix'),      '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix'),
301      '#disabled' => count($files) ? FALSE : TRUE      '#disabled' => count($files) ? FALSE : TRUE,
302    );    );
303    if (count($files)) {    if (count($files)) {
304      $form['path_image']['new_entry']['new_image'] = array(      $form['path_image']['new_entry']['new_image'] = array(
# Line 411  function _path_image_settings_form() { Line 306  function _path_image_settings_form() {
306        '#title' => t('Select an image'),        '#title' => t('Select an image'),
307        '#default_value' => '',        '#default_value' => '',
308        '#options' => $files,        '#options' => $files,
309        '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))),        '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))),
310        '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix')        '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
311      );      );
312    }    }
313    else {    else {
314      $form['path_image']['new_entry']['new_image_markup'] = array(      $form['path_image']['new_entry']['new_image_markup'] = array(
315        '#type' => 'markup',        '#type' => 'markup',
316        '#value' => t('No files available for selection'),        '#value' => t('No files available for selection'),
317        '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))),        '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))),
318        '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix')        '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
319      );      );
320    }    }
321    
322    $form['path_image']['existing_entries'] = array(    $result = db_query("SELECT * FROM {path_image_data} ORDER BY path ASC");
     '#type' => 'fieldset',  
     '#title' => t('Existing path %x', array('%x' => format_plural($num_of_rows, 'entry', 'entries'))),  
     '#tree' => TRUE,  
   );  
   $sql = 'SELECT * FROM {path_image_data} ORDER BY path ASC';  
   $result = db_query($sql);  
323    $row_counter = 0;    $row_counter = 0;
324    while ($row = db_fetch_object($result)) {    while ($row = db_fetch_object($result)) {
325      if (count($files)) {      if (count($files)) {
# Line 444  function _path_image_settings_form() { Line 333  function _path_image_settings_form() {
333          '#default_value' => $row->path,          '#default_value' => $row->path,
334          '#size' => 50,          '#size' => 50,
335          '#description' => t('Amend the path. <em>Note, no leading / (slash)</em>'),          '#description' => t('Amend the path. <em>Note, no leading / (slash)</em>'),
336          '#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'))),
337          '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix')          '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix'),
338        );        );
339        $form['path_image']['existing_entries'][$row_counter]['image_file'] = array(        $form['path_image']['existing_entries'][$row_counter]['image_file'] = array(
340          '#type' => 'select',          '#type' => 'select',
# Line 453  function _path_image_settings_form() { Line 342  function _path_image_settings_form() {
342          '#default_value' => $row->image_file,          '#default_value' => $row->image_file,
343          '#options' => $files,          '#options' => $files,
344          '#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'))),
345          '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix')          '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
346        );        );
347        $form['path_image']['existing_entries'][$row_counter]['delete'] = array(        $form['path_image']['existing_entries'][$row_counter]['delete'] = array(
348          '#type' => 'checkbox',          '#type' => 'checkbox',
349          '#title' => t('Delete'),          '#title' => t('Delete'),
350          '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'middle'))),          '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'middle'))),
351          '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix')          '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
352        );        );
353      }      }
354      else {      else {
355        $form['path_image']['existing_entries'][$row_counter]['path_markup'] = array(        $form['path_image']['existing_entries'][$row_counter]['path_markup'] = array(
356          '#type' => 'markup',          '#type' => 'markup',
357          '#value' => check_plain($row->path),          '#value' => check_plain($row->path),
358          '#suffix' => '&nbsp; -- &nbsp;'          '#suffix' => '&nbsp; -- &nbsp;',
359        );        );
360        $form['path_image']['existing_entries'][$row_counter]['image_file_markup'] = array(        $form['path_image']['existing_entries'][$row_counter]['image_file_markup'] = array(
361          '#type' => 'markup',          '#type' => 'markup',
362          '#value' => t('No files available for selection'),          '#value' => t('No files available for selection'),
363          '#suffix' => theme('path_image_form_wrapper', 'new_line')          '#suffix' => theme('path_image_form_wrapper', 'new_line'),
364        );        );
365      }      }
366      $row_counter++;      $row_counter++;
367    }    }
368    if (!$row_counter) {    if ($row_counter) {
369      $form['path_image']['existing_entries']['#access'] = FALSE;      $form['path_image']['existing_entries']['#type'] = 'fieldset';
370        $form['path_image']['existing_entries']['#title'] = format_plural($row_counter, '1 existing path entry', '@count existing path entries');
371        $form['path_image']['existing_entries']['#tree'] = TRUE;
372    }    }
373    
374    $form['path_image']['submit'] = array(    $form['path_image']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
375      '#type' => 'submit',  
     '#value' => t('Save'),  
   );  
   
376    return $form;    return $form;
377  }  }
378    
379  /**  /**
380   * Form validation function for the _settings page   * Form validation function for Path image settings form.
381   */   */
382  function _path_image_settings_form_validate($form, &$form_state) {  function path_image_settings_form_validate($form, &$form_state) {
   
   $return_value = TRUE;  
   
383    if ($form_state['values']['default_image'] == 'no image selected') {    if ($form_state['values']['default_image'] == 'no image selected') {
384      form_set_error(      form_set_error('default_image', t('You must set this even if you do not intend to use it'));
385        'default_image',    }
       t('You must set this even if you do not intend to use it'));  
     $return_value = FALSE;  
   }  
   
   return $return_value;  
386  }  }
387    
388  /**  /**
389   * Form submit function for the _settings page   * Form submit function for Path image settings form.
390   */   */
391  function _path_image_settings_form_submit($form, &$form_state) {  function path_image_settings_form_submit($form, &$form_state) {
   
392    variable_set('path_image_block_subject', $form_state['values']['block_subject']);    variable_set('path_image_block_subject', $form_state['values']['block_subject']);
393    variable_set('path_image_repository', $form_state['values']['repos']);    variable_set('path_image_repository', $form_state['values']['repos']);
394    variable_set('path_image_default_image', $form_state['values']['default_image']);    variable_set('path_image_default_image', $form_state['values']['default_image']);
# Line 517  function _path_image_settings_form_submi Line 396  function _path_image_settings_form_submi
396    variable_set('path_image_homepage_image', $form_state['values']['homepage_image']);    variable_set('path_image_homepage_image', $form_state['values']['homepage_image']);
397    variable_set('path_image_homepage_use', $form_state['values']['homepage_use']);    variable_set('path_image_homepage_use', $form_state['values']['homepage_use']);
398    variable_set('path_image_numeric_use', $form_state['values']['numeric_use']);    variable_set('path_image_numeric_use', $form_state['values']['numeric_use']);
399    
400    $file_types = explode(',', $form_state['values']['file_types']);    $file_types = explode(',', $form_state['values']['file_types']);
401    if (is_array($file_types)) {    if (is_array($file_types)) {
402      foreach ($file_types as $k => $v) {      foreach ($file_types as $k => $v) {
# Line 525  function _path_image_settings_form_submi Line 404  function _path_image_settings_form_submi
404      }      }
405      variable_set('path_image_file_types', $file_types);      variable_set('path_image_file_types', $file_types);
406    }    }
407    
408    if (strlen($form_state['values']['new_entry']['new_path']) &&    if (strlen($form_state['values']['new_entry']['new_path']) && strlen($form_state['values']['new_entry']['new_image'])) {
409        strlen($form_state['values']['new_entry']['new_image'])) {      db_query("INSERT INTO {path_image_data} SET path = '%s', image_file = '%s'", $form_state['values']['new_entry']['new_path'], $form_state['values']['new_entry']['new_image']);
   
     $sql = 'INSERT INTO {path_image_data} SET ' .  
            ' path = "%s", '.  
            ' image_file = "%s"';  
     db_query($sql, $form_state['values']['new_entry']['new_path'], $form_state['values']['new_entry']['new_image']);  
410    }    }
411    
412    if (isset($form_state['values']['existing_entries']) && is_array($form_state['values']['existing_entries'])) {    if (isset($form_state['values']['existing_entries']) && is_array($form_state['values']['existing_entries'])) {
413      foreach ($form_state['values']['existing_entries'] as $entry) {      foreach ($form_state['values']['existing_entries'] as $entry) {
414        if (isset($entry['delete']) && (int)$entry['delete']) {        if (isset($entry['delete']) && (int)$entry['delete']) {
415          $sql = 'DELETE FROM {path_image_data} WHERE pid = %d';          db_query("DELETE FROM {path_image_data} WHERE pid = %d", $entry['pid']);
         _path_image_expand_sql($sql, $entry['pid']);  
         db_query($sql, $entry['pid']);  
416        }        }
417        else {        else {
418          if (isset($entry['pid']) && is_numeric($entry['pid']) && strlen($entry['image_file'])) {          if (isset($entry['pid']) && is_numeric($entry['pid']) && strlen($entry['image_file'])) {
419            $sql ='UPDATE {path_image_data} SET path = "%s", image_file = "%s" WHERE pid = %d';            db_query("UPDATE {path_image_data} SET path = '%s', image_file = '%s' WHERE pid = %d", $entry['path'], $entry['image_file'], $entry['pid']);
           db_query($sql, $entry['path'], $entry['image_file'], $entry['pid']);  
420          }          }
421        }        }
422      }      }
423    }    }
424    
425    drupal_set_message(t('Your settings have been saved'));    drupal_set_message(t('Your settings have been saved'));
426  }  }
427    
428  /**  /**
429   * Theme function for form element wrappers   * Theme function for form element wrappers.
430   */   */
431  function theme_path_image_form_wrapper($arg, $params = array()) {  function theme_path_image_form_wrapper($arg, $params = array()) {
   
432    $table_attribs = '';    $table_attribs = '';
433    if (isset($params['table']) && is_array($params['table'])) {    if (isset($params['table']) && is_array($params['table'])) {
434      foreach ($params['table'] as $key => $attrib) {      foreach ($params['table'] as $key => $attrib) {
435        $table_attribs .= $key . '="' . $attrib . '" ';        $table_attribs .= $key . '="' . $attrib . '" ';
436      }      }
437    }    }
438    
439    $row_attribs = '';    $row_attribs = '';
440    if (isset($params['row']) && is_array($params['row'])) {    if (isset($params['row']) && is_array($params['row'])) {
441      foreach ($params['row'] as $key => $attrib) {      foreach ($params['row'] as $key => $attrib) {
442        $row_attribs .= $key . '="' . $attrib . '" ';        $row_attribs .= $key . '="' . $attrib . '" ';
443      }      }
444    }    }
445    
446    $cell_attribs = '';    $cell_attribs = '';
447    if (isset($params['cell']) && is_array($params['cell'])) {    if (isset($params['cell']) && is_array($params['cell'])) {
448      foreach ($params['cell'] as $key => $attrib) {      foreach ($params['cell'] as $key => $attrib) {
449        $cell_attribs .= $key . '="' . $attrib . '" ';        $cell_attribs .= $key . '="' . $attrib . '" ';
450      }      }
451    }    }
452    
453    switch ($arg) {    switch ($arg) {
454      case 'cell_first_prefix':      case 'cell_first_prefix':
455        return "<table $table_attribs ><tr $row_attribs ><td $cell_attribs >";        return "<table $table_attribs ><tr $row_attribs ><td $cell_attribs >";
456    
457      case 'cell_first_suffix':      case 'cell_first_suffix':
458        return "</td>";        return "</td>";
459    
460      case 'cell_middle_prefix':      case 'cell_middle_prefix':
461        return "<td $cell_attribs>&nbsp;</td><td $cell_attribs >";        return "<td $cell_attribs>&nbsp;</td><td $cell_attribs >";
462    
463      case 'cell_middle_suffix':      case 'cell_middle_suffix':
464        return "</td>";        return "</td>";
465    
466      case 'cell_last_prefix':      case 'cell_last_prefix':
467        return "<td $cell_attribs >&nbsp;</td><td $cell_attribs >";        return "<td $cell_attribs >&nbsp;</td><td $cell_attribs >";
468    
469      case 'cell_last_suffix':      case 'cell_last_suffix':
470        return "</td></tr></table>";        return "</td></tr></table>";
471    
472      case 'new_line':      case 'new_line':
473        return "<br $class />";        return "<br $class />";
474    
475      case 'new_para':      case 'new_para':
476        return "<p $class />";        return "<p $class />";
477    }    }
478  }  }
479    
480  /**  /**
481   * Get an array list of files.  The return type is suitable to   * Retrieve a list of available image files.
482   * pass directly to the '#options' of a "select" forms API element   *
483     * @return
484     *   An array suitable to use as Form API '#options' value.
485   */   */
486  function _path_image_get_image_files() {  function _path_image_get_image_files() {
487    static $files = FALSE;    static $files;
488    
489    // if cache exists use it to save hitting the disk system    if (isset($files)) {
   if ($files !== FALSE) {  
490      return $files;      return $files;
491    }    }
492    
493    $files = array();    $files = array();
   
494    $file_types = variable_get('path_image_file_types', array('gif', 'jpg', 'png'));    $file_types = variable_get('path_image_file_types', array('gif', 'jpg', 'png'));
495      $dir = file_create_path(variable_get('path_image_repository', 'images'));
496    $dir = $_SERVER['DOCUMENT_ROOT'] . '/' .  
          file_directory_path() . '/' .  
          variable_get('path_image_repository', 'images');  
   
497    if (is_dir($dir)) {    if (is_dir($dir)) {
498      if ($dh = opendir($dir)) {      if ($dh = opendir($dir)) {
499        while (($file = readdir($dh)) !== false) {        while (($file = readdir($dh)) !== FALSE) {
500          $file_info = pathinfo($file);          $file_info = pathinfo($file);
501          if (in_array($file_info['extension'], $file_types)) {          if (in_array($file_info['extension'], $file_types)) {
502            $files[$file] = $file;            $files[$file] = $file;
# Line 631  function _path_image_get_image_files() { Line 505  function _path_image_get_image_files() {
505        closedir($dh);        closedir($dh);
506      }      }
507    }    }
   
   return $files;  
 }  
508    
509  /**    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");  
     }  
   }  
510  }  }
511    
 /**  
  * 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);  
 }  

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11

  ViewVC Help
Powered by ViewVC 1.1.2