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