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

Contents of /contributions/modules/image_fupload/image_fupload.module

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


Revision 1.34 - (show annotations) (download) (as text)
Tue Jun 9 22:25:54 2009 UTC (5 months, 2 weeks ago) by grandcat
Branch: MAIN
CVS Tags: HEAD
Changes since 1.33: +105 -96 lines
File MIME type: text/x-php
[#450250] (great thx to Deciphered) Code cleanup (Drupals coding standards)
1 <?php
2 // $Id: image_fupload.module,v 1.33 2009/04/06 21:15:41 grandcat Exp $
3
4 define('IMAGE_UNMACHINED', 'image_raw');
5 define('IMAGE_HALFPROCESSED', 'image_halfwork');
6 define('IMAGE_PROCESSED', 'image_processed');
7
8 // mark images (using body field) whose captions aren't edited yet
9 define('IMAGE_NOT_COMPLETED', 'image_not_completed');
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function image_fupload_menu() {
15 $items['fupload/flash'] = array(
16 'title' => 'Image FUpload',
17 'page callback' => 'fupload_filetransfer',
18 'access callback' => TRUE, // must be examined later
19 'type' => MENU_CALLBACK,
20 );
21 $items['fupload/js/deletequeue/%'] = array(
22 'title' => 'Image FUpload',
23 'page callback' => 'fupload_empty_queue',
24 'page arguments' => array(3),
25 'access arguments' => array('mass upload images'),
26 'type' => MENU_CALLBACK,
27 );
28 $items['node/add/%image_node_type/list_images'] = array(
29 'title' => 'Edit Captions',
30 'access arguments' => array('edit captions'),
31 'page callback' => 'drupal_get_form',
32 'page arguments' => array('fupload_list_images_image', 2),
33 'type' => MENU_CALLBACK,
34 'file' => 'includes/images.previewlist.image.inc',
35 );
36 $items['node/add/%image_node_type/list_imagefields'] = array(
37 'title' => 'Edit Captions',
38 'access arguments' => array('edit captions'),
39 'page callback' => 'drupal_get_form',
40 'page arguments' => array('fupload_list_images_imagefield', 2),
41 'type' => MENU_CALLBACK,
42 'file' => 'includes/images.previewlist.imagefield.inc',
43 );
44 return $items;
45 }
46
47 /**
48 * Menu callback; loads a image node type object
49 */
50 function image_node_type_load($node_type, $simple_check = FALSE) {
51 $image_node_types = variable_get('image_node_types', array());
52 // test if given node type is image (field) node type
53 $node_type = str_replace("-", "_", $node_type);
54 // simple check without checking url arguments
55 if ($simple_check && isset($image_node_types[$node_type])) {
56 return $node_type;
57 }
58
59 if (isset($image_node_types[$node_type])) {
60 if (arg(3) == "list_imagefields" && $image_node_types[$node_type]['type'] == "cck") {
61 return $node_type;
62 }
63 elseif (arg(3) == "list_images" && $image_node_types[$node_type]['type'] == "image") {
64 return $node_type;
65 }
66 else {
67 return FALSE;
68 }
69 }
70 else {
71 return FALSE;
72 }
73 }
74
75 /**
76 * returns a identifier for images so that their status (current step) can easily be set
77 */
78 function image_fupload_image_status($field_name, $status = IMAGE_UNMACHINED) {
79 switch ($status) {
80 case IMAGE_NOT_COMPLETED:
81 return "<!--". $field_name ."/". IMAGE_NOT_COMPLETED ."-->";
82 break;
83 default:
84 return $field_name ."/". $status;
85 break;
86 }
87 }
88
89 /**
90 * Implementation of hook_perm().
91 */
92 function image_fupload_perm() {
93 return array('mass upload images', 'edit captions');
94 }
95
96 /**
97 * Implementation of hook_help().
98 */
99 function image_fupload_help($path, $arg) {
100 switch ($path) {
101 case 'admin/help#image_fupload':
102 $output = '<p>'. t("The Image FUpload module is used to provide an alternate upload form to image module itself.") .'</p>';
103 $output .= '<p>'. t("This is a great advantage because multiple images can be selected with one click which are automatically uploaded and processed without any further user interaction. Additionally, this module fully integrates in image module. Consequently, all settings made by image module are observed (thumb creation, file size limit etc.).") .'</p>';
104 $output .= '<p>'. t("Image FUpload administration allows to define some characters which are replaced in the node title by a whitespace. In addition to that, the option can be selected to show a link to the original upload form to those users whose browser doesn't support this Flash / JS solution.") .'</p>';
105 $output .= t('<p>You can</p>
106 <ul>
107 <li>create images using F(lash)Upload at <a href="!node-create-image">node &gt;&gt; create &gt;&gt; image</a>.</li>
108 <li>configure Image FUpload settings at <a href="!admin-settings-image-fupload">administer &gt;&gt; settings &gt;&gt; image &gt;&gt; image_fupload</a>.</li>
109 ', array('!node-create-image' => url('node/add/image'), '!admin-image-galleries' => url('admin/image/galleries'), '!admin-settings-image-fupload' => url('admin/settings/image/image_fupload'))) .'</ul>';
110 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="!image">Image FUpload page</a>.', array('!image' => 'http://www.drupal.org/handbook/modules/image/')) .'</p>';
111 return $output;
112 }
113 }
114
115 /**
116 * Implementation of hook_theme() registry.
117 */
118 function image_fupload_theme() {
119 return array(
120 'swfupload_settings' => array(
121 'template' => 'swfupload-settings',
122 'arguments' => array('modulepath' => NULL, 'uploadpath' => NULL, 'maxfilesize' => NULL, 'fileextensions' => NULL, 'sessionid' => NULL, 'uploadlimit' => NULL, 'nodetype' => NULL, 'fieldname' => NULL, 'field_required' => NULL, 'storage_mode' => NULL, 'redirect_url' => NULL),
123 ),
124 'fupload_create_filename' => array(
125 'arguments' => array('image' => NULL, 'replacements' => NULL),
126 ),
127 'fupload_imagepreview_image' => array(
128 'arguments' => array('image' => NULL, 'image_info' => NULL, 'node_image' => NULL, 'attributes' => NULL),
129 'file' => 'includes/images.previewlist.image.inc',
130 ),
131 );
132 }
133
134 function fupload_filetransfer() {
135 // huh.. swfUpload sends some data...let's see
136 global $user;
137 $sid = $_POST['PHPSESSID'];
138 $node_type = check_plain($_POST['nodetype']);
139 $field_name = check_plain($_POST['fieldname']);
140
141 // validate given session id
142 $result = db_query("SELECT * FROM {sessions} WHERE sid = '%s' AND hostname = '%s' LIMIT 1", $sid, ip_address());
143 $upload_user = db_fetch_object($result);
144 // Get users profile
145 $user = user_load(array('uid' => $upload_user->uid));
146
147 // Do some checks before upload
148 $field_access = FALSE;
149 if (!empty($upload_user) && !empty($node_type) && !empty($field_name)) { // user valid? node_type & field_name received?
150 // Check if given node_type & field_name combination is possible for user
151 switch ($node_type) {
152 case "image":
153 // image node type
154 if ($field_name == "images") {
155 $field_access = node_access('create', $node_type, $user);
156
157 // get suitable validators for our upload: image module
158 $validators = array(
159 'file_validate_is_image' => array(),
160 'file_validate_size' => array(variable_get('image_max_upload_size', 800) * 1024),
161 );
162 }
163 break;
164 default:
165 // probably cck image node type
166 if (module_exists('content')) {
167 if (image_node_type_load($node_type, TRUE) && ($field = content_fields($field_name, $node_type))) {
168 $field_access = node_access('create', $node_type, $user);
169
170 // also get suitable validators for our upload: cck imagefield module
171 $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
172 }
173 }
174 break;
175 }
176 }
177
178 if (user_access('mass upload images', $user) && $field_access) {
179 // Adapt to drupal files structure
180 $_FILES['files']['name']['image'] = $_FILES['Filedata']['name'];
181 $_FILES['files']['type']['image'] = $_FILES['Filedata']['type'];
182 $_FILES['files']['tmp_name']['image'] = $_FILES['Filedata']['tmp_name'];
183 $_FILES['files']['error']['image'] = $_FILES['Filedata']['error'];
184 $_FILES['files']['size']['image'] = $_FILES['Filedata']['size'];
185
186 // do some checks via transliteration module if available
187 if (module_exists('transliteration')) {
188 require_once(drupal_get_path('module', 'transliteration') .'/transliteration.inc');
189 $_FILES['files']['name']['image'] = transliteration_clean_filename($_FILES['Filedata']['name']);
190 }
191
192 if ($file = file_save_upload('image', $validators)) {
193 $image = image_get_info($file->filepath); // Get real mime-type
194 if (!db_query("UPDATE {files} SET filename = '%s', filemime = '%s' WHERE fid = %d", image_fupload_image_status($field_name, IMAGE_UNMACHINED), $image['mime_type'], $file->fid)) {
195 drupal_json(array('status' => FALSE, 'data' => t('A database related problem appeared during upload. Please inform the site administrator if this is a permanent problem.')));
196 exit;
197 }
198
199 // Get all status messages and add them to server message for swfUpload --> inform client
200 $messages = drupal_get_messages('status');
201 drupal_json(array('status' => TRUE, 'data' => t('Complete. !messages', array('!messages' => (!empty($messages['status']) ? implode(' ', $messages['status']) : ''))))); // Reply a status message to satisfy swfUpload
202 }
203 else {
204 // Get responsible error messages and send it to swfUpload
205 $messages = form_get_errors();
206 drupal_json(array('status' => FALSE, 'data' => t('Upload failed: !errors', array('!errors' => implode(' ', $messages)))));
207 }
208
209 }
210 else {
211 drupal_access_denied();
212 }
213 }
214
215 function fupload_empty_queue($field_name) {
216 global $user;
217
218 // Set "processed" flag so that these images aren't processed again; images are deleted later by cron (--> temporary files)
219 db_query("UPDATE {files} SET filename = '%s' WHERE uid = %d AND status = %d AND filename = '%s'", image_fupload_image_status(check_plain($field_name), IMAGE_PROCESSED), $user->uid, FILE_STATUS_TEMPORARY, image_fupload_image_status(check_plain($field_name), IMAGE_UNMACHINED));
220
221 // Output message to user via AJAX
222 drupal_set_message();
223 drupal_set_message(t('All queued images were deleted.'), 'warning');
224 drupal_json(array('status' => TRUE, 'data' => theme('status_messages')));
225 }
226
227 function theme_fupload_create_filename($image, $replacements = NULL) {
228 // Get filename out of filepath
229 $filename = trim(basename($image->filepath), ' ');
230 $length1 = drupal_strlen(strrchr($filename, '.'));
231 $length2 = drupal_strlen($filename);
232 $image_name = drupal_ucfirst(drupal_substr($filename, 0, ($length2 - $length1)));
233
234 // Remove some given (userdefined) elements
235 if (is_null($replacements)) {
236 $replacements = variable_get('fupload_title_replacements', '_;{;}');
237 }
238 $image_name = str_replace(explode(';', $replacements), ' ', $image_name);
239
240 return $image_name;
241 }
242
243 /**
244 * Helper function which emulates "node_form_validate" of node.pages.inc
245 * Need this because body causes validation error with some settings, but I don't need a body field, later yes =)
246 */
247 function fupload_node_form_validate($form, &$form_state) {
248 $node = (object)$form_state['values'];
249 $node->body = NULL; // Little Hack to disable body validation
250 node_validate($node, $form);
251 }
252
253 /**
254 * This function allows to validate a file on given $validator functions.
255 * This code is taken from file_save_upload (D 6.4) (22.09.2008)
256 *
257 * @param object $file
258 * @param array $validators
259 * @return Boolean
260 * returns TRUE, if validation was successful, otherwise FALSE
261 */
262 function file_validate($file, $validators = array()) {
263 // Call the validation functions.
264 $errors = array();
265 foreach ($validators as $function => $args) {
266 array_unshift($args, $file);
267 $errors = array_merge($errors, call_user_func_array($function, $args));
268 }
269
270 // Check for validation errors.
271 if (!empty($errors)) {
272 $message = t('The selected file %name could not be uploaded.', array('%name' => $file->filename));
273 if (count($errors) > 1) {
274 $message .= '<ul><li>'. implode('</li><li>', $errors) .'</li></ul>';
275 }
276 else {
277 $message .= ' '. array_pop($errors);
278 }
279 form_set_error($source, $message);
280 return FALSE;
281 }
282 else {
283 // Validation successful =)
284 return TRUE;
285 }
286 }
287
288 /**
289 * This helper function lists/read/write/delete special settings for the image previewlist
290 * depending on the node type
291 *
292 * @param string $op
293 * @param string $node_type
294 * @param array $data_write
295 * @return array or NULL
296 */
297 function _fupload_imagepreview_settings($op, $node_type, $data_write = NULL) {
298 // switch to the right operation: what's up?
299 switch ($op) {
300 case 'list':
301 // generate a list of available presets for image_preview: image module or/and imagecache
302 $preview_presets_list = array();
303
304 // image module presets
305 if (module_exists('image') && $node_type == "image") {
306 $image_sizes = image_get_sizes();
307
308 foreach ($image_sizes as $key => $size) {
309 $preview_presets_list['image']['0_'. $key] = t('Image size') .': '. $size['label'];
310 }
311 }
312
313 // imagecache module presets
314 if (module_exists('imagecache')) {
315 $image_sizes = imagecache_presets();
316
317 foreach ($image_sizes as $key => $size) {
318 $preview_presets_list['imagecache']['1_'. $size['presetname']] = t('Image size') .': '. $size['presetname'];
319 }
320 }
321
322 //output list
323 return $preview_presets_list;
324 break;
325
326 case 'read':
327 // read actual setting for this node type
328 $image_node_types = variable_get('image_node_types', array());
329
330 switch ($node_type) {
331 case 'image':
332 // image node type
333 if (!empty($image_node_types['image']['image_selection'])) {
334 // image module preset
335 return '0_'. $image_node_types['image']['image_selection'];
336
337 }
338 elseif (!empty($image_node_types['image']['imagecache_preset'])) {
339 // imagecache module preset
340 return '1_'. $image_node_types['image']['imagecache_preset'];
341
342 }
343 else {
344 // no setting saved yet?
345 return '';
346 }
347 break;
348 default:
349 // node type with CCK ImageField
350
351 // return imagecache preset
352 return '1_'. $image_node_types[$node_type]['imagecache_preset'];
353 break;
354 }
355 break;
356
357 case 'write':
358 if (isset($data_write)) { // is some data available?
359 // write input data to database
360 $image_node_types = variable_get('image_node_types', array());
361
362 // prepare type of image preview before writing into db
363 $preview_preset = explode('_', $data_write['preview_preset'], 2);
364 switch ($preview_preset[0]) {
365 case '0':
366 // image module preset
367 $preview_preset_image = $preview_preset[1];
368 break;
369 case '1':
370 // imagecache module preset
371 $preview_preset_imagecache = $preview_preset[1];
372 break;
373 }
374
375 switch ($node_type) {
376 case 'image':
377 // image node type
378 $image_node_types['image'] = array('type' => 'image', 'fieldname' => 'images', 'image_selection' => $preview_preset_image, 'imagecache_preset' => $preview_preset_imagecache);
379 break;
380
381 default:
382 // node type with CCK ImageField
383 $image_node_types[$node_type] = array('type' => 'cck', 'fieldname' => $data_write['fieldname'], 'image_selection' => $preview_preset_image, 'imagecache_preset' => $preview_preset_imagecache);
384 break;
385 }
386 // write data to database
387 variable_set('image_node_types', $image_node_types);
388 }
389 break;
390
391 case 'delete':
392 // delete an entry from settings variable
393 $image_node_types = variable_get('image_node_types', array());
394 unset($image_node_types[$node_type]);
395 // write data to database
396 variable_set('image_node_types', $image_node_types);
397 break;
398
399 }
400 }

  ViewVC Help
Powered by ViewVC 1.1.2