| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @file 4 * Common widget functions 5 */ 6 7 /** 8 * Process elements loads on settings 9 * @param array $element 10 */ 11 function video_widget_element_settings(&$element) { 12 $file = $element['#value']; 13 $delta = $element['#delta']; 14 $field = content_fields($element['#field_name'], $element['#type_name']); 15 // Check if using the default width and replace tokens. 16 $default_dimensions = user_access('override player dimensions'); 17 $description = t('Set your video dimensions. This will create your player with these dimensions.'); 18 //setup our default dimensions. 19 $dimensions = $field['widget']['default_dimensions']; 20 $player_dimensions = $field['widget']['default_player_dimensions']; 21 // Lets figure out our dimensions for our video and add astericks next to our options. 22 $options = video_explode("\n", variable_get("video_metadata_dimensions", video_default_dimensions())); 23 if ($field['widget']['autoconversion'] && isset($element['preview']) && $file['fid'] != 0 && $default_dimensions) { 24 $video_info = _video_dimensions_options($options, $file['filepath']); 25 $description = t('Set your video dimensions. This will create your player and transcode your video with these dimensions.'); 26 if (!empty($video_info)) { 27 $description .= t('Your video size is !size, if you choose a higher resolution, this could cause video distortion. You are shown dimensions that match your aspect ratio, if you choose dimensions that do not match your ratio, we will pad your video by adding black bars on either the top or bottom while maintaining your videos original aspect ratio.', array('!size' => $video_info['width'] .'x'. $video_info['height'])); 28 } 29 30 //setup our default display of dimensions. 31 //lets go through our options looking for a matching resolution 32 foreach ($options as $key => $value) { 33 if (stristr($value, t('(Matches Resolution)')) == TRUE) { 34 $dimensions = $key; 35 break; 36 } 37 } 38 } 39 // Override our dimensions to the user selected. 40 if (isset($file['data']['dimensions']) && !empty($file['data']['dimensions'])) { 41 $dimensions = $file['data']['dimensions']; 42 } 43 44 // Override our player dimensions to the user selected. 45 if (isset($file['data']['player_dimensions']) && !empty($file['data']['player_dimensions'])) { 46 $player_dimensions = $file['data']['player_dimensions']; 47 } 48 49 $element['data']['dimensions'] = array( 50 '#type' => 'select', 51 '#title' => t('Dimensions for Video Transcoding'), 52 '#default_value' => $dimensions, 53 '#description' => $description, 54 '#options' => $options, 55 ); 56 $element['data']['player_dimensions'] = array( 57 '#type' => 'select', 58 '#title' => t('Dimensions for Video Player'), 59 '#default_value' => $player_dimensions, 60 '#description' => t('WxH of your video player.'), 61 '#options' => $options, 62 ); 63 // If users cannot change the default dimensions, lets change this to a value. 64 if (!$default_dimensions) { 65 $element['data']['dimensions']['#type'] = 'value'; 66 $element['data']['dimensions']['#value'] = $dimensions; 67 $element['data']['player_dimensions']['#type'] = 'value'; 68 $element['data']['player_dimensions']['#value'] = $player_dimensions; 69 } 70 71 // only in preview mode and then create thumbnails 72 if ($field['widget']['autoconversion']) { 73 $bypass = isset($file['data']['bypass_autoconversion']) ? $file['data']['bypass_autoconversion'] : variable_get('video_bypass_conversion', FALSE); 74 if (user_access('bypass conversion video')) { 75 $element['data']['bypass_autoconversion'] = array( 76 '#type' => 'checkbox', 77 '#title' => t('Bypass auto conversion'), 78 '#default_value' => $bypass, 79 '#description' => t('This will bypass your auto conversion of videos.'), 80 '#attributes' => array('class' => 'video-bypass-auto-conversion'), 81 ); 82 83 if ($file['status'] == FILE_STATUS_TEMPORARY) { 84 // Checkbox #default_value does not work when the checkbox is inserted using AHAH 85 $element['data']['bypass_autoconversion']['#value'] = $bypass; 86 } 87 } 88 else { 89 $element['data']['bypass_autoconversion'] = array( 90 '#type' => 'value', 91 '#value' => $bypass, 92 ); 93 } 94 95 $convert = isset($file['data']['convert_video_on_save']) ? $file['data']['convert_video_on_save'] : variable_get('video_convert_on_save', FALSE); 96 if (user_access('convert on submission')) { 97 $element['data']['convert_video_on_save'] = array( 98 '#type' => 'checkbox', 99 '#title' => t('Convert video on save'), 100 '#default_value' => $convert, 101 '#description' => t('This will convert your video to flv format when you save, instead of scheduling it for cron.'), 102 '#attributes' => array('class' => 'video-convert-video-on-save'), 103 ); 104 105 if ($file['status'] == FILE_STATUS_TEMPORARY) { 106 // Checkbox #default_value does not work when the checkbox is inserted using AHAH 107 $element['data']['convert_video_on_save']['#value'] = $convert; 108 } 109 } else { 110 $element['data']['convert_video_on_save'] = array( 111 '#type' => 'value', 112 '#value' => $convert, 113 ); 114 } 115 116 $default_thumb = isset($file['data']['use_default_video_thumb']) ? $file['data']['use_default_video_thumb'] : variable_get('video_use_default_thumb', FALSE); 117 if (user_access('use default thumb')) { 118 $element['data']['use_default_video_thumb'] = array( 119 '#type' => 'checkbox', 120 '#title' => t('Use the default thumbnail for this video?'), 121 '#default_value' => $default_thumb, 122 '#description' => t('This will set a flag for this video to use the default video thumbnail when outputed..'), 123 '#attributes' => array('class' => 'video-use-default-video-thumb'), 124 ); 125 126 if ($file['status'] == FILE_STATUS_TEMPORARY) { 127 // Checkbox #default_value does not work when the checkbox is inserted using AHAH 128 $element['data']['use_default_video_thumb']['#value'] = $default_thumb; 129 } 130 } else { 131 $element['data']['use_default_video_thumb'] = array( 132 '#type' => 'value', 133 '#value' => $default_thumb, 134 ); 135 } 136 } 137 } 138 139 /** 140 * Function updates our options list to show matching aspect ratios and if we have a matching resolution. 141 * 142 * We will update the options array by reference and return the aspect ratio of the file. 143 */ 144 function _video_dimensions_options(&$options, $video) { 145 $aspect_ratio = _video_aspect_ratio($video); 146 147 if (empty($aspect_ratio)) { 148 return $aspect_ratio; 149 } 150 151 //loop through our options and find matching ratio's and also the exact width/height 152 foreach ($options as $key => $value) { 153 $wxh = explode('x', $value); 154 //lets check our width and height first 155 if ($aspect_ratio['width'] == $wxh[0] && $aspect_ratio['height'] == $wxh[1]) { 156 $options[$key] = $value . ' ' . t('(Matches Resolution)'); 157 } else { 158 //now lets check our ratio's 159 $ratio = number_format($wxh[0] / $wxh[1], 4); 160 if ($ratio == $aspect_ratio['ratio']) { 161 $options[$key] = $value . ' ' . t('(Matches Ratio)'); 162 } 163 } 164 } 165 166 return $aspect_ratio; 167 } 168 169 /** 170 * Video_widget_process for API handlers for any video types. 171 * @param array $element 172 * @param array $form_state 173 */ 174 function video_widget_process(&$element, &$form_state) { 175 $item = $element['#value']; 176 $field = content_fields($element['#field_name'], $element['#type_name']); 177 switch ($form_state['clicked_button']['#submit'][0]) { 178 case 'node_form_submit': 179 // Auto convert our video file 180 if ($field['widget']['autoconversion']) { 181 video_convert_process($element); 182 //lets set our node status to unpublished if our video is not converted. 183 if (isset($element['#unpublish']) && $element['#unpublish']) { 184 //unpublish the node 185 $form_state['values']['status'] = 0; 186 } 187 } 188 189 // Save manually uploaded thumbs (if they exist) and add them to element 190 $filename = $field['field_name'] . '_' . $element['#delta'] . '_thumbs'; 191 if (isset($_FILES['files']) && is_array($_FILES['files']['name']) && !empty($_FILES['files']['name'][$filename])) { 192 video_upload_manual_thumb($element); 193 } 194 195 // Call hook_video_submit API 196 video_module_invoke('insert', $element, $form_state); 197 // 198 //queue up the file id to update the node id in the video rendering / cdn tables. 199 $form_state['values']['video_id'][] = $item['fid']; 200 break; 201 case 'node_form_build_preview': 202 // Call hook_video_preview API 203 video_module_invoke('preview', $element, $form_state); 204 break; 205 case 'node_form_delete_submit': 206 //moved to hook_file_delete in video module. 207 break; 208 } 209 } 210 211 /** 212 * Adds a video to the video rendering table. 213 * 214 * If auto converting, it will convert your video to flv right now. We are passing the element by reference 215 * just in case we ever want to add more to the element during this process. 216 * 217 * @param array $element Form element to get the video file from. 218 */ 219 function video_convert_process(&$element) { 220 $file = $element['#value']; 221 222 // If the dimensions #value is empty, it is probably because the user 223 // clicked the Save button directly and did not choose dimensions. 224 // Take the default dimensions. 225 if (empty($file['data']['dimensions']) && isset($element['data']['dimensions'])) { 226 $file['data']['dimensions'] = $element['data']['dimensions']['#default_value']; 227 } 228 229 if (!empty($file['fid']) && empty($file['data']['bypass_autoconversion'])) { 230 $convert = false; 231 $fid = $file['fid']; 232 //setup our conversion class and check for the fid existence. 233 module_load_include('inc', 'video', '/includes/conversion'); 234 $video_conversion = new video_conversion; 235 236 // Lets verify that we haven't added this video already. Multiple validation fails will cause this to be ran more than once 237 if (!$video = $video_conversion->load_job($fid)) { 238 // Video has not been added to the queue yet so lets add it. 239 $video = array('fid' => $fid, 'dimensions' => $file['data']['dimensions']); 240 if (!($video_conversion->create_job($video))) 241 drupal_set_message(t('Something went wrong with your video job creation. Please check your recent log entries for further debugging.'), 'error'); 242 $convert = true; 243 //lets queue our node status to unpublished. 244 $element['#unpublish'] = true; 245 } elseif ($video->video_status != VIDEO_RENDERING_COMPLETE) { 246 //lets queue our node status to unpublished. 247 $element['#unpublish'] = true; 248 } 249 250 // Our video should be in the database pending, lets see if we need to convert it now. 251 // Check if we are going from unselected to selected or if this is a new video and we have checked the checkbox 252 $convert_video_on_save = false; 253 $element_data_convert_on_save = ''; 254 $file_date_convet_on_save = ''; 255 $convert_on_save = variable_get('video_convert_on_save', FALSE); 256 if (isset($element['data']['convert_video_on_save']['#value'])) 257 $element_data_convert_on_save = $element['data']['convert_video_on_save']['#value']; 258 if (isset($file['data']['convert_video_on_save'])) 259 $file_date_convet_on_save = $file['data']['convert_video_on_save']; 260 $convert_video_on_save = $element_data_convert_on_save || $file_date_convet_on_save; 261 if (((!isset($element['#default_value']['data']['convert_video_on_save']) || !$element['#default_value']['data']['convert_video_on_save']) 262 && $convert_video_on_save) || ($convert && $convert_video_on_save) || $convert_on_save) { 263 $return = $video_conversion->process($fid); 264 if ($return === FALSE) { 265 drupal_set_message(t('Something went wrong with your video conversion. Please check your recent log entries for further debugging.'), 'error'); 266 } elseif ($return === TRUE) { 267 //we are always unpublished until we are converted. 268 unset($element['#unpublish']); 269 drupal_set_message(t('Successfully converted your video.')); 270 } 271 } elseif ($convert) { 272 drupal_set_message(t('Video submission queued for processing. Please wait: our servers are preparing your video for display.')); 273 } 274 } 275 } 276 277 /** 278 * Handle saving of manual thumbs 279 */ 280 function video_upload_manual_thumb(&$element) { 281 // TODO: test this 282 $destination = video_thumb_path($element['#value']); 283 if (!is_dir($destination)) { 284 form_set_error('video_thumb_upload', t('The thumbnail image could not be uploaded. The destination %destination does not exist or is not writable by the server.', array('%destination' => $destination))); 285 return; 286 } 287 288 $validators = array('file_validate_is_image' => array()); 289 if (!$file = file_save_upload($element['#field_name'] .'_'. $element['#delta'] .'_thumbs', $validators, $destination)) { 290 // No upload to save we hope... or file_save_upload() reported an error on its own. 291 return; 292 } 293 294 // Remove old image (if any) & clean up database. 295 $old_thumb = $element['data']['video_thumb']['#value']; 296 if (!empty($old_thumb)) { 297 if (file_delete($old_thumb)) { 298 db_query('DELETE FROM {files} WHERE filepath=\'%s\'', $old_thumb); 299 } 300 } 301 // Make the file permanent and store it in the form. 302 file_set_status($file, FILE_STATUS_PERMANENT); 303 $element['data']['video_thumb']['#value'] = $file->filepath; 304 } 305 306 function video_default_widget_settings($widget) { 307 $form = array(); 308 // Default video settings. 309 $form['plugins'] = array( 310 '#type' => 'fieldset', 311 '#title' => t('Video Advanced Settings'), 312 '#collapsible' => TRUE, 313 '#collapsed' => FALSE, 314 '#weight' => 10 315 ); 316 $form['plugins']['default_dimensions'] = array( 317 '#type' => 'select', 318 '#title' => t('Default Video Resolution Dimensions'), 319 '#default_value' => !empty($widget['default_dimensions']) ? $widget['default_dimensions'] : '', 320 '#options' => video_explode("\n", variable_get("video_metadata_dimensions", video_default_dimensions())), 321 '#description' => t('Default transcoding resolution WIDTHxHEIGHT, in px, that FFMPEG will use to transcode your video files.') 322 ); 323 $form['plugins']['default_player_dimensions'] = array( 324 '#type' => 'select', 325 '#title' => t('Default Video Player Dimensions'), 326 '#default_value' => !empty($widget['default_player_dimensions']) ? $widget['default_player_dimensions'] : '', 327 '#options' => video_explode("\n", variable_get("video_metadata_dimensions", video_default_dimensions())), 328 '#description' => t('Default player WIDTHxHEIGHT in px. This is your actual player dimensions that your video will be playing in.') 329 ); 330 $form['plugins']['autoconversion'] = array( 331 '#type' => 'checkbox', 332 '#title' => t('Enable video conversion.'), 333 '#description' => t('Use ffmpeg(Default) to automatically convert videos to web compatible types eg. FLV, Please make sure to configure your transcoder settings.'), 334 '#default_value' => $widget['autoconversion'], 335 ); 336 337 // Default thumbnail settings. 338 $form['default'] = array( 339 '#type' => 'fieldset', 340 '#title' => t('Video Thumbnail Settings'), 341 '#element_validate' => array('video_default_widget_settings_validate'), 342 '#collapsible' => TRUE, 343 '#collapsed' => FALSE, 344 '#weight' => 11 345 ); 346 347 $thumb_options = array( 348 'auto' => 'Automatically generate thumbnails', 349 'auto_fallback' => 'Automatically generate thumbnails, with fallback to manual upload', 350 'manual_upload' => 'Manually upload a thumbnail', 351 'no' => 'Don\'t create thumbnail', 352 ); 353 354 $form['default']['autothumbnail'] = array( 355 '#type' => 'radios', 356 '#title' => t('Thumbnail Generation'), 357 '#options' => $thumb_options, 358 '#description' => t('To use ffmpeg(Default) to create thumbnails, Please make sure to configure your transcoder settings before using ffmpeg to create thumbnails.'), 359 '#default_value' => isset($widget['autothumbnail']) ? $widget['autothumbnail'] : 'no', 360 ); 361 362 // @TODO: Move this to the actual upload/attach when creating a node to allow the user to upload their own thumbnail for each video. 363 // Present a video image of the current default image. 364 if (!empty($widget['default_video_thumb'])) { 365 $form['default']['default_video_thumbnail'] = array( 366 '#type' => 'markup', 367 '#value' => theme('video_image', $widget['default_video_thumb'], '', '', array('width' => '150'), FALSE), 368 '#prefix' => '<div class="video_thumbnail">', 369 '#suffix' => '</div>' 370 ); 371 $form['default']['delete_default_video_thumbnail'] = array( 372 '#type' => 'checkbox', 373 '#title' => t('Delete the default thumbnail'), 374 ); 375 } 376 $form['default']['default_video_thumb_upload'] = array( 377 '#type' => 'file', 378 '#title' => empty($widget['default_video_thumb']) ? t('Upload default video thumbnail') : t('Replace default video thumbnail with'), 379 '#description' => t('Choose a image that will be used as video thumbnail when you don\'t have video thumbnails for videos.'), 380 ); 381 // We set this value on 'validate' so we can get CCK to add it 382 // as a standard field setting. 383 $form['default_video_thumb'] = array( 384 '#type' => 'value', 385 '#value' => isset($widget['default_video_thumb']) ? $widget['default_video_thumb'] : NULL, 386 ); 387 return $form; 388 } 389 390 /** 391 * Element specific validation for video default value. 392 * 393 */ 394 function video_default_widget_settings_validate($element, &$form_state) { 395 // Verify the destination exists 396 $video_thumb_path = variable_get('video_thumb_path', 'video_thumbs'); 397 $destination = file_directory_path() . '/' . $video_thumb_path; 398 if (!field_file_check_directory($destination, FILE_CREATE_DIRECTORY)) { 399 form_set_error('default_video_thumb', 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)))); 400 return; 401 } 402 403 $oldthumb = $form_state['values']['default_video_thumb']; 404 405 // We save the upload here because we can't know the correct path until the file is saved. 406 $newthumb = file_save_upload('default_video_thumb_upload', array('file_validate_is_image' => array()), $destination); 407 408 // Delete the current file if there is a new one or the delete_default_video_thumbnail checkbox is checked 409 if (!empty($oldthumb['fid']) && ($newthumb || !empty($form_state['values']['delete_default_video_thumbnail']))) { 410 if (file_delete(file_create_path($oldthumb['filepath']))) { 411 db_query('DELETE FROM {files} WHERE fid=%d', $oldthumb['fid']); 412 } 413 $form_state['values']['default_video_thumb'] = array(); 414 } 415 416 if ($newthumb) { 417 // Make the file permanent and store it in the form. 418 file_set_status($newthumb, FILE_STATUS_PERMANENT); 419 $newthumb->timestamp = time(); 420 $form_state['values']['default_video_thumb'] = (array) $newthumb; 421 } 422 } 423 424 function video_widget_settings_file_path_validate($element, &$form_state) { 425 //lets prepend our video folder to the path settings. first truncate videos/ off the end if it exists. 426 // #848804 427 if (!module_exists('filefield_paths')) { 428 $form_state['values']['file_path'] = 'videos/'. $form_state['values']['file_path']; 429 } 430 } 431 432 /** 433 * Compares passed extensions with normal video web extensions. 434 * 435 * @param string $ext 436 * @return bool 437 */ 438 function video_web_extensions($ext) { 439 $extensions = array_filter(explode(' ', $ext)); 440 $web_extensions = array( 441 'mov', 'mp4', '3gp', '3g2', 'mpg', 'mpeg', // quicktime 442 'divx', 'mkv', //divx 443 'rm', // realplayer 444 'flv', 'f4v', //flash player 445 'swf', // swf player 446 'dir', 'dcr', // dcr player 447 'asf', 'wmv', 'avi', 'mpg', 'mpeg', // windows media 448 'ogg', 'ogv', 'webm' // ogg/ogv theora 449 ); 450 451 return count(array_diff($extensions, $web_extensions)) == 0; 452 } 453 454 /** 455 * Utility function that will add a preview of thumbnails for you to select when uploading videos. 456 */ 457 function video_thumb_process(&$element) { 458 // Developed for ffmpeg support 459 $file = $element['#value']; 460 $delta = $file['fid']; 461 $field = content_fields($element['#field_name'], $element['#type_name']); 462 $gen_fail = FALSE; 463 464 if (isset($element['preview']) && $file['fid'] != 0) { 465 if (in_array($field['widget']['autothumbnail'], array('auto', 'auto_fallback'))) { 466 module_load_include('inc', 'video', '/includes/transcoder'); 467 $transcoder = new video_transcoder(); 468 $thumbs = $transcoder->generate_thumbnails($file); 469 470 if (!empty($thumbs)) { 471 drupal_add_js(drupal_get_path('module', 'video') .'/js/video.admin.js'); 472 473 $thumbss = array(); 474 foreach ($thumbs as $fid => $img) { 475 $thumbss[$img->filepath] = theme('video_thumbnails', $img, '', '', array('width' => '50'), FALSE); 476 } 477 478 if (!empty($file['data']['video_thumb']) && isset($thumbss[$file['data']['video_thumb']])) { 479 $currentthumb = $file['data']['video_thumb']; 480 } 481 else { 482 $currentthumb = array_rand($thumbss); 483 } 484 485 $element['data']['video_thumb'] = array( 486 '#type' => 'radios', 487 '#title' => t('Video Thumbnail'), 488 '#options' => $thumbss, 489 '#default_value' => $currentthumb, 490 '#weight' => 10, 491 '#attributes' => array('class' => 'video-thumbnails', 'onchange' => 'videoftp_thumbnail_change()', 'rel' => 'video_large_thumbnail-' . $delta), 492 ); 493 } else { 494 $gen_fail = TRUE; 495 } 496 } 497 498 if ((!empty($gen_fail) && $field['widget']['autothumbnail'] == 'auto_fallback') || 499 $field['widget']['autothumbnail'] == 'manual_upload') { 500 501 $element['data']['video_thumb_file'] = array( 502 '#name' => 'files[' . $element['#field_name'] . '_' . $element['#delta'] . '_thumbs]', 503 '#type' => 'file', 504 '#size' => '40', 505 '#title' => !empty($file['data']['video_thumb']) ? t('Replace the video thumbnail') : t('Upload a video thumbnail'), 506 '#description' => t('This thumbnail will be uploaded when the node is saved.'), 507 ); 508 509 $element['data']['video_thumb'] = array( 510 '#type' => 'value', 511 '#value' => isset($file['data']['video_thumb']) ? $file['data']['video_thumb'] : false, 512 ); 513 } 514 515 // Setup our large thumbnail that is on the left. 516 // @todo Add smaller video preview instead of thumbnail? 517 if (!empty($file['data']['video_thumb'])) { 518 $large_thumb = array('filepath' => $file['data']['video_thumb']); 519 } 520 elseif (!empty($field['widget']['default_video_thumb'])) { 521 $large_thumb = $field['widget']['default_video_thumb']; 522 } 523 elseif (!empty($currentthumb)) { 524 $large_thumb = array('filepath' => $currentthumb); 525 } 526 527 if (!empty($large_thumb)) { 528 // @todo Integrate the thumbnails with imagecache. 529 $element['preview']['#suffix'] = '<div class="video_large_thumbnail-'. $delta .'">'. theme('video_thumbnails', $large_thumb, '', '', array('width' => 150), FALSE) .'</div>'; 530 } 531 } 532 } 533 534 /** 535 * #options helper function to set our key=value for the form api. 536 */ 537 function video_explode($delimeter, $dimensions) { 538 $options = array(); 539 $values = explode($delimeter, $dimensions); 540 foreach ($values as $value) { 541 //lets check we have a value and its in the right format 542 if (!empty($value) && video_format_right($value)) { 543 $options[trim($value)] = trim($value); 544 } 545 } 546 return $options; 547 } 548 549 function video_format_right($value) { 550 $format = explode('x', $value, 2); 551 if (!isset($format[0]) || !is_numeric(trim($format[0]))) { 552 return false; 553 } 554 if (!isset($format[1]) || !is_numeric(trim($format[1]))) { 555 return false; 556 } 557 return true; 558 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |