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