diff -urp /home/konsumer/taxonomy_image-original/taxonomy_image.module ./modules/taxonomy_image/taxonomy_image.module --- /home/konsumer/taxonomy_image-original/taxonomy_image.module 2007-01-17 21:00:24.000000000 -0800 +++ ./modules/taxonomy_image/taxonomy_image.module 2007-11-27 11:22:25.000000000 -0800 @@ -8,18 +8,75 @@ ** Written by Jeremy Andrews , May 2004. */ + +/** + * Use this wherever you need the path to the original image + * + * @param int $tid the term id. + * @param string $ic_profile the imagecache profile to use + * + * @return string The path to the original image + */ +function taxonomy_image_path($tid) { + global $user; + static $image = array(); + + if (user_access('access taxonomy images') && + !$user->taxonomy_image_disable_images) { + // do lookup, return full display path + if (!$image[$tid]->url) { + if ($image[$tid] = db_fetch_object(db_query('SELECT i.path, d.name FROM {term_image} i INNER JOIN {term_data} d WHERE i.tid = d.tid AND i.tid = %d', $tid))) { + return $image[$tid]->path; + } + else if (variable_get('taxonomy_image_recursive', 0)) { + // walk up the taxonomy hierarchy and look for an image + while ($parent = db_fetch_object(db_query('SELECT t.tid FROM {term_hierarchy} h, {term_data} t WHERE h.parent = t.tid AND h.tid = %d ORDER BY weight, name', $tid))) { + $tid = $parent->tid; + if ($image[$tid] = db_fetch_object(db_query('SELECT i.path, d.name FROM {term_image} i INNER JOIN {term_data} d WHERE i.tid = d.tid AND i.tid = %d', $tid))) { + // we found a parent with a configured image, use it + return $image[$tid]->path; + } + } + } + } + } + return ''; +} + /** * Call this function from your theme or other php code to display the * image associated with the given term id. An html tag will be returned * if an image is found. The format of the link can be modified with the * tags parameter. * - * @param int $tid the term id. + * @param int $tid the term id. * @param string $tags optional tags to add into the link + * @param string $ic_profile the imagecache profile to use * - * @return string An html link. + * @return string Imagecache themed image tag. */ -function taxonomy_image_display($tid, $tags = NULL) { +function taxonomy_image_display($tid, $tags = NULL, $ic_profile=NULL) { + global $user; + + if (empty($ic_profile)){ + $ic_profile=variable_get('taxonomy_image_defaultprofile', ''); + } + + if (user_access('access taxonomy images') && + !$user->taxonomy_image_disable_images) { + if (module_exists('imagecache')){ + return theme('imagecache', $ic_profile, taxonomy_image_path($tid), $tags); + }else{ + // @todo: replace this with a trimmer re-use of taxonomy_image_path() + return taxonomy_image_display_old($tid, $tags); + } + } + return ''; +} + +// quick hack to just re-use old function +// this function should be removed, and the new function should use taxonomy_image_path better. +function taxonomy_image_display_old($tid, $tags = NULL) { global $user; static $image = array(); @@ -102,6 +159,8 @@ function taxonomy_image_display($tid, $t return ''; } + + // standard Drupal functions function taxonomy_image_perm() { return array ('access taxonomy images', 'administer taxonomy images', 'can disable taxonomy images'); @@ -134,6 +193,14 @@ function taxonomy_image_help($section = $output .= "$image"; } +

Views

+

Views support has been added, so you can use the field "Taxonomy Image" in your views.

+

Imagecache

+

If you have imagecache enabled, you can use imagecache profiles instead of image height/width. You can set a default profile in the admin, or use it manually with taxonomy_image_display(), like this:

+
+taxonomy_image_display($term->tid, NULL, \'PROFILE_NAME\');
+
+

set PROFILE_NAME to whatever profile you want to use.

'); } } @@ -197,6 +264,11 @@ function taxonomy_image_admin_settings() '#type' => 'fieldset', '#title' => t('Pictures'), ); + + if (!module_exists('imagecache')){ + $form['pictures']['#description'] = t('Be sure to enable the imagecache module, for extra functionality.'); + } + $form['pictures']['taxonomy_image_path'] = array( '#type' => 'textfield', '#title' => t('Picture image path'), @@ -205,29 +277,44 @@ function taxonomy_image_admin_settings() '#maxlength' => 255, '#description' => t('Subdirectory in the directory "%dir" where category pictures will be stored.', array('%dir' => variable_get('file_directory_path', 'files') . '/')) . $error, ); - $form['pictures']['taxonomy_image_resize'] = array( - '#type' => 'radios', - '#title' => t('Picture resize'), - '#default_value' => variable_get('taxonomy_image_resize', 0), - '#options' => array(3 => 'Exact', 2 => 'Not less than', 1 => 'Not greater than', 0 => 'Disabled'), - '#description' => t('This option allows you to control the size of pictures displayed by this module. If set to \'disabled\', pictures will not be resized, displayed exactly as they are uploaded. If set to \'not greater than\', pictures larger than the specified size will be scaled down. If set to \'not less than\', pictures smaller than the specified size will be scaled up. If set to \'exact\', pictures will be resized to exactly the specified dimension(s).'), - ); - $form['pictures']['taxonomy_image_height'] = array( - '#type' => 'textfield', - '#title' => t('Picture height'), - '#default_value' => variable_get('taxonomy_image_height', ''), - '#size' => 5, - '#maxlength' => 6, - '#description' => t('Specify a height in pixels. If left blank or set to 0 this field is ignored.'), - ); - $form['pictures']['taxonomy_image_width'] = array( - '#type' => 'textfield', - '#title' => t('Picture width'), - '#default_value' => variable_get('taxonomy_image_width', ''), - '#size' => 5, - '#maxlength' => 6, - '#description' => t('Specify a width in pixels. If left blank or set to 0 this field is ignored.'), - ); + + if (module_exists('imagecache')){ + $presets=_imagecache_get_presets(); + $presets=array_combine($presets,$presets); + $presets['']='ORIGINAL'; + $form['pictures']['taxonomy_image_defaultprofile'] = array( + '#type' => 'select', + '#title' => t('Default Imagecache Profile'), + '#default_value' => variable_get('taxonomy_image_defaultprofile', ''), + '#description' => t("If you don't set an imagecache profile in your view, or when you call taxonomy_image_display(), this one will be used. Set it to ORIGINAL to bypass imagecache altogether (by default.)"), + '#options' =>$presets, + ); + } + else{ + $form['pictures']['taxonomy_image_resize'] = array( + '#type' => 'radios', + '#title' => t('Picture resize'), + '#default_value' => variable_get('taxonomy_image_resize', 0), + '#options' => array(3 => 'Exact', 2 => 'Not less than', 1 => 'Not greater than', 0 => 'Disabled'), + '#description' => t('This option allows you to control the size of pictures displayed by this module. If set to \'disabled\', pictures will not be resized, displayed exactly as they are uploaded. If set to \'not greater than\', pictures larger than the specified size will be scaled down. If set to \'not less than\', pictures smaller than the specified size will be scaled up. If set to \'exact\', pictures will be resized to exactly the specified dimension(s).'), + ); + $form['pictures']['taxonomy_image_height'] = array( + '#type' => 'textfield', + '#title' => t('Picture height'), + '#default_value' => variable_get('taxonomy_image_height', ''), + '#size' => 5, + '#maxlength' => 6, + '#description' => t('Specify a height in pixels. If left blank or set to 0 this field is ignored.'), + ); + $form['pictures']['taxonomy_image_width'] = array( + '#type' => 'textfield', + '#title' => t('Picture width'), + '#default_value' => variable_get('taxonomy_image_width', ''), + '#size' => 5, + '#maxlength' => 6, + '#description' => t('Specify a width in pixels. If left blank or set to 0 this field is ignored.'), + ); + } $form['advanced'] = array( '#type' => 'fieldset', @@ -441,4 +528,75 @@ function _taxonomy_image_exists($tid) { return 0; } -?> +function taxonomy_image_views_tables() { + $tables['term_node'] = array( + 'name' => 'term_node', + 'join' => array( + 'left' => array( + 'table' => 'node', + 'field' => 'nid' + ), + 'right' => array( + 'field' => 'nid' + ) + ), + ); + $tables['term_image'] = array( + 'name' => 'term_image', + 'join' => array( + 'left' => array( + 'table' => 'term_node', + 'field' => 'tid', + ), + 'right' => array( + 'field' => 'tid' + ) + ), + 'fields' => array( + 'tid' => array( + 'name' => t('Taxonomy Image: Image'), + 'help' => t('The image associated with the node\'s taxonomy term.'), + 'handler' => array( + 'views_handler_image_path' => t('Image'), + ) + ) + ), + ); + + if (module_exists('imagecache')){ + $raw_presets=_imagecache_get_presets(); + $presets=array(); + foreach($raw_presets as $preset){ + $presets[$preset]=$preset; + $presets["{$preset}_link"]="{$preset} as link"; + } + $presets['']='Default'; + $tables['term_image']['fields']['tid']['option']=array( + '#type' => 'select', + '#options' => $presets, + ); + + } + + return $tables; +} + +function views_handler_image_path($fieldinfo, $fielddata, $value, $data) { + if (empty($fielddata['options'])){ + return taxonomy_image_display($value); + } + else{ + if (substr($fielddata['options'], -5, 5) == '_link'){ + $profile=substr($fielddata['options'], 0, -5); + $img = taxonomy_image_display($value, $data->name, $profile); + $img=l($img, 'taxonomy/term/'.$value, array(), NULL, NULL, FALSE, TRUE); + return $img; + }else{ + $profile = $fielddata['options']; + $img = taxonomy_image_display($value, $data->name, $profile); + return $img; + } + + + } +}