aboutsummaryrefslogtreecommitdiff
path: root/types/uploadfield/uploadfield.module
blob: 73d42473c792a72cc6c738ad096bac93d729f51c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php
// $Id$

/**
 * @file
 * uploadfield core hooks and menu callbacks.
 */

include_once dirname(__FILE__) . '/uploadfield_file.inc';
include_once dirname(__FILE__) . '/uploadfield_widget.inc';

/**
 * Implementation of hook_init().
 *
 * Load required includes.
 */
function uploadfield_init() {
// add CSS file
  drupal_add_css(drupal_get_path('module', 'video') .'/types/uploadfield/uploadfield.css');
  // If FileField is not available, immediately disable uploadfield.
  if (!module_exists('filefield')) {
    module_disable(array('uploadfield'));
    drupal_set_message(t('The uploadfield module has been disabled. The <a href="http://drupal.org/project/filefield">FileField module</a> needs to be installed for it to work properly.'));
    return;
  }
}

/**
 * Implementation of hook_theme().
 */
function uploadfield_theme() {
  $theme = array();
  $theme = array(
                // Theme an image uploaded to uploadfield with alt and title.
                // TODO: Switch to core theme image if possible.
                'uploadfield_image' => array(
                'arguments' => array('file' => NULL,
                                     'alt' => '',
                                     'title' => '',
                                     'attributes' => NULL,
                                     'getsize' => TRUE),
                                    ),
              // Theme an uploadfield field item. It calls imagefied_image with the proper
              // item properties as arguments.
              'uploadfield_item' => array(
              'arguments' => array('item' => NULL),
              ),
              // uploadfield_widget form element type theme function.
              'uploadfield_widget' => array(
              'arguments' => array('element' => NULL),
              'file' => 'uploadfield_widget.inc',
              ),
              // Use to generate a preview (admin view) of an uploadfield item for use in
              // field item forms and filefield widgets. Invoked by filefield_widget_process.
              'uploadfield_widget_preview' => array(
              'arguments' => array('item' => NULL),
              ),
              // Theme function for the field item elements. allows you to place children
              // within the context of the parent.
              'uploadfield_widget_item' => array(
              'arguments' => array('element' => NULL),
              ),
              // Generates and img tag to the admin thumbnail of an uploadfield upload.
              'uploadfield_admin_thumbnail' => array(
              'arguments' => array('item' => NULL),
              ),
              // Generates and img tag to the video thumbnails.
              'uploadfield_widget_video_thumb' => array(
              'arguments' => array('item' => NULL),
              ),

              // uploadfield formatter theme functions.
              'uploadfield_formatter_video_plain' => array(
              'arguments' => array('element' => NULL),
              'file' => 'uploadfield_formatter.inc',
              ),
              'uploadfield_formatter_video_nodelink' => array(
              'arguments' => array('element' => NULL),
              'file' => 'uploadfield_formatter.inc',
              ),
              'uploadfield_formatter_video_imagelink' => array(
              'arguments' => array('element' => NULL),
              'file' => 'uploadfield_formatter.inc',
              ),

              //from includes/common.inc
              'video_format_play' => array(
              'arguments' => array( 'output' => NULL,
                                    'url' => NULL,
                                    'title' => NULL,
                                    'link_text' => NULL
                                  ),
              ),
              'video_play_dcr' => array(
              'arguments' => array('node' => NULL),
              ),
              'video_play_divx' => array(
              'arguments' => array('node' => NULL),
              ),
              'video_play_flash' => array(
              'arguments' => array('node' => NULL),
              ),
              'video_play_ogg_theora' => array(
              'arguments' => array('node' => NULL),
              ),
              'video_play_quicktime' => array(
              'arguments' => array('node' => NULL),
              ),
              'video_play_realmedia' => array(
              'arguments' => array('node' => NULL),
              ),
              'video_play_swf' => array(
              'arguments' => array('node' => NULL),
              ),
              'video_play_windowsmedia' => array(
              'arguments' => array('node' => NULL),
              ),
              'video_encoding_failed' => array(
              'arguments' => array(),
              ),
              'video_inprogress' => array(
              'arguments' => array(),
              ),
          );
  if(module_exists('imagecache')) {
    // imagecache presets
    foreach (imagecache_presets() as $preset) {
      $theme['uploadfield_formatter_'. $preset['presetname'] .'_videolinked'] = array(
          'arguments' => array('element' => NULL),
          'function' => 'theme_uploadfield_formatter_videolinked',
          'file' => 'uploadfield_formatter.inc'
      );
    }
  }

  return $theme;
}

