You are here

filebrowser.form.create_folder.inc in Filebrowser 6.2

File

filebrowser.form.create_folder.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.
 */
function filebrowser_form_create_folder($form_state, $node) {
  $form = array();
  $form['filebrowser_create_folder'] = array(
    '#type' => 'fieldset',
    '#title' => t('Create a folder'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('This folder will be created within the current directory.'),
    '#weight' => 30,
  );
  $form['#node'] = $node;
  $form['#submit'][] = 'filebrowser_form_create_folder_submit';
  $form['#validate'][] = 'filebrowser_create_folder_validate';
  $form['filebrowser_create_folder']['folder_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Folder Name'),
    '#size' => 40,
    '#required' => TRUE,
  );
  $form['filebrowser_create_folder']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#size' => 255,
  );
  $form['submitted'] = array(
    '#tree' => TRUE,
  );
  $form['filebrowser_create_folder']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create'),
  );
  return $form;
}

/**
 * Implementation of hook_validate() for creating a folder
 */
function filebrowser_form_create_folder_validate($form, $form_state) {
  $node = $form['#node'];
  _filebrowser_load_files($node);
  $target = _filebrowser_convert_to_fs_encoding($node, "{$node->file_listing['.']['full-path']}/{$form_state['values']['folder_name']}");
  if (file_exists($target)) {
    form_error($form['filebrowser_create_folder']['folder_name'], t("This folder already exists."));
  }
}

/**
 * create folder submition.
 */
function filebrowser_form_create_folder_submit($form, &$form_state) {
  $i = 1;

  // Later we can have multi-upload
  $node = $form['#node'];
  $target = _filebrowser_convert_to_fs_encoding($node, "{$node->file_listing['.']['full-path']}/{$form_state['values']['folder_name']}");
  $success = mkdir($target, 0777);
  if (!$success) {
    drupal_set_message(t("Unable to create this folder, do you have filesystem right to do that ?"), 'error');
  }
  else {
    _filebrowser_load_files($node, NULL, TRUE);

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

Functions

Namesort descending Description
filebrowser_form_create_folder
filebrowser_form_create_folder_submit create folder submition.
filebrowser_form_create_folder_validate Implementation of hook_validate() for creating a folder