Projet

Général

Profil

Révision ca0757b9

Ajouté par Assos Assos il y a plus de 9 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/media/includes/media.admin.inc
2 2

  
3 3
/**
4 4
 * @file
5
 * This file contains the admin functions for the Media module.
5
 * Administration page callbacks for the Media module.
6 6
 */
7 7

  
8 8
/**
9
 * Include media.pages.inc since it has some form definitions we will use.
10
 */
11
require_once dirname(__FILE__) . '/media.pages.inc';
12

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

  
27
    $form['pattern'] = array(
28
      '#type' => 'textarea',
29
      '#title' => t('Pattern'),
30
      '#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')),
31
      '#default_value' => '*',
32
      '#required' => TRUE,
33
    );
34

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

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

  
55
}
56

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

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

  
87
/**
88
 * Submit handler for media_import().
89
 */
90
function media_import_submit($form, &$form_state) {
91
  if ($form_state['values']['op'] == t('Confirm')) {
92
    $files = $form_state['storage']['files'];
93
    $batch = array(
94
      'title' => t('Importing'),
95
      'operations' => array(
96
        array('media_import_batch_import_files', array($files)),
97
      ),
98
      'finished' => 'media_import_batch_import_complete',
99
      'file' => drupal_get_path('module', 'media') . '/includes/media.admin.inc',
100
    );
101
    batch_set($batch);
102
    return;
103

  
104
  }
105
  $form_state['rebuild'] = TRUE;
106
}
107

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

  
121
  // Take a cut of files.  Let's do 10 at a time.
122
  $import_batch_size = variable_get('media__import_batch_size', 20);
123
  $length = (count($files) > $import_batch_size) ? $import_batch_size : count($files);
124
  $to_process = array_splice($files, 0, $length);
125
  $image_in_message = '';
126

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

  
143
  $context['message'] = "Importing " . theme('item_list', array('items' => $to_process));
144
  // Show the image that is being imported.
145
  $context['message'] .= drupal_render($image_in_message);
146

  
147
  $context['finished'] = ($context['sandbox']['max'] - count($files)) / $context['sandbox']['max'];
148
}
149

  
150
/**
151
 * BatchAPI complete callback for media import.
152
 */
153
function media_import_batch_import_complete($success, $results, $operations) {
154
  if ($results['errors']) {
155
    drupal_set_message(theme('item_list', array('items' => $results['errors'])), 'error');
156
  }
157
  if ($results['success']) {
158
    drupal_set_message(theme('item_list', array('items' => $results['success'])));
159
  }
160
}
161

  
162
/**
163
 * Admin configruation form for media browser settings.
9
 * Displays the media administration page.
164 10
 */
