From b6e86040dd3faa3a70ec16e77220d852bdb09a04 Mon Sep 17 00:00:00 2001 From: Heshan Wanigasooriya Date: Sun, 5 Dec 2010 12:56:20 +0000 Subject: Adding latest files. --- types/.cvsignore | 1 + types/uploadfield/uploadfield.info | 9 + types/uploadfield/uploadfield.install | 25 +++ types/uploadfield/uploadfield.module | 146 ++++++++++++++ types/uploadfield/uploadfield.theme.inc | 18 ++ types/uploadfield/uploadfield_widget.inc | 129 +++++++++++++ types/videoftp/videoftp.info | 9 + types/videoftp/videoftp.install | 25 +++ types/videoftp/videoftp.module | 207 ++++++++++++++++++++ types/videoftp/videoftp.theme.inc | 57 ++++++ types/videoftp/videoftp_widget.inc | 313 +++++++++++++++++++++++++++++++ 11 files changed, 939 insertions(+) create mode 100644 types/.cvsignore create mode 100644 types/uploadfield/uploadfield.info create mode 100644 types/uploadfield/uploadfield.install create mode 100644 types/uploadfield/uploadfield.module create mode 100644 types/uploadfield/uploadfield.theme.inc create mode 100644 types/uploadfield/uploadfield_widget.inc create mode 100644 types/videoftp/videoftp.info create mode 100644 types/videoftp/videoftp.install create mode 100644 types/videoftp/videoftp.module create mode 100644 types/videoftp/videoftp.theme.inc create mode 100644 types/videoftp/videoftp_widget.inc (limited to 'types') diff --git a/types/.cvsignore b/types/.cvsignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/types/.cvsignore @@ -0,0 +1 @@ +.DS_Store diff --git a/types/uploadfield/uploadfield.info b/types/uploadfield/uploadfield.info new file mode 100644 index 0000000..9468461 --- /dev/null +++ b/types/uploadfield/uploadfield.info @@ -0,0 +1,9 @@ +; $Id$ +name = Video Upload +description = Handle video upload for video module using filefield and CCK. +core = 6.x +version = 6.x-4.x-dev +dependencies[] = content +dependencies[] = filefield +dependencies[] = video +package = "Video" \ No newline at end of file diff --git a/types/uploadfield/uploadfield.install b/types/uploadfield/uploadfield.install new file mode 100644 index 0000000..a2e1b65 --- /dev/null +++ b/types/uploadfield/uploadfield.install @@ -0,0 +1,25 @@ + array('element' => NULL), + 'file' => 'uploadfield.theme.inc', + ); + $theme['uploadfield_widget_item'] = array( + 'arguments' => array('element' => NULL), + 'file' => 'uploadfield.theme.inc', + ); + return $theme; +} + +/** + * Implementation of CCK's hook_widget_info(). + */ +function uploadfield_widget_info() { + 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 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) { + // Return headers for default images. + 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_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 hook_widget(). + */ +function uploadfield_widget(&$form, &$form_state, $field, $items, $delta = NULL) { + $item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => '', 'video_thumb' => '')); + if (isset($items[$delta])) { + $item = array_merge($item, $items[$delta]); + } + return filefield_widget($form, $form_state, $field, $items, $delta); +} + +/** + * 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 "Video" 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); + } + } +} + +/** + * filefield source support + */ +function uploadfield_filefield_sources_widgets() { + return array('uploadfield_widget'); +} \ No newline at end of file diff --git a/types/uploadfield/uploadfield.theme.inc b/types/uploadfield/uploadfield.theme.inc new file mode 100644 index 0000000..5a81efd --- /dev/null +++ b/types/uploadfield/uploadfield.theme.inc @@ -0,0 +1,18 @@ +files/videos/" directory where files will be stored. Do not include preceding or trailing slashes.'); + array_unshift($form['path_settings']['file_path']['#element_validate'], 'video_widget_settings_file_path_validate'); + + //default settings + $default = video_default_widget_settings($widget); + $form = $form + $default; + return $form; +} + +/** + * Implementation of CCK's hook_widget_settings($op = 'validate'). + */ +function uploadfield_widget_settings_validate($widget) { + // Check that only web images are specified in the callback. + if(!video_web_extensions($widget['file_extensions'])) { + form_set_error('file_extensions', t('Only web-standard videos are supported through the videoftp widget. If needing to upload other types of files, change the widget to use a standard file upload.')); + } +} + +/** + * Implementation of CCK's hook_widget_settings($op = 'save'). + */ +function uploadfield_widget_settings_save($widget) { + $filefield_settings = module_invoke('filefield', 'widget_settings', 'save', $widget); + return array_merge($filefield_settings, array('default_video_thumb', 'default_dimensions', 'default_player_dimensions', 'autoconversion', 'autothumbnail')); +} + +/** + * Element #value_callback function. + */ +function uploadfield_widget_value($element, $edit = FALSE) { + //copied from filefield_widget_value with one change to reset our data array + if (!$edit) { + $file = field_file_load($element['#default_value']['fid']); + $item = $element['#default_value']; + } + else { + $item = array_merge($element['#default_value'], $edit); + $field = content_fields($element['#field_name'], $element['#type_name']); + + // Uploads take priority over value of fid text field. + if ($fid = filefield_save_upload($element)) { + $item['fid'] = $fid; + $item['data'] = array(); //reset our data array for thumbnails and values. + } + // Check for #filefield_value_callback values. + // Because FAPI does not allow multiple #value_callback values like it does + // for #element_validate and #process, this fills the missing functionality + // to allow FileField to be extended purely through FAPI. + elseif (isset($element['#filefield_value_callback'])) { + foreach ($element['#filefield_value_callback'] as $callback) { + $callback($element, $item); + } + } + + // Load file if the FID has changed so that it can be saved by CCK. + $file = field_file_load($item['fid']); + + // If the file entry doesn't exist, don't save anything. + if (empty($file)) { + $item = array(); + } + + // Checkboxes loose their value when empty. + // If the list field is present make sure its unchecked value is saved. + if (!empty($field['list_field']) && empty($edit['list'])) { + $item['list'] = 0; + } + } + // Merge file and item data so it is available to all widgets. + $item = array_merge($item, $file); + + return $item; +} + +/** + * Element #process callback function. + */ +function uploadfield_widget_process($element, $edit, &$form_state, $form) { + $item = $element['#value']; + $field_name = $element['#field_name']; + $delta = $element['#delta']; + $field = content_fields($element['#field_name'], $element['#type_name']); + $element['#theme'] = 'uploadfield_widget_item'; + + if (isset($element['preview']) && $element['#value']['fid'] != 0) { + $element['preview']['#value'] = theme('video_widget_preview', $element['#value']); + } + + // Title is not necessary for each individual field. + if ($field['multiple'] > 0) { + unset($element['#title']); + } + + // Create our thumbnails + if($field['widget']['autothumbnail']) { + video_thumb_process($element); + } + + // Add our extra fields if in preview mode + if(!empty($item['fid'])) { + video_widget_element_settings($element); + } + + // Lets use the clicked_button #submit[0] value here instead and see how that works out for now... + if($form_state['submitted'] == 1) { + video_widget_process($element, $form_state); + } + return $element; +} \ No newline at end of file diff --git a/types/videoftp/videoftp.info b/types/videoftp/videoftp.info new file mode 100644 index 0000000..73b9318 --- /dev/null +++ b/types/videoftp/videoftp.info @@ -0,0 +1,9 @@ +; $Id$ +name = Video FTP +description = Allows you to attach videos uploaded through FTP to nodes. +core = 6.x +version = 6.x-4.x-dev +dependencies[] = content +dependencies[] = filefield +dependencies[] = video +package = "Video" \ No newline at end of file diff --git a/types/videoftp/videoftp.install b/types/videoftp/videoftp.install new file mode 100644 index 0000000..8349abc --- /dev/null +++ b/types/videoftp/videoftp.install @@ -0,0 +1,25 @@ + 'videoftp_js', + 'page arguments' => array(2, 3, 4), + //'access callback' => 'videoftp_edit_access', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + $items['videoftp/progress'] = array( + 'page callback' => 'videoftp_progress', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + return $items; +} + +/** + * Implementation of hook_theme(). + */ +function videoftp_theme() { + $theme = array(); + $theme['videoftp_widget'] = array( + 'arguments' => array('element' => NULL), + 'file' => 'videoftp.theme.inc', + ); + $theme['videoftp_widget_item'] = array( + 'arguments' => array('element' => NULL), + 'file' => 'videoftp.theme.inc', + ); + $theme['videoftp_widget_file'] = array( + 'arguments' => array('element' => NULL), + 'file' => 'videoftp.theme.inc', + ); + return $theme; +} + +/** + * Implementation of CCK's hook_widget_info(). + */ +function videoftp_widget_info() { + return array( + 'videoftp_widget' => array( + 'label' => t('Video FTP'), + 'field types' => array('filefield'), + 'multiple values' => CONTENT_HANDLE_CORE, + 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM), + 'description' => t('Widget allows you to select video files uploaded through FTP to be attached to the node.'), + ), + ); +} + +/** + * Implementation of hook_elements(). + */ +function videoftp_elements() { + return array( + 'videoftp_widget' => array( + '#input' => TRUE, + '#columns' => array('fid', 'list', 'data', 'filepath'), + '#process' => array('videoftp_widget_process'), + '#value_callback' => 'videoftp_widget_value', + ), + ); +} + +/** + * Implementation of CCK's hook_widget_settings(). + */ +function videoftp_widget_settings($op, $widget) { + //load up our include file for the widget + module_load_include('inc', 'videoftp', 'videoftp_widget'); + switch ($op) { + case 'form': + return videoftp_widget_settings_form($widget); + case 'validate': + return videoftp_widget_settings_validate($widget); + case 'save': + return videoftp_widget_settings_save($widget); + } +} + +/** + * Implementation of hook_widget(). + */ +function videoftp_widget(&$form, &$form_state, $field, $items, $delta = NULL) { + $item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => '', 'video_thumb' => '')); + if (isset($items[$delta])) { + $item = array_merge($item, $items[$delta]); + } + $element = array( + '#title' => $field['widget']['label'], + '#type' => $field['widget']['type'], + '#default_value' => $item, + ); + return $element; +} + +/** + * Menu callback; Shared AHAH callback for ftp file attachment and deletions. + * + * This rebuilds the form element for a particular field item. As long as the + * form processing is properly encapsulated in the widget element the form + * should rebuild correctly using FAPI without the need for additional callbacks + * or processing. + */ +function videoftp_js($type_name, $field_name, $delta) { + module_load_include('inc', 'videoftp', 'videoftp_widget'); + $field = content_fields($field_name, $type_name); + + if (empty($field) || empty($_POST['form_build_id'])) { + // Invalid request. + drupal_set_message(t('An unrecoverable error occurred.'), 'error'); + print drupal_to_js(array('data' => theme('status_messages'))); + exit; + } + + // Build the new form. + $form_state = array('submitted' => FALSE); + $form_build_id = $_POST['form_build_id']; + $form = form_get_cache($form_build_id, $form_state); + + if (!$form) { + // Invalid form_build_id. + drupal_set_message(t('An unrecoverable error occurred. This form was missing from the server cache. Try reloading the page and submitting again.'), 'error'); + print drupal_to_js(array('data' => theme('status_messages'))); + exit; + } + + // Build the form. This calls the file field's #value_callback function and + // saves the uploaded file. Since this form is already marked as cached + // (the #cache property is TRUE), the cache is updated automatically and we + // don't need to call form_set_cache(). + $args = $form['#parameters']; + $form_id = array_shift($args); + $form['#post'] = $_POST; + $form = form_builder($form_id, $form, $form_state); + + // Update the cached form with the new element at the right place in the form. + if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) { + if (isset($form['#multigroups']) && isset($form['#multigroups'][$group_name][$field_name])) { + $form_element = $form[$group_name][$delta][$field_name]; + } + else { + $form_element = $form[$group_name][$field_name][$delta]; + } + } + else { + $form_element = $form[$field_name][$delta]; + } + + if (isset($form_element['_weight'])) { + unset($form_element['_weight']); + } + $output = drupal_render($form_element); + // AHAH is not being nice to us and doesn't know the "other" button (that is, + // either "Attach" or "Delete") yet. Which in turn causes it not to attach + // AHAH behaviours after replacing the element. So we need to tell it first. + + // Loop through the JS settings and find the settings needed for our buttons. + $javascript = drupal_add_js(NULL, NULL); + $videoftp_ahah_settings = array(); + if (isset($javascript['setting'])) { + foreach ($javascript['setting'] as $settings) { + if (isset($settings['ahah'])) { + foreach ($settings['ahah'] as $id => $ahah_settings) { + if (strpos($id, 'videoftp-attach') || strpos($id, 'videoftp-remove')) { + $videoftp_ahah_settings[$id] = $ahah_settings; + } + } + } + } + } + + // Add the AHAH settings needed for our new buttons. + if (!empty($videoftp_ahah_settings)) { + $output .= ''; + } + + $output = theme('status_messages') . $output; + + // For some reason, file uploads don't like drupal_json() with its manual + // setting of the text/javascript HTTP header. So use this one instead. + $GLOBALS['devel_shutdown'] = FALSE; + print drupal_to_js(array('status' => TRUE, 'data' => $output)); + exit; +} + +/** + * filefield source support + */ +function videoftp_filefield_sources_widgets() { + return array('videoftp_widget'); +} \ No newline at end of file diff --git a/types/videoftp/videoftp.theme.inc b/types/videoftp/videoftp.theme.inc new file mode 100644 index 0000000..42203cb --- /dev/null +++ b/types/videoftp/videoftp.theme.inc @@ -0,0 +1,57 @@ +'; + + if ($element['fid']['#value'] != 0) { + $output .= '
'; + $output .= drupal_render($element['preview']); + $output .= '
'; + } + + $output .= '
'; + $output .= drupal_render($element); + $output .= '
'; + $output .= ''; + + return $output; +} + +/** + * Custom theme function for VideoFTP upload elements. + * + * This function allows us to put the "Attach" button immediately after the + * select field by respecting the #field_suffix property. + */ +function theme_videoftp_widget_file($element) { + $output .= '
'; + if (isset($element['#field_prefix'])) { + $output .= $element['#field_prefix']; + } + $size = $element['#size'] ? ' size="'. $element['#size'] .'"' : ''; + _form_set_class($element, array('form-select')); + $multiple = $element['#multiple']; + $output .= '\n"; + if (isset($element['#field_suffix'])) { + $output .= $element['#field_suffix']; + } + $output .= '
'; + + return theme('form_element', $element, $output); +} \ No newline at end of file diff --git a/types/videoftp/videoftp_widget.inc b/types/videoftp/videoftp_widget.inc new file mode 100644 index 0000000..d674660 --- /dev/null +++ b/types/videoftp/videoftp_widget.inc @@ -0,0 +1,313 @@ + 'textfield', + '#title' => t('Permitted upload file extensions.'), + '#default_value' => !empty($widget['file_extensions']) ? $widget['file_extensions'] : 'mp4 mpeg avi mpg wmv flv mov', + '#size' => 64, + '#description' => t('Extensions a user can attach to this field. Separate extensions with a space and do not include the leading dot. Leaving this blank will allow attachment of a file with any extension.'), + '#weight' => 1 + ); + $form['path_settings'] = array( + '#type' => 'fieldset', + '#title' => t('Path settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#weight' => 2, + ); + $form['path_settings']['file_path'] = array( + '#type' => 'textfield', + '#size' => 64, + '#title' => t('File path'), + '#default_value' => ltrim(ltrim($widget['file_path'], "videos"), "/"), + '#description' => t('Optional subdirectory within the "files/videos/" directory where files will be moved and stored. Do not include preceding or trailing slashes.'), + '#element_validate' => array('video_widget_settings_file_path_validate', '_filefield_widget_settings_file_path_validate'), + ); + $form['path_settings']['ftp_path'] = array( + '#type' => 'textfield', + '#title' => t('FTP Filepath'), + '#default_value' => !empty($widget['ftp_path']) ? $widget['ftp_path'] : 'ftpvideos', + '#description' => t('The subdirectory within the "files/" directory where you have upload the videos for attachment. Once the video is attached it will be moved from this directory to the main files directory.'), + '#required' => TRUE, + '#weight' => 3, + ); + //default settings + $default = video_default_widget_settings($widget); + $form = $form + $default; + return $form; +} + +/** + * Implementation of CCK's hook_widget_settings($op = 'validate'). + * + * //@todo Integrate this into a universal function to share with uploadfield_widget + */ +function videoftp_widget_settings_validate($widget) { + // Check that only web images are specified in the callback. + if(!video_web_extensions($widget['file_extensions'])) { + form_set_error('file_extensions', t('Only web-standard videos are supported through the videoftp widget. If needing to upload other types of files, change the widget to use a standard file upload.')); + } + // Check for our ftp filepath and try to create it, if not it will throw an error. + $ftp_path = file_directory_path().'/'.$widget['ftp_path']; + file_check_directory($ftp_path, true, 'ftp_path'); +} + +/** + * Implementation of CCK's hook_widget_settings($op = 'save'). + */ +function videoftp_widget_settings_save($widget) { + return array('file_extensions', 'file_path', 'ftp_path', 'autothumbnail','autoconversion', 'default_dimensions', 'default_player_dimensions', 'default_video_thumb'); +} + +/** + * The #value_callback for the videoftp_widget type element. + */ +function videoftp_widget_value($element, $edit = FALSE) { + if (!$edit) { + // Creating so we load up our empty values. + $file = field_file_load($element['#default_value']['fid']); + $item = $element['#default_value']; + } + else { + // Reset our item array for our data. + $item = array_merge($element['#default_value'], $edit); + $field = content_fields($element['#field_name'], $element['#type_name']); + + // Uploads take priority over value of fid text field. + if ($fid = videoftp_save_upload($element)) { + $item['fid'] = $fid; + $item['data'] = array(); //reset our thumbnail + } + // Check for #videoftp_value_callback values. + // Because FAPI does not allow multiple #value_callback values like it does + // for #element_validate and #process, this fills the missing functionality + // to allow VideoFtp to be extended purely through FAPI. + elseif (isset($element['#videoftp_value_callback'])) { + foreach ($element['#videoftp_value_callback'] as $callback) { + $callback($element, $item); + } + } + + // Load file if the FID has changed so that it can be saved by CCK. + $file = field_file_load($item['fid']); + + // If the file entry doesn't exist, don't save anything. + if (empty($file)) { + $item = array(); + } + + // Checkboxes loose their value when empty. + // If the list field is present make sure its unchecked value is saved. + if (!empty($field['list_field']) && empty($edit['list'])) { + $item['list'] = 0; + } + } + // Merge file and item data so it is available to all widgets. + $item = array_merge($item, $file); + return $item; +} + +/** + * Process an individual element. + */ +function videoftp_widget_process($element, $edit, &$form_state, $form) { + $item = $element['#value']; + $field_name = $element['#field_name']; + $delta = $element['#delta']; + $field = content_fields($element['#field_name'], $element['#type_name']); + $element['#theme'] = 'videoftp_widget_item'; + + if (isset($element['preview']) && $element['#value']['fid'] != 0) { + $element['preview']['#value'] = theme('videoftp_widget_preview', $element['#value']); + } + + // Title is not necessary for each individual field. + if ($field['multiple'] > 0) { + unset($element['#title']); + } + + // Set up the buttons first since we need to check if they were clicked. + $element['videoftp_attach'] = array( + '#type' => 'submit', + '#value' => t('Attach'), + '#submit' => array('node_form_submit_build_node'), + '#ahah' => array( // with JavaScript + 'path' => 'videoftp/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'], + 'wrapper' => $element['#id'] .'-ahah-wrapper', + 'method' => 'replace', + 'effect' => 'fade', + ), + '#field_name' => $element['#field_name'], + '#delta' => $element['#delta'], + '#type_name' => $element['#type_name'], + '#upload_validators' => $element['#upload_validators'], + '#weight' => 100, + '#post' => $element['#post'], + ); + $element['videoftp_remove'] = array( + // With default CCK edit forms, $element['#parents'] is array($element['#field_name'], $element['#delta']). + // However, if some module (for example, flexifield) places our widget deeper in the tree, we want to + // use that information in constructing the button name. + '#name' => implode('_', $element['#parents']) .'_videoftp_remove', + '#type' => 'submit', + '#value' => t('Remove'), + '#submit' => array('node_form_submit_build_node'), + '#ahah' => array( // with JavaScript + 'path' => 'videoftp/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'], + 'wrapper' => $element['#id'] .'-ahah-wrapper', + 'method' => 'replace', + 'effect' => 'fade', + ), + '#field_name' => $element['#field_name'], + '#delta' => $element['#delta'], + '#weight' => 101, + '#post' => $element['#post'], + ); + + // Because the output of this field changes depending on the button clicked, + // we need to ask FAPI immediately if the remove button was clicked. + // It's not good that we call this private function, but + // $form_state['clicked_button'] is only available after this #process + // callback is finished. + if (_form_button_was_clicked($element['videoftp_remove'])) { + // Delete the file if it is currently unused. Note that field_file_delete() + // does a reference check in addition to our basic status check. + if (isset($edit['fid'])) { + $removed_file = field_file_load($edit['fid']); + if ($removed_file['status'] == 0) { + field_file_delete($removed_file); + } + } + $item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => '', 'video_thumb' => '')); + } + + // Set access on the buttons and select. + $element['videoftp_attach']['#access'] = empty($item['fid']); + $element['videoftp_remove']['#access'] = !empty($item['fid']); + + $options = videoftp_options($field); + $element['ftpselect'] = array( + '#type' => 'select', + '#required' => isset($element['#required']) ? $element['#required'] : $field['required'], + '#options' => $options, + '#access' => empty($item['fid']), + ); + + // Set the FID. + $element['fid'] = array( + '#type' => 'hidden', + '#value' => $item['fid'], + ); + + if ($item['fid'] != 0) { + $element['preview'] = array( + '#type' => 'markup', + '#value' => theme('video_widget_preview', $item), + ); + } + + // placeholder.. will be serialized into the data column. this is a place for widgets + // to put additional data. + $element['data'] = array( + '#tree' => 'true', + '#access' => !empty($item['fid']), + ); + + // Create our thumbnails + if($field['widget']['autothumbnail']) { + video_thumb_process($element); + } + + // Add our extra fields if in preview mode + if(!empty($item['fid'])) { + video_widget_element_settings($element); + } + + // Set #element_validate in a way that it will not wipe out other + // validation functions already set by other modules. + if (empty($element['#element_validate'])) { + $element['#element_validate'] = array(); + } + array_unshift($element['#element_validate'], 'videoftp_widget_validate'); + + // Make sure field info will be available to the validator which + // does not get the values in $form. + $form_state['#field_info'][$field['field_name']] = $field; + + $element['#attributes']['id'] = $element['#id'] .'-ahah-wrapper'; + $element['#prefix'] = '
'; + $element['#suffix'] = '
'; + + // Lets use the clicked_button #submit[0] value here instead and see how that works out for now... + if($form_state['submitted'] == 1) { + video_widget_process($element, $form_state); + } + return $element; +} + +function videoftp_save_upload($element) { + global $user; + $upload_name = $element['#field_name'] .'_'. $element['#delta']; + $delta = $element['#delta']; + $field = content_fields($element['#field_name'], $element['#type_name']); + $video = $element['#post'][$field['field_name']][$delta]['ftpselect']; + $ftp_path = file_directory_path().'/'.$field['widget']['ftp_path']; + + if (empty($video) || !file_exists($ftp_path.'/'.$video)) { + return 0; + } + + $dest = filefield_widget_file_path($field); + if (!field_file_check_directory($dest, FILE_CREATE_DIRECTORY)) { + watchdog('filefield', 'The upload directory %directory for the file field %field (content type %type) could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array('%directory' => $dest, '%field' => $element['#field_name'], '%type' => $element['#type_name'])); + form_set_error($upload_name, t('The file could not be uploaded.')); + return 0; + } + + // Begin building the file object. + $file = new stdClass(); + $file->uid = $user->uid; + $file->filename = file_munge_filename(trim(basename($video), '.'), $field['widget']['file_extensions']); + $file->filepath = $ftp_path.'/'.$video; + $file->filemime = file_get_mimetype($file->filename); + $file->filesize = filesize($file->filepath); + $file->status = FILE_STATUS_TEMPORARY; + $file->timestamp = time(); + + //lets move our file from the ftp folder to the files directory + if(file_move($file, $dest)) { + // Insert new record to the database. + drupal_write_record('files', $file); + } + _field_file_cache($file); // cache the file in order to minimize load queries + return $file->fid; +} + +function videoftp_options($field) { + $options = array(); + $options[] = t('Select Video'); + // Lets setup our ftp_path. + $ftp_path = file_directory_path().'/'.$field['widget']['ftp_path']; + // We are going to scan the directory and pull out the available video types by extension. + $extensions = explode(" ",$field['widget']['file_extensions']); + $video_files = scandir($ftp_path); + foreach ($video_files as $file) { + $ext = pathinfo($file); + if (in_array($ext['extension'], $extensions)) { + //add the file to the options array for selection + $options["$file"] = $file; + } + } + return $options; +} \ No newline at end of file -- cgit v1.2.3