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

Contents of /contributions/modules/path_image/path_image.module

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


Revision 1.12 - (show annotations) (download) (as text)
Wed May 13 13:41:32 2009 UTC (6 months, 2 weeks ago) by sun
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Changes since 1.11: +73 -102 lines
File MIME type: text/x-php
#365480 by sun: Code clean-up; Added support for URL aliases.
1 <?php
2 // $Id: path_image.module,v 1.11 2009/02/01 01:18:01 sun Exp $
3
4 /**
5 * @file
6 * Path image module; provides an image block depending on the path.
7 */
8
9 /**
10 * Implementation of hook_perm().
11 */
12 function path_image_perm() {
13 return array('Path image admin');
14 }
15
16 /**
17 * Implementation of hook_theme().
18 */
19 function path_image_theme() {
20 return array(
21 'path_image_block_0' => array(
22 'arguments' => array('params' => NULL),
23 ),
24 'path_image_form_wrapper' => array(
25 'arguments' => array('arg' => NULL, 'params' => array()),
26 ),
27 );
28 }
29
30 /**
31 * Implementation of hook_menu().
32 */
33 function path_image_menu() {
34 $items['admin/settings/path_image'] = array(
35 'title' => 'Path image',
36 'access arguments' => array('Path image admin'),
37 'page callback' => 'drupal_get_form',
38 'page arguments' => array('path_image_settings_form'),
39 'description' => 'Provides an block containing an image dependent upon the URL path',
40 );
41 return $items;
42 }
43
44 /**
45 * Implementation of hook_form_FORM_ID_alter().
46 */
47 function path_image_form_block_admin_configure_alter(&$form, $form_state, $form_id) {
48 if (isset($form['module']['#value']) && 'path_image' == $form['module']['#value']) {
49 $form['path_image'] = array(
50 '#type' => 'fieldset',
51 '#title' => t('Block content set-up'),
52 '#collapsible' => TRUE,
53 '#weight' => -100,
54 );
55 $form['path_image']['markup_help'] = array(
56 '#type' => 'markup',
57 '#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'))),
58 );
59 }
60 }
61
62 /**
63 * Implementation of hook_block().
64 */
65 function path_image_block($op = 'list', $delta = 0, $edit = array()) {
66 switch ($op) {
67 case 'list':
68 $blocks[0] = array(
69 'info' => t('Path image'),
70 'weight' => -10,
71 'region' => 'left',
72 );
73 return $blocks;
74
75 case 'view':
76 return _path_image_block_main($delta);
77 }
78 }
79
80 function _path_image_block_main() {
81 global $base_url;
82
83 $is_forbidden = (strpos(drupal_set_header(), '403 Forbidden') !== FALSE);
84 $is_front = ($_GET['q'] == variable_get('site_frontpage', 'node'));
85
86 if (!$is_forbidden || !$is_front) {
87 $parts = array();
88 foreach (explode('/', $_GET['q']) as $arg) {
89 if (is_numeric($arg) && variable_get('path_image_numeric_use', 1)) {
90 break;
91 }
92 else {
93 $parts[] = $arg;
94 }
95 }
96 $alias = drupal_get_path_alias($_GET['q']);
97 if ($alias != $_GET['q']) {
98 $alias_parts = array();
99 foreach (explode('/', $alias) as $arg) {
100 if (is_numeric($arg) && variable_get('path_image_numeric_use', 1)) {
101 break;
102 }
103 else {
104 $alias_parts[] = $arg;
105 }
106 }
107 }
108 }
109
110 if (!empty($parts)) {
111 // Build query conditions by climbing up the path arguments.
112 $query_args = array();
113 $path_prefix = '';
114 $path_wildcard_prefix = '';
115 foreach ($parts as $arg) {
116 $path_prefix .= (empty($path_prefix) ? $arg : '/' . $arg);
117 $query_args[] = $path_prefix;
118 if (is_numeric($arg)) {
119 $arg = '%';
120 }
121 $path_wildcard_prefix .= (empty($path_wildcard_prefix) ? $arg : '/' . $arg);
122 if ($path_prefix != $path_wildcard_prefix) {
123 $query_args[] = $path_wildcard_prefix;
124 }
125 }
126 if (!empty($alias_parts)) {
127 $alias_prefix = '';
128 $alias_wildcard_prefix = '';
129 foreach ($alias_parts as $arg) {
130 $alias_prefix .= (empty($alias_prefix) ? $arg : '/' . $arg);
131 $query_args[] = $alias_prefix;
132 if (is_numeric($arg)) {
133 $arg = '%';
134 }
135 $alias_wildcard_prefix .= (empty($alias_wildcard_prefix) ? $arg : '/' . $arg);
136 if ($alias_prefix != $alias_wildcard_prefix) {
137 $query_args[] = $alias_wildcard_prefix;
138 }
139 }
140 }
141 $sql = "SELECT image_file FROM {path_image_data} WHERE path IN (" . db_placeholders($query_args, 'varchar') . ") ORDER BY LENGTH(path) DESC";
142 $file = db_result(db_query($sql, $query_args, 0, 1));
143 }
144
145 // If no matching path was found, determine whether we need to display the
146 // front page image or default image.
147 if (empty($file)) {
148 if ($is_front && variable_get('path_image_homepage_use', 0)) {
149 $file = variable_get('path_image_homepage_image', FALSE);
150 }
151 elseif (variable_get('path_image_default_use', 0)) {
152 $file = variable_get('path_image_default_image', FALSE);
153 }
154 // Return an empty block if we have no image file to display.
155 else {
156 return array();
157 }
158 }
159
160 $filepath = file_directory_path() . '/' . variable_get('path_image_repository', 'images') . '/' . $file;
161 if (!file_exists($filepath)) {
162 return array();
163 }
164 $params = array(
165 'filepath' => $filepath,
166 'full_uri' => file_create_url($filepath),
167 );
168 return array(
169 'subject' => variable_get('path_image_block_subject', t('Path image')),
170 'content' => theme('path_image_block_0', $params),
171 );
172 }
173
174 /**
175 * Render Path image block content.
176 *
177 * @param $params
178 * An array containing image file parameters.
179 */
180 function theme_path_image_block_0($params) {
181 return theme('image', $params['filepath'], '', '');
182 }
183
184 /**
185 * Form builder function for Path image settings.
186 */
187 function path_image_settings_form() {
188 $form = array();
189 $files = _path_image_get_image_files();
190
191 $form['path_image']['block_subject'] = array(
192 '#type' => 'textfield',
193 '#title' => t('Block title'),
194 '#default_value' => variable_get('path_image_block_subject', t('Path image')),
195 '#size' => 40,
196 '#description' => t('The title of the block. Leave blank to display no title.'),
197 );
198 $form['path_image']['repos'] = array(
199 '#type' => 'textfield',
200 '#title' => t('Image repository'),
201 '#default_value' => variable_get('path_image_repository', 'images'),
202 '#size' => 40,
203 '#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>'),
204 );
205
206 $form['path_image']['file_types'] = array(
207 '#type' => 'textfield',
208 '#title' => t('Allowable file types in repository'),
209 '#default_value' => implode(', ', variable_get('path_image_file_types', array('gif', 'jpg', 'png'))),
210 '#size' => 40,
211 '#description' => t('Comma seperated list of allowable file types to look for within the specified image file repository'),
212 );
213
214 $form['path_image']['numeric_use'] = array(
215 '#type' => 'checkbox',
216 '#title' => t('Numeric parse halt'),
217 '#default_value' => variable_get('path_image_numeric_use', 1),
218 '#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)'),
219 );
220
221 $form['path_image']['homepage_image'] = array(
222 '#type' => 'select',
223 '#title' => t('Default image for the homepage'),
224 '#default_value' => variable_get('path_image_homepage_image', reset($files)),
225 '#options' => $files,
226 '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('cell' => array('valign' => 'top'))),
227 );
228 $form['path_image']['homepage_use'] = array(
229 '#type' => 'checkbox',
230 '#title' => t('Use this image on the homepage'),
231 '#default_value' => variable_get('path_image_homepage_use', 0),
232 '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
233 );
234
235 $form['path_image']['mid_markup'] = array(
236 '#type' => 'markup',
237 // Add white-space between table cells.
238 '#value' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
239 '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),
240 '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
241 );
242
243 $form['path_image']['default_image'] = array(
244 '#type' => 'select',
245 '#title' => t('Default image when no match made'),
246 '#default_value' => variable_get('path_image_default_image', reset($files)),
247 '#options' => $files,
248 '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),
249 );
250 $form['path_image']['default_use'] = array(
251 '#type' => 'checkbox',
252 '#title' => t('Use this image when no match is made'),
253 '#default_value' => variable_get('path_image_default_use', 0),
254 '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
255 );
256
257 $form['path_image']['new_entry'] = array(
258 '#type' => 'fieldset',
259 '#title' => t('New path entry'),
260 '#tree' => TRUE,
261 );
262 $form['path_image']['new_entry']['new_path'] = array(
263 '#type' => 'textfield',
264 '#title' => t('Add a new path'),
265 '#default_value' => '',
266 '#size' => 50,
267 '#description' => t('Enter a new path. <em>Note, no leading / (slash)</em>'),
268 '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('table' => array('border' => '0'), 'cell' => array('valign' => 'top'))),
269 '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix'),
270 '#disabled' => count($files) ? FALSE : TRUE,
271 );
272 if (count($files)) {
273 $form['path_image']['new_entry']['new_image'] = array(
274 '#type' => 'select',
275 '#title' => t('Select an image'),
276 '#default_value' => '',
277 '#options' => $files,
278 '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))),
279 '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
280 );
281 }
282 else {
283 $form['path_image']['new_entry']['new_image_markup'] = array(
284 '#type' => 'markup',
285 '#value' => t('No files available for selection'),
286 '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'top'))),
287 '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
288 );
289 }
290
291 $result = db_query("SELECT * FROM {path_image_data} ORDER BY path ASC");
292 $row_counter = 0;
293 while ($row = db_fetch_object($result)) {
294 if (count($files)) {
295 $form['path_image']['existing_entries'][$row_counter]['pid'] = array(
296 '#type' => 'hidden',
297 '#default_value' => $row->pid,
298 );
299 $form['path_image']['existing_entries'][$row_counter]['path'] = array(
300 '#type' => 'textfield',
301 '#title' => t('Alter a path'),
302 '#default_value' => $row->path,
303 '#size' => 50,
304 '#description' => t('Amend the path. <em>Note, no leading / (slash)</em>'),
305 '#prefix' => theme('path_image_form_wrapper', 'cell_first_prefix', array('cell' => array('valign' => 'top'))),
306 '#suffix' => theme('path_image_form_wrapper', 'cell_first_suffix'),
307 );
308 $form['path_image']['existing_entries'][$row_counter]['image_file'] = array(
309 '#type' => 'select',
310 '#title' => t('Select an image'),
311 '#default_value' => $row->image_file,
312 '#options' => $files,
313 '#prefix' => theme('path_image_form_wrapper', 'cell_middle_prefix', array('cell' => array('valign' => 'top'))),
314 '#suffix' => theme('path_image_form_wrapper', 'cell_middle_suffix'),
315 );
316 $form['path_image']['existing_entries'][$row_counter]['delete'] = array(
317 '#type' => 'checkbox',
318 '#title' => t('Delete'),
319 '#prefix' => theme('path_image_form_wrapper', 'cell_last_prefix', array('cell' => array('valign' => 'middle'))),
320 '#suffix' => theme('path_image_form_wrapper', 'cell_last_suffix'),
321 );
322 }
323 else {
324 $form['path_image']['existing_entries'][$row_counter]['path_markup'] = array(
325 '#type' => 'markup',
326 '#value' => check_plain($row->path),
327 '#suffix' => '&nbsp; -- &nbsp;',
328 );
329 $form['path_image']['existing_entries'][$row_counter]['image_file_markup'] = array(
330 '#type' => 'markup',
331 '#value' => t('No files available for selection'),
332 '#suffix' => theme('path_image_form_wrapper', 'new_line'),
333 );
334 }
335 $row_counter++;
336 }
337 if ($row_counter) {
338 $form['path_image']['existing_entries']['#type'] = 'fieldset';
339 $form['path_image']['existing_entries']['#title'] = format_plural($row_counter, '1 existing path entry', '@count existing path entries');
340 $form['path_image']['existing_entries']['#tree'] = TRUE;
341 }
342
343 $form['path_image']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
344
345 return $form;
346 }
347
348 /**
349 * Form validation function for Path image settings form.
350 */
351 function path_image_settings_form_validate($form, &$form_state) {
352 if ($form_state['values']['default_image'] == 'no image selected') {
353 form_set_error('default_image', t('You must set this even if you do not intend to use it'));
354 }
355 }
356
357 /**
358 * Form submit function for Path image settings form.
359 */
360 function path_image_settings_form_submit($form, &$form_state) {
361 variable_set('path_image_block_subject', $form_state['values']['block_subject']);
362 variable_set('path_image_repository', $form_state['values']['repos']);
363 variable_set('path_image_default_image', $form_state['values']['default_image']);
364 variable_set('path_image_default_use', $form_state['values']['default_use']);
365 variable_set('path_image_homepage_image', $form_state['values']['homepage_image']);
366 variable_set('path_image_homepage_use', $form_state['values']['homepage_use']);
367 variable_set('path_image_numeric_use', $form_state['values']['numeric_use']);
368
369 $file_types = explode(',', $form_state['values']['file_types']);
370 if (is_array($file_types)) {
371 foreach ($file_types as $k => $v) {
372 $file_types[$k] = trim($v);
373 }
374 variable_set('path_image_file_types', $file_types);
375 }
376
377 if (strlen($form_state['values']['new_entry']['new_path']) && strlen($form_state['values']['new_entry']['new_image'])) {
378 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']);
379 }
380
381 if (isset($form_state['values']['existing_entries']) && is_array($form_state['values']['existing_entries'])) {
382 foreach ($form_state['values']['existing_entries'] as $entry) {
383 if (isset($entry['delete']) && (int)$entry['delete']) {
384 db_query("DELETE FROM {path_image_data} WHERE pid = %d", $entry['pid']);
385 }
386 else {
387 if (isset($entry['pid']) && is_numeric($entry['pid']) && strlen($entry['image_file'])) {
388 db_query("UPDATE {path_image_data} SET path = '%s', image_file = '%s' WHERE pid = %d", $entry['path'], $entry['image_file'], $entry['pid']);
389 }
390 }
391 }
392 }
393
394 drupal_set_message(t('Your settings have been saved'));
395 }
396
397 /**
398 * Theme function for form element wrappers.
399 */
400 function theme_path_image_form_wrapper($arg, $params = array()) {
401 $table_attribs = '';
402 if (isset($params['table']) && is_array($params['table'])) {
403 foreach ($params['table'] as $key => $attrib) {
404 $table_attribs .= $key . '="' . $attrib . '" ';
405 }
406 }
407
408 $row_attribs = '';
409 if (isset($params['row']) && is_array($params['row'])) {
410 foreach ($params['row'] as $key => $attrib) {
411 $row_attribs .= $key . '="' . $attrib . '" ';
412 }
413 }
414
415 $cell_attribs = '';
416 if (isset($params['cell']) && is_array($params['cell'])) {
417 foreach ($params['cell'] as $key => $attrib) {
418 $cell_attribs .= $key . '="' . $attrib . '" ';
419 }
420 }
421
422 switch ($arg) {
423 case 'cell_first_prefix':
424 return "<table $table_attribs ><tr $row_attribs ><td $cell_attribs >";
425
426 case 'cell_first_suffix':
427 return "</td>";
428
429 case 'cell_middle_prefix':
430 return "<td $cell_attribs>&nbsp;</td><td $cell_attribs >";
431
432 case 'cell_middle_suffix':
433 return "</td>";
434
435 case 'cell_last_prefix':
436 return "<td $cell_attribs >&nbsp;</td><td $cell_attribs >";
437
438 case 'cell_last_suffix':
439 return "</td></tr></table>";
440
441 case 'new_line':
442 return "<br $class />";
443
444 case 'new_para':
445 return "<p $class />";
446 }
447 }
448
449 /**
450 * Retrieve a list of available image files.
451 *
452 * @return
453 * An array suitable to use as Form API '#options' value.
454 */
455 function _path_image_get_image_files() {
456 static $files;
457
458 if (isset($files)) {
459 return $files;
460 }
461
462 $files = array();
463 $file_types = variable_get('path_image_file_types', array('gif', 'jpg', 'png'));
464 $dir = file_create_path(variable_get('path_image_repository', 'images'));
465
466 if (is_dir($dir)) {
467 if ($dh = opendir($dir)) {
468 while (($file = readdir($dh)) !== FALSE) {
469 $file_info = pathinfo($file);
470 if (in_array($file_info['extension'], $file_types)) {
471 $files[$file] = $file;
472 }
473 }
474 closedir($dh);
475 }
476 }
477 // Sort alphabetically.
478 ksort($files);
479
480 return $files;
481 }
482

  ViewVC Help
Powered by ViewVC 1.1.2