--- image.module 2007-01-13 01:47:54.000000000 -0500 +++ image.module.patch 2007-01-20 14:12:24.312500000 -0500 @@ -1,649 +1,656 @@ -'. t('The image module is used to create and administer images for your site. Each image is stored as a post, with thumbnails of the original generated automatically. There are two default thumbnail sizes, thumbnail and preview. The thumbnail size is shown as the preview for image posts and when browsing image galleries. The preview is the default size when first displaying an image node.') .'
'; - $output .= '' .t('Image administration allows the image directory and the image sizes to be set.
-Image galleries are used to organize and display images in galleries. The list tab allows users to edit existing image gallery names, descriptions, parents and relative position, known as a weight. The add galleries tab allows you to create a new image gallery defining name, description, parent and weight.') .'
'; - $output .= t('You can
-'. t('For more information please read the configuration and customization handbook Image page.', array('%image' => 'http://www.drupal.org/handbook/modules/image/')) .'
'; - return $output; - } -} - -/** - * Implementation of hook_node_info - */ -function image_node_info() { - return array( - 'image' => array( - 'name' => t('Image'), - 'module' => 'image', - 'description' => t('An image (with thumbnail). This is ideal for publishing photographs or screenshots.'), - ) - ); -} - -/** - * Implementation of hook_perm - */ -function image_perm() { - return array('create images', 'view original images', 'edit own images'); -} - -/** - * Implementation of hook_access - */ -function image_access($op, $node) { - global $user; - - if ($op == 'create' && user_access('create images')) { - return TRUE; - } - - if ($op == 'update' || $op == 'delete') { - if (user_access('edit own images') && ($user->uid == $node->uid)) { - return TRUE; - } - } -} - -/** - * Admin settings callback. - */ -function image_admin_settings() { - _image_check_settings(); - - $form['image_updated'] = array('#type' => 'hidden', '#value' => time()); - - $form['paths'] = array('#type' => 'fieldset', '#title' => t('File paths')); - $form['paths']['image_default_path'] = array('#type' => 'textfield', '#title' => t('Default image path'), '#default_value' => variable_get('image_default_path', 'images'), '#description' => t('Subdirectory in the directory "%dir" where pictures will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files')))); - - $form['sizes'] = array('#type' => 'fieldset', '#title' => t('Image sizes')); - $form['sizes']['image_sizes'] = image_settings_sizes_form(); - - return system_settings_form($form); -} - -function image_settings_sizes_form() { - $sizes = _image_get_sizes(); - - $form['#type'] = 'item'; - $form['#description'] = t('Select various pixel dimensions, "thumbnail" and "preview" are required.'); - $form['#tree'] = TRUE; - $form['#theme'] = 'image_settings_sizes_form'; - for ($i = 0; $i < 5; $i++) { - $form[$i]['label'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['label'], '#size' => 25); - if (in_array($sizes[$i]['label'], _image_required_sizes())) { - $form[$i]['label']['#attributes'] = array('disabled' => 'disabled'); - $form[$i]['label']['#value'] = $sizes[$i]['label']; - } - $form[$i]['width'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['width'], '#size' => 5, '#maxlength' => 5); - $form[$i]['height'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['height'], '#size' => 5, '#maxlength' => 5); - } - - return $form; -} - -function theme_image_settings_sizes_form(&$form) { - $header = array(t('Label'), t('Width'), t('Height')); - foreach (element_children($form) as $key) { - $row = array(); - $row[] = drupal_render($form[$key]['label']); - $row[] = drupal_render($form[$key]['width']); - $row[] = drupal_render($form[$key]['height']); - $rows[] = $row; - - } - $output = theme('table', $header, $rows); - $output .= drupal_render($form); - - return $output; -} - -/** - * Implementation of hook_menu - */ -function image_menu($may_cache) { - $items = array(); - - if ($may_cache) { - $items[] = array('path' => 'node/add/image', 'title' => t('Image'), - 'access' => user_access('create images')); - $items[] = array('path' => 'image/view', 'title' => t('image'), - 'access' => user_access('access content'), - 'type' => MENU_CALLBACK, - 'callback' => 'image_fetch'); - $items[] = array('path' => 'admin/settings/image', - 'title' => ('Image'), - 'callback' => 'drupal_get_form', - 'callback arguments' => array('image_admin_settings'), - 'access' => user_access('administer site configuration'), - 'type' => MENU_NORMAL_ITEM, - 'description' => t('Image module settings.'), - ); - } - - return $items; -} - -/** - * Implements hook_cron. (deletes old temp images) - */ -function image_cron() { - $path = variable_get('image_default_path', 'images') . '/temp'; - $files = file_scan_directory(file_create_path($path), '.*'); - foreach ($files as $file => $info) { - if (time() - filemtime($file) > 60*60*6) { - file_delete($file); - } - } -} - -/** - * Implementation of hook_prepare(). - */ -function image_prepare(&$node, $field_name) { - if (is_null($field_name)) { - $field_name = 'image'; - } - if ($file = file_check_upload($field_name)) { - $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE)); - if ($file) { - if (!image_get_info($file->filepath)) { - form_set_error($field_name, t('Uploaded file is not a valid image')); - return; - } - } - else { - return; - } - $node->images['_original'] = $file->filepath; - _image_build_derivatives($node, true); - $node->new_file = TRUE; - } -} - -/** - * implement hook_file_download - */ -function image_file_download($file) { - $size = image_get_info(file_create_path($file)); - if ($size) { - $headers = array('Content-Type: ' . $size['mime_type']); - return $headers; - } -} - -/** - * Implementation of hook_link. - */ -function image_link($type, $node, $main = 0) { - $links = array(); - - if ($type == 'node' && $node->type == 'image' && !$main) { - $request = ($_GET['size']) ? $_GET['size'] : 'preview'; - foreach (_image_get_sizes() as $size) { - if ($node->images[$request] != $node->images[$size['label']]) { - $links["image_size_$size"] = array('title' => t($size['label']), 'href' => 'node/' . $node->nid, 'query' => 'size=' . urlencode($size['label'])); - } - } - if (user_access('view original images') && ($node->images[$request] != $node->images['_original'])) { - $links['image_size_original'] = array('title' => t('original'), 'href' => 'node/' . $node->nid, 'query' => 'size=_original'); - } - } - - return $links; -} - -/** - * Implementation of hook_block. - * - * Offers 2 blocks: latest image and random image - */ -function image_block($op = 'list', $delta = 0) { - switch ($op) { - case 'list': - $block[0]['info'] = t('Latest image'); - $block[1]['info'] = t('Random image'); - - return $block; - case 'view': - if (user_access('access content')) { - switch($delta) { - case 0: - $images = image_get_latest(); - $block['subject'] = t('Latest image'); - $block['content'] = l(image_display($images[0], 'thumbnail'), 'node/'.$images[0]->nid, array(), NULL, NULL, FALSE, TRUE); - break; - case 1: - $images = image_get_random(); - $block['subject'] = t('Random image'); - $block['content'] = l(image_display($images[0], 'thumbnail'), 'node/'.$images[0]->nid, array(), NULL, NULL, FALSE, TRUE); - break; - } - } - return $block; - } -} - -function image_form_add_thumbnail($form_id, $edit) { - if ($edit['images']['thumbnail']) { - $node = (object)($edit); - $form = array('#type' => 'item', '#title' => t('Thumbnail'), '#value' => image_display($node, 'thumbnail'), '#weight' => -10); - } - return $form; -} - -/** - * Implementation of hook_form - */ -function image_form(&$node, &$param) { - _image_check_settings(); - - $form['#attributes'] = array("enctype" => "multipart/form-data"); - $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#size' => 60, '#maxlength' => 128, '#required' => TRUE, '#default_value' => $node->title); - - $sizes = _image_get_sizes(); - $form['images']['#tree'] = TRUE; - if ($node->new_file) { - $form['images']['_original'] = array('#type' => 'hidden', '#value' => $node->images['_original']); - } - else { - $form['images']['_original'] = array('#type' => 'hidden', '#default_value' => $node->images['_original']); - } - foreach ($sizes as $size) { - if ($node->new_file) { - $form['images'][$size['label']] = array('#type' => 'hidden', '#value' => $node->images[$size['label']]); - } - else { - $form['images'][$size['label']] = array('#type' => 'hidden', '#default_value' => $node->images[$size['label']]); - } - } - - $form['thumbnail']['#after_build'][] = 'image_form_add_thumbnail'; - - $form['image'] = array('#type' => 'file', '#title' => t('Image'), '#description' => t('Click "Browse..." to select an image to upload.'), '#weight' => -3); - $form['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#rows' => 20, '#default_value' => $node->body); - $form['format'] = filter_form($node->format); - - return $form; -} - -/** - * Implementation of hook_view - */ -function image_view(&$node, $teaser = 0, $page = 0) { - $request = ($_GET['size']) ? $_GET['size'] : 'preview'; - $request = check_plain($request); - $node = node_prepare($node, $teaser); - $node->content['image'] = array( - '#value' => theme('image_body', $node, $request), - '#weight' => 0, - ); - return $node; -} - -/** - * Implementation of hook_load - */ -function image_load(&$node) { - $result = db_query("SELECT filename, filepath FROM {files} WHERE nid=%d", $node->nid); - $node->images = array(); - while ($file = db_fetch_object($result)) { - $node->images[$file->filename] = $file->filepath; - } - // special images - if (empty($node->images['thumbnail'])) { - $node->images['thumbnail'] = $node->images['_original']; - } - if (empty($node->images['preview'])) { - $node->images['preview'] = $node->images['_original']; - } -} - -/** - * Implementation of hook_insert - */ -function image_insert($node) { - foreach ($node->images as $label => $image) { - _image_insert($node, $label, file_create_path($image)); - } -} - -/** - * Implementation of hook_update - */ -function image_update($node) { - foreach ($node->images as $label => $image) { - $old_path = db_result(db_query("SELECT filepath FROM {files} WHERE filename='%s' AND nid=%d", $label, $node->nid)); - // This is a new image. - if ($old_path != $image) { - file_delete(file_create_path($old_path)); - db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", $label, $node->nid); - _image_insert($node, $label, file_create_path($image)); - } - } -} - -/** - * Implementation of hook_delete. - */ -function image_delete($node) { - foreach ($node->images as $label => $image) { - file_delete(file_create_path($image)); - db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", $label, $node->nid); - } -} - -/** - * Implementation of hook_views_tables() - */ -function image_views_tables() { - $tables['image'] = array( - 'name' => 'node', - 'fields' => array( - 'nid' => array( - 'name' => t('Image: Display Image'), - 'handler' => array( - 'image_views_handler_image_img' => t('Image'), - 'image_views_handler_image_img_link' => t('Image with link'), - ), - 'option' => array( - '#type' => 'select', - '#options' => 'image_views_handler_filter_image_size', - ), - 'notafield' => true, - 'sortable' => false, - ), - ), - ); - return $tables; -} - -/** - * Create an' .t('Image administration allows the image directory and the image sizes to be set.
+Image galleries are used to organize and display images in galleries. The list tab allows users to edit existing image gallery names, descriptions, parents and relative position, known as a weight. The add galleries tab allows you to create a new image gallery defining name, description, parent and weight.') .'
'; + $output .= t('You can
+'. t('For more information please read the configuration and customization handbook Image page.', array('%image' => 'http://www.drupal.org/handbook/modules/image/')) .'
'; + return $output; + } +} + +/** + * Implementation of hook_node_info + */ +function image_node_info() { + return array( + 'image' => array( + 'name' => t('Image'), + 'module' => 'image', + 'description' => t('An image (with thumbnail). This is ideal for publishing photographs or screenshots.'), + ) + ); +} + +/** + * Implementation of hook_perm + */ +function image_perm() { + return array('create images', 'view original images', 'edit own images'); +} + +/** + * Implementation of hook_access + */ +function image_access($op, $node) { + global $user; + + if ($op == 'create' && user_access('create images')) { + return TRUE; + } + + if ($op == 'update' || $op == 'delete') { + if (user_access('edit own images') && ($user->uid == $node->uid)) { + return TRUE; + } + } +} + +/** + * Admin settings callback. + */ +function image_admin_settings() { + _image_check_settings(); + + $form['image_updated'] = array('#type' => 'hidden', '#value' => time()); + + $form['paths'] = array('#type' => 'fieldset', '#title' => t('File paths')); + $form['paths']['image_default_path'] = array('#type' => 'textfield', '#title' => t('Default image path'), '#default_value' => variable_get('image_default_path', 'images'), '#description' => t('Subdirectory in the directory "%dir" where pictures will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files')))); + + $form['sizes'] = array('#type' => 'fieldset', '#title' => t('Image sizes')); + $form['sizes']['image_sizes'] = image_settings_sizes_form(); + + return system_settings_form($form); +} + +function image_settings_sizes_form() { + $sizes = _image_get_sizes(); + + $form['#type'] = 'item'; + $form['#description'] = t('Select various pixel dimensions, "thumbnail" and "preview" are required.'); + $form['#tree'] = TRUE; + $form['#theme'] = 'image_settings_sizes_form'; + for ($i = 0; $i < 5; $i++) { + $form[$i]['label'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['label'], '#size' => 25); + if (in_array($sizes[$i]['label'], _image_required_sizes())) { + $form[$i]['label']['#attributes'] = array('disabled' => 'disabled'); + $form[$i]['label']['#value'] = $sizes[$i]['label']; + } + $form[$i]['width'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['width'], '#size' => 5, '#maxlength' => 5); + $form[$i]['height'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['height'], '#size' => 5, '#maxlength' => 5); + } + + return $form; +} + +function theme_image_settings_sizes_form(&$form) { + $header = array(t('Label'), t('Width'), t('Height')); + foreach (element_children($form) as $key) { + $row = array(); + $row[] = drupal_render($form[$key]['label']); + $row[] = drupal_render($form[$key]['width']); + $row[] = drupal_render($form[$key]['height']); + $rows[] = $row; + + } + $output = theme('table', $header, $rows); + $output .= drupal_render($form); + + return $output; +} + +/** + * Implementation of hook_menu + */ +function image_menu($may_cache) { + $items = array(); + + if ($may_cache) { + $items[] = array('path' => 'node/add/image', 'title' => t('Image'), + 'access' => user_access('create images')); + $items[] = array('path' => 'image/view', 'title' => t('image'), + 'access' => user_access('access content'), + 'type' => MENU_CALLBACK, + 'callback' => 'image_fetch'); + $items[] = array('path' => 'admin/settings/image', + 'title' => ('Image'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('image_admin_settings'), + 'access' => user_access('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + 'description' => t('Image module settings.'), + ); + } + + return $items; +} + +/** + * Implements hook_cron. (deletes old temp images) + */ +function image_cron() { + $path = variable_get('image_default_path', 'images') . '/temp'; + $files = file_scan_directory(file_create_path($path), '.*'); + foreach ($files as $file => $info) { + if (time() - filemtime($file) > 60*60*6) { + file_delete($file); + } + } +} + +/** + * Implementation of hook_prepare(). + */ +function image_prepare(&$node, $field_name) { + if (is_null($field_name)) { + $field_name = 'image'; + } + if ($file = file_check_upload($field_name)) { + $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE)); + if ($file) { + if (!image_get_info($file->filepath)) { + form_set_error($field_name, t('Uploaded file is not a valid image')); + return; + } + } + else { + return; + } + $node->images['_original'] = $file->filepath; + _image_build_derivatives($node, true); + $node->new_file = TRUE; + } +} + +/** + * implement hook_file_download + */ +function image_file_download($file) { + $size = image_get_info(file_create_path($file)); + if ($size) { + $headers = array('Content-Type: ' . $size['mime_type']); + return $headers; + } +} + +/** + * Implementation of hook_link. + */ +function image_link($type, $node, $main = 0) { + $links = array(); + + if ($type == 'node' && $node->type == 'image' && !$main) { + $request = ($_GET['size']) ? $_GET['size'] : 'preview'; + foreach (_image_get_sizes() as $size) { + if ($node->images[$request] != $node->images[$size['label']]) { + $links["image_size_$size"] = array('title' => t($size['label']), 'href' => 'node/' . $node->nid, 'query' => 'size=' . urlencode($size['label'])); + } + } + if (user_access('view original images') && ($node->images[$request] != $node->images['_original'])) { + $links['image_size_original'] = array('title' => t('original'), 'href' => 'node/' . $node->nid, 'query' => 'size=_original'); + } + } + + return $links; +} + +/** + * Implementation of hook_block. + * + * Offers 2 blocks: latest image and random image + */ +function image_block($op = 'list', $delta = 0) { + switch ($op) { + case 'list': + $block[0]['info'] = t('Latest image'); + $block[1]['info'] = t('Random image'); + + return $block; + case 'view': + if (user_access('access content')) { + switch($delta) { + case 0: + $images = image_get_latest(); + $block['subject'] = t('Latest image'); + $block['content'] = l(image_display($images[0], 'thumbnail'), 'node/'.$images[0]->nid, array(), NULL, NULL, FALSE, TRUE); + break; + case 1: + $images = image_get_random(); + $block['subject'] = t('Random image'); + $block['content'] = l(image_display($images[0], 'thumbnail'), 'node/'.$images[0]->nid, array(), NULL, NULL, FALSE, TRUE); + break; + } + } + return $block; + } +} + +function image_form_add_thumbnail($form_id, $edit) { + if ($edit['images']['thumbnail']) { + $node = (object)($edit); + $form = array('#type' => 'item', '#title' => t('Thumbnail'), '#value' => image_display($node, 'thumbnail'), '#weight' => -10); + } + return $form; +} + +/** + * Implementation of hook_form + */ +function image_form(&$node, &$param) { + _image_check_settings(); + + $form['#attributes'] = array("enctype" => "multipart/form-data"); + $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#size' => 60, '#maxlength' => 128, '#required' => TRUE, '#default_value' => $node->title); + + $sizes = _image_get_sizes(); + $form['images']['#tree'] = TRUE; + if ($node->new_file) { + $form['images']['_original'] = array('#type' => 'hidden', '#value' => $node->images['_original']); + } + else { + $form['images']['_original'] = array('#type' => 'hidden', '#default_value' => $node->images['_original']); + } + foreach ($sizes as $size) { + if ($node->new_file) { + $form['images'][$size['label']] = array('#type' => 'hidden', '#value' => $node->images[$size['label']]); + } + else { + $form['images'][$size['label']] = array('#type' => 'hidden', '#default_value' => $node->images[$size['label']]); + } + } + + $form['thumbnail']['#after_build'][] = 'image_form_add_thumbnail'; + + $form['image'] = array('#type' => 'file', '#title' => t('Image'), '#description' => t('Click "Browse..." to select an image to upload.'), '#weight' => -3); + $form['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#rows' => 20, '#default_value' => $node->body); + $form['format'] = filter_form($node->format); + + return $form; +} + +/** + * Implementation of hook_view + */ +function image_view(&$node, $teaser = 0, $page = 0) { + $request = ($_GET['size']) ? $_GET['size'] : 'preview'; + $request = check_plain($request); + $node = node_prepare($node, $teaser); + if ($teaser){ + $node->content['image'] = array( + '#value' => theme('image_teaser', $node, $request), + '#weight' => 0, + ); + } else { + $node->content['image'] = array( + '#value' => theme('image_body', $node, $request), + '#weight' => 0, + ); + } + return $node; +} + +/** + * Implementation of hook_load + */ +function image_load(&$node) { + $result = db_query("SELECT filename, filepath FROM {files} WHERE nid=%d", $node->nid); + $node->images = array(); + while ($file = db_fetch_object($result)) { + $node->images[$file->filename] = $file->filepath; + } + // special images + if (empty($node->images['thumbnail'])) { + $node->images['thumbnail'] = $node->images['_original']; + } + if (empty($node->images['preview'])) { + $node->images['preview'] = $node->images['_original']; + } +} + +/** + * Implementation of hook_insert + */ +function image_insert($node) { + foreach ($node->images as $label => $image) { + _image_insert($node, $label, file_create_path($image)); + } +} + +/** + * Implementation of hook_update + */ +function image_update($node) { + foreach ($node->images as $label => $image) { + $old_path = db_result(db_query("SELECT filepath FROM {files} WHERE filename='%s' AND nid=%d", $label, $node->nid)); + // This is a new image. + if ($old_path != $image) { + file_delete(file_create_path($old_path)); + db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", $label, $node->nid); + _image_insert($node, $label, file_create_path($image)); + } + } +} + +/** + * Implementation of hook_delete. + */ +function image_delete($node) { + foreach ($node->images as $label => $image) { + file_delete(file_create_path($image)); + db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", $label, $node->nid); + } +} + +/** + * Implementation of hook_views_tables() + */ +function image_views_tables() { + $tables['image'] = array( + 'name' => 'node', + 'fields' => array( + 'nid' => array( + 'name' => t('Image: Display Image'), + 'handler' => array( + 'image_views_handler_image_img' => t('Image'), + 'image_views_handler_image_img_link' => t('Image with link'), + ), + 'option' => array( + '#type' => 'select', + '#options' => 'image_views_handler_filter_image_size', + ), + 'notafield' => true, + 'sortable' => false, + ), + ), + ); + return $tables; +} + +/** + * Create an