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

Contents of /contributions/modules/taxonomy_image/taxonomy_image.module

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


Revision 1.13 - (show annotations) (download) (as text)
Thu Jan 18 04:43:13 2007 UTC (2 years, 10 months ago) by jeremy
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +35 -23 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id: taxonomy_image.module,v 1.12 2006/04/27 02:00:24 jeremy Exp $ taxonomy_image.module
3
4 /*
5 ** taxonomy_image.module:
6 ** Simple module for providing an association between taxonomy terms and
7 ** images.
8 ** Written by Jeremy Andrews <jeremy@kerneltrap.org>, May 2004.
9 */
10
11 /**
12 * Call this function from your theme or other php code to display the
13 * image associated with the given term id. An html <img> tag will be returned
14 * if an image is found. The format of the link can be modified with the
15 * tags parameter.
16 *
17 * @param int $tid the term id.
18 * @param string $tags optional tags to add into the <img src=''> link
19 *
20 * @return string An html <img src> link.
21 */
22 function taxonomy_image_display($tid, $tags = NULL) {
23 global $user;
24 static $image = array();
25
26 if (user_access('access taxonomy images') &&
27 !$user->taxonomy_image_disable_images) {
28 // do lookup, return full display path
29 if (!$image[$tid]->url) {
30 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))) {
31 $image[$tid]->url = file_create_url($image[$tid]->path);
32 }
33 else if (variable_get('taxonomy_image_recursive', 0)) {
34 // walk up the taxonomy hierarchy and look for an image
35 $orig = $tid;
36 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))) {
37 $tid = $parent->tid;
38 if ($image[$tid]->url) {
39 // we have already loaded this image from the database
40 $image[$orig] = $image[$tid];
41 break;
42 }
43 else 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))) {
44 // we found a parent with a configured image, use it
45 $image[$tid]->url = file_create_url($image[$tid]->path);
46 $image[$orig] = $image[$tid];
47 break;
48 }
49 }
50 }
51 }
52 if ($image[$tid]->url) {
53 if (!$image[$tid]->width || !$image[$tid]->height) {
54 list($image[$tid]->width, $image[$tid]->height) = getimagesize($image[$tid]->path);
55 // handle image resizing
56 switch (variable_get('taxonomy_image_resize', 0)) {
57 case 3: // exact
58 if ($width = variable_get('taxonomy_image_width', 0))
59 $image[$tid]->width = $width;
60 if ($height = variable_get('taxonomy_image_height', 0))
61 $image[$tid]->height = $height;
62 break;
63 case 2: // not less than
64 if (($width = variable_get('taxonomy_image_width', 0)) &&
65 ($width > $image[$tid]->width)) {
66 $width_scale = $image[$tid]->width / $width;
67 }
68 if (($height = variable_get('taxonomy_image_height', 0)) &&
69 ($height > $image[$tid]->height)) {
70 $height_scale = $image[$tid]->height / $height;
71 }
72 if ($height_scale || $width_scale) {
73 if ($width_scale && $height_scale)
74 $scale = min($width_scale, $height_scale);
75 else
76 $scale = $width_scale ? $width_scale : $height_scale;
77 $image[$tid]->height = $image[$tid]->height / $scale;
78 $image[$tid]->width = $image[$tid]->width / $scale;
79 }
80 break;
81 case 1: // not greater than
82 if (($width = variable_get('taxonomy_image_width', 0)) &&
83 ($width < $image[$tid]->width)) {
84 $width_scale = $image[$tid]->width / $width;
85 }
86 if (($height = variable_get('taxonomy_image_height', 0)) &&
87 ($height < $image[$tid]->height)) {
88 $height_scale = $image[$tid]->height / $height;
89 }
90 if ($height_scale || $width_scale) {
91 $scale = max($width_scale, $height_scale);
92 $image[$tid]->height = $image[$tid]->height / $scale;
93 $image[$tid]->width = $image[$tid]->width / $scale;
94 }
95 break;
96 }
97 }
98 $current = $image[$tid];
99 return "<img src='$current->url' width='$current->width' height='$current->height' alt='$current->name' $tags />";
100 }
101 }
102 return '';
103 }
104
105 // standard Drupal functions
106 function taxonomy_image_perm() {
107 return array ('access taxonomy images', 'administer taxonomy images', 'can disable taxonomy images');
108 }
109
110 function taxonomy_image_help($section = '') {
111 switch ($section) {
112 case 'admin/content/taxonomy/image':
113 return t('The taxonomy_image module allows site administrators to associate images with category terms. Once defined, this association allows Drupal themes to display images with site content. For example, this module might be used to display a penguin with content about Linux, and a cheeseburger with content about junk food. To upload a new image for a specific term, click "upload image" next to the term. To modify or delete and existing image, click "edit image". To learn more about how to create vocabularies and terms, review the <a href="%taxonomy-help">taxonomy help page</a>.', array('%taxonomy-help' => url('admin/help/taxonomy')));
114 case 'admin/help#taxonomy_image':
115 return t('
116 <h3>Introduction</h3>
117 <p>The taxonomy_image module allows site administrators to associate images with category 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.</p>
118 <p>The module allows both a one-to-one term-to-image relationship, and a many-to-one terms-to-image relationship.</p>
119 <p>The taxonomy_image module requires that the taxonomy module also be enabled.</p>
120 <h3>Configuration</h3>
121 <h4>Uploading images</h4>
122 <p>With the taxonomy_image module enabled, images can be uploaded and associated with category terms at \'administer >> categories >> 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\'.
123 <p>Continue this process to upload appropriate images for your category terms. Note that by default images will be displayed at the size they were uploaded. Alternatively, you can go to \'administer >> settings >> taxonomy_image\' to force the display height and/or width of all taxonomy images.</p>
124 <h4>Permissions</h4>
125 <p>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.</p>
126 <p>A third permission, \'administer taxonomy images\', controls which users are allowed to configure taxonomy images.</p>
127 <h4>Recursive image display</h4>
128 <p>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 >> settings >> 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).</p>
129 <h3>Displaying images</h3>
130 <p>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:
131 <pre>
132 foreach (taxonomy_node_get_terms($node->nid) as $term) {
133 if ($image = taxonomy_image_display($term->tid)) {
134 $output .= "$image";
135 }
136 </pre>
137 ');
138 }
139 }
140
141 function taxonomy_image_menu($may_cache) {
142 $items = array();
143
144 if ($may_cache) {
145 $items[] = array('path' => 'admin/content/taxonomy/image', 'title' => t('Category Images'),
146 'callback' => 'taxonomy_image_admin',
147 'access' => user_access('administer taxonomy images'),
148 'type' => MENU_LOCAL_TASK);
149 // Admin Settings
150 $items[]= array (
151 'path' => 'admin/settings/taxonomy_image',
152 'title' => t('Taxonomy Image'),
153 'callback' => 'drupal_get_form',
154 'callback arguments' => array('taxonomy_image_admin_settings'),
155 'access' => user_access('administer site configuration'),
156 'description' => t('Global configuration of taxonomy image functionality.'),
157 'type' => MENU_NORMAL_ITEM,
158 );
159 }
160
161 return $items;
162 }
163
164 /**
165 * Implementation of hook_user().
166 */
167 function taxonomy_image_user($type, $edit, $user) {
168 switch ($type) {
169 case 'form':
170 if (user_access('can disable taxonomy images')) {
171 $form['content_images'] = array(
172 '#type' => 'fieldset',
173 '#title' => t('Content images'),
174 );
175 $form['content_images']['taxonomy_image_disable_images'] = array(
176 '#type' => 'checkbox',
177 '#title' => t('Disable images'),
178 '#return_value' => 1,
179 '#default_value' => $user->taxonomy_image_disable_images,
180 '#description' => t('Check this box to disable the display of content images.'),
181 );
182 return $form;
183 }
184 break;
185 }
186 }
187
188 /**
189 * Administration Page
190 */
191 function taxonomy_image_admin_settings() {
192 if (!file_check_directory(file_create_path(variable_get('taxonomy_image_path', 'category_pictures')), FILE_CREATE_DIRECTORY)) {
193 $error = theme('error', t('The picture directory does not exist, or is not writable.'));
194 }
195
196 $form['pictures'] = array(
197 '#type' => 'fieldset',
198 '#title' => t('Pictures'),
199 );
200 $form['pictures']['taxonomy_image_path'] = array(
201 '#type' => 'textfield',
202 '#title' => t('Picture image path'),
203 '#default_value' => variable_get('taxonomy_image_path', 'category_pictures'),
204 '#size' => 45,
205 '#maxlength' => 255,
206 '#description' => t('Subdirectory in the directory "%dir" where category pictures will be stored.', array('%dir' => variable_get('file_directory_path', 'files') . '/')) . $error,
207 );
208 $form['pictures']['taxonomy_image_resize'] = array(
209 '#type' => 'radios',
210 '#title' => t('Picture resize'),
211 '#default_value' => variable_get('taxonomy_image_resize', 0),
212 '#options' => array(3 => 'Exact', 2 => 'Not less than', 1 => 'Not greater than', 0 => 'Disabled'),
213 '#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).'),
214 );
215 $form['pictures']['taxonomy_image_height'] = array(
216 '#type' => 'textfield',
217 '#title' => t('Picture height'),
218 '#default_value' => variable_get('taxonomy_image_height', ''),
219 '#size' => 5,
220 '#maxlength' => 6,
221 '#description' => t('Specify a height in pixels. If left blank or set to 0 this field is ignored.'),
222 );
223 $form['pictures']['taxonomy_image_width'] = array(
224 '#type' => 'textfield',
225 '#title' => t('Picture width'),
226 '#default_value' => variable_get('taxonomy_image_width', ''),
227 '#size' => 5,
228 '#maxlength' => 6,
229 '#description' => t('Specify a width in pixels. If left blank or set to 0 this field is ignored.'),
230 );
231
232 $form['advanced'] = array(
233 '#type' => 'fieldset',
234 '#title' => t('Advanced'),
235 );
236 $form['advanced']['taxonomy_image_recursive'] = array(
237 '#type' => 'radios',
238 '#title' => t('Recursive image display'),
239 '#default_value' => variable_get('taxonomy_image_recursive', 0),
240 '#options' => array(1 => 'Enabled', 0 => 'Disabled'),
241 '#description' => 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.'),
242 );
243
244 return system_settings_form($form);
245 }
246
247 function taxonomy_image_file_download($file) {
248 if (user_access('access taxonomy images')) {
249 $path = file_create_path($file);
250 if (function_exists('mime_content_type')) {
251 if ($type = mime_content_type($path))
252 return array("Content-type: $type");
253 }
254 // support for pre-PHP 4.3+
255 list($width, $height, $type, $attr) = getimagesize($path);
256 $types = array(
257 IMAGETYPE_GIF => 'image/gif',
258 IMAGETYPE_JPEG => 'image/jpeg',
259 IMAGETYPE_PNG => 'image/png',
260 IMAGETYPE_SWF => 'application/x-shockwave-flash',
261 IMAGETYPE_PSD => 'image/psd',
262 IMAGETYPE_BMP => 'image/bmp',
263 IMAGETYPE_TIFF_II => 'image/tiff',
264 IMAGETYPE_TIFF_MM => 'image/tiff',
265 IMAGETYPE_JPC => 'application/octet-stream',
266 IMAGETYPE_JP2 => 'image/jp2',
267 IMAGETYPE_JPX => 'application/octet-stream',
268 IMAGETYPE_JB2 => 'application/octet-stream',
269 IMAGETYPE_SWC => 'application/x-shockwave-flash',
270 IMAGETYPE_IFF => 'image/iff',
271 IMAGETYPE_WBMP => 'image/vnd.wap.wbmp',
272 IMAGETYPE_XBM => 'image/xbm'
273 );
274 if (isset($types[$type])) {
275 return array('Content-type: '. $types[$type]);
276 }
277 }
278 }
279
280 // taxonomy_image specific functions
281 function taxonomy_image_admin() {
282 global $form_values;
283 $op = $_POST['op'];
284 $tid = $_POST['tid'];
285
286 // TODO: Use menus, not arg()
287
288 if (empty($op)) {
289 $op = arg(3);
290 }
291
292 switch ($op) {
293 case 'image':
294 if (arg(4) == 'add' || arg(4) == 'edit') {
295 $output = drupal_get_form('taxonomy_image_form', (array)(taxonomy_image_get_term(arg(5))));
296 break;
297 }
298 $output = taxonomy_image_overview();
299 break;
300 case t('Save'):
301 $output = taxonomy_image_save($tid);
302 $output = taxonomy_image_overview();
303 break;
304 case t('Delete'):
305 $output = taxonomy_image_delete($tid);
306 $output = taxonomy_image_overview();
307 break;
308 default:
309 $output = taxonomy_image_overview();
310 }
311
312 print theme('page', $output);
313 }
314
315 function taxonomy_image_overview() {
316 $output = '';
317 if (variable_get('taxonomy_image_recursive', 0)) {
318 $output .= '<h4>'. t('Recursively displaying images.') .'</h4>';
319 }
320
321 $header = array(t('name'), t('node types'), t('image'));
322
323 $vocabularies = taxonomy_get_vocabularies();
324
325 foreach ($vocabularies as $vocabulary) {
326 $types = array();
327 $rows = array();
328 foreach(explode(',', $vocabulary->nodes) as $type) {
329 $types[] = node_invoke($type, 'node_name');
330 }
331 $rows[] = array($vocabulary->name, array('data' => implode(', ', $types), 'align' => 'center'), '');
332
333 $tree = taxonomy_get_tree($vocabulary->vid);
334 if ($tree) {
335 foreach ($tree as $term) {
336 $data = str_repeat('--', $term->depth) .' '. $term->name .' ('. ( _taxonomy_image_exists($term->tid) ? l(t('edit image'), "admin/content/taxonomy/image/edit/$term->tid") : l(t('upload image'), "admin/content/taxonomy/image/add/$term->tid") ) .')<br />';
337 /* use taxonomy_image_display() instead of _taxonomy_image_exists() in
338 ** case image display recursion is enabled...
339 */
340 $image = taxonomy_image_display($term->tid) ? taxonomy_image_display($term->tid) : '';
341 $rows[] = array(array('data' => $data, 'colspan' => 2), $image);
342 }
343 }
344
345 $output .= theme('table', $header, $rows);
346 }
347
348 return $output;
349 }
350
351 function taxonomy_image_get_term($tid) {
352 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));
353 }
354
355 function taxonomy_image_form($edit = array()) {
356 $form['#method'] = 'post';
357 $form['#action'] = 0;
358 $form['#attributes'] = array('enctype' => 'multipart/form-data');
359
360 // A path may be set when the image doesn't exist if using recursion
361 if (!is_null($edit['path']) && _taxonomy_image_exists($edit['tid'])) {
362 $form['current_image'] = array(
363 '#type' => 'fieldset',
364 '#title' => t('Current Image'),
365 );
366 $form['current_image']['image'] = array(
367 '#value' => taxonomy_image_display($edit['tid']) .'<br />'
368 );
369 $form['current_image']['delete'] = array(
370 '#type' => 'submit',
371 '#value' => t('Delete'),
372 );
373 drupal_set_title(t('Edit image'));
374 }
375
376 $form['new_image'] = array(
377 '#type' => 'fieldset',
378 '#title' => t('Upload new image'),
379 );
380 $form['new_image']['path'] = array(
381 '#type' => 'file',
382 '#title' => t('Taxonomy image file'),
383 '#size' => 40,
384 '#description' => t("The image file you wish to associate with the '%term' term.", array('%term' => $edit['name'])),
385 );
386 $form['new_image']['submit'] = array(
387 '#type' => 'submit',
388 '#value' => t('Save'),
389 );
390 if (!is_null($edit['tid'])) {
391 $form['new_image']['tid'] = array(
392 '#type' => 'hidden',
393 '#value' => $edit['tid'],
394 );
395 }
396
397 return $form;
398 }
399
400 function taxonomy_image_save($tid) {
401 $edit['tid'] = $tid;
402 $fields = array('tid', 'path');
403 if ($file = file_save_upload('path', file_create_path(variable_get('taxonomy_image_path', 'category_pictures')))) {
404 $edit['path'] = $file->filepath;
405
406 if ($old_image = db_fetch_object(db_query('SELECT tid FROM {term_image} WHERE tid = %d', $edit['tid']))) {
407 // delete old image before saving the new one
408 taxonomy_image_delete($old_image->tid);
409 }
410
411 foreach ($fields as $field) {
412 $values[] = (string)db_escape_string($edit[$field]);
413 }
414
415 db_query('INSERT INTO {term_image} (' .implode(', ', $fields). ') VALUES (\'' .implode('\', \'', $values). '\')');
416 cache_clear_all();
417
418 $message = t('Image uploaded.');
419 }
420 else if (!file_check_directory(file_create_path(variable_get('taxonomy_image_path', 'category_pictures')))) {
421 // we know waht's wrong, so generate a more useful error message
422 $message = theme('error', t('The category picture directory "%dir" does not exist, or is not writable.', array('%dir' => variable_get('file_directory_path', 'files'). '/' . variable_get('taxonomy_image_path', 'category_pictures'))));
423 }
424 else {
425 $message = theme('error', t('Image upload failed.'));
426 }
427 return drupal_set_message($message);
428 }
429
430 function taxonomy_image_delete($tid) {
431 file_delete(db_result(db_query('SELECT path FROM {term_image} WHERE tid = %d', $tid)));
432 db_query('DELETE FROM {term_image} WHERE tid = %d', $tid);
433 cache_clear_all();
434 return drupal_set_message(t('Image deleted.'));
435 }
436
437 function _taxonomy_image_exists($tid) {
438 if (db_fetch_object(db_query('SELECT path FROM {term_image} WHERE tid = %d', $tid))) {
439 return 1;
440 }
441 return 0;
442 }
443
444 ?>

  ViewVC Help
Powered by ViewVC 1.1.2