165 11
function media_admin_config_browser($form, &$form_state) {
166 12
  $theme_options = array();
167
  $theme_options[NULL] = 'Default administration theme';
13
  $theme_options[NULL] = t('Default administration theme');
14

  
168 15
  foreach (list_themes() as $key => $theme) {
169 16
    if ($theme->status) {
170 17
      $theme_options[$key] = $theme->info['name'];
171 18
    }
172 19
  }
173 20

  
174
  $form['media__dialog_theme'] = array(
21
  $form['media_dialog_theme'] = array(
175 22
    '#type' => 'select',
176 23
    '#title' => t('Media browser theme'),
177 24
    '#options' => $theme_options,
178
    '#description' => t("This theme will be used for all media related dialogs.
179
      It can be different from your site's theme because many site themes do not
180
      work well in the small windows which media uses."),
181
    '#default_value' => variable_get('media__dialog_theme', ''),
25
    '#description' => t("This theme will be used for all media related dialogs. It can be different from your site's theme because many site themes do not work well in the small windows which media uses."),
26
    '#default_value' => variable_get('media_dialog_theme', ''),
182 27
  );
183 28

  
184
  // Additional configuration if the WYSIWYG module is enabled.
185
  $form['wysiwyg'] = array(
29
  $form['array_filter'] = array(
30
    '#type' => 'value',
31
    '#value' => TRUE,
32
  );
33

  
34
  $form['mediapopup'] = array(
186 35
    '#type' => 'fieldset',
187
    '#title' => t('WYSIWYG configuration'),
36
    '#title' => t('Media Popup'),
188 37
    '#collapsible' => TRUE,
189
    '#collapsed' => FALSE,
190
    '#access' => module_exists('wysiwyg'),
38
    '#collapsed' => TRUE,
191 39
  );
192
  $plugins = media_get_browser_plugin_info();
193
  $form['wysiwyg']['media__wysiwyg_browser_plugins'] = array(
194
    '#type' => 'checkboxes',
195
    '#title' => t('Enabled browser plugins'),
196
    '#options' => array(),
197
    '#required' => FALSE,
198
    '#default_value' => variable_get('media__wysiwyg_browser_plugins', array()),
199
    '#description' => t('If no plugins are selected, they will all be available.'),
40
  $form['mediapopup']['media_dialogclass'] = array(
41
    '#type' => 'textfield',
42
    '#title' => t('Dialog Class'),
43
    '#default_value' => variable_get('media_dialogclass', 'media-wrapper'),
44
    '#description' => t('The class used to identify the popup wrapper element.'),
200 45
  );
201
  foreach ($plugins as $key => $plugin) {
202
    $form['wysiwyg']['media__wysiwyg_browser_plugins']['#options'][$key] = !empty($plugin['title']) ? $plugin['title'] : $key;
203
  }
204
  $form['wysiwyg']['media__wysiwyg_upload_directory'] = array(
46
  $form['mediapopup']['media_modal'] = array(
47
    '#type' => 'select',
48
    '#title' => t('Modal'),
49
    '#options' => array(
50
      FALSE => t('False'),
51
      TRUE => t('True'),
52
    ),
53
    '#default_value' => variable_get('media_modal', TRUE),
54
    '#description' => t('Open as modal window.'),
55
  );
56
  $form['mediapopup']['media_draggable'] = array(
57
    '#type' => 'select',
58
    '#title' => t('Draggable'),
59
    '#options' => array(
60
      FALSE => t('False'),
61
      TRUE => t('True'),
62
    ),
63
    '#default_value' => variable_get('media_draggable', FALSE),
64
    '#description' => t('Draggable modal window.'),
65
  );
66
  $form['mediapopup']['media_resizable'] = array(
67
    '#type' => 'select',
68
    '#title' => t('Resizable'),
69
    '#options' => array(
70
      FALSE => t('False'),
71
      TRUE => t('True'),
72
    ),
73
    '#default_value' => variable_get('media_resizable', FALSE),
74
    '#description' => t('Resizable modal window.'),
75
  );
76
  $form['mediapopup']['media_minwidth'] = array(
205 77
    '#type' => 'textfield',
206
    '#title' => t("File directory for uploaded media"),
207
    '#default_value' => variable_get('media__wysiwyg_upload_directory', ''),
208
    '#description' => t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
78
    '#title' => t('Min Width'),
79
    '#default_value' => variable_get('media_minwidth', 500),
80
    '#description' => t('CSS property min-width.'),
209 81
  );
210

  
211
  if (module_exists('token')) {
212
    $form['wysiwyg']['media__wysiwyg_upload_directory']['#description'] .= t('This field supports tokens.');
213
    $form['wysiwyg']['tokens'] = array(
214
      '#theme' => 'token_tree',
215
      '#dialog' => TRUE,
216
    );
217
  }
218

  
219
  $form['wysiwyg']['media__wysiwyg_allowed_types'] = array(
220
    '#type' => 'checkboxes',
221
    '#title' => t('Allowed types in WYSIWYG'),
222
    '#options' => file_entity_type_get_names(),
223
    '#default_value' => variable_get('media__wysiwyg_allowed_types', array('audio', 'image', 'video', 'document')),
82
  $form['mediapopup']['media_width'] = array(
83
    '#type' => 'textfield',
84
    '#title' => t('Width'),
85
    '#default_value' => variable_get('media_width', 670),
86
    '#description' => t('CSS property width.'),
87
  );
88
  $form['mediapopup']['media_height'] = array(
89
    '#type' => 'textfield',
90
    '#title' => t('Height'),
91
    '#default_value' => variable_get('media_height', 280),
92
    '#description' => t('CSS property height.'),
93
  );
94
  $form['mediapopup']['media_position'] = array(
95
    '#type' => 'textfield',
96
    '#title' => t('Position'),
97
    '#default_value' => variable_get('media_position', 'center'),
98
    '#description' => t('CSS property position.'),
99
  );
100
  $form['mediapopup']['media_zindex'] = array(
101
    '#type' => 'textfield',
102
    '#title' => t('Z-Index'),
103
    '#default_value' => variable_get('media_zindex', 10000),
104
    '#description' => t('CSS property z-index.'),
105
  );
106
  $form['mediapopup']['overlay'] = array(
107
    '#type' => 'fieldset',
108
    '#title' => t('Overlay'),
109
  );
110
  $form['mediapopup']['overlay']['media_backgroundcolor'] = array(
111
    '#type' => 'textfield',
112
    '#title' => t('Background Color'),
113
    '#default_value' => variable_get('media_backgroundcolor', '#000000'),
114
    '#description' => t('CSS property background-color; used with overlay.'),
115
  );
116
  $form['mediapopup']['overlay']['media_opacity'] = array(
117
    '#type' => 'textfield',
118
    '#title' => t('Opacity'),
119
    '#default_value' => variable_get('media_opacity', 0.4),
120
    '#description' => t('CSS property opacity; used with overlay.'),
224 121
  );
225
  $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
226 122

  
227 123
  $form['#submit'][] = 'media_admin_config_browser_pre_submit';
124

  
228 125
  return system_settings_form($form);
229 126
}
230 127

  
231 128
/**
232
 * Manipulate values before form is submitted.
129
 * Form submission handler for media_admin_config_browser().
233 130
 */
234 131
function media_admin_config_browser_pre_submit(&$form, &$form_state) {
235
  if (!$form_state['values']['media__dialog_theme']) {
236
    variable_del('media__dialog_theme');
237
    unset($form_state['values']['media__dialog_theme']);
238
  }
239
  $wysiwyg_browser_plugins = array_unique(array_values($form_state['values']['media__wysiwyg_browser_plugins']));
240
  if (empty($wysiwyg_browser_plugins[0])) {
241
    variable_del('media__wysiwyg_browser_plugins');
242
    unset($form_state['values']['media__wysiwyg_browser_plugins']);
132
  if (!$form_state['values']['media_dialog_theme']) {
133
    variable_del('media_dialog_theme');
134
    unset($form_state['values']['media_dialog_theme']);
243 135
  }
244 136
}

Formats disponibles : Unified diff