1 |
ca0757b9
|
Assos Assos
|
<?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['pattern'] = array(
|
23 |
|
|
'#type' => 'textarea',
|
24 |
|
|
'#title' => t('Pattern'),
|
25 |
|
|
'#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')),
|
26 |
|
|
'#default_value' => '*',
|
27 |
|
|
'#required' => TRUE,
|
28 |
|
|
);
|
29 |
|
|
|
30 |
|
|
$form['actions'] = array('#type' => 'actions');
|
31 |
|
|
$form['actions']['submit'] = array(
|
32 |
|
|
'#type' => 'submit',
|
33 |
|
|
'#value' => t('Preview'),
|
34 |
|
|
);
|
35 |
|
|
$form['actions']['cancel'] = array(
|
36 |
|
|
'#type' => 'link',
|
37 |
|
|
'#title' => t('Cancel'),
|
38 |
|
|
'#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/content/file',
|
39 |
|
|
);
|
40 |
|
|
}
|
41 |
|
|
else {
|
42 |
|
|
$form['preview'] = array(
|
43 |
|
|
'#markup' => theme('item_list', array('items' => $form_state['storage']['files'])),
|
44 |
|
|
);
|
45 |
|
|
|
46 |
|
|
$form = confirm_form($form, t('Import these files?'), 'admin/content/file/import');
|
47 |
|
|
}
|
48 |
|
|
return $form;
|
49 |
|
|
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* Validate handler for media_import().
|
54 |
|
|
*/
|
55 |
|
|
function media_bulk_upload_import_validate($form, &$form_state) {
|
56 |
|
|
if ($form_state['values']['op'] != t('Confirm')) {
|
57 |
|
|
$directory = $form_state['values']['directory'];
|
58 |
|
|
$pattern = $form_state['values']['pattern'];
|
59 |
|
|
if (!is_dir($directory)) {
|
60 |
|
|
form_set_error('directory', t('The provided directory does not exist.'));
|
61 |
|
|
}
|
62 |
|
|
if (!is_readable($directory)) {
|
63 |
|
|
form_set_error('directory', t('The provided directory is not readable.'));
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
$pattern_quoted = preg_quote($pattern, '/');
|
67 |
|
|
$pattern_quoted = preg_replace('/(\r\n?|\n)/', '|', $pattern_quoted);
|
68 |
|
|
$pattern_quoted = strtr($pattern_quoted, array(
|
69 |
|
|
'\\|' => '|',
|
70 |
|
|
'\\*' => '.*',
|
71 |
|
|
'\\?' => '.?',
|
72 |
|
|
));
|
73 |
|
|
$files = file_scan_directory($directory, '/^(' . $pattern_quoted . ')$/', array('recurse' => FALSE));
|
74 |
|
|
$files = array_keys($files);
|
75 |
|
|
if (empty($files)) {
|
76 |
|
|
form_set_error('pattern', t('No files were found in %directory matching the regular expression %pattern', array('%directory' => $directory, '%pattern' => $pattern_quoted)));
|
77 |
|
|
}
|
78 |
|
|
$form_state['storage']['files'] = $files;
|
79 |
|
|
}
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Submit handler for media_import().
|
84 |
|
|
*/
|
85 |
|
|
function media_bulk_upload_import_submit($form, &$form_state) {
|
86 |
|
|
if ($form_state['values']['op'] == t('Confirm')) {
|
87 |
|
|
$files = $form_state['storage']['files'];
|
88 |
|
|
$batch = array(
|
89 |
|
|
'title' => t('Importing'),
|
90 |
|
|
'operations' => array(
|
91 |
|
|
array('media_bulk_upload_import_batch_import_files', array($files)),
|
92 |
|
|
),
|
93 |
|
|
'finished' => 'media_bulk_upload_import_batch_import_complete',
|
94 |
|
|
'file' => drupal_get_path('module', 'media_bulk_upload') . '/includes/media_bulk_upload.admin.inc',
|
95 |
|
|
);
|
96 |
|
|
batch_set($batch);
|
97 |
|
|
return;
|
98 |
|
|
|
99 |
|
|
}
|
100 |
|
|
$form_state['rebuild'] = TRUE;
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/**
|
104 |
|
|
* BatchAPI callback op for media import.
|
105 |
|
|
*/
|
106 |
|
|
function media_bulk_upload_import_batch_import_files($files, &$context) {
|
107 |
|
|
if (!isset($context['sandbox']['files'])) {
|
108 |
|
|
// This runs the first time the batch runs.
|
109 |
|
|
// This is stupid, but otherwise, I don't think it will work...
|
110 |
|
|
$context['results'] = array('success' => array(), 'errors' => array());
|
111 |
|
|
$context['sandbox']['max'] = count($files);
|
112 |
|
|
$context['sandbox']['files'] = $files;
|
113 |
|
|
}
|
114 |
|
|
$files =& $context['sandbox']['files'];
|
115 |
|
|
|
116 |
|
|
// Take a cut of files. Let's do 10 at a time.
|
117 |
|
|
$import_batch_size = variable_get('media_bulk_upload_import_batch_size', 20);
|
118 |
|
|
$length = (count($files) > $import_batch_size) ? $import_batch_size : count($files);
|
119 |
|
|
$to_process = array_splice($files, 0, $length);
|
120 |
|
|
$image_in_message = '';
|
121 |
|
|
|
122 |
|
|
foreach ($to_process as $file) {
|
123 |
|
|
try {
|
124 |
|
|
$file_obj = media_parse_to_file($file);
|
125 |
|
|
$context['results']['success'][] = $file;
|
126 |
|
|
if (!$image_in_message) {
|
127 |
|
|
// @todo Is this load step really necessary? When there's time, test
|
128 |
|
|
// this, and either remove it, or comment why it's needed.
|
129 |
|
|
$loaded_file = file_load($file_obj->fid);
|
130 |
|
|
$image_in_message = file_view_file($loaded_file, 'preview');
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
catch (Exception $e) {
|
134 |
|
|
$context['results']['errors'][] = $file . " Reason: " . $e->getMessage();
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
$context['message'] = "Importing " . theme('item_list', array('items' => $to_process));
|
139 |
|
|
// Show the image that is being imported.
|
140 |
|
|
$context['message'] .= drupal_render($image_in_message);
|
141 |
|
|
|
142 |
|
|
$context['finished'] = ($context['sandbox']['max'] - count($files)) / $context['sandbox']['max'];
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/**
|
146 |
|
|
* BatchAPI complete callback for media import.
|
147 |
|
|
*/
|
148 |
|
|
function media_bulk_upload_import_batch_import_complete($success, $results, $operations) {
|
149 |
|
|
if ($results['errors']) {
|
150 |
|
|
drupal_set_message(theme('item_list', array('items' => $results['errors'])), 'error');
|
151 |
|
|
}
|
152 |
|
|
if ($results['success']) {
|
153 |
|
|
drupal_set_message(theme('item_list', array('items' => $results['success'])));
|
154 |
|
|
}
|
155 |
|
|
} |