You are here

webform_multiple_file.module in Webform Multiple File 7

Provides functions and hook implementations for webform multiple file module.

File

webform_multiple_file.module
View source
<?php

/**
 * @file
 * Provides functions and hook implementations for webform multiple file module.
 */

/**
 * Value for field API indicating a field accepts an unlimited number of values.
 */
define('WEBFORM_MULTIPLE_FILE_CARDINALITY_UNLIMITED', -1);

/**
 * Implements hook_webform_component_info().
 */
function webform_multiple_file_webform_component_info() {
  $components = array();
  webform_component_include('file');
  $components['multiple_file'] = array(
    'label' => t('Multiple file'),
    'description' => t('Multiple file upload support for webform.'),
    'features' => array(
      'conditional' => FALSE,
      'attachment' => TRUE,
    ),
    'file' => 'webform_multiple_file.webform.inc',
  );
  return $components;
}

/**
 * Implements hook_webform_submission_presave().
 *
 * @see webform_webform_submission_presave()
 */
function webform_multiple_file_webform_submission_presave($node, &$submission) {
  $has_file_components = FALSE;
  $new_fids = array();
  $old_fids = array();
  $renameable = array();
  foreach ($node->webform['components'] as $cid => $component) {
    if ($component['type'] == 'multiple_file') {
      $has_file_components = TRUE;
      if (!empty($submission->data[$cid])) {
        foreach ($submission->data[$cid] as $key => $value) {
          if (empty($value)) {
            unset($submission->data[$cid][$key]);
          }
          if (strlen($component['extra']['rename'])) {
            $renameable[$cid][] = $value;
          }
        }
        $new_fids = array_merge($new_fids, $submission->data[$cid]);
      }
    }
  }
  if ($has_file_components) {

    // If we're updating a submission, build a list of previous files.
    if (isset($submission->sid)) {
      drupal_static_reset('webform_get_submission');
      $old_submission = webform_get_submission($node->nid, $submission->sid);
      foreach ($node->webform['components'] as $cid => $component) {
        if ($component['type'] == 'multiple_file') {
          if (!empty($old_submission->data[$cid])) {
            $old_fids = array_merge($old_fids, $old_submission->data[$cid]);
          }
        }
      }
    }

    // Only rename files if this is the first time the submission is being saved as finished.
    if ($submission->is_draft || isset($old_submission) && !$old_submission->is_draft) {
      $renameable = array();
    }

    // Save the list of added or removed files so we can add usage in
    // hook_webform_submission_insert() or _update().
    // Also we should keep $submission->file_usage values from previous hook implementations.
    $deleted_fids = isset($submission->file_usage['deleted_fids']) ? $submission->file_usage['deleted_fids'] : array();
    $added_fids = isset($submission->file_usage['added_fids']) ? $submission->file_usage['added_fids'] : array();
    $previous_renameable = isset($submission->file_usage['renameable']) ? $submission->file_usage['renameable'] : array();
    $submission->file_usage = array(
      'deleted_fids' => array_merge($deleted_fids, array_diff($old_fids, $new_fids)),
      'added_fids' => array_merge($added_fids, array_diff($new_fids, $old_fids)),
      'renameable' => $previous_renameable + $renameable,
    );
  }
}

Functions

Constants

Namesort descending Description
WEBFORM_MULTIPLE_FILE_CARDINALITY_UNLIMITED Value for field API indicating a field accepts an unlimited number of values.