You are here

filebrowser.form.upload.inc in Filebrowser 6.2

File

filebrowser.form.upload.inc
View source
<?php

/* This file is part of "filebrowser".
 *    Copyright 2009-2011, arNuméral
 *    Author : Yoran Brault
 *    eMail  : yoran.brault@bad_arnumeral.fr (remove bad_ before sending an email)
 *    Site   : http://www.arnumeral.fr
 *
 * "filebrowser" is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * "filebrowser" is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with "filebrowser"; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

/**
 * @param form_state
 * @param i
 * @param path_info_new
 * @param path_info_old
 */
function _filebrowser_build_new_upload_file_name($node, $form_state, $i) {
  $file_name = $_FILES['files']['name']["file_{$i}"];
  if (!empty($form_state['values']["file_name_{$i}"])) {
    $path_info_new = pathinfo($form_state['values']["file_name_{$i}"]);
    $path_info_old = pathinfo($file_name);
    $file_name = $path_info_new['filename'];
    if (isset($path_info_old['extension'])) {
      $file_name .= ".{$path_info_old['extension']}";
    }
  }
  _filebrowser_load_files($node);
  $target = _filebrowser_convert_to_fs_encoding($node, $node->file_listing['.']['full-path'] . "/" . $file_name);
  return $target;
}

/**
 * upload form definition.
 */
function filebrowser_form_upload($form_state, $node) {
  $form = array();
  $form['filebrowser_uploads'] = array(
    '#type' => 'fieldset',
    '#title' => t('File Upload'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Uploaded file will be saved to the current directory.'),
    '#prefix' => '<div class="attachments">',
    '#suffix' => '</div>',
    '#weight' => 30,
  );
  $form['#node'] = $node;
  $form['#attributes'] = array(
    'enctype' => "multipart/form-data",
  );
  _filebrowser_load_files($node);
  $form['#submit'][] = 'filebrowser_form_upload_submit';
  $form['#validate'][] = 'filebrowser_form_upload_validate';
  $i = 1;

  // Later we can have multi-upload
  $form['filebrowser_uploads']["file_{$i}"] = array(
    '#type' => 'file',
    '#title' => t('Upload file'),
    '#size' => 40,
  );
  $form['filebrowser_uploads']["description_{$i}"] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#size' => 255,
  );
  $form['filebrowser_uploads']["file_name_{$i}"] = array(
    '#type' => 'textfield',
    '#description' => t('Just put filename with NO EXTENSION here if you want to rename the file you want to upload'),
    '#title' => t('New name'),
    '#size' => 40,
  );
  $form['submitted'] = array(
    '#tree' => TRUE,
  );
  $form['filebrowser_uploads']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Upload'),
  );
  $form['#redirect'] = NULL;
  return $form;
}

/**
 * uploads validation.
 */
function filebrowser_form_upload_validate($form, $form_state) {
  $node = $form['#node'];
  _filebrowser_load_files($node);
  $i = 1;

  // Later we can have multi-upload
  $target = _filebrowser_build_new_upload_file_name($node, $form_state, $i);
  if (!$node->folder_uploads->allow_overwrite && file_exists($target)) {
    form_error($form['filebrowser_uploads']["file_{$i}"], t("This file already exists."));
  }
  if (!empty($node->folder_uploads->accepted_uploaded_files) && !_filebrowser_match_path($target, $node->folder_uploads->accepted_uploaded_files)) {
    form_error($form['filebrowser_uploads']["file_{$i}"], t("Sorry, you can't upload this kind of file."));
  }
}

/**
 * uploads submition.
 */
function filebrowser_form_upload_submit($form, &$form_state) {
  $i = 1;

  // Later we can have multi-upload
  $node = $form['#node'];
  $target = _filebrowser_build_new_upload_file_name($node, $form_state, $i);
  $success = copy($_FILES['files']['tmp_name']["file_{$i}"], $target);
  if (!$success) {
    drupal_set_message(t("Unable to upload this file, do you have filesystem right to do that ?"), 'error');
  }
  else {
    _filebrowser_load_files($node, NULL, TRUE);

    // force listing rebuild
    if (!empty($form['filebrowser_uploads']["description_{$i}"])) {
      $file = $node->file_listing[_filebrowser_safe_basename($target)];
      module_invoke_all('filebrowser_metadata_set', $file, array(
        'description' => $form_state['values']["description_{$i}"],
      ));
    }
  }
  drupal_goto("node/{$node->nid}/{$node->file_listing['.']['fid']}", _filebrowser_url_query());
}

Functions