| 1 |
<?php
|
| 2 |
// $Id: imagefield_widget.inc,v 1.39 2009/05/17 02:17:31 quicksketch Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* ImageField widget hooks and callbacks.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of CCK's hook_widget_settings($op = 'form').
|
| 11 |
*/
|
| 12 |
function imagefield_widget_settings_form($widget) {
|
| 13 |
$form = module_invoke('filefield', 'widget_settings', 'form', $widget);
|
| 14 |
|
| 15 |
if ($form['file_extensions']['#default_value'] == 'txt') {
|
| 16 |
$form['file_extensions']['#default_value'] = 'png gif jpg jpeg';
|
| 17 |
}
|
| 18 |
|
| 19 |
$form['max_resolution'] = array(
|
| 20 |
'#type' => 'textfield',
|
| 21 |
'#title' => t('Maximum resolution for Images'),
|
| 22 |
'#default_value' => !empty($widget['max_resolution']) ? $widget['max_resolution'] : 0,
|
| 23 |
'#size' => 15,
|
| 24 |
'#maxlength' => 10,
|
| 25 |
'#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. Resizing images on upload will cause the loss of <a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image.'),
|
| 26 |
'#weight' => 2.1,
|
| 27 |
);
|
| 28 |
$form['min_resolution'] = array(
|
| 29 |
'#type' => 'textfield',
|
| 30 |
'#title' => t('Minimum resolution for Images'),
|
| 31 |
'#default_value' => !empty($widget['min_resolution']) ? $widget['min_resolution'] : 0,
|
| 32 |
'#size' => 15,
|
| 33 |
'#maxlength' => 10,
|
| 34 |
'#description' =>
|
| 35 |
t('The minimum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction. If an image that is smaller than these dimensions is uploaded it will be rejected.'),
|
| 36 |
'#weight' => 2.2,
|
| 37 |
);
|
| 38 |
|
| 39 |
$form['alt_settings'] = array(
|
| 40 |
'#type' => 'fieldset',
|
| 41 |
'#title' => t('ALT text settings'),
|
| 42 |
'#collapsible' => TRUE,
|
| 43 |
'#collapsed' => TRUE,
|
| 44 |
'#weight' => 8,
|
| 45 |
);
|
| 46 |
$form['alt_settings']['custom_alt'] = array(
|
| 47 |
'#type' => 'checkbox',
|
| 48 |
'#title' => t('Enable custom alternate text'),
|
| 49 |
'#default_value' => !empty($widget['custom_alt']) ? $widget['custom_alt'] : 0,
|
| 50 |
'#description' => t('Enable user input alternate text for images.'),
|
| 51 |
);
|
| 52 |
$form['alt_settings']['alt'] = array(
|
| 53 |
'#type' => 'textfield',
|
| 54 |
'#title' => t('Default ALT text'),
|
| 55 |
'#default_value' => !empty($widget['alt']) ? $widget['alt'] : '',
|
| 56 |
'#description' => t('This value will be used for alternate text by default.'),
|
| 57 |
'#suffix' => theme('token_help', 'file'),
|
| 58 |
);
|
| 59 |
|
| 60 |
$form['title_settings'] = array(
|
| 61 |
'#type' => 'fieldset',
|
| 62 |
'#title' => t('Title text settings'),
|
| 63 |
'#collapsible' => TRUE,
|
| 64 |
'#collapsed' => TRUE,
|
| 65 |
'#weight' => 8,
|
| 66 |
);
|
| 67 |
$form['title_settings']['custom_title'] = array(
|
| 68 |
'#type' => 'checkbox',
|
| 69 |
'#title' => t('Enable custom title text'),
|
| 70 |
'#default_value' => !empty($widget['custom_title']) ? $widget['custom_title'] : 0,
|
| 71 |
'#description' => t('Enable user input title text for images.'),
|
| 72 |
);
|
| 73 |
$form['title_settings']['title_type'] = array(
|
| 74 |
'#type' => 'select',
|
| 75 |
'#title' => t('Input type'),
|
| 76 |
'#options' => array(
|
| 77 |
'textfield' => 'textfield',
|
| 78 |
'textarea' => 'textarea'),
|
| 79 |
'#default_value' => !empty($widget['title_type']) ? $widget['title_type'] : 'textfield',
|
| 80 |
'#description' => t('Choose type of field to be displayed to the user.'),
|
| 81 |
);
|
| 82 |
$form['title_settings']['title'] = array(
|
| 83 |
'#type' => 'textfield',
|
| 84 |
'#title' => t('Default Title text'),
|
| 85 |
'#default_value' => !empty($widget['title']) ? $widget['title'] : '',
|
| 86 |
'#description' => t('This value will be used as the image title by default.'),
|
| 87 |
'#suffix' => theme('token_help', 'file'),
|
| 88 |
);
|
| 89 |
|
| 90 |
// Default image settings.
|
| 91 |
$form['default'] = array(
|
| 92 |
'#type' => 'fieldset',
|
| 93 |
'#title' => t('Default image'),
|
| 94 |
'#element_validate' => array('_imagefield_widget_settings_default_validate'),
|
| 95 |
'#collapsible' => TRUE,
|
| 96 |
'#collapsed' => TRUE,
|
| 97 |
'#weight' => 10
|
| 98 |
);
|
| 99 |
|
| 100 |
// Present a thumbnail of the current default image.
|
| 101 |
$form['default']['use_default_image'] = array(
|
| 102 |
'#type' => 'checkbox',
|
| 103 |
'#title' => t('Use default image'),
|
| 104 |
'#default_value' => $widget['use_default_image'],
|
| 105 |
'#description' => t('When an image is not uploaded, show a default image on display.'),
|
| 106 |
);
|
| 107 |
|
| 108 |
if (!empty($widget['default_image'])) {
|
| 109 |
$form['default']['default_image_thumbnail'] = array(
|
| 110 |
'#type' => 'markup',
|
| 111 |
'#value' => theme('imagefield_image', $widget['default_image'], '', '', array('width' => '150'), FALSE),
|
| 112 |
);
|
| 113 |
}
|
| 114 |
$form['default']['default_image_upload'] = array(
|
| 115 |
'#type' => 'file',
|
| 116 |
'#title' => t('Upload image'),
|
| 117 |
'#description' => t('Choose a image that will be used as default.'),
|
| 118 |
);
|
| 119 |
|
| 120 |
// We set this value on 'validate' so we can get CCK to add it
|
| 121 |
// as a standard field setting.
|
| 122 |
$form['default_image'] = array(
|
| 123 |
'#type' => 'value',
|
| 124 |
'#value' => $widget['default_image'],
|
| 125 |
);
|
| 126 |
|
| 127 |
return $form;
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Element specific validation for imagefield default value.
|
| 132 |
*
|
| 133 |
* Validated in a separate function from imagefield_field() to get access
|
| 134 |
* to the $form_state variable.
|
| 135 |
*/
|
| 136 |
function _imagefield_widget_settings_default_validate($element, &$form_state) {
|
| 137 |
// Verify the destination exists
|
| 138 |
$destination = file_directory_path() .'/imagefield_default_images';
|
| 139 |
if (!field_file_check_directory($destination, FILE_CREATE_DIRECTORY)) {
|
| 140 |
form_set_error('default_image', t('The default image could not be uploaded. The destination %destination does not exist or is not writable by the server.', array('%destination' => dirname($destination))));
|
| 141 |
return;
|
| 142 |
}
|
| 143 |
|
| 144 |
$validators = array(
|
| 145 |
'file_validate_is_image' => array(),
|
| 146 |
);
|
| 147 |
|
| 148 |
// We save the upload here because we can't know the correct path until the file is saved.
|
| 149 |
if (!$file = file_save_upload('default_image_upload', $validators, $destination)) {
|
| 150 |
// No upload to save we hope... or file_save_upload() reported an error on its own.
|
| 151 |
return;
|
| 152 |
}
|
| 153 |
|
| 154 |
// Remove old image (if any) & clean up database.
|
| 155 |
$old_default = $form_state['values']['default_image'];
|
| 156 |
if (!empty($old_default['fid'])) {
|
| 157 |
if (file_delete(file_create_path($old_default['filepath']))) {
|
| 158 |
db_query('DELETE FROM {files} WHERE fid=%d', $old_default['fid']);
|
| 159 |
}
|
| 160 |
}
|
| 161 |
|
| 162 |
// Make the file permanent and store it in the form.
|
| 163 |
file_set_status($file, FILE_STATUS_PERMANENT);
|
| 164 |
$file->timestamp = time();
|
| 165 |
$form_state['values']['default_image'] = (array)$file;
|
| 166 |
}
|
| 167 |
|
| 168 |
/**
|
| 169 |
* Implementation of CCK's hook_widget_settings($op = 'validate').
|
| 170 |
*/
|
| 171 |
function imagefield_widget_settings_validate($widget) {
|
| 172 |
// Check that only web images are specified in the callback.
|
| 173 |
$extensions = array_filter(explode(' ', $widget['file_extensions']));
|
| 174 |
$web_extensions = array('jpg', 'jpeg', 'gif', 'png');
|
| 175 |
if (count(array_diff($extensions, $web_extensions))) {
|
| 176 |
form_set_error('file_extensions', t('Only web-standard images (jpg, gif, and png) are supported through the image widget. If needing to upload other types of images, change the widget to use a standard file upload.'));
|
| 177 |
}
|
| 178 |
|
| 179 |
// Check that set resolutions are valid.
|
| 180 |
foreach (array('min_resolution', 'max_resolution') as $resolution) {
|
| 181 |
if (!empty($widget[$resolution]) && !preg_match('/^[0-9]+x[0-9]+$/', $widget[$resolution])) {
|
| 182 |
form_set_error($resolution, t('Please specify a resolution in the format WIDTHxHEIGHT (e.g. 640x480).'));
|
| 183 |
}
|
| 184 |
}
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* Implementation of CCK's hook_widget_settings($op = 'save').
|
| 189 |
*/
|
| 190 |
function imagefield_widget_settings_save($widget) {
|
| 191 |
$filefield_settings = module_invoke('filefield', 'widget_settings', 'save', $widget);
|
| 192 |
return array_merge($filefield_settings, array('max_resolution', 'min_resolution', 'alt', 'custom_alt', 'title', 'custom_title', 'title_type', 'default_image', 'use_default_image'));
|
| 193 |
}
|
| 194 |
|
| 195 |
/**
|
| 196 |
* Element #value_callback function.
|
| 197 |
*/
|
| 198 |
function imagefield_widget_value($element, $edit = FALSE) {
|
| 199 |
$item = filefield_widget_value($element, $edit);
|
| 200 |
if ($edit) {
|
| 201 |
$item['alt'] = isset($edit['alt']) ? $edit['alt'] : '';
|
| 202 |
$item['title'] = isset($edit['title']) ? $edit['title'] : '';
|
| 203 |
}
|
| 204 |
else {
|
| 205 |
$item['alt'] = '';
|
| 206 |
$item['title'] = '';
|
| 207 |
}
|
| 208 |
return $item;
|
| 209 |
}
|
| 210 |
|
| 211 |
/**
|
| 212 |
* Element #process callback function.
|
| 213 |
*/
|
| 214 |
function imagefield_widget_process($element, $edit, &$form_state, $form) {
|
| 215 |
$file = $element['#value'];
|
| 216 |
$field = content_fields($element['#field_name'], $element['#type_name']);
|
| 217 |
|
| 218 |
$element['#theme'] = 'imagefield_widget_item';
|
| 219 |
|
| 220 |
if (isset($element['preview']) && $element['#value']['fid'] != 0) {
|
| 221 |
$element['preview']['#value'] = theme('imagefield_widget_preview', $element['#value']);
|
| 222 |
}
|
| 223 |
|
| 224 |
// Check if using the default alt text and replace tokens.
|
| 225 |
$default_alt = (!$field['widget']['custom_alt'] || (empty($file['status']) && empty($file['data']['alt'])));
|
| 226 |
if ($default_alt && function_exists('token_replace')) {
|
| 227 |
$field['widget']['alt'] = empty($field['widget']['alt']) ? '' : token_replace($field['widget']['alt'], 'user', $GLOBALS['user']);
|
| 228 |
}
|
| 229 |
$element['data']['alt'] = array(
|
| 230 |
'#title' => t('Alternate Text'),
|
| 231 |
'#type' => $field['widget']['custom_alt'] ? 'textfield' : 'value',
|
| 232 |
'#default_value' => $default_alt ? $field['widget']['alt'] : $file['data']['alt'],
|
| 233 |
'#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
|
| 234 |
'#maxlength' => variable_get('imagefield_alt_length', 80), // See http://www.gawds.org/show.php?contentid=28.
|
| 235 |
'#attributes' => array('class' => 'imagefield-text'),
|
| 236 |
);
|
| 237 |
// #value must be hard-coded if #type = 'value'.
|
| 238 |
if ($default_alt) {
|
| 239 |
$element['data']['alt']['#value'] = $field['widget']['alt'];
|
| 240 |
}
|
| 241 |
|
| 242 |
// Check if using the default title and replace tokens.
|
| 243 |
$default_title = (!$field['widget']['custom_title'] || (empty($file['status']) && empty($file['data']['title'])));
|
| 244 |
if ($default_title && function_exists('token_replace')) {
|
| 245 |
$field['widget']['title'] = empty($field['widget']['title']) ? '' : token_replace($field['widget']['title'], 'user', $GLOBALS['user']);
|
| 246 |
}
|
| 247 |
|
| 248 |
// If the custom title is enabled, input type defaults to textfield.
|
| 249 |
if (!empty($field['widget']['custom_title'])) {
|
| 250 |
$title_type = !empty($field['widget']['title_type']) ? $field['widget']['title_type'] : 'textfield';
|
| 251 |
}
|
| 252 |
else {
|
| 253 |
$title_type = 'value';
|
| 254 |
}
|
| 255 |
|
| 256 |
$element['data']['title'] = array(
|
| 257 |
'#type' => $title_type,
|
| 258 |
'#title' => t('Title'),
|
| 259 |
'#default_value' => $default_title ? $field['widget']['title'] : $file['data']['title'],
|
| 260 |
'#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
|
| 261 |
'#maxlength' => variable_get('imagefield_title_length', 500),
|
| 262 |
'#attributes' => array('class' => 'imagefield-text'),
|
| 263 |
);
|
| 264 |
// #value must be hard-coded if #type = 'value'.
|
| 265 |
if ($default_title) {
|
| 266 |
$element['data']['title']['#value'] = $field['widget']['title'];
|
| 267 |
}
|
| 268 |
|
| 269 |
return $element;
|
| 270 |
}
|
| 271 |
|
| 272 |
/**
|
| 273 |
* FormAPI theme function. Theme the output of an image field.
|
| 274 |
*/
|
| 275 |
function theme_imagefield_widget($element) {
|
| 276 |
drupal_add_css(drupal_get_path('module', 'imagefield') .'/imagefield.css');
|
| 277 |
return theme('form_element', $element, $element['#children']);
|
| 278 |
}
|