/**
 * Implementation of hook_elements().
 */
function uploadfield_elements() {
  $elements = array();

  // An uploadfield is really just a FileField with extra processing.
  $filefield_elements = module_invoke('filefield', 'elements');
  $elements['uploadfield_widget'] = $filefield_elements['filefield_widget'];
  $elements['uploadfield_widget']['#process'][] = 'uploadfield_widget_process';
  $elements['uploadfield_widget']['#element_validate'][] = 'uploadfield_widget_validate';

  // uploadfield needs a separate value callback to save its alt and title texts.
  $elements['uploadfield_widget']['#value_callback'] = 'uploadfield_widget_value';

  return $elements;
}

/**
 * Implementation of hook_file_download.
 */
function uploadfield_file_download($filepath) {
//  watchdog('video', 'file path is ' . $filepath);
//  echo $filepath;
// Return headers for admin thumbnails if private files are enabled.
//  if (strpos($filepath, 'video_thumbs') !== FALSE) {
//    $original_path = str_replace('video_thumbs/', '', $filepath);
//    $original_full_path = file_create_path($original_path);
//    $thumb_full_path = file_create_path($filepath);
//
//    // Allow access to temporary thumbnails, since they're not yet associated
//    // with a node. If not temporary, check access on the original file.
//    $status = db_result(db_query("SELECT status FROM {files} WHERE filepath = '%s'", $original_full_path));
//    $access = ($status == 0 || module_invoke_all('file_download', $original_path));
//    if ($access && $info = getimagesize($thumb_full_path)) {
//      return array(
//      'Content-Type: ' . $info['mime'],
//      'Content-Length: ' . filesize($thumb_full_path)
//      );
//    }
//  }

  // Return headers for default images.
//  if (strpos($filepath, 'uploadfield_default_images') !== FALSE) {
  if (strpos($filepath, 'video_thumbs') !== FALSE) {
    $full_path = file_create_path($filepath);
    if ($info = getimagesize($full_path)) {
      return array(
      'Content-Type: ' . $info['mime'],
      'Content-Length: ' . filesize($full_path)
      );
    }
  }
}

/**
 * Implementation of CCK's hook_widget_info().
 */
function uploadfield_widget_info() {
  $module_path = drupal_get_path('module', 'uploadfield');
  return array(
  'uploadfield_widget' => array(
  'label' => t('Video'),
  'field types' => array('filefield'),
  'multiple values' => CONTENT_HANDLE_CORE,
  'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM),
  'description' => t('An edit widget for video files, including video thumbnails and transcoding to flash.'),
  ),
  );
}

/**
 * Implementation of CCK's hook_widget_settings().
 */
function uploadfield_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      return uploadfield_widget_settings_form($widget);
    case 'validate':
      return uploadfield_widget_settings_validate($widget);
    case 'save':
      return uploadfield_widget_settings_save($widget);
  }
}

/**
 * Implementation of CCK's hook_widget().
 *
 * Assign default properties to item and delegate to FileField.
 */
function uploadfield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
// Add default values to items.
// TODO: use CCK's default value callback.
  if (empty($items[$delta])) {
    $items[$delta] = array('alt' => '', 'title' => '');
  }

  // Start with the FileField widget as a basic start.
  // Note that FileField needs to modify $form by reference.
  $element = filefield_widget($form, $form_state, $field, $items, $delta);

  // Add uploadfield specific validators.
  //  $element['#upload_validators'] = array_merge($element['#upload_validators'], uploadfield_widget_upload_validators($field));

  return $element;
}

/**
 * Get the additional upload validators for an image field.
 *
 * @param $field
 *   The CCK field array.
 * @return
 *   An array suitable for passing to file_save_upload() or the file field
 *   element's '#upload_validators' property.
 */
