--- modules/taxonomy_image.module.orig 2004-05-31 20:09:10.065748665 -0400 +++ modules/taxonomy_image.module 2004-05-31 20:42:05.959680018 -0400 @@ -0,0 +1,291 @@ +, May 2004. +*/ + +/** + * 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 string $tags optional tags to add into the link + * + * @return string An html link. + */ +function taxonomy_image_display($tid, $tags = NULL) { + global $user; + + if (user_access('access taxonomy images') && + !$user->taxonomy_image_disable_images) { + // do lookup, return full display path + if ($image = db_fetch_object(db_query('SELECT path FROM {term_image} WHERE tid = %d', $tid))) { + $image->url = file_create_url($image->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 = db_fetch_object(db_query('SELECT path FROM {term_image} WHERE tid = %d', $tid))) { + // we found a parent with a configured image, use it + $image->url = file_create_url($image->path); + break; + } + } + } + if ($image->url) { + list($image->width, $image->height) = getimagesize($image->path); + // handle image resizing + switch (variable_get('taxonomy_image_resize', 0)) { + case 3: // exact + if ($width = variable_get('taxonomy_image_width', 0)) + $image->width = $width; + if ($height = variable_get('taxonomy_image_height', 0)) + $image->height = $height; + break; + case 2: // not less than + if (($width = variable_get('taxonomy_image_width', 0)) && + ($width > $image->width)) { + $width_scale = $image->width / $width; + } + if (($height = variable_get('taxonomy_image_height', 0)) && + ($height > $image->height)) { + $height_scale = $image->height / $height; + } + if ($height_scale || $width_scale) { + if ($width_scale && $height_scale) + $scale = min($width_scale, $height_scale); + else + $scale = $width_scale ? $width_scale : $height_scale; + $image->height = $image->height / $scale; + $image->width = $image->width / $scale; + } + break; + case 1: // not greater than + if (($width = variable_get('taxonomy_image_width', 0)) && + ($width < $image->width)) { + $width_scale = $image->width / $width; + } + if (($height = variable_get('taxonomy_image_height', 0)) && + ($height < $image->height)) { + $height_scale = $image->height / $height; + } + if ($height_scale || $width_scale) { + $scale = max($width_scale, $height_scale); + $image->height = $image->height / $scale; + $image->width = $image->width / $scale; + } + break; + } + return ""; + } + } + return ''; +} + +// standard Drupal functions +function taxonomy_image_perm() { + return array ('access taxonomy images', 'administer taxonomy images', 'can disable taxonomy images'); +} + +function taxonomy_image_help($section = '') { + switch ($section) { + case 'admin/system/modules/taxonomy_image': + case 'admin/system/modules#description': + $output = t('Upload and associate images with taxonomy terms.'); + break; + case 'admin/help#taxonomy_image': + $output .= t(' +

Introduction

+

The taxonomy_image module allows site administrators to associate images with taxonomy terms. Once defined, this association allows Drupal themes to display images with site content. For example, the taxonomy_image module might be used to display a penguin with content about Linux, and a cheeseburger with content about junk food.

+

The module allows both a one-to-one term-to-image relationship, and a many-to-one terms-to-image relationship.

+

The taxonomy_image module requires that the taxonomy module also be enabled.

+

Configuration

+

Uploading images

+

With the taxonomy_image module enabled, images can be uploaded and associated with taxonomy terms at \'administer >> taxonomy >> images\'. On that page you will find tables containing all your vocabularies and terms. Next to each term is a link titled \'upload image\' which you can click to upload an image for that term. After clicking that link, you will be brought to another page with a small \'Add images\' form. Using the \'browse\' button you can select your image then click \'Save\'. +

Continue this process to upload appropriate images for your taxonomy terms. Note that by default images will be displayed at the size they were uploaded. Alternatively, you can go to \'administer >> configuration >> modules >> taxonomy_image\' to force the display height and/or width of all taxonomy images.

+

Permissions

+

For your users to be able to view the images you have uploaded, you will need to give them the necessary permissions. Only users with the \'access taxonomy images\' permission will see images. If you wish to give your users the ability to disable the images, also give them the \'can disable taxonomy images\' permission.

+

A third permission, \'administer taxonomy images\', controls which users are allowed to configure taxonomy images.

+

Recursive image display

+

Taxonomy is a very powerful tool. One of its features is providing the ability to create hierarchical vocabularies, with which you can build a tree of terms. It is possible that an entire tree of terms, or even just a branch of terms, are all about a similar subject and should all be associated with the same image. By going to \'administer >> configuration >> modules >> taxonomy_image\', you can enable \'Recursive image disaply\'. With this option enabled, you only need to configure an image for the parent term, and all children will automatically inheret the same image (unless they are manually configured to display an alternative image).

+

Displaying images

+

To actually display images from your theme, you will have to modify the theme to make a call to taxonomy_image_display(). When calling this function, you will need to pass in the taxonomy term for which an image should be displayed. For example, from your theme\'s \'_node\' function you might do the following: +

+  foreach (taxonomy_node_get_terms($node->nid) as $term) {
+    if ($image = taxonomy_image_display($term->tid)) {
+      $output .= "$image";
+    }
+
+'); + break; + } + return $output; +} + +function taxonomy_image_link($type) { + if ($type == 'system' && user_access('administer taxonomy images')) { + menu('admin/taxonomy/image', t('images'), 'taxonomy_image_admin', 4); + menu('admin/taxonomy/image/add', t('Upload image'), 'taxonomy_image_admin', 4, MENU_HIDE); + return; + } + + return $links ? $links : array(); +} + +function taxonomy_image_user($type, $edit, $user) { + switch ($type) { + case 'edit_form': + if (user_access('can disable taxonomy images')) { + $form = form_checkbox(t('Disable images'), 'taxonomy_image_disable_images', 1, $user->taxonomy_image_disable_images, t('Check this box to disable the display of content images.')); + return array(t('Content images') => $form); + } + break; + } +} + +function taxonomy_image_settings() { + $group = form_radios(t('Resize action'), 'taxonomy_image_resize', variable_get('taxonomy_image_resize', 0), array(3 => 'Exact', 2 => 'Not less than', 1 => 'Not greater than', 0 => 'None'), t('This option allows you to control the size of images displayed by this module. If set to \'none\', images will not be resized, displayed exactly as they are uploaded. If set to \'not greater than\', images larger than the specified size will be scaled down. If set to \'not less than\', images smaller than the specified size will be scaled up. If set to \'exact\', images will be resized to exactly the specified dimension(s).')); + $group .= form_textfield(t('Image height'), 'taxonomy_image_height', variable_get('taxonomy_image_height', ''), 5, 6, t('Specify a height in pixels. If left blank or set to 0 this field is ignored.')); + $group .= form_textfield(t('Image width'), 'taxonomy_image_width', variable_get('taxonomy_image_width', ''), 5, 6, t('Specify a width in pixels. If left blank or set to 0 this field is ignored.')); + $output = form_group(t('Image size'), $group); + $group = form_radios(t('Recursive image display'), 'taxonomy_image_recursive', variable_get('taxonomy_image_recursive', 0), array(1 => 'Enabled', 0 => 'Disabled'), t('When enabled, taxonomy_image_display() will recursively search for an image to display, starting with the passed in term, then trying the term\'s parents. This functionality is only useful if you have defined hierarchical taxonomies, and multiple terms within a tree will share the same image. If this doesn\'t mean anything to you, leave this option disabled.')); + $output .= form_group(t('Advanced'), $group); + return $output; +} + +// taxonomy_image specific functions +function taxonomy_image_admin() { + $op = $_POST['op']; + $edit = $_POST['edit']; + + if (empty($op)) { + $op = arg(2); + } + + switch ($op) { + case 'image': + if (arg(3) == 'add') { + $output = taxonomy_image_form(object2array(taxonomy_image_get_term(arg(4)))); + break; + } + $output = taxonomy_image_overview(); + break; + case t('Save'): + $output = taxonomy_image_save($edit); + $output = taxonomy_image_overview(); + break; + case t('Delete'): + $output = taxonomy_image_delete($edit); + $output = taxonomy_image_overview(); + break; + default: + $output = taxonomy_image_overview(); + } + + print theme('page', $output); +} + +function taxonomy_image_overview() { + $output = '

