| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: imagefield.module,v 1.117 2010/12/12 23:36:07 quicksketch Exp $ 3 4 /** 5 * @file 6 * ImageField core hooks and menu callbacks. 7 */ 8 9 require_once dirname(__FILE__) . '/imagefield_file.inc'; 10 require_once dirname(__FILE__) . '/imagefield_widget.inc'; 11 12 /** 13 * Implementation of hook_theme(). 14 */ 15 function imagefield_theme() { 16 return array( 17 // Theme an image uploaded to ImageField with alt and title. 18 // TODO: Switch to core theme image if possible. 19 'imagefield_image' => array( 20 'arguments' => array('file' => NULL, 'alt' => '', 'title' => '', 'attributes' => NULL, 'getsize' => TRUE), 21 ), 22 // Theme an ImageField field item. It calls imagefied_image with the proper 23 // item properties as arguments. 24 'imagefield_item' => array( 25 'arguments' => array('item' => NULL), 26 ), 27 // imagefield_widget form element type theme function. 28 'imagefield_widget' => array( 29 'arguments' => array('element' => NULL), 30 'file' => 'imagefield_widget.inc', 31 ), 32 // Use to generate a preview (admin view) of an imagefield item for use in 33 // field item forms and filefield widgets. Invoked by filefield_widget_process. 34 'imagefield_widget_preview' => array( 35 'arguments' => array('item' => NULL), 36 ), 37 // Theme function for the field item elements. allows you to place children 38 // within the context of the parent. 39 'imagefield_widget_item' => array( 40 'arguments' => array('element' => NULL), 41 ), 42 // Generates and img tag to the admin thumbnail of an ImageField upload. 43 'imagefield_admin_thumbnail' => array( 44 'arguments' => array('item' => NULL), 45 ), 46 // ImageField formatter theme functions. 47 'imagefield_formatter_image_plain' => array( 48 'arguments' => array('element' => NULL), 49 'file' => 'imagefield_formatter.inc', 50 ), 51 'imagefield_formatter_image_nodelink' => array( 52 'arguments' => array('element' => NULL), 53 'file' => 'imagefield_formatter.inc', 54 ), 55 'imagefield_formatter_image_imagelink' => array( 56 'arguments' => array('element' => NULL), 57 'file' => 'imagefield_formatter.inc', 58 ), 59 ); 60 } 61 62 /** 63 * Implementation of hook_elements(). 64 */ 65 function imagefield_elements() { 66 $elements = array(); 67 68 // Catch problems when this is called too early during installation or update. 69 if (!module_exists('filefield')) { 70 return $elements; 71 } 72 73 // An ImageField is really just a FileField with extra processing. 74 $filefield_elements = module_invoke('filefield', 'elements'); 75 $elements['imagefield_widget'] = $filefield_elements['filefield_widget']; 76 $elements['imagefield_widget']['#process'][] = 'imagefield_widget_process'; 77 $elements['imagefield_widget']['#element_validate'][] = 'imagefield_widget_validate'; 78 79 // ImageField needs a separate value callback to save its alt and title texts. 80 $elements['imagefield_widget']['#value_callback'] = 'imagefield_widget_value'; 81 82 return $elements; 83 } 84 85 /** 86 * Implementation of hook_file_download. 87 */ 88 function imagefield_file_download($filepath) { 89 // Return headers for admin thumbnails if private files are enabled. 90 if (strpos($filepath, 'imagefield_thumbs') !== FALSE) { 91 $original_path = str_replace('imagefield_thumbs/', '', $filepath); 92 $original_full_path = file_create_path($original_path); 93 $thumb_full_path = file_create_path($filepath); 94 95 // Allow access to temporary thumbnails, since they're not yet associated 96 // with a node. If not temporary, check access on the original file. 97 $status = db_result(db_query("SELECT status FROM {files} WHERE filepath = '%s'", $original_full_path)); 98 $access = ($status == 0 || !in_array(-1, module_invoke_all('file_download', $original_path))); 99 if ($access && $info = getimagesize($thumb_full_path)) { 100 return array( 101 'Content-Type: ' . $info['mime'], 102 'Content-Length: ' . filesize($thumb_full_path) 103 ); 104 } 105 } 106 107 // Return headers for default images. 108 if (strpos($filepath, 'imagefield_default_images') !== FALSE) { 109 $full_path = file_create_path($filepath); 110 if ($info = getimagesize($full_path)) { 111 return array( 112 'Content-Type: ' . $info['mime'], 113 'Content-Length: ' . filesize($full_path) 114 ); 115 } 116 } 117 } 118 119 /** 120 * Implementation of hook_nodeapi(). 121 * 122 * Add ALT and title texts to the search index. 123 */ 124 function imagefield_nodeapi($node, $op) { 125 if ($op == 'update index') { 126 static $fields; 127 if (!isset($fields)) { 128 $fields = filefield_get_field_list(); 129 } 130 131 $texts = array(); 132 foreach ($fields as $field) { 133 $name = $field['field_name']; 134 // Check this node for ImageField alt and title data. 135 if (isset($node->$name) && is_array($node->$name)) { 136 foreach ($node->$name as $item) { 137 $texts[] = isset($item['data']['alt']) ? $item['data']['alt'] : ''; 138 $texts[] = isset($item['data']['title']) ? $item['data']['title'] : ''; 139 } 140 } 141 } 142 return implode(' ', $texts); 143 } 144 } 145 146 /** 147 * Implementation of CCK's hook_widget_info(). 148 */ 149 function imagefield_widget_info() { 150 $module_path = drupal_get_path('module', 'imagefield'); 151 return array( 152 'imagefield_widget' => array( 153 'label' => t('Image'), 154 'field types' => array('filefield'), 155 'multiple values' => CONTENT_HANDLE_CORE, 156 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM), 157 'description' => t('An edit widget for image files, including a preview of the image.'), 158 ), 159 ); 160 } 161 162 /** 163 * Implementation of CCK's hook_widget_settings(). 164 */ 165 function imagefield_widget_settings($op, $widget) { 166 switch ($op) { 167 case 'form': 168 return imagefield_widget_settings_form($widget); 169 case 'validate': 170 return imagefield_widget_settings_validate($widget); 171 case 'save': 172 return imagefield_widget_settings_save($widget); 173 } 174 } 175 176 /** 177 * Implementation of CCK's hook_widget(). 178 * 179 * Assign default properties to item and delegate to FileField. 180 */ 181 function imagefield_widget(&$form, &$form_state, $field, $items, $delta = 0) { 182 // Add default values to items. 183 // TODO: use CCK's default value callback. 184 if (empty($items[$delta])) { 185 $items[$delta] = array('alt' => '', 'title' => ''); 186 } 187 188 // Start with the FileField widget as a basic start. 189 // Note that FileField needs to modify $form by reference. 190 $element = filefield_widget($form, $form_state, $field, $items, $delta); 191 192 // Add ImageField specific validators. 193 $element['#upload_validators'] = array_merge($element['#upload_validators'], imagefield_widget_upload_validators($field)); 194 195 return $element; 196 } 197 198 /** 199 * Get the additional upload validators for an image field. 200 * 201 * @param $field 202 * The CCK field array. 203 * @return 204 * An array suitable for passing to file_save_upload() or the file field 205 * element's '#upload_validators' property. 206 */ 207 function imagefield_widget_upload_validators($field) { 208 $validators = array(); 209 210 // Match the default value if no file extensions have been saved at all. 211 if (!isset($field['widget']['file_extensions'])) { 212 $field['widget']['file_extensions'] = 'png gif jpg jpeg'; 213 } 214 215 // Ensure that only web images are supported. 216 $web_extensions = array('png', 'gif', 'jpg', 'jpeg'); 217 $extensions = array_filter(explode(' ', $field['widget']['file_extensions'])); 218 if (empty($extensions)) { 219 $extensions = $web_extensions; 220 } 221 $validators['filefield_validate_extensions'][0] = implode(' ', array_intersect($extensions, $web_extensions)); 222 223 // Add the image validator as a basic safety check. 224 $validators['filefield_validate_is_image'] = array(); 225 226 // Add validators for resolutions. 227 if (!empty($field['widget']['max_resolution']) || !empty($field['widget']['min_resolution'])) { 228 $validators['filefield_validate_image_resolution'] = array( 229 $field['widget']['max_resolution'], 230 $field['widget']['min_resolution'], 231 ); 232 } 233 234 return $validators; 235 } 236 237 /** 238 * Implementation of CCK's hook_field_formatter_info(). 239 */ 240 function imagefield_field_formatter_info() { 241 $module_path = drupal_get_path('module', 'imagefield'); 242 $formatters = array( 243 'image_plain' => array( 244 'label' => t('Image'), 245 'field types' => array('filefield'), 246 'description' => t('Displays image files in their original size.'), 247 ), 248 'image_nodelink' => array( 249 'label' => t('Image linked to node'), 250 'field types' => array('filefield'), 251 'description' => t('Displays image files in their original size.'), 252 ), 253 'image_imagelink' => array( 254 'label' => t('Image linked to file'), 255 'field types' => array('filefield'), 256 'description' => t('Displays image files in their original size.'), 257 ), 258 ); 259 return $formatters; 260 } 261 262 /** 263 * Implementation of CCK's hook_default_value(). 264 */ 265 function imagefield_default_value(&$form, &$form_state, $field, $delta) { 266 return filefield_default_value($form, $form_state, $field, $delta); 267 } 268 269 /** 270 * Implementation of hook_form_[form_id]_alter(). 271 * 272 * Modify the add new field form to make "Image" the default formatter. 273 */ 274 function imagefield_form_content_field_overview_form_alter(&$form, &$form_state) { 275 $form['#submit'][] = 'imagefield_form_content_field_overview_submit'; 276 } 277 278 /** 279 * Submit handler to set a new field's formatter to "image_plain". 280 */ 281 function imagefield_form_content_field_overview_submit(&$form, &$form_state) { 282 if (isset($form_state['fields_added']['_add_new_field']) && isset($form['#type_name'])) { 283 $new_field = $form_state['fields_added']['_add_new_field']; 284 $node_type = $form['#type_name']; 285 $field = content_fields($new_field, $node_type); 286 if ($field['widget']['module'] == 'imagefield') { 287 foreach ($field['display_settings'] as $display_type => $display_settings) { 288 if ($field['display_settings'][$display_type]['format'] == 'default') { 289 $field['display_settings'][$display_type]['format'] = 'image_plain'; 290 } 291 } 292 content_field_instance_update($field); 293 } 294 } 295 } 296 297 /** 298 * Implementation of hook_filefield_data_info(). 299 */ 300 function imagefield_filefield_data_info() { 301 return array( 302 'alt' => array( 303 'title' => t('Alt text'), 304 'callback' => 'check_plain', 305 ), 306 'title' => array( 307 'title' => t('Title'), 308 'callback' => 'check_plain', 309 ), 310 ); 311 } 312 313 /** 314 * @defgroup "Theme Callbacks" 315 * @{ 316 * @see imagefield_theme(). 317 */ 318 function theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) { 319 $file = (array) $file; 320 321 if ($getsize) { 322 // Use cached width and height if available. 323 if (!empty($file['data']['width']) && !empty($file['data']['height'])) { 324 $attributes['width'] = $file['data']['width']; 325 $attributes['height'] = $file['data']['height']; 326 } 327 // Otherwise pull the width and height from the file. 328 elseif (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])) { 329 $attributes['width'] = $width; 330 $attributes['height'] = $height; 331 } 332 } 333 334 if (!empty($title)) { 335 $attributes['title'] = $title; 336 } 337 338 // Alt text should be added even if it is an empty string. 339 $attributes['alt'] = $alt; 340 341 // Add a timestamp to the URL to ensure it is immediately updated after editing. 342 $query_string = ''; 343 if (isset($file['timestamp'])) { 344 $query_character = (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && variable_get('clean_url', '0') == '0') ? '&' : '?'; 345 $query_string = $query_character . $file['timestamp']; 346 } 347 348 // Encode the path so that unusual characters are printed correctly. 349 $path = field_file_urlencode_path($file['filepath']); 350 351 // Construct the URL. 352 $url = file_create_url($path) . $query_string; 353 $attributes['src'] = $url; 354 $attributes = drupal_attributes($attributes); 355 return '<img '. $attributes .' />'; 356 } 357 358 function theme_imagefield_item($item) { 359 return theme('imagefield_image', $item, $item['alt'], $item['title']); 360 } 361 362 function theme_imagefield_widget_preview($item = NULL) { 363 return '<div class="imagefield-preview">' . theme('imagefield_admin_thumbnail', $item) . '</div>'; 364 } 365 366 function theme_imagefield_widget_item($element) { 367 return theme('filefield_widget_item', $element); 368 } 369 370 function theme_imagefield_admin_thumbnail($item = NULL) { 371 if (is_null($item) || empty($item['filepath'])) { 372 return '<!-- link to default admin thumb -->'; 373 } 374 $thumb_path = imagefield_file_admin_thumb_path($item); 375 376 // Encode the path so that unusual characters are printed correctly. 377 $thumb_path = field_file_urlencode_path($thumb_path); 378 379 // Add a timestamp to the URL to ensure it is immediately updated after editing. 380 $query_string = ''; 381 if (isset($item['timestamp'])) { 382 $query_character = (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && variable_get('clean_url', '0') == '0') ? '&' : '?'; 383 $query_string = $query_character . $item['timestamp']; 384 } 385 386 return '<img src="'. file_create_url($thumb_path) . $query_string . '" title="' . check_plain($item['filename']) . '" alt="' . t('Image preview') . '" />'; 387 } 388 /** 389 * @} End defgroup "Theme Callbacks". 390 */
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 |