| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id$
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file video_upload.browser.inc
|
| 7 |
* Provides functions for the Browser upload method.
|
| 8 |
* @todo (Works for 5.x, but not yet in 6.x)
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Prepare form for the browser upload method (video transmitted
|
| 13 |
* directly to 3rd party from the browser).
|
| 14 |
*/
|
| 15 |
function _video_upload_browser_method_widget(&$form, &$form_state, $field, $items, $delta) {
|
| 16 |
// set form class (the jquery depends on this), and encoding type
|
| 17 |
$form['#attributes'] = array(
|
| 18 |
'class' => 'video-upload',
|
| 19 |
'enctype' => 'multipart/form-data',
|
| 20 |
);
|
| 21 |
|
| 22 |
$element = _video_upload_browser_widget_form($form['#node'], $field, $items);
|
| 23 |
return $element;
|
| 24 |
switch ($op) {
|
| 25 |
case 'prepare form values' :
|
| 26 |
_video_upload_widget_browser_prepare_form_values($node, $field, $items);
|
| 27 |
break;
|
| 28 |
|
| 29 |
case 'process form values':
|
| 30 |
_video_upload_widget_browser_process_form_values($node, $field, $items);
|
| 31 |
break;
|
| 32 |
|
| 33 |
case 'form' :
|
| 34 |
return _video_upload_browser_widget_form($node, $field, $items);
|
| 35 |
|
| 36 |
case 'validate' :
|
| 37 |
// this doesn't work yet
|
| 38 |
_video_upload_browser_widget_validate($node, $field, $items);
|
| 39 |
break;
|
| 40 |
|
| 41 |
case 'submit' :
|
| 42 |
break;
|
| 43 |
|
| 44 |
case 'default value' :
|
| 45 |
return array(
|
| 46 |
array(
|
| 47 |
'id' => '0',
|
| 48 |
'status' => VIDEO_UPLOAD_STATUS_UNKNOWN,
|
| 49 |
'status_ts' => '',
|
| 50 |
'fid' => '',
|
| 51 |
// @todo see other inline delete todo items
|
| 52 |
// 'delete' => 0,
|
| 53 |
),
|
| 54 |
);
|
| 55 |
break;
|
| 56 |
}
|
| 57 |
return;
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Depending on if $node_field contains a YouTube ID or not, the form returned
|
| 62 |
* will be drastically different.
|
| 63 |
* @param boolean $save
|
| 64 |
* This is only passed when rendering the form from
|
| 65 |
*/
|
| 66 |
function _video_upload_browser_widget_form(&$node, $field, &$items) {
|
| 67 |
$field_name = $field['field_name'];
|
| 68 |
|
| 69 |
// this will be set to false if a video already exists on the field,
|
| 70 |
// and multiple uploads aren't allowed.
|
| 71 |
$generate_upload_form = true;
|
| 72 |
|
| 73 |
$form[$field_name] = array(
|
| 74 |
'#type' => 'fieldset',
|
| 75 |
'#title' => t($field['widget']['label']),
|
| 76 |
'#weight' => $field['widget']['weight'],
|
| 77 |
'#description' => t('<strong>Note</strong>: Uploaded videos will not be saved until this post has been saved.'),
|
| 78 |
'#collapsible' => TRUE,
|
| 79 |
'#collapsed' => FALSE,
|
| 80 |
'#tree' => TRUE,
|
| 81 |
'#prefix' => '<div id="'. form_clean_id($field_name . '-attach-wrapper') . '" >',
|
| 82 |
'#suffix' => '</div>',
|
| 83 |
);
|
| 84 |
|
| 85 |
$form[$field_name][0]['id'] = array(
|
| 86 |
'#type' => 'hidden',
|
| 87 |
'#value' => $items[0]['id'],
|
| 88 |
);
|
| 89 |
|
| 90 |
// Attempt a preview (won't show much on adding) for videos
|
| 91 |
// that have an id, and have been confirmed
|
| 92 |
if ($items[0]['id']) {
|
| 93 |
if ($items[0]['status'] > VIDEO_UPLOAD_STATUS_UNKNOWN) {
|
| 94 |
$form[$field_name][0]['preview'] = array(
|
| 95 |
'#type' => 'markup',
|
| 96 |
'#value' => theme('video_upload_video', $items[0]['id'], $field['widget']['display']['small_width'], $field['widget']['display']['small_height']),
|
| 97 |
'#suffix' => t('Use the form below to replace this video with a different one.'),
|
| 98 |
);
|
| 99 |
}
|
| 100 |
elseif ($items[0]['status'] === VIDEO_UPLOAD_STATUS_UNKNOWN) {
|
| 101 |
$form[$field_name][0]['preview'] = array(
|
| 102 |
'#type' => 'markup',
|
| 103 |
'#value' => '<div class="messages">' . t('Video is processing...') . '</div>',
|
| 104 |
);
|
| 105 |
}
|
| 106 |
// @todo make this work (see todo in form processing step for
|
| 107 |
// some initial work
|
| 108 |
|
| 109 |
// $form[$field_name][0]['delete'] = array(
|
| 110 |
// '#type' => 'checkbox',
|
| 111 |
// '#title' => t('Delete video ID %id from YouTube', array('%id' => $items[0]['id'])),
|
| 112 |
// );
|
| 113 |
}
|
| 114 |
|
| 115 |
// include Drupal js for dynamic upload handling
|
| 116 |
// drupal_add_js('misc/progress.js');
|
| 117 |
// drupal_add_js('misc/upload.js');
|
| 118 |
drupal_add_js(drupal_get_path('module', 'video_upload') . '/video_upload.js');
|
| 119 |
|
| 120 |
$form[$field_name]['new'] = array(
|
| 121 |
'#tree' => FALSE,
|
| 122 |
'#prefix' => '<div id="' . form_clean_id($field_name .'-attach-hide') . '">',
|
| 123 |
'#suffix' => '</div>',
|
| 124 |
'#weight' => 100,
|
| 125 |
);
|
| 126 |
|
| 127 |
// youtube expects the POST variable to be named "file"
|
| 128 |
if (!empty($field['widget']['file_extensions'])) {
|
| 129 |
$allowed_extensions = t('<br />Allowed file types: <strong>@types</strong>', array('@types' => $field['widget']['file_extensions']));
|
| 130 |
// format for accept attribute below, the attribute gets used
|
| 131 |
// by video_upload.js as a regex pattern, thus the '|' separator
|
| 132 |
$allowed_types = implode('|', array_filter(explode(' ', $field['widget']['file_extensions'])));
|
| 133 |
}
|
| 134 |
|
| 135 |
// overwrite warning
|
| 136 |
if (!$field['multiple'] && $items[0]['id']) {
|
| 137 |
$overwrite_warning = t('<strong>WARNING</strong>: Uploading another video will overwrite the above video (and be rejected if it is a duplicate).');
|
| 138 |
|
| 139 |
// @todo the delete widget must work first
|
| 140 |
// $generate_upload_form = FALSE;
|
| 141 |
}
|
| 142 |
|
| 143 |
|
| 144 |
// If the browser upload method is being used, we construct an upload form
|
| 145 |
// to YouTube
|
| 146 |
if ($generate_upload_form && $field['widget']['use_browser_upload_method']) {
|
| 147 |
|
| 148 |
drupal_add_js(drupal_get_path('module', 'video_upload') . '/video_upload_browser.js');
|
| 149 |
|
| 150 |
// authenticate to youtube
|
| 151 |
if ($http_client = _video_upload_authenticate_youtube()) {
|
| 152 |
|
| 153 |
// The following is taken and modified from
|
| 154 |
// http://code.google.com/apis/youtube/developers_guide_php.html
|
| 155 |
// @todo perhaps store in SESSION...perhaps not
|
| 156 |
$yt = _video_upload_youtube($http_client);
|
| 157 |
|
| 158 |
// create a Zend_Gdata_YouTube_VideoEntry
|
| 159 |
$video = _video_upload_video_entry();
|
| 160 |
|
| 161 |
// set up media group
|
| 162 |
_video_upload_construct_media_group($yt, $video, $node, $field);
|
| 163 |
|
| 164 |
$token_array = _video_upload_get_token_array($yt, $video);
|
| 165 |
|
| 166 |
// Set action to YouTube @todo this will need to be done in order for
|
| 167 |
// this to work w/o js enabled and then have the js re-construct the
|
| 168 |
// action back to this site
|
| 169 |
// $form['#action'] = url($token_array['url'], 'nexturl=' . _video_upload_get_next_url());
|
| 170 |
|
| 171 |
if ($token_array) {
|
| 172 |
// Set youtube as the upload destination, which will then redirect to
|
| 173 |
// the js callback url. The dynamic behavior is triggered by setting
|
| 174 |
// the class to "upload"
|
| 175 |
|
| 176 |
// token
|
| 177 |
$form[$field_name]['new']['token'] = array(
|
| 178 |
'#type' => 'hidden',
|
| 179 |
// YouTube requires the name to be 'token'
|
| 180 |
'#name' => 'token',
|
| 181 |
'#value' => $token_array['token'],
|
| 182 |
);
|
| 183 |
// this gets changed, as YouTube redirects to the video_upload ajax
|
| 184 |
// handler
|
| 185 |
$ajax_form_submit_url = url($token_array['url'], array('query' => 'nexturl=' . urlencode(_video_upload_get_next_url($field_name, $node))));
|
| 186 |
}
|
| 187 |
else {
|
| 188 |
// can't generate upload form w/o a token
|
| 189 |
$generate_upload_form = false;
|
| 190 |
$auth_error = true;
|
| 191 |
watchdog('video_upload', t('Authentication to YouTube succeeded, but failed to generate a token'), WATCHDOG_ERROR);
|
| 192 |
}
|
| 193 |
}
|
| 194 |
else {
|
| 195 |
// auth error will provide a warning
|
| 196 |
$auth_error = true;
|
| 197 |
$generate_upload_form = false;
|
| 198 |
}
|
| 199 |
|
| 200 |
if ($auth_error) {
|
| 201 |
$form[$field_name]['holder'] = array(
|
| 202 |
'#type' => 'markup',
|
| 203 |
'#value' => t('Video Uploads currently unavailable. Please try back soon'),
|
| 204 |
);
|
| 205 |
}
|
| 206 |
}
|
| 207 |
elseif ($generate_upload_form) {
|
| 208 |
// The Direct Upload method transfers the file here first
|
| 209 |
|
| 210 |
// Submission to YouTube is handled later, send file here
|
| 211 |
$ajax_form_submit_url = _video_upload_get_next_url($field_name, $node);
|
| 212 |
|
| 213 |
foreach ($items as $delta => $item) {
|
| 214 |
if ($item['fid']) {
|
| 215 |
$form[$field_name][$delta]['fid'] = array(
|
| 216 |
'#type' => 'hidden',
|
| 217 |
'#value' => $item['fid'],
|
| 218 |
);
|
| 219 |
}
|
| 220 |
}
|
| 221 |
}
|
| 222 |
|
| 223 |
if ($generate_upload_form) {
|
| 224 |
// Note, youtube requires that this field be called 'file', but the Drupal
|
| 225 |
// Form API doesn't work with renaming of this field. Since Browser
|
| 226 |
// uploads only work with js enabled, this field is renamed properly
|
| 227 |
// client side when the user attempts a YouTube upload.
|
| 228 |
$form[$field_name]['new'][$field_name . '_file'] = array(
|
| 229 |
'#type' => 'file',
|
| 230 |
'#description' => $overwrite_warning . $field['widget']['description'] . $allowed_extensions,
|
| 231 |
'#title' => t('File'),
|
| 232 |
'#tree' => FALSE,
|
| 233 |
'#attributes' => array(
|
| 234 |
'class' => 'video-upload-file video-upload video-upload-' . form_clean_id($field_name),
|
| 235 |
// browsers tend not to enforce this, but some javascript can
|
| 236 |
'accept' => $allowed_types,
|
| 237 |
'id' => 'blaz',
|
| 238 |
),
|
| 239 |
);
|
| 240 |
}
|
| 241 |
|
| 242 |
if ($generate_upload_form) {
|
| 243 |
// The dynamic behavior of the form is triggered by the 'upload' and
|
| 244 |
// 'video-upload-url' classes.
|
| 245 |
$form[$field_name .'-attach-url'] = array(
|
| 246 |
'#type' => 'hidden',
|
| 247 |
'#value' => $ajax_form_submit_url,
|
| 248 |
'#attributes' => array('class' => 'upload video-upload-url')
|
| 249 |
);
|
| 250 |
|
| 251 |
// button
|
| 252 |
$form[$field_name]['new']['submit'] = array(
|
| 253 |
'#type' => 'button',
|
| 254 |
'#value' => t('Upload Video File'),
|
| 255 |
'#id' => form_clean_id($field_name . '-attach-button'),
|
| 256 |
'#attributes' => array(
|
| 257 |
'class' => 'video-upload video-upload-submit',
|
| 258 |
),
|
| 259 |
);
|
| 260 |
}
|
| 261 |
|
| 262 |
return $form;
|
| 263 |
}
|
| 264 |
|
| 265 |
/**
|
| 266 |
* Validate a set of items
|
| 267 |
*/
|
| 268 |
function _video_upload_browser_widget_validate(&$node, $field, &$items) {
|
| 269 |
if ($field['required']) {
|
| 270 |
if ($field['widget']['use_browser_upload_method']) {
|
| 271 |
// if using the browser upload method, we need a YouTube ID
|
| 272 |
if (!$items[0]['id']) {
|
| 273 |
$error = true;
|
| 274 |
}
|
| 275 |
}
|
| 276 |
else {
|
| 277 |
// for the direct upload method, we need a file
|
| 278 |
if (!$items[0]['fid']) {
|
| 279 |
$error = true;
|
| 280 |
}
|
| 281 |
}
|
| 282 |
if ($error) {
|
| 283 |
form_set_error($field['field_name'], t('@field is required. Please select a video and hit the <em>Upload Video File</em> button.', array('@field' => $field['widget']['label'])));
|
| 284 |
}
|
| 285 |
}
|
| 286 |
}
|
| 287 |
|
| 288 |
/**
|
| 289 |
* Prepare data for form
|
| 290 |
*/
|
| 291 |
function _video_upload_browser_widget_prepare_form_values(&$node, $field, &$items) {
|
| 292 |
if (!count($_POST)) {
|
| 293 |
// start fresh if no post datas
|
| 294 |
video_upload_clear_session_data();
|
| 295 |
}
|
| 296 |
|
| 297 |
$field_name = $field['field_name'];
|
| 298 |
|
| 299 |
// @fixme - This need to be reviewed. The reason this is required
|
| 300 |
// is that the Drupal Form API doesn't allow js to change
|
| 301 |
// the values of hidden variables, so it must be done here.
|
| 302 |
// Supposedly it is dangerous to do so, thus the need for a
|
| 303 |
// review.
|
| 304 |
// @fixme - This won't work once multiple items are in play
|
| 305 |
if ($_POST[$field_name][0]['id']) {
|
| 306 |
$node->{$field_name}[0]['id'] = $_POST[$field_name][0]['id'];
|
| 307 |
$items[0]['id'] = $node->{$field_name}[0]['id'];
|
| 308 |
}
|
| 309 |
|
| 310 |
// Check for file uploaded to local server
|
| 311 |
if ($file = file_check_upload($field_name . '_file')) {
|
| 312 |
$valid = _video_upload_widget_validate_video_file($node, $field, $items, $file);
|
| 313 |
if ($valid) {
|
| 314 |
_video_upload_upload_video_file($node, $field, $items, $file);
|
| 315 |
}
|
| 316 |
else {
|
| 317 |
// @todo delete the invalid image
|
| 318 |
}
|
| 319 |
}
|
| 320 |
}
|
| 321 |
|
| 322 |
/**
|
| 323 |
* Convert back to native storage
|
| 324 |
*/
|
| 325 |
function _video_upload_browser_widget_process_form_values(&$node, $field, &$items) {
|
| 326 |
foreach ($items as $delta => $item) {
|
| 327 |
|
| 328 |
if ($item['id'] && !$item['status']) {
|
| 329 |
// @todo make this work
|
| 330 |
if (false && $item['delete'] && $item['id']) {
|
| 331 |
// delete video from youtube
|
| 332 |
_video_upload_delete(array($item), $field);
|
| 333 |
$item['id'] = '';
|
| 334 |
$item['delete'] = 0;
|
| 335 |
}
|
| 336 |
if ($status = video_upload_youtube_validate($item['id'], $node)) {
|
| 337 |
$item['status'] = $status->status;
|
| 338 |
$item['status_ts'] = time();
|
| 339 |
}
|
| 340 |
$items[$delta] = $item;
|
| 341 |
}
|
| 342 |
}
|
| 343 |
}
|
| 344 |
|
| 345 |
/**
|
| 346 |
* Get the url that youtube will redirect to
|
| 347 |
* @param string $field_name
|
| 348 |
* @param object $node
|
| 349 |
* @return string
|
| 350 |
*/
|
| 351 |
function _video_upload_get_next_url($field_name, $node) {
|
| 352 |
global $base_url;
|
| 353 |
// the field name is passed as part of the path because the YouTube API
|
| 354 |
// doesn't handle encoded urls, and submitting a ? in the url doesn't work
|
| 355 |
// properly
|
| 356 |
return url('video-upload/js/' . $field_name . '/' . $node->type, array('absolute' => TRUE));
|
| 357 |
}
|