function uploadfield_widget_upload_validators($field) {
  $validators = array();

  // Ensure that only web images are supported.
  $web_extensions = array(
      'mov', 'mp4', '3gp', '3g2', 'mpg', 'mpeg', // quicktime
      'divx', //divx
      'rm', // realplayer
      'flv', 'f4v', //flash player
      'swf', // swf player
      'dir', 'dcr', // dcr player
      'asf', 'wmv', 'avi', 'mpg', 'mpeg', // windows media
      'ogg' // ogg theora
  );
  $extensions = array_filter(explode(' ', $field['widget']['file_extensions']));
  if (empty($extensions)) {
    $extensions = $web_extensions;
  }
  $validators['filefield_validate_extensions'][0] = implode(' ', array_intersect($extensions, $web_extensions));

  // Add the image validator as a basic safety check.
  //  $validators['filefield_validate_is_image'] = array();

  // Add validators for resolutions.
  //  if (!empty($field['widget']['max_resolution']) || !empty($field['widget']['min_resolution'])) {
  //    $validators['filefield_validate_image_resolution'] = array(
  //        $field['widget']['max_resolution'],
  //        $field['widget']['min_resolution'],
  //    );
  //  }

  return $validators;
}

/**
 * Implementation of CCK's hook_field_formatter_info().
 */
function uploadfield_field_formatter_info() {
  $module_path = drupal_get_path('module', 'uploadfield');
  $formatters = array(
      'video_plain' => array(
      'label' => t('video'),
      'field types' => array('filefield'),
      'description' => t('Displays video files with player embedded.'),
      ),
      'video_nodelink' => array(
      'label' => t('video thumbnail linked to video'),
      'field types' => array('filefield'),
      'description' => t('Displays video thumb files then the video.'),
      ),
      //      'video_imagelink' => array(
      //      'label' => t('Video Thumbnail linked to video file download'),
      //      'field types' => array('filefield'),
      //      'description' => t('Displays video files in their original size.'),
      //      ),
  );

if(module_exists('imagecache')) {
  foreach (imagecache_presets() as $preset) {
    $formatters[$preset['presetname'] .'_videolinked'] = array(
        'label' => t('Preset @preset of video thumbnail linked to video', array('@preset' => $preset['presetname'])),
        'field types' => array('filefield'),
    );
  }
}
  return $formatters;
}

/**
 * Implementation of CCK's hook_default_value().
 */
function uploadfield_default_value(&$form, &$form_state, $field, $delta) {
  return filefield_default_value($form, $form_state, $field, $delta);
}

/**
 * Implementation of hook_form_[form_id]_alter().
 *
 * Modify the add new field form to make "Image" the default formatter.
 */
function uploadfield_form_content_field_overview_form_alter(&$form, &$form_state) {
  $form['#submit'][] = 'uploadfield_form_content_field_overview_submit';
}

/**
 * Submit handler to set a new field's formatter to "video_plain".
 */
function uploadfield_form_content_field_overview_submit(&$form, &$form_state) {
  if (isset($form_state['fields_added']['_add_new_field']) && isset($form['#type_name'])) {
    $new_field = $form_state['fields_added']['_add_new_field'];
    $node_type = $form['#type_name'];
    $field = content_fields($new_field, $node_type);
    if ($field['widget']['module'] == 'uploadfield') {
      foreach ($field['display_settings'] as $display_type => $display_settings) {
        if ($field['display_settings'][$display_type]['format'] == 'default') {
          $field['display_settings'][$display_type]['format'] = 'video_plain';
        }
      }
      content_field_instance_update($field);
    }
  }
}

/**
 * @defgroup "Theme Callbacks"
 * @{
 * @see uploadfield_theme().
 */
function theme_uploadfield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  $file = (array)$file;
  //  return $file['filepath'];
  if (!is_file($file['filepath'])) {
    return '<!-- File not found: '. $file['filepath'] .' -->';
  }

  if ($getsize) {
  // Use cached width and height if available.
    if (!empty($file['data']['width']) && !empty($file['data']['height'])) {
      $attributes['width']  = $file['data']['width'];
      $attributes['height'] = $file['data']['height'];
    }
    // Otherwise pull the width and height from the file.
    elseif (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])) {
      $attributes['width'] = $width;
      $attributes['height'] = $height;
    }
  }

  if (!empty($title)) {
    $attributes['title'] = $title;
  }

  // Alt text should be added even if it is an empty string.
  $attributes['alt'] = $alt;

  // Add a timestamp to the URL to ensure it is immediately updated after editing.
  $query_string = '';
  if (isset($file['timestamp'])) {
    $query_character = (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && variable_get('clean_url', '0') == '0') ? '&' : '?';
    $query_string = $query_character . $file['timestamp'];
  }

  $url = file_create_url($file['filepath']) . $query_string;
  $attributes['src'] = $url;
  $attributes = drupal_attributes($attributes);
  return '<span></span><img '. $attributes .' />';
}

