Index: contributions/modules/upload_image/upload_image.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/upload_image/upload_image.module,v retrieving revision 1.24 diff -u -p -r1.24 upload_image.module --- contributions/modules/upload_image/upload_image.module 29 Aug 2007 15:20:58 -0000 1.24 +++ contributions/modules/upload_image/upload_image.module 15 Jan 2008 21:23:40 -0000 @@ -4,6 +4,17 @@ */ /** + * Implementation of hook_init(). + * + * Adds views integration if views.module installed + */ +function upload_image_init() { + if (module_exists('views')) { + include_once drupal_get_path('module', 'upload_image') .'/upload_image.views.inc'; + } +} + +/** * Implementation of hook_link(). */ function upload_image_link($type, $node = 0, $main = 0) { @@ -77,7 +88,7 @@ function upload_image_nodeapi(&$node, $o case 'view': if (variable_get("upload_$node->type", 1) == 1 && $node->files && $node->nid && user_access('view uploaded files')) { $images = upload_image_load($node); - drupal_add_css(drupal_get_path('module','upload_image') .'/upload_image.css'); + drupal_add_css(drupal_get_path('module', 'upload_image') .'/upload_image.css'); $rows = array(); $thumbnails = array(); @@ -235,3 +246,12 @@ function theme_upload_images_body($thumb function theme_upload_images_teaser($thumbnails) { return theme('upload_images', $thumbnails); } + +function theme_upload_images_titled($images) { + $list = array(); + foreach ($images as $image) { + $list[] = $image['image'] .''. $image['title'] .''; + } + + return theme('upload_images', $list); +} Index: contributions/modules/upload_image/upload_image.views.inc =================================================================== RCS file: contributions/modules/upload_image/upload_image.views.inc diff -N contributions/modules/upload_image/upload_image.views.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ contributions/modules/upload_image/upload_image.views.inc 15 Jan 2008 21:23:40 -0000 @@ -0,0 +1,248 @@ + 'upload_images', + // don't want to multiple join onto the node table so on file and adding query_handler + 'join' => array( + 'left' => array( + 'table' => 'file_revisions', + 'field' => 'fid', + ), + 'right' => array( + 'field' => 'fid', + ), + ), + 'filters' => array( + 'nid' => array( + 'name' => t('Upload Image: Image attached'), + 'value' => array( + '#type' => 'select', + '#options' => 'views_handler_operator_yesno', + ), + 'operator' => array('=' => t('Equals')), + 'handler' => 'upload_image_filter_handler_image', + 'help' => t('Filter on if a node has a file uploaded with it as an image node.'), + ), + ), + ); + + $fields = array( + 'upload_other_files' => array( // minor edit on views/modules/views_upload.inc + 'name' => t('File: All files not attached images'), + 'notafield' => true, + 'query_handler' => 'upload_image_query_handler_file_all', + 'handler' => array( + 'upload_image_field_handler_file_all_other' => t('All files'), + 'upload_image_field_handler_file_all_other_listed' => t('Listed files')), + 'option' => array( + '#type' => 'select', + '#options' => array( + // kept original behind-the-scenes names link/nolink so patch to add description wouldn't affect existing Views + 'link' => t('File names with links'), + 'nolink' => t('File names without links'), + 'linkdesc' => t('File descriptions with links'), + 'nolinkdesc' => t('File descriptions without links'), + ), + ), + 'sortable' => false, + 'help' => t('Display all attached files in one field.'), + ), + ); + // create a field for each image size + $sizes = image_get_sizes(); + foreach ($sizes as $id => $size) { + $fields["upload_images_$id"] = array( + 'name' => t('File: Upload image files as @size', array( '@size' => $size['label'] )), + 'notafield' => true, + 'query_handler' => 'upload_image_query_handler_image', + 'handler' => array( + 'upload_image_field_handler_image' => t('Without title, as link'), + 'upload_image_field_handler_image_nolink' => t('Without title, no link'), + 'upload_image_field_handler_image_title' => t('With title, as link'), + 'upload_image_field_handler_image_title_nolink' => t('With title, no link'), + ), + 'option' => 'string', + 'derivative' => $id, + 'help' => t('Image(s) attached as files to a node and uploaded as image nodes.') .' '. + t('Options field can be used to restrict the number of images shown: start,number.') .' '. + t('Examples: 1,1 for just first image. 2 for all other images'), + ); + } + $tables['upload_images']['fields'] = $fields; + + return $tables; +} + +/** + * Query handler - for image upload. + */ +function upload_image_query_handler_image($field, $fieldinfo, &$query) { + $query->add_field('type', 'node'); + $query->add_field('vid', 'file_revisions'); +} + +/** + * Field handler - for uploaded images displayed without title. + */ +function upload_image_field_handler_image_nolink($fieldinfo, $fielddata, $value, $data) { + return upload_image_field_handler_image($fieldinfo, $fielddata, $value, $data, FALSE); +} +function upload_image_field_handler_image($fieldinfo, $fielddata, $value, $data, $link = TRUE) { + if (variable_get("upload_$data->node_type", 1) == 1 && user_access('view uploaded files')) { + $derivative = $fieldinfo['derivative']; + list($start, $count) = _upload_image_field_handler_image_options($fielddata['options']); + $n = 0; + $images = array(); + $result = db_query("SELECT f.*, u.* FROM {upload_images} u INNER JOIN {files} f ON u.fid = f.fid INNER JOIN {file_revisions} r ON f.fid = r.fid WHERE u.oid = %d AND r.vid = %d AND r.list = 1 ORDER BY f.fid", $data->nid, $data->vid); + $n = 0; + while ($file = db_fetch_object($result)) { + $n++; + if ($n >= $start && ($count == 0 || $n < $start + $count)) { + $image = new stdClass(); + $image->nid = $file->nid; + image_load($image); + $display = module_invoke('image', 'display', $image, $derivative); + $images[] = $link ? l($display, "node/$file->nid", array(), NULL, NULL, FALSE, TRUE) : $display; + } + } + } + + return (count($images)) ? theme('upload_images', $images) : NULL; +} + +/** + * Field handler - for uploaded images displayed with title. + */ +function upload_image_field_handler_image_title_nolink($fieldinfo, $fielddata, $value, $data) { + return upload_image_field_handler_image_title($fieldinfo, $fielddata, $value, $data, FALSE); +} +function upload_image_field_handler_image_title($fieldinfo, $fielddata, $value, $data, $link = TRUE) { + if (variable_get("upload_$data->node_type", 1) == 1 && user_access('view uploaded files')) { + $derivative = $fieldinfo['derivative']; + list($start, $count) = _upload_image_field_handler_image_options($fielddata['options']); + $n = 0; + $images = array(); + + $result = db_query("SELECT n.title, f.*, u.* FROM {upload_images} u INNER JOIN {files} f ON u.fid = f.fid INNER JOIN {file_revisions} r ON f.fid = r.fid INNER JOIN {node} n ON n.nid = u.nid WHERE oid = %d AND r.vid = %d AND r.list = 1 ORDER BY f.fid", $data->nid, $data->vid); + $n = 0; + while ($file = db_fetch_object($result)) { + $n++; + if ($n >= $start && ($count == 0 || $n < $start + $count)) { + $image = new stdClass(); + $image->nid = $file->nid; + image_load($image); + $display = module_invoke('image', 'display', $image, $derivative); + if ($link) { + $images[] = array( + 'image' => l($display, "node/$file->nid", array(), NULL, NULL, FALSE, TRUE), + 'title' => l($file->title, "node/$file->nid"), + ); + } + else { + $images[] = array( + 'image' => $display, + 'title' => check_plain($file->title), + ); + } + } + } + } + + return (count($images)) ? theme('upload_images_titled', $images) : NULL; +} + +/** + * Filter handler - on if there are upload images attached. + */ +function upload_image_filter_handler_image($op, $filter, $filterdata, &$query) { + switch ($op) { + case 'handler': + $query->ensure_table('upload_images'); + if ($filter['value']) { + $query->add_where("upload_images.oid"); + } + else { + $query->add_where("ISNULL(upload_images.oid)"); + } + } +} + +/** + * Private function - sorting start and offset options. + * + * For upload_image_field_handler_image() and upload_image_field_handler_image_title(). + */ +function _upload_image_field_handler_image_options($optionslist) { + // Programatically doing limit offset as + // should be a small sample + // db_query_range requires offset and limit + if ($optionslist) { + $options = explode(',', $optionslist); + $start = (int) $options[0]; + $count = $options[1] ? (int) $options[1] : 0; + } + else { + $start = 0; + $count = 0; + } + + return array($start, $count); +} + +/***************************************************************************** + * Other files display + * + * Based on views/modules/views_upload.inc + */ + +/** + * Query handler - for all other files. + * + * from views_query_handler_file_all_files(). + */ +function upload_image_query_handler_file_all($field, $fieldinfo, &$query) { + $query->add_field('vid', 'node'); +} + +/** + * Field handler - Display all files attached to a given node that are in upload_images. + * + * edit of views_handler_file_all_files() and views_handler_file_all_files(). + */ +function upload_image_field_handler_file_all_other($fieldinfo, $fielddata, $value, $data, $listed = false) { + if ($listed) { + $and = " AND list = 1"; + } + $links = array(); + + $result = db_query("SELECT f.*, fr.*, u.fid FROM {file_revisions} fr INNER JOIN {files} f ON f.fid = fr.fid LEFT JOIN {upload_images} u ON f.fid = u.fid WHERE fr.vid = %d AND ISNULL(u.fid) $and", $data->vid); + while ($file = db_fetch_object($result)) { + // link/nolink use file filename; linkdesc/nolinkdesc use file description + if ($fielddata['options'] == 'link' || $fielddata['options'] == 'nolink') { + $display_string = $file->filename; + } + else { + $display_string = $file->description; + } + if ($fielddata['options'] == 'nolink' || $fielddata['options'] == 'nolinkdesc') { + $links[] = check_plain($display_string); + } + else { + $links[] = l($display_string, check_url(file_create_url($file->filepath))); + } + } + return implode(' | ', $links); +} +function upload_image_field_handler_file_all_other_listed($fieldinfo, $fielddata, $value, $data) { + return upload_image_field_handler_file_all_other($fieldinfo, $fielddata, $value, $data, TRUE); +}