'imagefield/js', 'callback' => 'imagefield_js', //'access' => user_access(), 'access' => true, 'type' => MENU_CALLBACK ); } elseif ($_SESSION['imagefield']) { // Iterate over each field stored in session imagefield looking // for files in a preview state to add menu items for. This // allows us to preview new uploads before a node is submitted. foreach ($_SESSION['imagefield'] as $fieldname => $files) { // move on to the next field if there is nothing to process in files. if (empty($files)) continue; foreach ($files as $delta => $file) { // If the file is not a preview do not display it. if (empty($file['preview'])) continue; $items[] = array( 'path' => $file['preview'], 'callback' => '_imagefield_preview', 'callback arguments' => array($file['preview']), 'access' => true, 'type' => MENU_CALLBACK, ); } } } return $items; } /** * Transfer a file that is in a 'preview' state. * * @todo multiple support */ function _imagefield_preview($filepath) { foreach ($_SESSION['imagefield'] as $fieldname => $files) { foreach ($files as $delta => $file) { if ($file['preview'] == $filepath) { // Emulate a normal file transfer by setting cache flags that will // prevent the image from needing to be resent on previews. Without // setting the cache headers, transfered images still get the expire // headers set in drupal_page_header(). $modified_time = filemtime($file['filepath']); $last_modified = gmdate('D, d M Y H:i:s', $modified_time) .' GMT'; $etag = '"'. md5($last_modified) .'"'; file_transfer($file['filepath'], array( 'Content-Type: '. mime_header_encode($file['filemime']), 'Content-Length: '. $file['filesize'], 'Cache-Control: max-age=1209600', 'Expires: '. gmdate('D, d M Y H:i:s', time() + 1209600) .' GMT', // Two weeks. 'Last-Modified: '. $last_modified, 'ETag: '. $etag, )); } } } } /** * Implementation of hook_perm(). */ function imagefield_perm() { return array('view imagefield uploads'); } /** * Implementation of hook_field_info(). */ function imagefield_field_info() { return array( 'image' => array('label' => 'Image'), ); } /** * Implementation of hook_field_settings(). */ function imagefield_field_settings($op, $field) { switch ($op) { case 'callbacks': return array('view' => CONTENT_CALLBACK_CUSTOM); case 'form': $form = array(); $form['default'] = array( '#type' => 'fieldset', '#title' => t('Default image'), ); // Present a thumbnail of the current default image. $form['default']['use_default_image'] = array( '#type' => 'checkbox', '#title' => t('Use default image'), '#default_value' => $field['use_default_image'], '#description' => t('Check here if you want to use a image as default.'), ); if (!empty($field['default_image'])) { $form['default']['default_image_thumbnail'] = array( '#type' => 'markup', '#value' => theme('imagefield_image', $field['default_image'], '', '', array('width' => '150'), false), ); } $form['default']['default_image_upload'] = array( '#type' => 'file', '#title' => t('Upload image'), '#description' => t('Choose a image that will be used as default.'), ); // We set this value on 'validate' so we can get cck to add it // as a standard field setting. $form['default_image'] = array( '#type' => 'value', '#value' => $field['default_image'], ); return $form; case 'validate': // We save the upload here because we can't know the correct // file path until we save the file. // Check of we got an new upload. if (!$file = file_check_upload('default_image_upload')) { break; } // figure steal the file extension and construct a filename for this // fields default image. This is standardized for default image handling // with private files. $ext = array_pop(explode('.', $file->filename)); $filename = $field['field_name'] .'.'. $ext; // verify the destination exists and is writeable... $dst = 'imagefield_default_images/'. $filename; if (!imagefield_check_directory(dirname($dst))) { form_set_error('default_image', t("The default image could not be uploaded. The destination(%d) does not exist or is not writable by the webserver.", array('%d' => dirname($dst)))); break; } // save the upload to its resting place. if (!$file = file_save_upload('default_image_upload', $dst, FILE_EXISTS_REPLACE)) { form_set_error('default_image', t("The default image could not be uploaded. Failed saving to destination(%d).", array('%d' => $dst))); break; } // set the value of the form_item so we can store this in the settings // from validate. form_set_value(array('#parents' => array('default_image')), (array) $file); break; case 'save': return array('default_image', 'use_default_image'); case 'database columns': $columns = array( 'fid' => array('type' => 'int', 'not null' => true, 'default' => '0'), 'title' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'default' => "''", 'sortable' => true), 'alt' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'default' => "''", 'sortable' => true), ); return $columns; case 'filters': return array( 'not null' => array( 'operator' => array('=' => t('Has Image')), 'list' => 'views_handler_operator_yesno', 'list-type' => 'select', 'handler' => 'imagefield_views_handler_filter_is_not_null', ), ); } } /** * Custom filter for imagefield NOT null. */ function imagefield_views_handler_filter_is_not_null($op, $filter, $filterinfo, &$query) { if ($op == 'handler') { $query->ensure_table($filterinfo['table']); if ($filter['value']) { $qs = '%s.%s > 0'; $query->add_where($qs, $filterinfo['table'], $filterinfo['field']); } else { $qs = '%s.%s = 0 OR %s.%s IS null'; $query->add_where($qs, $filterinfo['table'], $filterinfo['field'], $filterinfo['table'], $filterinfo['field']); } } } function imagefield_default_item() { return array( 'fid' => 0, 'title' => '', 'alt' => '', ); } /** * Insert a file into the database. * * @param $node * Node object this file is be associated with. * @param $file * File to be inserted, passed by reference since fid should be attached. */ function imagefield_file_insert($node, &$file, $field) { $fieldname = $field['field_name']; // allow tokenized paths. if (function_exists('token_replace')) { global $user; $widget_image_path = token_replace($field['widget']['image_path'], 'user', $user); } else { $widget_image_path = $field['widget']['image_path']; } if (imagefield_check_directory($widget_image_path)){ $filepath = file_create_path($widget_image_path) .'/'. $file['filename']; if ($file = file_save_upload((object) $file, $filepath)) { $file = (array) $file; $file['fid'] = db_next_id('{files}_fid'); db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s','%s','%s',%d)", $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']); module_invoke_all('imagefield_file', 'save', $file); return (array) $file; } } // Either directory creation or the upload failed. // Include file name in upload error. form_set_error(null, t('Image upload was unsuccessful.')); return FALSE; } /** * Update the file record if necessary. * * @param $node * Node object this file is be associated with. * @param $file * A single CCK image field item to be updated. * @param $field * The field definition for this image field. */ function imagefield_file_update($node, &$file, $field) { $file = (array)$file; if ($file['flags']['delete'] == true) { // don't delete files if we're creating new revisions, but still return an // empty array... if (empty($node->old_vid)) { _imagefield_file_delete($file, $field['field_name']); } if ($field['multiple']) { // If multiple, return an empty array so the file entry is removed from // the content field table. $file = array(); } else { // If not multiple, empty the array so it's updated to 0 in the main // content type table. If we don't do this, a reference to a non existent // file (fid) will be left on the content type table. foreach ($file as $key => $value) { $file[$key] = NULL; } } return $file; } if ($file['fid'] == 'upload') { return imagefield_file_insert($node, $file, $field); } else { // empty files without fid. if ($file['fid'] == 0) { $file = array(); } // if fid is not numeric here we should complain. // else we update the file table. } return $file; } /** * Implementation of hook_field(). */ function imagefield_field($op, $node, $field, &$items, $teaser, $page) { $fieldname = $field['field_name']; switch ($op) { // called after content.module loads default data. case 'load': if (!count($items)) { return; } foreach ($items as $delta => $item) { if (empty($item)) { unset($items[$delta]); } elseif (!empty($item['fid'])) { $items[$delta] = array_merge($item, _imagefield_file_load($item['fid'])); } } $items = array_values(array_filter($items)); // compact deltas return array($fieldname => $items); break; // called before content.module defaults. case 'insert': foreach ($items as $delta => $item) { if ($item['flags']['delete']) { unset($items[$delta]); } else { $items[$delta] = imagefield_file_insert($node, $item, $field); } } $items = array_values(array_filter($items)); // compact deltas imagefield_clear_field_session($fieldname); break; // called before content.module defaults. case 'update': foreach ($items as $delta => $item) { // If we're dealing with a single value field, and we just received // a new file item, we need to mark the existing (old) one for // deletion. Otherwise, it will become orphaned. if (!$field['multiple'] && !empty($items) && count($items) > 1 && $delta === 0) { $item['flags']['hidden'] = true; $item['flags']['delete'] = true; } // Update each file item. $items[$delta] = imagefield_file_update($node, $item, $field); // If the file has been deleted, unset the file entry so that it's // actually deleted from the database, or at least set it to a // default item if CCK won't delete it. if (empty($items[$delta])) { if ($field['multiple']) { unset($items[$delta]); } } } $items = array_values(array_filter($items)); // compact deltas imagefield_clear_field_session($fieldname); break; case 'delete revision': $db_info = content_database_info($field); foreach ($items as $delta => $item) { $references = db_result(db_query("SELECT count(vid) FROM {%s} WHERE nid=%d AND %s=%d and vid!=%d", $db_info['table'], $node->nid, $db_info['columns']['fid']['column'], $item['fid'], $node->vid)); if ($references || _imagefield_file_delete($item, $field['field_name'])) { $items[$delta] = array(); } } $items = array_values($items); // compact deltas break; case 'delete': foreach ($items as $delta => $item) { _imagefield_file_delete($item, $field['field_name']); } break; case 'view': $context = $teaser ? 'teaser' : 'full'; $formatter = isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default'; foreach ($items as $delta => $item) { if ($item['fid'] == 0) { unset($items[$delta]); } } if ($field['use_default_image'] && empty($items)) { $items[0] = $field['default_image']; } foreach ($items as $delta => $item) { $items[$delta]['view'] = content_format($field, $item, $formatter, $node); } return theme('field', $node, $field, $items, $teaser, $page); } } /** * Implementation of hook_widget_info(). */ function imagefield_widget_info() { return array( 'image' => array( 'label' => 'Image', 'field types' => array('image'), ), ); } /** * Implementation of hook_widget_settings(). */ function imagefield_widget_settings($op, $widget) { switch ($op) { case 'callbacks': return array('default value' => CONTENT_CALLBACK_CUSTOM); case 'form': $form = array(); $form['max_resolution'] = array( '#type' => 'textfield', '#title' => t('Maximum resolution for Images'), '#default_value' => $widget['max_resolution'] ? $widget['max_resolution'] : 0, '#size' => 15, '#maxlength' => 10, '#description' => t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height.'), ); $form['max_filesize'] = array( '#type' => 'textfield', '#title' => t('Maximum filesize for Images'), '#default_value' => $widget['max_filesize'] ? $widget['max_filesize'] : 0, '#size' => 6, '#description' => t('The maximum allowed image file size expressed in kilobytes. Set to 0 for no restriction.') ); $form['max_number_images'] = array( '#type' => 'textfield', '#title' => t('Maximum number of images'), '#default_value' => $widget['max_number_images'] ? $widget['max_number_images'] : 0, '#size' => 4, '#description' => t('The maximum number of images allowed. Set to 0 for no restriction. This only applies if multiple values are enabled.') ); $form['image_path'] = array( '#type' => 'textfield', '#title' => t('Image path'), '#default_value' => $widget['image_path'] ? $widget['image_path'] : '', '#description' => t('Optional subdirectory within the "%dir" directory where images will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))), ); if (function_exists('token_replace')) { $form['image_path']['#description'] .= ' '. t('You can use the following tokens in the image path.'); $form['image_path']['#suffix'] = theme('token_help', 'user'); } $form['file_extensions'] = array( '#type' => 'textfield', '#title' => t('Permitted upload file extensions.'), '#default_value' => $widget['file_extensions'] ? $widget['file_extensions'] : 'jpg jpeg png gif', '#size' => 64, '#maxlength' => 64, '#description' => t('Extensions a user can upload to this field. Seperate extensions with a space and do not include the leading dot.') ); $form['custom_alt'] = array( '#type' => 'checkbox', '#title' => t('Enable custom alternate text'), '#default_value' => $widget['custom_alt'] ? $widget['custom_alt'] : 0, '#description' => t('Enable custom alternate text for images. Filename will be used if not checked.'), ); $form['custom_title'] = array( '#type' => 'checkbox', '#title' => t('Enable custom title text'), '#default_value' => $widget['custom_title'] ? $widget['custom_title'] : 0, '#description' => t('Enable custom title text for images. Filename will be used if not checked.'), ); return $form; case 'validate': // strip slashes from the beginning and end of $widget['image_path'] $widget['image_path'] = trim($widget['image_path'], '\\/'); form_set_value(array('#parents' => array('image_path')), $widget['image_path']); break; case 'save': return array('max_resolution', 'max_filesize', 'max_number_images', 'image_path', 'file_extensions', 'custom_alt', 'custom_title'); } } /** * Implementation of hook_form_alter(). Set the appropriate * attibutes to allow file uploads on the field settings form. */ function imagefield_form_alter($form_id, &$form) { if ($form_id == '_content_admin_field') { $form['#attributes'] = array('enctype' => 'multipart/form-data'); } } /** * Create the image directory relative to the 'files' dir recursively for every * directory in the path. * * @param $directory * The directory path under files to check, such as 'photo/path/here' * @param $form_element * A form element to throw an error on if the directory is not writable */ function imagefield_check_directory($directory, $form_element = array()) { foreach (explode('/', $directory) as $dir) { $dirs[] = $dir; $path = file_create_path(implode($dirs, '/')); if (!file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0])) { watchdog('imagefield', t('Imagefield failed to create directory(%d) at (%p).', array('%d' => $directory, '%p' => $path)), WATCHDOG_ERROR); return false; } } return true; } function _imagefield_scale_image($file, $resolution = 0) { $info = image_get_info($file['filepath']); if ($info) { list($width, $height) = explode('x', $resolution); if ($width && $height) { $result = image_scale($file['filepath'], $file['filepath'], $width, $height); if ($result) { $file['filesize'] = filesize($file['filepath']); drupal_set_message(t('The image %filename was resized to fit within the maximum allowed resolution of %resolution pixels', array('%resolution' => $resolution, '%filename' => $file['filename']))); } } } return $file; } function imagefield_clear_session() { if (is_array($_SESSION['imagefield']) && count($_SESSION['imagefield'])) { foreach (array_keys($_SESSION['imagefield']) as $fieldname) { imagefield_clear_field_session($fieldname); } unset($_SESSION['imagefield']); } } function imagefield_clear_field_session($fieldname) { if (is_array($_SESSION['imagefield'][$fieldname]) && count($_SESSION['imagefield'][$fieldname])) { foreach ($_SESSION['imagefield'][$fieldname] as $delta => $file) { if (is_file($file['filepath'])) { file_delete($file['filepath']); } } unset($_SESSION['imagefield'][$fieldname]); } } function _imagefield_file_delete($file, $fieldname) { if (is_numeric($file['fid'])) { db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']); } else { unset($_SESSION['imagefield'][$fieldname][$file['sessionid']]); } module_invoke_all('imagefield_file', 'delete', $file); return file_delete($file['filepath']); } /** * Implementation of hook_widget(). */ function imagefield_widget($op, &$node, $field, &$items) { switch ($op) { case 'default value': return array(); case 'prepare form values': _imagefield_widget_prepare_form_values($node, $field, $items); return; case 'form': return _imagefield_widget_form($node, $field, $items); case 'validate': _imagefield_widget_validate($node, $field, $items); return; } } function _imagefield_widget_prepare_form_values(&$node, $field, &$items) { $fieldname = $field['field_name']; // clean up the session if we weren't posted. if (!count($_POST)) { imagefield_clear_session(); } // Attach new files if ($file = file_check_upload($fieldname .'_upload')) { $file = (array)$file; // Validation must happen immediately after the image is uploaded so we // can discard the file if it turns out not to be a valid mime type $valid_image = _imagefield_widget_upload_validate($node, $field, $items, $file); //$valid_image = true; if ($valid_image) { $file = _imagefield_scale_image($file, $field['widget']['max_resolution']); // Allow tokenized paths if available if (function_exists('token_replace')) { global $user; $widget_image_path = token_replace($field['widget']['image_path'], 'user', $user); } else { $widget_image_path = $field['widget']['image_path']; } imagefield_check_directory($widget_image_path); // Create the filepath for the image preview $filepath = file_create_filename($file['filename'], file_create_path($widget_image_path)); if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) { if (strpos($filepath, file_directory_path()) !== FALSE) { $filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/'); } $filepath = 'system/files/' . $filepath; } $file['fid'] = 'upload'; $file['preview'] = $filepath; // If a single field, mark any other images for deletion and delete files in session if (!$field['multiple']) { if (is_array($items)) { foreach ($items as $delta => $session_file) { $items[$delta]['flags']['hidden'] = true; $items[$delta]['flags']['delete'] = true; } } imagefield_clear_field_session($fieldname); } // Add the file to the session $file_id = count($items) + count($_SESSION['imagefield'][$fieldname]); $file['sessionid'] = $file_id; $_SESSION['imagefield'][$fieldname][$file_id] = $file; } else { // Delete the invalid file file_delete($file['filepath']); // If a single field and a valid file is in the session, mark existing image for deletion if (!$field['multiple']) { if (!empty($_SESSION['imagefield'][$fieldname]) && !empty($items)) { foreach ($items as $delta => $session_file) { $items[$delta]['flags']['hidden'] = true; $items[$delta]['flags']['delete'] = true; } } } } } // Load files from preview state. before committing actions. if (is_array($_SESSION['imagefield'][$fieldname]) && count($_SESSION['imagefield'][$fieldname])) { foreach ($_SESSION['imagefield'][$fieldname] as $delta => $file) { $items[] = $file; } } } function _imagefield_widget_form($node, $field, &$items) { drupal_add_js('misc/progress.js'); drupal_add_js('misc/upload.js'); drupal_add_js(drupal_get_path('module', 'imagefield') .'/imagefield.js'); drupal_add_css(drupal_get_path('module', 'imagefield') .'/imagefield.css'); $fieldname = $field['field_name']; $form = array(); $form[$fieldname] = array( '#type' => 'fieldset', '#title' => t($field['widget']['label']), '#weight' => $field['widget']['weight'], '#description' => t('Images are not saved until the form is submitted.'), '#collapsible' => true, '#collapsed' => false, '#tree' => true, '#prefix' => '