#447402 by drewish: Not removing derivates for deleted filefield images.
[project/imagecache.git] / imagecache_ui.pages.inc
CommitLineData
a699da53 1<?php
2// $Id$
3
4/**
5 * Preset Admin callbacks and required functions.
6 */
7function imagecache_ui_preset_overview() {
8 $header = array(t('Preset Name'), t('Storage'), t('Actions'));
9 $rows = array();
10 // Always clear the preset cache on this display.
11 foreach (imagecache_presets(TRUE) as $preset) {
12 $row = array();
13 $row[] = l($preset['presetname'], 'admin/build/imagecache/'. $preset['presetid']);
14 $links = array();
15 switch ($preset['storage']) {
16 case IMAGECACHE_STORAGE_DEFAULT:
17 $row[] = t('Default');
18 $links[] = l(t('View'), 'admin/build/imagecache/'. $preset['presetid']);
19 $links[] = l(t('Flush'), 'admin/build/imagecache/'. $preset['presetid'] .'/flush' );
20 break;
21 case IMAGECACHE_STORAGE_OVERRIDE:
22 $row[] = t('Override');
23 $links[] = l(t('Edit'), 'admin/build/imagecache/'. $preset['presetid']);
24 $links[] = l(t('Revert'), 'admin/build/imagecache/'. $preset['presetid'] .'/delete');
25 $links[] = l(t('Flush'), 'admin/build/imagecache/'. $preset['presetid'] .'/flush' );
26 $links[] = l(t('Export'), 'admin/build/imagecache/'. $preset['presetid'] .'/export' );
27 break;
28 case IMAGECACHE_STORAGE_NORMAL:
29 $row[] = t('Normal');
30 $links[] = l(t('Edit'), 'admin/build/imagecache/'. $preset['presetid']);
31 $links[] = l(t('Delete'), 'admin/build/imagecache/'. $preset['presetid'] .'/delete');
32 $links[] = l(t('Flush'), 'admin/build/imagecache/'. $preset['presetid'] .'/flush' );
33 $links[] = l(t('Export'), 'admin/build/imagecache/'. $preset['presetid'] .'/export' );
34 break;
35 }
36 $row[] = implode('&nbsp;&nbsp;&nbsp;&nbsp;', $links);
37 $rows[] = $row;
38 }
39 $output = theme('table', $header, $rows);
40 return $output;
41}
42
43
44function imagecache_ui_preset_add_form($form_state) {
45 $form = array();
46 $form['presetname'] = array(
47 '#type' => 'textfield',
48 '#size' => '64',
49 '#title' => t('Preset Namespace'),
50 '#default_value' => '',
51 '#description' => t('The namespace is used in URLs for images to tell imagecache how to process an image. Please only use alphanumeric characters, underscores (_), and hyphens (-) for preset names.'),
52 '#required' => TRUE,
53 );
54 $form['submit'] = array(
55 '#type' => 'submit',
56 '#value' => t('Create New Preset'),
57 );
58 return $form;
59}
60
61function imagecache_ui_preset_add_form_validate($form, &$form_state) {
62 $values = $form_state['values'];
63 // Check for duplicates
64 foreach (imagecache_presets() as $preset) {
65 if ($values['presetname'] == $preset['presetname']) {
66 form_set_error('presetname', t('The namespace you have chosen is already in use.'));
67 break;
68 }
69 }
70
71 // Check for illegal characters in preset names
72 if (preg_match('/[^0-9a-zA-Z_\-]/', $values['presetname'])) {
73 form_set_error('presetname', t('Please only use alphanumeric characters, underscores (_), and hyphens (-) for preset names.'));
74 }
75}
76
77function imagecache_ui_preset_add_form_submit($form, &$form_state) {
78 $preset = array('presetname' => $form_state['values']['presetname']);
79 $preset = imagecache_preset_save($preset);
80 drupal_set_message(t('Preset %name (ID: @id) was created.', array('%name' => $preset['presetname'], '@id' => $preset['presetid'])));
81 $form_state['redirect'] = 'admin/build/imagecache/'. $preset['presetid'];
82}
83
84
85function imagecache_ui_preset_delete_form($form_state, $preset = array()) {
86 if (empty($preset)) {
87 drupal_set_message(t('The specified preset was not found'), 'error');
88 drupal_goto('admin/build/imagecache');
89 }
90
91 $form = array();
92 $form['presetid'] = array('#type' => 'value', '#value' => $preset['presetid']);
93 return confirm_form(
94 $form,
95 t('Are you sure you want to delete the preset %preset?',
96 array('%preset' => $preset['presetname'])
97 ),
98 'admin/build/imagecache',
99 t('This action cannot be undone.'),
100 t('Delete'), t('Cancel')
101 );
102}
103
104function imagecache_ui_preset_delete_form_submit($form, &$form_state) {
105 $preset = imagecache_preset($form_state['values']['presetid']);
106 imagecache_preset_delete($preset);
107 drupal_set_message(t('Preset %name (ID: @id) was deleted.', array('%name' => $preset['presetname'], '@id' => $preset['presetid'])));
108 $form_state['redirect'] = 'admin/build/imagecache';
109}
110
111
112function imagecache_ui_preset_flush_form(&$form_state, $preset = array()) {
113 if (empty($preset)) {
114 drupal_set_message(t('The specified preset was not found'), 'error');
115 $form_state['redirect'] = 'admin/build/imagecache';
116 }
117
118 $form = array();
119 $form['presetid'] = array('#type' => 'value', '#value' => $preset['presetid']);
120 return confirm_form(
121 $form,
122 t('Are you sure you want to flush the preset %preset?',
123 array('%preset' => $preset['presetname'])
124 ),
125 'admin/build/imagecache',
126 t('This action cannot be undone.'),
127 t('Flush'), t('Cancel')
128 );
129}
130
131function imagecache_ui_preset_flush_form_submit($form, &$form_state) {
132 $preset = imagecache_preset($form_state['values']['presetid']);
133 imagecache_preset_flush($preset);
134 drupal_set_message(t('Preset %name (ID: @id) was flushed.', array('%name' => $preset['presetname'], '@id' => $preset['presetid'])));
135 $form_state['redirect'] = 'admin/build/imagecache';
136}
137
138
139/**
140 * Imagecache preset export form.
141 */
142function imagecache_ui_preset_export_form(&$form_state, $preset = array()) {
143 if (empty($preset)) {
144 drupal_set_message(t('The specified preset was not found'), 'error');
145 $form_state['redirect'] = 'admin/build/imagecache';
146 }
147
148 if (isset($preset['presetid'])) {
149 unset($preset['presetid']);
150 }
151 if (isset($preset['storage'])) {
152 unset($preset['storage']);
153 }
154 foreach ($preset['actions'] as $id => $action) {
155 unset($preset['actions'][$id]['actionid']);
156 unset($preset['actions'][$id]['presetid']);
157 }
158 $exported = '$presets = array();';
159 $exported .= "\n";
160 $exported .= '$presets[\''. $preset['presetname'] .'\'] = ';
161 $exported .= var_export($preset, TRUE) .';';
162 $rows = substr_count($exported, "\n") + 1;
163
164 $form = array();
165 $form['export'] = array(
166 '#type' => 'textarea',
167 '#default_value' => $exported,
168 '#rows' => $rows,
169 '#resizable' => FALSE,
170 );
171 return $form;
172}
173
174
175function imagecache_ui_preset_form($form_state, $preset = array()) {
176 if (empty($preset)) {
177 drupal_set_message(t('The specified preset was not found.'), 'error');
178 drupal_goto('admin/build/imagecache');
179 }
180
181 $form = array();
182 // @TODO: for some reason, simply setting '#disabled' on the normal presetname
183 // textfield fails to pass the values successfully to the submit handler.
184 if ($preset['storage'] === IMAGECACHE_STORAGE_DEFAULT) {
185 $form['presetname'] = array(
186 '#type' => 'value',
187 '#value' => $preset['presetname'],
188 );
189 $form['presetname_display'] = array(
190 '#type' => 'textfield',
191 '#size' => '64',
192 '#title' => t('Preset Namespace'),
193 '#default_value' => $preset['presetname'],
194 '#disabled' => TRUE,
195 );
196 }
197 else {
198 $form['presetname'] = array(
199 '#type' => 'textfield',
200 '#size' => '64',
201 '#title' => t('Preset Namespace'),
202 '#default_value' => $preset['presetname'],
203 '#description' => t('The namespace is used in URL\'s for images to tell imagecache how to process an image. Please only use alphanumeric characters, underscores (_), and hyphens (-) for preset names.'),
204 '#validate' => array('imagecache_element_presetname_validate' => array()),
205 );
206 }
207
208 $form['presetid'] = array(
209 '#type' => 'value',
210 '#value' => $preset['presetid'],
211 );
212
213 $form['actions'] = array(
214 '#type' => 'fieldset',
215 '#title' => t('Actions'),
216 '#tree' => TRUE,
217 '#theme' => 'imagecache_ui_preset_actions',
218 );
219
220 foreach ($preset['actions'] as $i => $action) {
221 // skip unknown actions...
222 if (!$definition = imagecache_action_definition($action['action'])) {
223 continue;
224 }
225 $action_name = t($definition['name']);
226 $action_form['name'] = array(
227 '#value' => $action_name,
228 );
229
230 $action_form['action'] = array(
231 '#type' => 'value',
232 '#value' => $action['action'],
233 );
234 $action_form['actionid'] = array(
235 '#type' => 'value',
236 '#value' => $action['actionid'],
237 );
238 $action_form['presetid'] = array(
239 '#type' => 'value',
240 '#value' => $action['presetid'],
241 );
242
243 $action_form['settings'] = array(
244 '#theme' => $action['action'],
245 '#value' => $action['data'],
246 );
247 $action_form['data'] = array(
248 '#type' => 'value',
249 '#value' => $action['data'],
250 );
251 $action_form['weight'] = array(
252 '#type' => 'weight',
253 '#default_value' => $action['weight'],
254 '#access' => $preset['storage'] !== IMAGECACHE_STORAGE_DEFAULT,
255 );
256 $action_form['configure'] = array(
257 '#value' => l(t('Configure'), 'admin/build/imagecache/'. $action['presetid'] .'/'. $action['actionid'] ),
258 '#access' => $preset['storage'] !== IMAGECACHE_STORAGE_DEFAULT,
259 );
260 $action_form['remove'] = array(
261 '#value' => l(t('Delete'), 'admin/build/imagecache/'. $action['presetid'] .'/'. $action['actionid'] .'/delete'),
262 '#access' => $preset['storage'] !== IMAGECACHE_STORAGE_DEFAULT,
263 );
264 $form['actions'][$i] = $action_form;
265 }
266
267 $form['actions']['new'] = array(
268 '#tree' => FALSE,
269 '#type' => 'fieldset',
270 '#title' => t('New Actions'),
271 '#collapsible' => TRUE,
272 '#collapsed' => count($preset['actions']) > 0,
273 '#access' => $preset['storage'] !== IMAGECACHE_STORAGE_DEFAULT,
274 );
275
276
277 foreach (imagecache_action_definitions() as $action => $definition) {
278 $form['actions']['new'][] = array(
279 '#type' => 'markup',
280 '#prefix' => '<div>',
281 '#suffix' => '</div>',
282 '#value' => l(t('Add !action', array('!action' => $definition['name'])),
283 'admin/build/imagecache/'. $preset['presetid'] .'/add/'. $action) .
284 ' - '. $definition['description'],
285 );
286 }
287
288/**
289 @todo: 404/403 image per preset.
290 @todo: global 404/403 image.
291
292 $form['files'] = array(
293 '#type' => 'fieldset',
294 '#collapsible' => TRUE,
295 '#collapsed' => TRUE,
296 '#title' => t('Error Files'),
297 );
298
299 $form['files']['403']['file'] = array(
300 '#type' => 'file',
301 '#title' => t('403 Image'),
302 '#description' => t('Image that will be used when access is denied to the source image.'),
303 );
304
305 $path403 = imagecache/'. $preset['presetname'] .'/403.png';
306 if (file_exists($path403)) {
307 $url403 = imagecache_create_url($preset['presetname'], $path403);
308
309 $form['files']['403']['view'] = array(
310 '#value' => '<img src="'. $url403 .'">',
311 );
312 }
313
314 $form['files']['404'] = array(
315 '#type' => 'file',
316 '#title' => t('404 Image'),
317 '#description' => t('Image that will be used when the source image cannot be found.'),
318 );
319 */
320
321 $form['submit'] = array(
322 '#type' => 'submit',
323 '#value' => $preset['storage'] === IMAGECACHE_STORAGE_DEFAULT ? t('Override Defaults') : t('Update Preset'),
324 );
325
326 // Display a preview image for this action. Put it below the submit button so
327 // users don't have to do a bunch of scrolling.
328 $preview_path = file_create_path('imagecache_sample.png');
329 if (!file_exists($preview_path)) {
330 // Copy of the image into the files directory so rather than generating it
331 // from the original so we don't end up creating subdirectories mirroring
332 // the path to the this module.
333 $preview_path = drupal_get_path('module', 'imagecache_ui') .'/sample.png';
334 file_copy($preview_path, 'imagecache_sample.png');
335 }
336 imagecache_image_flush($preview_path);
d13572ff 337 $imagecache_path = imagecache_create_url($preset['presetname'], $preview_path, TRUE);
a699da53 338 $form['preview'] = array(
339 '#type' => 'item',
340 '#title' => t('Preview'),
341 '#value' => l($imagecache_path, $imagecache_path) .'<br />'. l("<img src='$imagecache_path' />", $imagecache_path, array('html' => TRUE)),
342 );
343
344 return $form;
345}
346
347function imagecache_ui_preset_form_submit($form, &$form_state) {
348 // Save the preset first to retrieve the presetid when overriding
349 $preset = imagecache_preset_save($form_state['values']);
350
351 // Populate the presetid as needed for overrides
352 if (isset($form_state['values']['actions'])) {
353 foreach($form_state['values']['actions'] as $action) {
354 if (empty($action['presetid'])) {
355 $action['presetid'] = $preset['presetid'];
356 }
357 imagecache_action_save($action);
358 }
359 }
360
361 // Push back to the preset form
362 $form_state['redirect'] = 'admin/build/imagecache/'. $preset['presetid'];
363
364 // Clear preset cache
365 imagecache_presets(TRUE);
366}
367
368function theme_imagecache_ui_preset_actions($form) {
369 $header = array(t('Action'), t('Settings'));
370 // $header = array(, t('Weight'), '','');
371 $rows = array();
372 foreach(element_children($form) as $key) {
373 if (!is_numeric($key)) {
374 continue;
375 }
376 $row = array();
377 $row[] = drupal_render($form[$key]['name']);
378 $row[] = drupal_render($form[$key]['settings']);
379
380 // Check for form access before rendering these portions of the table / header
381 if (!empty($form[$key]['weight']['#access'])) {
382 if (empty($header['weight'])) {
383 $header['weight'] = t('Weight');
384 }
385 $form[$key]['weight']['#attributes']['class'] = 'imagecache-action-order-weight';
386 $row[] = drupal_render($form[$key]['weight']);
387 }
388 if (!empty($form[$key]['configure']['#access'])) {
389 if (empty($header['configure'])) {
390 $header['configure'] = '';
391 }
392 $row[] = drupal_render($form[$key]['configure']);
393 }
394 if (!empty($form[$key]['remove']['#access'])) {
395 if (empty($header['remove'])) {
396 $header['remove'] = '';
397 }
398 $row[] = drupal_render($form[$key]['remove']);
399 }
400 $rows[] = array(
401 'data' => $row,
402 'class' => 'draggable',
403 );
404 }
405 $output = theme('table', $header, $rows, array('id' => 'imagecache-preset-actions'));
406 drupal_add_tabledrag('imagecache-preset-actions', 'order', 'sibling', 'imagecache-action-order-weight');
407 $output .= drupal_render($form);
408 return $output;
409}
410
411
412function imagecache_ui_action_form($form_state, $preset, $action) {
413 $definitions = imagecache_action_definitions();
414
415 if (empty($action)) {
416 drupal_set_message(t('Unknown Action.'), 'error');
417 drupal_goto('admin/build/imagecache');
418 }
419
420 if (empty($preset)) {
421 drupal_set_message(t('Unknown Preset.'), 'error');
422 drupal_goto('admin/build/imagecache');
423 }
424
425 $form = array(
426 '#tree' => TRUE,
427 );
428
429 $form['actionid'] = array(
430 '#type' => 'value',
431 '#value' => $action['actionid'],
432 );
433
434
435 if (!empty($definitions[$action['action']]['file'])) {
436 require_once($definitions[$action['action']]['file']);
437 }
438 $form['data'] = call_user_func($action['action'] .'_form', $action['data']);
439 $form['submit'] = array(
440 '#type' => 'submit',
441 '#value' => t('Update Action'),
442 );
443 return $form;
444}
445
446function imagecache_ui_action_form_submit($form, &$form_state) {
447 if ($action = imagecache_action($form_state['values']['actionid'])) {
448 $action = array_merge($action, $form_state['values']);
449 imagecache_action_save($action);
450 drupal_set_message(t('The action was succesfully updated.'));
451 $form_state['redirect'] = 'admin/build/imagecache/'. $action['presetid'];
452 }
453 else {
454 drupal_set_message(t('Unknown Action: @action_id', array('@action_id' => $form_state['values']['actionid'])));
455 $form_state['redirect'] = 'admin/build/imagecache';
456 }
457}
458
459
460function imagecache_ui_action_delete_form($form_state, $preset = array(), $action = array()) {
461 if (empty($action)) {
462 drupal_set_message(t('Unknown Action.'), 'error');
463 drupal_goto('admin/build/imagecache');
464 }
465 if (empty($preset)) {
466 drupal_set_message(t('Unknown Preset.'), 'error');
467 drupal_goto('admin/build/imagecache');
468 }
469
470 $form = array();
471 $form['actionid'] = array('#type' => 'value', '#value' => $action['actionid']);
472 return confirm_form(
473 $form,
474 t('Are you sure you want to delete the !action action from preset !preset?',
475 array('!preset' => $preset['presetname'], '!action' => $action['name'])
476 ),
477 'admin/build/imagecache',
478 t('This action cannot be undone.'),
479 t('Delete'), t('Cancel')
480 );
481}
482
483function imagecache_ui_action_delete_form_submit($form, &$form_state) {
484 $action = imagecache_action($form_state['values']['actionid']);
485 imagecache_action_delete($action);
486 drupal_set_message(t('The action has been deleted.'));
487 $form_state['redirect'] = 'admin/build/imagecache/'. $action['presetid'];
488}
489
490
491function imagecache_ui_action_add_form($form_state, $preset, $actionname) {
492 $definition = imagecache_action_definition($actionname);
493
494 $form = array(
495 '#tree' => TRUE,
496 );
497 $form['action'] = array(
498 '#type' => 'value',
499 '#value' => $actionname,
500 );
501 $form['presetid'] = array(
502 '#type' => 'value',
503 '#value' => $preset['presetid'],
504 );
505 $form['weight'] = array(
506 '#type' => 'weight',
507 '#title' => t('Weight'),
508 );
509
510 $form['data'] = call_user_func($actionname .'_form', array());
511 $form['submit'] = array(
512 '#type' => 'submit',
513 '#value' => t('Add Action'),
514 );
515 return $form;
516}
517
518function imagecache_ui_action_add_form_submit($form, &$form_state) {
519 imagecache_action_save($form_state['values']);
520 $form_state['redirect'] = 'admin/build/imagecache/'. $form_state['values']['presetid'];
521}