function theme_uploadfield_item($item) {
  return theme('uploadfield_image', $item, $item['alt'], $item['title']);
}

function theme_uploadfield_widget_preview($item = NULL) {
  return '<div class="uploadfield-preview">' . theme('uploadfield_admin_thumbnail', $item) . '</div>';
}

function theme_uploadfield_widget_item($element) {
  return theme('filefield_widget_item', $element);
}

function theme_uploadfield_admin_thumbnail($item = NULL) {
  if (is_null($item) || empty($item['filepath'])) {
    return '<!-- link to default admin thumb -->';
  }
  $thumb_path = uploadfield_file_admin_thumb_path($item);
  return '<img src="'. file_create_url($thumb_path) .'" title="' . check_plain($item['filename']) . '" />';
}

function theme_uploadfield_widget_video_thumb($item = NULL) {
  return '<div class="uploadfield-video-thumb">' . theme('uploadfield_image', $item, '', '', '', FALSE) . '</div>';
}
/**
 * @} End defgroup "Theme Callbacks".
 */

/**
 * Implementing video module API
 * hook_v_perm()
 */

function uploadfield_v_perm() {
  return array('use default thumbnail', 'bypass conversion video');
}

/**
 * Implementation of hook_v_help
 */
function video_upload_v_help() {

  $help = array();
  $help['upload']['data'] = '<b>' . t('Upload support') . '</b>';
  $help['upload']['children'] = array(t('You can upload a video file from your computer to this website.'));

  return $help;
}


/**
 * Implements the hook_v_auto_resolution
 */
function uploadfield_v_auto_resolution($vidfile) {
//TODO : add dynamic converter selector
  module_load_include('inc', 'video', '/plugins/ffmpeg');
  if(module_hook('ffmpeg', 'auto_playtime')) {
    return ffmpeg_auto_playtime($vidfile);
  }
  return false;
}


/**
 * Implements the hook_v_auto_resolution
 */
function uploadfield_v_auto_playtime($vidfile) {
//TODO : add dynamic converter selector
  module_load_include('inc', 'video', '/plugins/ffmpeg');
  if(module_hook('ffmpeg', 'auto_playtime')) {
    return ffmpeg_auto_playtime($vidfile);
  }
  return false;
}

/**
 * Implementation of hook_v_video()
 */
function uploadfield_v_video($op, &$element) {
  global $user;
  switch ($op) {
    case 'insert':
    case 'update':
    // TODO : need to add validation on just submit scenario
      $field = content_fields($element['#field_name'], $element['#type_name']);
      // auto thumbnailing
      if($field['widget']['autothumbnail']) {
        $update = NULL;
        $vidfile = $element['#value'];
        $vid = $vidfile['fid'];
        $vid_path = $vidfile['data']['video_thumb'];
        $vid_dir = explode('/', $vid_path);
        $vid_name = $vid_dir[count($vid_dir) - 1];

        $file = new stdClass();
        $file->uid      = $user->uid;
        $file->status   = FILE_STATUS_PERMANENT;
        $file->filename = $vid_name;
        $file->filepath = $vid_path;
        $file->filemime = file_get_mimetype($vid_name);
        $file->filesize = filesize($vid_path);
        $file->timestamp = time();

        if($op == 'update') {
          $old_fid = $element['#default_value']['data']['video_thumb_fid'];
          $update = 'fid';
          $file->fid = $old_fid;
        }
        drupal_write_record ('files', $file, $update);
        // get fid if and only if insert
        if($op == 'insert')
          $file->fid = db_last_insert_id('files', 'fid');
        // add file id to the data
        $element['data']['video_thumb_fid'] = array (
            '#type'=> 'value',
            '#value' => $file->fid
        );
      }

      // auto covnersion
      if($field['widget']['autoconversion']) {
        video_auto_transcode_add_to_queue($element, $op);
      }
      break;
    case 'delete':
    //delete thumbnails
      break;
    case 'thumbs':
      include_once dirname(__FILE__) . '/uploadfield_thumb.inc';
      //create thumbs and display
      video_auto_thumb_process($element);
      break;
    case 'convert':
      include_once dirname(__FILE__) . '/uploadfield_convert.inc';
      //create FLV file
//      video_auto_transcode_process($element);
      break;
  }
}