From 38a1a35973751a4a7f9216c054b3c5209a82a7a4 Sat, 15 Oct 2011 19:01:18 +0200
From: Jorrit Schippers <jorrit@161217.no-reply.drupal.org>
Date: Sat, 15 Oct 2011 19:00:23 +0200
Subject: [PATCH] #1310878 - Add option to widget instance form to allow the user to copy the file from the FTP directory to the destination directory instead of moving it.

diff --git a/types/videoftp/videoftp_widget.inc b/types/videoftp/videoftp_widget.inc
index d674660..5a72883 100644
--- a/types/videoftp/videoftp_widget.inc
+++ b/types/videoftp/videoftp_widget.inc
@@ -33,7 +33,7 @@
     '#default_value' => ltrim(ltrim($widget['file_path'], "videos"), "/"),
     '#description' => t('Optional subdirectory within the "<em>files/videos/</em>" 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'),
@@ -41,6 +41,20 @@
     '#description' => t('The subdirectory within the "<em>files/</em>" 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,
+  );
+  $form['path_settings']['ftp_copymode'] = array(
+    '#type' => 'select',
+    '#title' => t('FTP copy mode'),
+    '#default_value' => !empty($widget['ftp_copymode']) ? $widget['ftp_copymode'] : 'alwaysmove',
+    '#description' => t('Whether to move or copy the file from the FTP directory to the destination directory.'),
+    '#options' => array(
+      'alwaysmove' => t('Always move the file'),
+      'alwayscopy' => t('Always copy the file'),
+      'askmove' => t('Move by default, allow change by user'),
+      'askcopy' => t('Copy by default, allow change by user'),
+    ),
+    '#required' => TRUE,
+    '#weight' => 4,
   );
   //default settings
   $default = video_default_widget_settings($widget);
@@ -67,7 +81,7 @@
  * 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');
+  return array('file_extensions', 'file_path', 'ftp_path', 'ftp_copymode', 'autothumbnail','autoconversion', 'default_dimensions', 'default_player_dimensions', 'default_video_thumb');
 }
 
 /**
@@ -204,6 +218,15 @@
     '#access' => empty($item['fid']),
   );
 
+  if (isset($field['widget']['ftp_copymode']) && strncmp($field['widget']['ftp_copymode'], 'ask', 3) === 0) {
+    $element['ftpcopy'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Leave a copy of the file in the FTP upload directory.'),
+      '#default_value' => $field['widget']['ftp_copymode'] == 'askcopy',
+      '#access' => empty($item['fid']),
+    );
+  }
+
   // Set the FID.
   $element['fid'] = array(
     '#type' => 'hidden',
@@ -261,7 +284,8 @@
   $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'];
+  $value = $element['#post'][$field['field_name']][$delta];
+  $video = $value['ftpselect'];
   $ftp_path = file_directory_path().'/'.$field['widget']['ftp_path'];
 
   if (empty($video) || !file_exists($ftp_path.'/'.$video)) {
@@ -285,8 +309,21 @@
   $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)) {
+  // Determine if the file should be copied. Move is the default.
+  $cpfunc = 'file_move';
+  if (isset($field['widget']['ftp_copymode'])) {
+    if ($field['widget']['ftp_copymode'] == 'alwayscopy') {
+      // Copying is forced
+      $cpfunc = 'file_copy';
+    }
+    elseif(strncmp($field['widget']['ftp_copymode'], 'ask', 3) === 0 && !empty($value['ftpcopy'])) {
+      // The user wants to copy
+      $cpfunc = 'file_copy';
+    }
+  }
+
+  //lets move or copy our file from the ftp folder to the files directory
+  if ($cpfunc($file, $dest)) {
     // Insert new record to the database.
     drupal_write_record('files', $file);
   }