Projet

Général

Profil

Paste
Télécharger (5,83 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_bulk_upload / includes / media_bulk_upload.admin.inc @ 0125e073

1
<?php
2

    
3
/**
4
 * @file
5
 * This file contains the admin functions for the Media Bulk Upload module.
6
 */
7

    
8
/**
9
 * Form callback for mass import.
10
 */
11
function media_bulk_upload_import($form, &$form_state) {
12
  if (!isset($form_state['storage']['files'])) {
13
    $form_state['storage']['step'] = 'choose';
14
    $form_state['storage']['next_step'] = 'preview';
15
    $form['directory'] = array(
16
      '#type' => 'textfield',
17
      '#title' => t('Directory'),
18
      '#description' => t('Enter the absolute directory on the web server to look for files. Subdirectories inside this directory will not be scanned.'),
19
      '#required' => TRUE,
20
    );
21

    
22
    $form['to_directory'] = array(
23
      '#type' => 'textfield',
24
      '#title' => t('To Directory'),
25
      '#description' => t('Enter the subdirectory of /sites/default/files where files will be copied. If empty the default directory will be use.'),
26
      '#required' => FALSE,
27
    );
28

    
29
    $form['pattern'] = array(
30
      '#type' => 'textarea',
31
      '#title' => t('Pattern'),
32
      '#description' => t("Only files matching these patterns will be imported. Enter one pattern per line. The '*' character is a wildcard. Example patterns are %png_example to import all PNG files.", array('%png_example' => '*.png')),
33
      '#default_value' => '*',
34
      '#required' => TRUE,
35
    );
36

    
37
    $form['actions'] = array('#type' => 'actions');
38
    $form['actions']['submit'] = array(
39
      '#type' => 'submit',
40
      '#value' => t('Preview'),
41
    );
42
    $form['actions']['cancel'] = array(
43
      '#type' => 'link',
44
      '#title' => t('Cancel'),
45
      '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/content/file',
46
    );
47
  }
48
  else {
49
    $form['preview'] = array(
50
      '#markup' => theme('item_list', array('items' => $form_state['storage']['files'])),
51
    );
52

    
53
    $form = confirm_form($form, t('Import these files?'), 'admin/content/file/import');
54
  }
55
  return $form;
56

    
57
}
58

    
59
/**
60
 * Validate handler for media_import().
61
 */
62
function media_bulk_upload_import_validate($form, &$form_state) {
63
  if ($form_state['values']['op'] != t('Confirm')) {
64
    $directory = $form_state['values']['directory'];
65
    $pattern = $form_state['values']['pattern'];
66
    if (!is_dir($directory)) {
67
      form_set_error('directory', t('The provided directory does not exist.'));
68
    }
69
    if (!is_readable($directory)) {
70
      form_set_error('directory', t('The provided directory is not readable.'));
71
    }
72

    
73
    $pattern_quoted = preg_quote($pattern, '/');
74
    $pattern_quoted = preg_replace('/(\r\n?|\n)/', '|', $pattern_quoted);
75
    $pattern_quoted = strtr($pattern_quoted, array(
76
      '\\|' => '|',
77
      '\\*' => '.*',
78
      '\\?' => '.?',
79
    ));
80
    $files = file_scan_directory($directory, '/^(' . $pattern_quoted . ')$/', array('recurse' => FALSE));
81
    $files = array_keys($files);
82
    if (empty($files)) {
83
      form_set_error('pattern', t('No files were found in %directory matching the regular expression %pattern', array('%directory' => $directory, '%pattern' => $pattern_quoted)));
84
    }
85
    $form_state['storage']['files'] = $files;
86
    $form_state['storage']['to_directory'] =  $form_state['values']['to_directory'];
87
  }
88
}
89

    
90
/**
91
 * Submit handler for media_import().
92
 */
93
function media_bulk_upload_import_submit($form, &$form_state) {
94
  if ($form_state['values']['op'] == t('Confirm')) {
95
    $files = $form_state['storage']['files'];
96
    $params = array();
97
    $params['to_directory'] = $form_state['storage']['to_directory'];
98
    $batch = array(
99
      'title' => t('Importing'),
100
      'operations' => array(
101
        array('media_bulk_upload_import_batch_import_files', array($files, $params)),
102
      ),
103
      'finished' => 'media_bulk_upload_import_batch_import_complete',
104
      'file' => drupal_get_path('module', 'media_bulk_upload') . '/includes/media_bulk_upload.admin.inc',
105
    );
106
    batch_set($batch);
107
    return;
108

    
109
  }
110
  $form_state['rebuild'] = TRUE;
111
}
112

    
113
/**
114
 * BatchAPI callback op for media import.
115
 */
116
function media_bulk_upload_import_batch_import_files($files, $params, &$context) {
117
  if (!isset($context['sandbox']['files'])) {
118
    // This runs the first time the batch runs.
119
    // This is stupid, but otherwise, I don't think it will work...
120
    $context['results'] = array('success' => array(), 'errors' => array());
121
    $context['sandbox']['max'] = count($files);
122
    $context['sandbox']['files'] = $files;
123
  }
124
  $files =& $context['sandbox']['files'];
125

    
126
  // Take a cut of files.  Let's do 10 at a time.
127
  $import_batch_size = variable_get('media_bulk_upload_import_batch_size', 20);
128
  $length = (count($files) > $import_batch_size) ? $import_batch_size : count($files);
129
  $to_process = array_splice($files, 0, $length);
130
  $image_in_message = '';
131

    
132
  foreach ($to_process as $file) {
133
    try {
134
      $file_obj = media_parse_to_file($file, $params);
135
      $context['results']['success'][] = $file;
136
      if (!$image_in_message) {
137
        // @todo Is this load step really necessary? When there's time, test
138
        //   this, and either remove it, or comment why it's needed.
139
        $loaded_file = file_load($file_obj->fid);
140
        $image_in_message = file_view_file($loaded_file, 'preview');
141
      }
142
    }
143
    catch (Exception $e) {
144
      $context['results']['errors'][] = $file . " Reason: " . $e->getMessage();
145
    }
146
  }
147

    
148
  $context['message'] = "Importing " . theme('item_list', array('items' => $to_process));
149
  // Show the image that is being imported.
150
  $context['message'] .= drupal_render($image_in_message);
151

    
152
  $context['finished'] = ($context['sandbox']['max'] - count($files)) / $context['sandbox']['max'];
153
}
154

    
155
/**
156
 * BatchAPI complete callback for media import.
157
 */
158
function media_bulk_upload_import_batch_import_complete($success, $results, $operations) {
159
  if ($results['errors']) {
160
    drupal_set_message(theme('item_list', array('items' => $results['errors'])), 'error');
161
  }
162
  if ($results['success']) {
163
    drupal_set_message(theme('item_list', array('items' => $results['success'])));
164
  }
165
}