You are here

pack_upload.module in Pack & Upload 7

Same filename and directory in other branches
  1. 8 pack_upload.module
  2. 2.0.x pack_upload.module

This is the module file for pack and upload.

All hooks are implemented in this file only.

File

pack_upload.module
View source
<?php

/**
 * @file
 * This is the module file for pack and upload.
 *
 * All hooks are implemented in this file only.
 */

/**
 * Implements hook_help().
 */
function pack_upload_help($path, $arg) {
  switch ($path) {
    case 'admin/help#pack_upload':

      // Construct the path of this module's readme file.
      $path_readme = drupal_get_path('module', 'pack_upload') . '/README.txt';

      // If the readme is available, return the contents.
      if (file_exists($path_readme)) {
        $readme = file_get_contents($path_readme);
        return '<h1>README.txt</h1><pre>' . check_plain($readme) . '</pre>';
      }
  }
}

/**
 * Implements hook_permission().
 */
function pack_upload_permission() {
  return array(
    'pack and upload' => array(
      'description' => t('This permission allow a user to upload packed files & assets to server and extract them to a specified directory.'),
      'title' => t('Use pack & upload'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function pack_upload_menu() {
  $items['admin/config/pack-upload'] = array(
    'title' => 'Pack & Upload',
    'description' => 'Pack your media & assets, upload them and extract to a specified directory.',
    'access arguments' => array(
      'pack and upload',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pack_upload_settings_form',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/pack-upload/settings'] = array(
    'title' => 'Settings',
    'description' => 'Setup directory name for extraction of media & assets pack. ',
    'access arguments' => array(
      'pack and upload',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pack_upload_settings_form',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/pack-upload/bulk-media-upload'] = array(
    'title' => 'Upload bulk media',
    'description' => 'Upload bulk media to a directory.',
    'access arguments' => array(
      'pack and upload',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pack_upload_media_upload_form',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Form constructor for the pack and upload media form.
 *
 * @ingroup forms
 */
function pack_upload_media_upload_form() {
  $form = array();
  $form['panel'] = array(
    '#title' => t('Bulk Media Uploader'),
    '#type' => 'fieldset',
  );
  $form['panel']['file'] = array(
    '#title' => t('Upload file'),
    '#type' => 'file',
    '#description' => t('Create package of media files, for e.g., PDFs, images, text files and upload to Drupal. Valid extensions are .zip, .tar.gz, .tar. All files will be extracted to !directory', array(
      '!directory' => file_build_uri(variable_get('pack_upload_bulk_media_path')),
    )),
  );
  $form['panel']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

/**
 * Implements hook_form_submit().
 */
function pack_upload_media_upload_form_submit($form, $form_state) {
  $file = file_save_upload('file', array(
    'file_validate_extensions' => array(
      'zip tar tar.gz',
    ),
  ));

  // Created a directory if not available.
  if (!is_dir(variable_get('pack_upload_bulk_media_path'))) {
    drupal_mkdir(variable_get('pack_upload_bulk_media_path'), variable_get('file_chmod_directory', 0755));
  }

  // Creating a streamwrapper URI.
  $uri = variable_get('pack_upload_bulk_media_path');
  if ($file) {
    if ($file = file_move($file, $uri, FILE_EXISTS_RENAME)) {
      $form_state['values']['file'] = $file;
      $filename = $file->filename;
      $ext = pathinfo($filename, PATHINFO_EXTENSION);
      $realpath = drupal_realpath($file->uri);
      if ($ext == 'zip') {
        $zip = new ZipArchive();
        $zip
          ->open($realpath);
        $result = @$zip
          ->extractTo(drupal_realpath($uri));
        if ($result === TRUE) {
          drupal_set_message(t('All media have been extracted to %path', array(
            '%path' => drupal_realpath($uri),
          )));
        }
        else {
          watchdog('error', 'There is some problem related to extraction of the file. Please upload and try again.', array(), WATCHDOG_ERROR, NULL);
          drupal_set_message(t('There is some problem related to extraction of the file. Please upload and try again.'), 'error', FALSE);
        }
        $zip
          ->close();
      }
      else {
        try {
          $phar = new PharData($realpath);
          $phar
            ->extractTo(drupal_realpath($uri), null, true);

          // extract all files, and overwrite
          drupal_set_message(t('All media have been extracted to %path', array(
            '%path' => drupal_realpath($uri),
          )));
        } catch (Exception $e) {
          watchdog('error', 'There is some problem related to extraction of the file. Please upload and try again.', array(), WATCHDOG_ERROR, NULL);
          drupal_set_message(t('There is some problem related to extraction of the file. Please upload and try again.'), 'error', FALSE);
        }
      }
    }
    else {
      form_set_error('file', t("Failed to write the uploaded file the file folder."));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
}

/**
 * Form constructor for the pack and upload settings form.
 *
 * @ingroup forms
 */
function pack_upload_settings_form() {
  $form['panel'] = array(
    '#title' => t('Bulk Media Uploader'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
  );
  $form['panel']['pack_upload_bulk_media_path'] = array(
    '#title' => t('Bulk Media Extraction Path'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#description' => t('Path to media directory from content feeder would upload media to nodes. Example: public://bulk_media'),
    '#default_value' => variable_get('pack_upload_bulk_media_path', 'bulk_media'),
    '#after_build' => array(
      'system_check_directory',
    ),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_file_validate().
 */
function pack_upload_file_validate($file) {
  $errors = array();
  $result = db_query('SELECT f.fid FROM {file_managed} f WHERE f.uri = :uri', array(
    ':uri' => $file->destination,
  ));
  $output = $result
    ->fetchObject();
  if (is_object($output)) {
    $errors[] = t("A file already exist with the same name. Please try uploading by renaming this file.");
  }
  return $errors;
}

Functions

Namesort descending Description
pack_upload_file_validate Implements hook_file_validate().
pack_upload_help Implements hook_help().
pack_upload_media_upload_form Form constructor for the pack and upload media form.
pack_upload_media_upload_form_submit Implements hook_form_submit().
pack_upload_menu Implements hook_menu().
pack_upload_permission Implements hook_permission().
pack_upload_settings_form Form constructor for the pack and upload settings form.