/[drupal]/contributions/modules/imagefield/imagefield.module
ViewVC logotype

Contents of /contributions/modules/imagefield/imagefield.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


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

  ViewVC Help
Powered by ViewVC 1.1.2