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