/[drupal]/contributions/modules/filefield/filefield_widget.inc
ViewVC logotype

Diff of /contributions/modules/filefield/filefield_widget.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.28, Thu Jul 31 06:02:43 2008 UTC revision 1.29, Fri Aug 1 04:58:59 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: filefield_widget.inc,v 1.27 2008/07/31 04:24:41 dopry Exp $  // $Id: filefield_widget.inc,v 1.28 2008/07/31 06:02:43 dopry Exp $
3  /**  /**
4   * @file   * @file
5   * FileField: Defines a CCK file field type.   * FileField: Defines a CCK file field type.
# Line 108  function _filefield_widget_settings_max_ Line 108  function _filefield_widget_settings_max_
108    }    }
109  }  }
110    
111    function filefield_save_upload($element) {
112      $upload_name = $element['#field_name'] .'_'. $element['#delta'];
113      $field_instance = content_fields($element['#field_name'], $element['#type_name']);
114    
115      if (empty($_FILES['files']['name'][$upload_name])) {
116        return 0;
117      }
118    
119      $dest = $field_instance['widget']['file_path'];
120      if (module_exists('token')) {
121        global $user;
122        $dest = token_replace($dest, 'user', $user);
123      }
124    
125      $dest = file_directory_path() .'/'. $dest;
126      if (!field_file_check_directory($dest, FILE_CREATE_DIRECTORY)) {
127        watchdog('imagefield', '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']));
128        form_set_error($upload_name, t('The file could not be uploaded.'));
129        return 0;
130      }
131    
132      if (!$file = field_file_save_upload($upload_name, $element['#upload_validators'], $dest)) {
133        watchdog('imagefield', 'The file upload failed. %upload', array('%upload' => $upload_name));
134        form_set_error($upload_name, t('The Image upload failed.'));
135        return 0;
136      }
137      return $file['fid'];
138    }
139    
140  /**  /**
141   * FileField widget element callbacks.   * FileField widget element callbacks.
142   */   */
# Line 117  function filefield_widget_value($element Line 146  function filefield_widget_value($element
146      $item = $element['#default_value'];      $item = $element['#default_value'];
147    }    }
148    else {    else {
149        // uploads take priority over value of fid text field.
150        if ($fid = filefield_save_upload($element)) {
151          $edit['fid'] = $fid;
152        }
153    
154        // load file.
155      $file = field_file_load($edit['fid']);      $file = field_file_load($edit['fid']);
156      $item = array(      $item = array(
157        'fid' => $edit['fid'],        'fid' => $edit['fid'],
       'description' => isset($edit['description']) ? $edit['description'] : '',  
158        'list' => empty($edit['list']),        'list' => empty($edit['list']),
159        'data' => empty($edit['data']) ? array() : $edit['data'],        'data' => empty($edit['data']) ? array() : $edit['data'],
160      );      );
161    }    }
162      // merge file and item data so it is available to all widgets.
163    $item = array_merge($item, $file);    $item = array_merge($item, $file);
164    
165    // if this widget is another type and leaning on filefield to do the dirty work....    // if this widget is another type and leaning on filefield to do the dirty work....
# Line 137  function filefield_widget_value($element Line 172  function filefield_widget_value($element
172  }  }
173    
174  function filefield_widget_process($element, $edit, &$form_state, $form) {  function filefield_widget_process($element, $edit, &$form_state, $form) {
175    $item = $element['#default_value'];  
176      $item = $element['#value'];
177      $field_name = $element['#field_name'];
178      $delta = $element['#delta'];
179    
180      // check remove buttons...
181      $remove_name = $element['#field_name'] .'_'. $element['#delta'] .'_remove_btn';
182      if (isset($form_state['clicked_button']) && $form_state['clicked_button']['#name'] == $remove_name) {
183        $item['fid'] = 0;
184      }
185    
186    // this should happen here, not in #value_callback? does #value_callback not happen the first time    // this should happen here, not in #value_callback? does #value_callback not happen the first time
187    // the form is built? or is item not fully loaded when returned from field?    // the form is built? or is item not fully loaded when returned from field?
188    $item = array_merge($item, field_file_load($item['fid']));    //$item = array_merge($item, field_file_load($item['fid']));
189    $field = content_fields($element['#field_name'], $element['#type_name']);    $field = content_fields($element['#field_name'], $element['#type_name']);
190    
191    
192    
193    // figute out our fid...    // figute out our fid...
194    $element['fid'] = array('#type' => 'hidden', '#default_value' =>  $item['fid']);    $element['fid'] = array('#type' => 'hidden', '#value' =>  $item['fid']);
195    
196    if ($item['fid'] != 0) {    if ($item['fid'] != 0) {
197      $element['preview'] = array('#type' => 'markup', '#value' => theme($element['#type'] .'_preview', $item));      $element['preview'] = array('#type' => 'markup', '#value' => theme($element['#type'] .'_preview', $item));
# Line 157  function filefield_widget_process($eleme Line 203  function filefield_widget_process($eleme
203    $element['data']['description'] = array(    $element['data']['description'] = array(
204      '#type' => 'textfield',      '#type' => 'textfield',
205      '#title' => t('Description'),      '#title' => t('Description'),
206      '#default_value' => $item['data']['description'],      '#value' => isset($item['data']['description']) ? $item['data']['description'] : '',
207    );    );
208    
209    if ($field['force_list_default']) {    if ($field['force_list_default']) {
# Line 170  function filefield_widget_process($eleme Line 216  function filefield_widget_process($eleme
216      $element['list'] = array(      $element['list'] = array(
217        '#type' => 'checkbox',        '#type' => 'checkbox',
218        '#title' => t('List'),        '#title' => t('List'),
219        '#default_value' => isset($item['list']) ? $item['list'] : $field['list_default'],        '#value' => isset($item['list']) ? $item['list'] : $field['list_default'],
220        '#attributes' => array('class' => 'filefield-list'),        '#attributes' => array('class' => 'filefield-list'),
221      );      );
222    }    }
223    
224    
225    
226      foreach ($element['#upload_validators'] as $callback => $arguments) {
227        $help_func = $callback .'_help';
228        $desc[] = call_user_func_array($help_func, $arguments);
229      }
230      $element['upload'] = array(
231        '#name' => 'files['. $element['#field_name'] .'_'. $element['#delta'] .']',
232        '#type' => 'file',
233        '#title' => t('New Upload'),
234        '#description' => implode('<br />', $desc),
235        '#attributes' => array(
236          'accept' => str_replace(' ', '|', trim($field['widget']['file_extensions']))
237        )
238      );
239    
240    
241      if ($item['fid'] != 0) {
242        $element['upload']['#title'] = t('Replace');
243      }
244      $element['upload_btn'] = array(
245        '#type' => 'submit',
246        '#value' => t('Upload'),
247        '#process' => array('filefield_widget_process_upload_btn', 'form_expand_ahah'),
248        '#process' => array('filefield_widget_process_upload_btn'),
249        '#submit' => array('node_form_submit_build_node'),
250        '#ahah' => array( // with JavaScript
251           'path' => 'filefield/js/upload/'. $element['#field_name'] .'/'. $element['#type_name'] .'/'. $element['#delta'],
252           'wrapper' => $element['#id'],
253           'method' => 'replace',
254           'effect' => 'fade',
255        ),
256        '#field_name' => $element['#field_name'],
257        '#delta' => $element['#delta'],
258        '#type_name' => $element['#type_name'],
259        '#upload_validators' => $element['#upload_validators'],
260      );
261    if ($item['fid'] != 0) {    if ($item['fid'] != 0) {
262      $element['remove_btn'] = array(      $element['remove_btn'] = array(
263        '#name' => $element['#field_name'] .'_'. $element['#delta'] .'_remove_btn',        '#name' => $element['#field_name'] .'_'. $element['#delta'] .'_remove_btn',
264        '#type' => 'submit',        '#type' => 'submit',
265        '#value' => t('Remove'),        '#value' => t('Remove'),
266        //'#process' => array('filefield_widget_process_remove_btn', 'form_expand_ahah'),        '#process' => array('filefield_widget_process_remove_btn', 'form_expand_ahah'),
267        '#process' => array('filefield_widget_process_remove_btn'),        '#process' => array('filefield_widget_process_remove_btn'),
268        '#submit' => array('filefield_widget_submit_remove_btn'),        '#submit' => array('filefield_widget_submit_remove_btn'),
269    /*
270        '#ahah' => array( // with JavaScript        '#ahah' => array( // with JavaScript
271           'path' => 'filefield/js/remove/'. $element['#field_name'] .'/'. $element['#type_name'] .'/'. $element['#delta'],           'path' => 'filefield/js/delete/'. $element['#field_name'] .'/'. $element['#type_name'] .'/'. $element['#delta'],
272           'wrapper' => $element['#id'],           'wrapper' => $element['#id'],
273           'method' => 'replace',           'method' => 'replace',
274           'effect' => 'fade',           'effect' => 'fade',
275        ),        ),
276    */
277        '#field_name' => $element['#field_name'],        '#field_name' => $element['#field_name'],
278        '#delta' => $element['#delta'],        '#delta' => $element['#delta'],
279      );      );
280    }    }
281    
   if ($item['fid'] == 0) {  
     $element['data']['description']['#type'] = 'hidden';  
   
     foreach ($element['#upload_validators'] as $callback => $arguments) {  
       $help_func = $callback .'_help';  
       $desc[] = call_user_func_array($help_func, $arguments);  
     }  
     $element['upload'] = array(  
       '#name' => 'files['. $element['#field_name'] .'_'. $element['#delta'] .']',  
       '#type' => 'file',  
       '#title' => t('New Upload'),  
       '#description' => implode('<br />', $desc),  
       '#attributes' => array(  
         'accept' => str_replace(' ', '|', trim($field['widget']['file_extensions']))  
       )  
     );  
282    
     $element['upload_btn'] = array(  
       '#type' => 'submit',  
       '#value' => t('Upload'),  
       //'#process' => array('filefield_widget_process_upload_btn', 'form_expand_ahah'),  
       '#process' => array('filefield_widget_process_upload_btn'),  
       '#submit' => array('filefield_widget_submit_upload_btn'),  
       '#ahah' => array( // with JavaScript  
          'path' => 'filefield/js/upload/'. $element['#field_name'] .'/'. $element['#type_name'] .'/'. $element['#delta'],  
          'wrapper' => $element['#id'],  
          'method' => 'replace',  
          'effect' => 'fade',  
       ),  
       '#field_name' => $element['#field_name'],  
       '#delta' => $element['#delta'],  
       '#type_name' => $element['#type_name'],  
       '#upload_validators' => $element['#upload_validators'],  
     );  
   }  
   
283    return $element;    return $element;
284  }  }
285    
# Line 240  function filefield_widget_validate($elem Line 288  function filefield_widget_validate($elem
288  }  }
289    
290  function filefield_widget_submit_remove_btn($form, &$form_state) {  function filefield_widget_submit_remove_btn($form, &$form_state) {
291    $field_name = $form_state['clicked_button']['#field_name'];    node_form_submit_build_node($form, $form_state);
   $delta = $form_state['clicked_button']['#delta'];  
   
   $form_state['values'][$field_name][$delta] = array(  
     'fid' => 0,  
     'alt' => '',  
     'filepath' => '',  
     'filename' => '',  
     'title' => '',  
     'data' => array(),  
   );  
   $form_state['rebuild'] = true;  
 }  
   
 function filefield_widget_process_upload_btn($element, $edit, &$form_state, &$form) {  
   // nothing to do if nothing clicked.  
   if (empty($form_state['clicked_button'])) return $element;  
   $button = $form_state['clicked_button']['#name'];  
   
   $field_name = $element['#field_name'];  
   $node_type =  $element['#type_name'];  
   $delta = $element['#delta'];  
   $upload_name = $field_name .'_'. $delta;  
   
   // Do not process uploads if some other button besides save or preview was  
   // pressed.  
   if ( !($button == 'op' && !empty($_FILES['files']['name'][$upload_name]))  
       || $button != $element['#name']) return $element;  
   
   $field = content_fields($field_name, $node_type);  
   
   $widget_file_path = $field['widget']['file_path'];  
   if (module_exists('token')) {  
     global $user;  
     $widget_file_path = token_replace($widget_file_path, 'user', $user);  
   }  
   
   $complete_file_path = file_directory_path() .'/'. $widget_file_path;  
   $upload_name = $field_name .'_'. $delta;  
   if (!field_file_check_directory($complete_file_path, FILE_CREATE_DIRECTORY)) {  
     watchdog('imagefield', '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' => $widget_file_path, '%field' => $field_name, '%type' => $field['type_name']));  
     form_set_error($upload_name, t('The file could not be uploaded.'));  
     return $element;  
   }  
   
   $validators = $element['#upload_validators'];  
   
   if (!$file = field_file_save_upload($upload_name, $validators, $complete_file_path)) {  
     watchdog('imagefield', 'The file upload failed. %upload', array('%upload' => $upload_name));  
     form_set_error($upload_name, t('The Image upload failed.'));  
     return $element;  
   }  
   
   $form_state['values'][$field_name][$delta]['fid'] = $file['fid'];  
   $form_state['values'][$field_name][$delta]['data'] = $file['data'];  
   
   return $element;  
292  }  }
293    
 function filefield_widget_submit_upload_btn($form, &$form_state) {  
   $form_state['rebuild'] = true;  
 }  
294    
295  function _filefield_widget_validate($element, &$form_state) {  function _filefield_widget_validate($element, &$form_state) {
296  }  }

Legend:
Removed from v.1.28  
changed lines
  Added in v.1.29

  ViewVC Help
Powered by ViewVC 1.1.2