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