'. t('Taxonomy images overview') .'

'; + if (variable_get('taxonomy_image_recursive', 0)) { + $output .= '

'. t('Recursively displaying images.') .'

'; + } + + $header = array(t('name'), t('node types'), t('image')); + + $vocabularies = taxonomy_get_vocabularies(); + + foreach ($vocabularies as $vocabulary) { + $types = array(); + $rows = array(); + foreach(explode(',', $vocabulary->nodes) as $type) { + $types[] = node_invoke($type, 'node_name'); + } + $rows[] = array($vocabulary->name, array('data' => implode(', ', $types), 'align' => 'center'), ''); + + $tree = taxonomy_get_tree($vocabulary->vid); + if ($tree) { + foreach ($tree as $term) { + $data = _taxonomy_depth($term->depth) .' '. $term->name .' ('. ( _taxonomy_image_exists($term->tid) ? l(t('edit image'), "admin/taxonomy/image/add/$term->tid") : l(t('upload image'), "admin/taxonomy/image/add/$term->tid") ) .')
'; + /* use taxonomy_image_display() instead of _taxonomy_image_exists() in + ** case image display recursion is enabled... + */ + $image = taxonomy_image_display($term->tid) ? taxonomy_image_display($term->tid) : ''; + $rows[] = array(array('data' => $data, 'colspan' => 2), $image); + } + } + $output .= theme('table', $header, $rows); + } + + return $output; +} + +function taxonomy_image_get_term($tid) { + return db_fetch_object(db_query('SELECT d.name, d.description, d.tid, i.path FROM {term_data} d LEFT JOIN {term_image} i ON d.tid = i.tid WHERE d.tid = %d', $tid)); +} + +function taxonomy_image_form($edit = array()) { + + unset ($form); + + // A path may be set when the image doesn't exist if using recursion + if (!is_null($edit['path']) && _taxonomy_image_exists($edit['tid'])) { + $group = taxonomy_image_display($edit['tid']) .'
'; + $group .= form_submit(t('Delete')); + $form = form_group(t('Current image'), $group); + drupal_set_title(t('Edit image')); + } + + $group = form_file(t('Taxonomy image file'), 'path', 40, t('The image file you wish to associate with the \'%term\' term.', array('%term' => $edit['name']))); + $group .= form_submit(t('Save')); + if (!is_null($edit['tid'])) { + $group .= form_hidden('tid', $edit['tid']); + } + $form .= form_group(t('Upload new image'), $group); + + return form($form, 'post', 0, array('enctype' => 'multipart/form-data')); +} + +function taxonomy_image_save($edit) { + $fields = array('tid', 'path'); + if ($file = file_save_upload('path', file_create_path())) { + $edit['path'] = $file->path; + } + + if ($old_image = db_fetch_object(db_query('SELECT tid FROM {term_image} WHERE tid = %d', $edit['tid']))) { + // delete old image before saving the new one + taxonomy_image_delete($old_image->tid); + } + + foreach ($fields as $field) { + $values[] = (string)check_query($edit[$field]); + } + + db_query('INSERT INTO {term_image} (' .implode(', ', $fields). ') VALUES (\'' .implode('\', \'', $values). '\')'); + cache_clear_all(); + + return drupal_set_message(t('Image uploaded.')); +} + +function taxonomy_image_delete($tid) { + file_delete(db_result(db_query('SELECT path FROM {term_image} WHERE tid = %d', $tid))); + db_query('DELETE FROM {term_image} WHERE tid = %d', $tid); + cache_clear_all(); + return drupal_set_message(t('Image deleted.')); +} + +function _taxonomy_image_exists($tid) { + if (db_fetch_object(db_query('SELECT path FROM {term_image} WHERE tid = %d', $tid))) { + return 1; + } + return 0; +} + +?> --- themes/xtemplate/xtemplate.theme.orig 2004-05-31 19:45:18.649358407 -0400 +++ themes/xtemplate/xtemplate.theme 2004-05-31 20:08:50.662776484 -0400 @@ -34,7 +34,10 @@ $group = form_checkbox(t('Display pictures with posts'), 'xtemplate_picture_node', 1, variable_get('xtemplate_picture_node', 0), t('Display individualized pictures identifying users with posts they start.')); $group .= form_checkbox(t('Display pictures with comments'), 'xtemplate_picture_comment', 1, variable_get('xtemplate_picture_comment', 0), t('Display individualized pictures identifying users with their comments.')); - $output .= form_group(t('Avatar settings'), $group); + if(module_exist(taxonomy_image)) { + $group .= form_checkbox(t('Display category pictures with content'), 'xtemplate_taxonomy_images', 1, variable_get('xtemplate_taxonomy_images', 0), t('Display category pictures with site content.')); + } + $output .= form_group(t('Picture settings'), $group); return $output; } @@ -55,6 +58,16 @@ function xtemplate_node($node, $main = 0, $page = 0) { global $xtemplate; + if (module_exist("taxonomy") && ($taxonomy = taxonomy_link("taxonomy terms", $node))) { + $xtemplate->template->assign("taxonomy", theme_links($taxonomy)); + $xtemplate->template->parse("node.taxonomy"); + if (module_exist("taxonomy_image") && variable_get('xtemplate_taxonomy_images', 0)) { + foreach (taxonomy_node_get_terms($node->nid) as $term) { + $images[] = taxonomy_image_display($term->tid, "alt='$term->name'"); + } + } + } + $xtemplate->template->assign(array( "submitted" => t("Submitted by %a on %b.", array("%a" => format_name($node), @@ -64,7 +77,7 @@ "author" => format_name($node), "date" => format_date($node->created), "static" => ($main && $node->static) ? 'static' : '', - "content" => ($main && $node->teaser) ? $node->teaser : $node->body)); + "content" => ($images ? "
". implode(' ', $images) ."
" : '').(($main && $node->teaser) ? $node->teaser : $node->body))); if ($page == 0) { $xtemplate->template->parse("node.title"); @@ -75,11 +88,6 @@ $xtemplate->template->parse("node.picture"); } - if (module_exist("taxonomy") && ($taxonomy = taxonomy_link("taxonomy terms", $node))) { - $xtemplate->template->assign("taxonomy", theme_links($taxonomy)); - $xtemplate->template->parse("node.taxonomy"); - } - if ($links = link_node($node, $main)) { $xtemplate->template->assign("links", theme_links($links)); $xtemplate->template->parse("node.links"); --- database/updates.inc.orig 2004-05-31 20:13:49.889500672 -0400 +++ database/updates.inc 2004-05-31 20:17:52.041170667 -0400 @@ -58,7 +58,8 @@ "2004-04-21" => "update_84", "2004-04-27" => "update_85", "2004-05-10" => "update_86", - "2004-05-18" => "update_87" + "2004-05-18" => "update_87", + "2004-05-31" => "update_88" ); function update_32() { @@ -1093,6 +1094,26 @@ return $ret; } +function update_88() { + $ret = array(); + if ($GLOBALS["db_type"] == "mysql") { + $ret[] = update_sql("CREATE TABLE {term_image} ( + tid int(10) unsigned NOT NULL default '0', + path varchar(255) NOT NULL default '', + PRIMARY KEY (tid) + );"); + } + else { + $ret[] = update_sql("CREATE TABLE {term_image} ( + tid integer NOT NULL default '0', + path varchar(255) NOT NULL default '', + PRIMARY KEY (tid) +);"); + + } + return $ret; +} + function update_sql($sql) { $edit = $_POST["edit"]; $result = db_query($sql); --- database/database.mysql.orig 2004-05-31 20:19:28.171107949 -0400 +++ database/database.mysql 2004-05-31 20:22:06.992481982 -0400 @@ -540,6 +540,16 @@ ) TYPE=MyISAM; -- +-- Table structure for table 'term_image' +-- + +CREATE TABLE term_image ( + tid int(10) unsigned NOT NULL default '0', + path varchar(255) NULL, + PRIMARY KEY (tid) +) TYPE=MyISAM; + +-- -- Table structure for table 'term_node' -- --- database/database.pgsql.orig 2004-05-31 20:19:34.505444868 -0400 +++ database/database.pgsql 2004-05-31 20:23:34.222350050 -0400 @@ -533,6 +533,16 @@ CREATE INDEX term_hierarchy_parent_idx ON term_hierarchy(parent); -- +-- Table structure for term_image +-- + +CREATE TABLE term_image ( + tid integer NOT NULL default '0', + path varchar(255) NOT NULL default '', + PRIMARY KEY (tid) +); + +-- -- Table structure for term_node --