/[drupal]/contributions/modules/flashfield/flashfield_widget.inc
ViewVC logotype

Contents of /contributions/modules/flashfield/flashfield_widget.inc

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


Revision 1.3 - (show annotations) (download) (as text)
Sun Apr 26 21:19:48 2009 UTC (7 months ago) by stuartgreenfield
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +145 -156 lines
File MIME type: text/x-php
Further work on flashfield. Usable, but not yet complete.
1 <?php
2 // $Id: flashfield_widget.inc,v 1.2 2009/04/26 06:25:32 stuartgreenfield Exp $
3
4 /**
5 * @file
6 * flashfield widget hooks and callbacks.
7 */
8
9 /**
10 * Implementation of CCK's hook_widget_settings($op = 'form').
11 */
12 function flashfield_widget_settings_form($widget) {
13
14 $form = module_invoke('filefield', 'widget_settings', 'form', $widget);
15
16 // Sizes
17 $form['size_settings'] = array(
18 '#type' => 'fieldset',
19 '#title' => t('Size settings'),
20 '#collapsible' => TRUE,
21 '#collapsed' => TRUE,
22 '#weight' => 8,
23 );
24
25 $form['size_settings']['custom_size'] = array(
26 '#type' => 'checkbox',
27 '#title' => t('Custom sizes'),
28 '#default_value' => !empty($widget['custom_size']) ? $widget['custom_size'] : 0,
29 '#description' => t('Allow users to specify a custom height and width for their content.'),
30 );
31
32 $form['size_settings']['max_height'] = array(
33 '#type' => 'textfield',
34 '#title' => t('Maximum height'),
35 '#default_value' => !empty($widget['max_height']) ? $widget['max_height'] : '',
36 '#size' => 15,
37 '#maxlength' => 10,
38 '#description' => t('The displayed flash height - content will be scaled to comply.'),
39 );
40
41 $form['size_settings']['max_width'] = array(
42 '#type' => 'textfield',
43 '#title' => t('Maximum width'),
44 '#default_value' => !empty($widget['max_width']) ? $widget['max_width'] : '',
45 '#size' => 15,
46 '#maxlength' => 10,
47 '#description' => t('The displayed flash width - content will be scaled to comply.'),
48 );
49
50 // Alt
51 $form['alt_settings'] = array(
52 '#type' => 'fieldset',
53 '#title' => t('Alternate text settings'),
54 '#collapsible' => TRUE,
55 '#collapsed' => TRUE,
56 '#weight' => 8,
57 );
58 $form['alt_settings']['custom_alt'] = array(
59 '#type' => 'checkbox',
60 '#title' => t('Enable custom alternate text'),
61 '#default_value' => !empty($widget['custom_alt']) ? $widget['custom_alt'] : 0,
62 '#description' => t('Enable user input alternate text for images.'),
63 );
64 $form['alt_settings']['alt'] = array(
65 '#type' => 'textfield',
66 '#title' => t('Default alternate text'),
67 '#default_value' => !empty($widget['alt']) ? $widget['alt'] : '!default',
68 '#description' => t('This value will be used for alternate text by default.'),
69 '#suffix' => theme('token_help', 'file'),
70 );
71
72 // Flashvars
73 $form['flashvars_settings'] = array(
74 '#type' => 'fieldset',
75 '#title' => t('Flashvars settings'),
76 '#collapsible' => TRUE,
77 '#collapsed' => TRUE,
78 '#weight' => 8,
79 );
80 $form['flashvars_settings']['custom_flashvars'] = array(
81 '#type' => 'checkbox',
82 '#title' => t('Enable custom flashvars'),
83 '#default_value' => !empty($widget['custom_flashvars']) ? $widget['custom_flashvars'] : 0,
84 '#description' => t('Allow users to enter custom flashvars.'),
85 );
86
87 // Base
88 $form['base_settings'] = array(
89 '#type' => 'fieldset',
90 '#title' => t('Base settings'),
91 '#collapsible' => TRUE,
92 '#collapsed' => TRUE,
93 '#weight' => 8,
94 );
95 $form['base_settings']['custom_base'] = array(
96 '#type' => 'checkbox',
97 '#title' => t('Enable custom base'),
98 '#default_value' => !empty($widget['custom_base']) ? $widget['custom_base'] : 0,
99 '#description' => t('Allow users to customize the base path.'),
100 );
101 $form['base_settings']['base'] = array(
102 '#type' => 'textfield',
103 '#title' => t('Default base'),
104 '#default_value' => !empty($widget['base']) ? $widget['base'] : '',
105 '#description' => t('Leave blank to use the default value of the system files\' directory.'),
106 );
107
108 // Params
109 $form['params_settings'] = array(
110 '#type' => 'fieldset',
111 '#title' => t('Params settings'),
112 '#collapsible' => TRUE,
113 '#collapsed' => TRUE,
114 '#weight' => 8,
115 );
116 $form['params_settings']['custom_params'] = array(
117 '#type' => 'checkbox',
118 '#title' => t('Enable custom parameters'),
119 '#default_value' => !empty($widget['custom_params']) ? $widget['custom_params'] : 0,
120 '#description' => t('Allow users to customize the flash parameters.'),
121 );
122
123 $form['params_settings']['params'] = array(
124 '#type' => 'textfield',
125 '#title' => t('Default parameters'),
126 '#default_value' => !empty($widget['params']) ? $widget['params'] : '',
127 '#description' => t('This value will be used for alternate text by default.'),
128 );
129
130 return $form;
131 }
132
133 /**
134 * Element specific validation for flashfield default value.
135 *
136 * Validated in a separate function from flashfield_field() to get access
137 * to the $form_state variable.
138 */
139 function _flashfield_widget_settings_default_validate($element, &$form_state) {
140 // Verify the destination exists
141 $destination = file_directory_path() .'/flashfield_default_images';
142 if (!field_file_check_directory($destination, FILE_CREATE_DIRECTORY)) {
143 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))));
144 return;
145 }
146
147 $validators = array(
148 'file_validate_is_image' => array(),
149 );
150
151 // We save the upload here because we can't know the correct path until the file is saved.
152 if (!$file = file_save_upload('default_image_upload', $validators, $destination)) {
153 // No upload to save we hope... or file_save_upload() reported an error on its own.
154 return;
155 }
156
157 // Remove old image (if any) & clean up database.
158 $old_default = $form_state['values']['default_image'];
159 if (!empty($old_default['fid'])) {
160 if (file_delete(file_create_path($old_default['filepath']))) {
161 db_query('DELETE FROM {files} WHERE fid=%d', $old_default['fid']);
162 }
163 }
164
165 // Make the file permanent and store it in the form.
166 file_set_status($file, FILE_STATUS_PERMANENT);
167 $file->timestamp = time();
168 $form_state['values']['default_image'] = (array)$file;
169 }
170
171 /**
172 * Implementation of CCK's hook_widget_settings($op = 'validate').
173 */
174 function flashfield_widget_settings_validate($widget) {
175
176 // // Check that only web images are specified in the callback.
177 // $extensions = array_filter(explode(' ', $widget['file_extensions']));
178 // $web_extensions = array('swf', 'flv', 'mp3' ,'jpg', 'jpeg', 'gif', 'png');
179 // if (count(array_diff($extensions, $web_extensions))) {
180 // form_set_error('file_extensions', t('Only swf, flv, mp3 and web-standard images (jpg, gif, and png) are supported through the flash widget. If needing to upload other types of images, change the widget to use a standard file upload.'));
181 // }
182
183 // // Check that set resolutions are valid.
184 // foreach (array('min_resolution', 'max_resolution') as $resolution) {
185 // if (!empty($widget[$resolution]) && !preg_match('/^[0-9]+x[0-9]+$/', $widget[$resolution])) {
186 // form_set_error($resolution, t('Please specify a resolution in the format WIDTHxHEIGHT (e.g. 640x480).'));
187 // }
188 // }
189
190 // TODO - Validate max height and width
191
192 }
193
194 /**
195 * Implementation of CCK's hook_widget_settings($op = 'save').
196 */
197 function flashfield_widget_settings_save($widget) {
198 $filefield_settings = module_invoke('filefield', 'widget_settings', 'save', $widget);
199 return array_merge($filefield_settings, array('custom_size', 'max_height', 'max_width', 'alt', 'custom_alt', 'custom_flashvars', 'base', 'custom_base', 'params', 'custom_params'));
200 }
201
202 /**
203 * Element #value_callback function.
204 */
205 function flashfield_widget_value($element, $edit = FALSE) {
206
207
208 $item = filefield_widget_value($element, $edit);
209
210 if ($edit) {
211 $item['height'] = isset($edit['height']) ? $edit['height'] : '';
212 $item['width'] = isset($edit['width']) ? $edit['width'] : '';
213 $item['alt'] = isset($edit['alt']) ? $edit['alt'] : '';
214 $item['flashvars'] = isset($edit['flashvars']) ? $edit['flashvars'] : '';
215 $item['base'] = isset($edit['base']) ? $edit['base'] : '';
216 $item['params'] = isset($edit['params']) ? $edit['params'] : '';
217 }
218 else {
219 $item['height'] = '';
220 $item['width'] = '';
221 $item['alt'] = '';
222 $item['flashvars'] = '';
223 $item['base'] = '';
224 $item['params'] = '';
225 }
226
227 return $item;
228 }
229
230 /**
231 * Element #process callback function.
232 */
233 function flashfield_widget_process($element, $edit, &$form_state, $form) {
234
235 // dsm('Processing');
236 // dsm($element);
237
238 $file = $element['#value'];
239 $field = content_fields($element['#field_name'], $element['#type_name']);
240
241 // Assign default height and width - TODO implement flashnode mechanism to handle case when this isn't visible?
242 if (isset($element['preview']) && $element['#value']['fid'] != 0) {
243
244 // Get info
245 $info = image_get_info($file['filepath']);
246
247 // Assign height
248 if (((empty($file['data']['height']) && $file['data']['height'] !== '0') || $field['widget']['custom_size']) && $info) {
249 dsm('Setting height');
250 $file['data']['height'] = $info['height'];
251 }
252 else {
253 $file['data']['height'] = '';
254 }
255
256 // Assign width
257 if (((empty($file['data']['width']) && $file['data']['width'] !== '0') || $field['widget']['custom_size']) && $info) {
258 $file['data']['width'] = $info['width'];
259 }
260 else {
261 $file['data']['width'] = '';
262 }
263 }
264
265 $element['data']['height'] = array(
266 '#type' => $field['widget']['custom_size'] ? 'textfield' : 'value',
267 '#title' => t('Height'),
268 '#default_value' => $file['data']['height'],
269 '#description' => t('Height.'),
270 );
271
272 // #value must be hard-coded if #type = 'value'.
273 if (!$field['widget']['custom_size']) {
274 $element['data']['height']['#value'] = $file['data']['height'];
275 }
276
277 $element['data']['width'] = array(
278 '#type' => $field['widget']['custom_size'] ? 'textfield' : 'value',
279 '#title' => t('Width'),
280 '#default_value' => $file['data']['width'],
281 '#description' => t('Width.'),
282 );
283
284 // #value must be hard-coded if #type = 'value'.
285 if (!$field['widget']['custom_size']) {
286 $element['data']['width']['#value'] = $file['data']['width'];
287 }
288
289 $element['data']['alt'] = array(
290 '#title' => t('Alternate text'),
291 '#type' => $field['widget']['custom_alt'] ? 'textfield' : 'value',
292 '#default_value' => $file['data']['alt'],
293 '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
294 );
295
296 // #value must be hard-coded if #type = 'value'.
297 if (!$field['widget']['custom_alt']) {
298 $element['data']['alt']['#value'] = $field['widget']['alt'];
299 }
300
301 $element['data']['flashvars'] = array(
302 '#type' => $field['widget']['custom_flashvars'] ? 'textfield' : 'value',
303 '#title' => t('Flashvars'),
304 '#default_value' => $file['data']['flashvars'],
305 '#description' => t('Flashvars.'),
306 );
307
308 // #value must be hard-coded if #type = 'value'.
309 if (!$field['widget']['custom_flashvars']) {
310 $element['data']['flashvars']['#value'] = '';
311 }
312
313 $element['data']['base'] = array(
314 '#type' => $field['widget']['custom_base'] ? 'textfield' : 'value',
315 '#title' => t('Base'),
316 '#default_value' => $file['data']['base'],
317 '#description' => t('Base.'),
318 );
319
320 // #value must be hard-coded if #type = 'value'.
321 if (!$field['widget']['custom_base']) {
322 $element['data']['base']['#value'] = $field['widget']['base'];
323 }
324
325 $element['data']['params'] = array(
326 '#type' => $field['widget']['custom_params'] ? 'textfield' : 'value',
327 '#title' => t('Params'),
328 '#default_value' => $file['data']['params'],
329 '#description' => t('Params.'),
330 );
331
332 // #value must be hard-coded if #type = 'value'.
333 if (!$field['widget']['custom_params']) {
334 $element['data']['params']['#value'] = $field['widget']['params'];
335 }
336
337 return $element;
338 }
339
340 /**
341 * FormAPI theme function. Theme the output of an image field.
342 */
343 function theme_flashfield_widget(&$element) {
344 // drupal_add_css(drupal_get_path('module', 'flashfield') .'/flashfield.css');
345 return theme('form_element', $element, $element['#children']);
346 }
347
348
349 /**
350 * widget_validate().
351 */
352 function flashfield_widget_validate(&$element, &$form_state) {
353 dsm('Validating');
354 // dsm('Element');
355 // dsm($element);
356 // dsm('Form state');
357 dsm($form_state);
358 //
359
360 if (!empty($element['data']['height']['#value']) && !is_numeric($element['data']['height']['#value'])) {
361 form_set_error($element['#field_name'] . '][' . $element['#delta'] . '][data][height', t('Height must be a number, or blank.'));
362 }
363
364 if (!empty($element['data']['width']['#value']) && !is_numeric($element['data']['width']['#value'])) {
365 form_set_error($element['#field_name'] . '][' . $element['#delta'] . '][data][width', t('Width must be a number, or blank.'));
366 }
367
368 }

  ViewVC Help
Powered by ViewVC 1.1.2