Newer
Older
Darrel O'Pry
committed
<?php
// $Id$
/**
* Administrative interface to imagecache.
*
*/
function imagecache_ui_help($section) {
switch($section) {
case 'admin/build/imagecache': return t('Manage imagecache preset.');
}
}
function imagecache_ui_menu($may_cache) {
$items = array();
if ($may_cache) {
Darrel O'Pry
committed
$items[] = array(
'path' => 'admin/build/imagecache',
'title' => t('Imagecache Presets'),
'description' => t('Administer imagecache presets and actions.'),
'callback' => 'imagecache_ui_presets',
'access' => user_access('administer imagecache'),
);
$items[] = array(
'path' => 'admin/build/imagecache/list',
'title' => t('List'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[] = array(
'path' => 'admin/build/imagecache/add',
'title' => t('Add New Preset'),
'callback' => 'drupal_get_form',
'callback arguments' => array('imagecache_ui_preset_add_form'),
'access' => user_access('administer imagecache'),
'type' => MENU_LOCAL_TASK,
);
Darrel O'Pry
committed
}
// Use Dynamic menu items to get better breadcrumb trails by default.
Darrel O'Pry
committed
elseif (arg(0) == 'admin' && arg(1) == 'build' && arg(2) == 'imagecache' && arg(3) == 'preset') {
$preset = imagecache_preset(arg(4));
if (empty($preset)) {
return $items;
}
$t = array('!presetname' => $preset['presetname']);
$items[] = array(
'path' => 'admin/build/imagecache/preset/'. arg(4) .'/delete',
'title' => t('Delete Preset: !presetname', $t),
'callback' => 'drupal_get_form',
'callback arguments' => array('imagecache_ui_preset_delete_form', arg(4)),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/imagecache/preset/'. arg(4) .'/flush',
'title' => t('Flush Preset: !presetname', $t),
'callback' => 'drupal_get_form',
'callback arguments' => array('imagecache_ui_preset_flush_form', arg(4)),
'access' => user_access('flush imagecache'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/imagecache/preset/'. arg(4),
Darrel O'Pry
committed
'callback' => 'drupal_get_form',
'callback arguments' => array('imagecache_ui_preset_form', arg(4)),
'type' => MENU_CALLBACK,
);
/*
$items[] = array(
'path' => 'admin/build/imagecache/preset/'. arg(4) .'/action/add',
'title' => t('Add !action to !presetname', $t),
'callback' => 'imagecache_ui_action_add_list',
'callback arguments' => array(arg(4)),
'type' => MENU_CALLBACK,
);
*/
$definition = imagecache_action_definition(arg(7));
if (!empty($definition)) {
Darrel O'Pry
committed
$t['!action'] = $definition['name'];
$items[] = array(
'path' => 'admin/build/imagecache/preset/'. arg(4) .'/action/add/'. arg(7),
'title' => t('Add !action to !presetname', $t),
'callback' => 'drupal_get_form',
'callback arguments' => array('imagecache_ui_action_add_form', arg(4), arg(7)),
'type' => MENU_CALLBACK,
);
}
$action = imagecache_action(arg(6));
if ($action) {
$t['!action'] = $action['name'];
Darrel O'Pry
committed
$items[] = array(
Darrel O'Pry
committed
'path' => 'admin/build/imagecache/preset/'. arg(4) .'/action/'. arg(6),
'title' => t('!action for preset !presetname', $t),
Darrel O'Pry
committed
'callback' => 'drupal_get_form',
Darrel O'Pry
committed
'callback arguments' => array('imagecache_ui_action_form', arg(6)),
'type' => MENU_CALLBACK,
Darrel O'Pry
committed
);
Darrel O'Pry
committed
$items[] = array(
'path' => 'admin/build/imagecache/preset/'. arg(4) .'/action/'. arg(6) .'/delete',
'title' => t('Delete !action for preset !presetname', $t),
Darrel O'Pry
committed
'callback' => 'drupal_get_form',
'callback arguments' => array('imagecache_ui_action_delete_form', arg(4), arg(6)),
Darrel O'Pry
committed
'type' => MENU_CALLBACK,
);
}
}
Darrel O'Pry
committed
return $items;
}
/**
Darrel O'Pry
committed
* Preset Admin callbacks and required functions.
Darrel O'Pry
committed
*/
function imagecache_ui_presets() {
Darrel O'Pry
committed
$header = array(t('Preset Name'), t('Actions'));
Darrel O'Pry
committed
$rows = array();
Darrel O'Pry
committed
foreach(imagecache_presets() as $preset) {
Darrel O'Pry
committed
$row = array();
Darrel O'Pry
committed
$row[] = l($preset['presetname'], 'admin/build/imagecache/preset/'. $preset['presetid']);
$links = array();
$links[] = l(t('edit'), 'admin/build/imagecache/preset/'. $preset['presetid']);
$links[] = l(t('remove'),'admin/build/imagecache/preset/'. $preset['presetid'] .'/delete');
Darrel O'Pry
committed
$links[] = l(t('flush'),'admin/build/imagecache/preset/'. $preset['presetid'] .'/flush' );
$row[] = implode (' ', $links);
Darrel O'Pry
committed
$rows[] = $row;
}
$output = theme('table', $header, $rows);
return $output;
}
function imagecache_ui_preset_add_form($presetid = 0) {
$form = array();
$form['presetname'] = array(
'#type' => 'textfield',
'#size' => '64',
'#title' => t('Preset Namespace'),
'#default_value' => '',
'#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.'),
'#validate' => array('imagecache_element_presetname_validate' => array()),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create New Preset'),
);
return $form;
}
Darrel O'Pry
committed
function imagecache_ui_preset_add_form_submit($id, $form_values) {
$preset = array('presetname' => $form_values['presetname']);
$preset = imagecache_preset_save($preset);
drupal_set_message(t('Preset "%name" (ID: @id) Created.', array('%name' => $preset['presetname'], '@id' => $preset['presetid'])));
Darrel O'Pry
committed
return 'admin/build/imagecache/preset/'. $preset['presetid'];
Darrel O'Pry
committed
}
function imagecache_element_presetname_validate($element) {
// Check for duplicates
$presets = imagecache_presets();
Darrel O'Pry
committed
if (in_array($element['#value'], $presets)) {
form_set_error($element['#name'], t('The namespace you have chosen is already in use.'));
}
// Check for illegal characters in preset names
if (preg_match('/[^0-9a-zA-Z_\-]/', $element['#value'])) {
form_set_error($element['#name'], t('Please only use alphanumic characters, underscores (_), and hyphens (-) for preset names.'));
}
}
Darrel O'Pry
committed
function imagecache_ui_preset_delete_form($presetid) {
$preset = imagecache_preset($presetid);
Darrel O'Pry
committed
Darrel O'Pry
committed
if (!$preset) {
drupal_set_message(t('The specified preset was not found'), 'error');
drupal_goto('admin/build/imagecache');
}
Darrel O'Pry
committed
$form = array();
Darrel O'Pry
committed
$form['presetid'] = array('#type' => 'value', '#value' => $preset['presetid']);
return confirm_form(
$form,
t('Are you sure you want to delete the preset %preset?',
array('%preset' => $preset['presetname'])
),
'admin/build/imagecache',
t('This action cannot be undone.'),
t('Delete'), t('Cancel')
Darrel O'Pry
committed
);
}
Darrel O'Pry
committed
function imagecache_ui_preset_delete_form_submit($form_id, $form_values) {
$preset = imagecache_preset($form_values['presetid']);
imagecache_preset_delete($preset);
drupal_set_message(t('Preset "%name" (ID: @id) deleted.', array('%name' => $preset['presetname'], '@id' => $preset['presetid'])));
return 'admin/build/imagecache';
Darrel O'Pry
committed
}
Darrel O'Pry
committed
function imagecache_ui_preset_flush_form($presetid) {
$preset = imagecache_preset($presetid);
if (!$preset) {
drupal_set_message(t('The specified preset was not found'), 'error');
drupal_goto('admin/build/imagecache');
Darrel O'Pry
committed
}
$form = array();
Darrel O'Pry
committed
$form['presetid'] = array('#type' => 'value', '#value' => $preset['presetid']);
return confirm_form(
$form,
t('Are you sure you want to flush the preset %preset?',
array('%preset' => $preset['presetname'])
),
'admin/build/imagecache',
t('This action cannot be undone.'),
t('Flush'), t('Cancel')
Darrel O'Pry
committed
);
Darrel O'Pry
committed
}
Darrel O'Pry
committed
Darrel O'Pry
committed
function imagecache_ui_preset_flush_form_submit($form_id, $form_values) {
$preset = imagecache_preset($form_values['presetid']);
imagecache_preset_flush($preset);
drupal_set_message(t('Preset "%name" (ID: @id) flushed.', array('%name' => $preset['presetname'], '@id' => $preset['presetid'])));
return 'admin/build/imagecache';
}
Darrel O'Pry
committed
Darrel O'Pry
committed
function imagecache_ui_preset_form($presetid) {
$preset = imagecache_preset($presetid, TRUE);
Darrel O'Pry
committed
Darrel O'Pry
committed
if (!$preset) {
drupal_set_message(t('The specified preset was not found'), 'error');
drupal_goto('admin/build/imagecache');
Darrel O'Pry
committed
}
$form = array();
Darrel O'Pry
committed
$form['presetname'] = array(
'#type' => 'textfield',
'#size' => '64',
'#title' => t('Preset Namespace'),
'#default_value' => $preset['presetname'],
'#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.'),
'#validate' => array('imagecache_element_presetname_validate' => array()),
);
$form['presetid'] = array(
'#type' => 'value',
'#value' => $preset['presetid'],
Darrel O'Pry
committed
);
Darrel O'Pry
committed
$form['actions'] = array(
'#type' => 'fieldset',
'#title' => t('Actions'),
'#tree' => TRUE,
'#theme' => 'imagecache_ui_preset_actions',
);
Darrel O'Pry
committed
Darrel O'Pry
committed
foreach($preset['actions'] as $i => $action) {
// skip unknown actions...
if(!$definition = imagecache_action_definition($action['action'])) {
Darrel O'Pry
committed
continue;
}
Darrel O'Pry
committed
$action_form['name'] = array(
'#value' => $action_name,
);
$action_form['action'] = array(
'#type' => 'value',
'#value' => $action['action'],
);
Darrel O'Pry
committed
$action_form['actionid'] = array(
'#type' => 'value',
'#value' => $action['actionid'],
);
$action_form['presetid'] = array(
'#type' => 'value',
'#value' => $action['presetid'],
);
Darrel O'Pry
committed
$action_form['settings'] = array(
'#theme' => $action['action'],
'#value' => $action['data'],
);
$action_form['data'] = array(
'#type' => 'value',
'#value' => $action['data'],
);
Darrel O'Pry
committed
$action_form['weight'] = array(
'#type' => 'weight',
Darrel O'Pry
committed
);
$action_form['configure'] = array(
'#value' => l(t('Configure'), 'admin/build/imagecache/preset/'. $action['presetid'] .'/action/'. $action['actionid'] ),
);
$action_form['remove'] = array(
'#value' => l(t('Delete'), 'admin/build/imagecache/preset/'. $action['presetid'] .'/action/'. $action['actionid'] .'/delete'),
Darrel O'Pry
committed
);
$form['actions'][$i] = $action_form;
}
$form['actions']['new'] = array(
'#tree' => FALSE,
'#type' => 'fieldset',
'#title' => t('New Actions'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
Darrel O'Pry
committed
);
Darrel O'Pry
committed
foreach(imagecache_action_definitions() as $action => $definition) {
Darrel O'Pry
committed
$form['actions']['new'][] = array(
'#type' => 'markup',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#value' => l(t('Add !action', array('!action' => $definition['name'])),
'admin/build/imagecache/preset/'. $preset['presetid'] .'/action/add/'. $action) .
Darrel O'Pry
committed
' - '. $definition['description'],
);
}
/**
@todo: 404/403 image per preset.
@todo: global 404/403 image.
$form['files'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Error Files'),
);
$form['files']['403']['file'] = array(
'#type' => 'file',
'#title' => t('403 Image'),
'#description' => t('Image that will be used when access is denied to the source image.'),
);
Darrel O'Pry
committed
$path403 = imagecache/'. $preset['presetname'] .'/403.png';
if (file_exists($path403)) {
$url403 = imagecache_create_url($preset['presetname'], $path403);
$form['files']['403']['view'] = array(
'#value' => '<img src="'. $url403 .'">',
);
}
$form['files']['404'] = array(
'#type' => 'file',
'#title' => t('404 Image'),
'#description' => t('Image that will be used when the source image cannot be found.'),
);
*/
Darrel O'Pry
committed
$form['submit'] = array(
'#type' => 'submit',
Darrel O'Pry
committed
'#value' => t('Update Preset'),
Darrel O'Pry
committed
);
return $form;
}
Darrel O'Pry
committed
function theme_imagecache_admin_title($element) {
return '<h2>'.$element['value'].'</h2>';
Darrel O'Pry
committed
}
Darrel O'Pry
committed
function theme_imagecache_ui_preset_actions($element) {
$header = array(t('Action'), t('Settings'), t('Weight'), '','');
$rows = array();
foreach(element_children($element) as $key) {
$row = array();
$row[] = drupal_render($element[$key]['name']);
$row[] = drupal_render($element[$key]['settings']);
$row[] = drupal_render($element[$key]['weight']);
$row[] = drupal_render($element[$key]['configure']);
$row[] = drupal_render($element[$key]['remove']);
$rows[] = $row;
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($element);
return $output;
Darrel O'Pry
committed
}
function imagecache_ui_preset_form_submit($form_id, $form_values) {
if (isset($form_values['actions'])) {
foreach($form_values['actions'] as $action) {
imagecache_action_save($action);
}
}
imagecache_preset_save($form_values);
return 'admin/build/imagecache/preset/'. $form_values['presetid'];
}
Darrel O'Pry
committed
Darrel O'Pry
committed
function imagecache_ui_action_form($actionid) {
$definitions = imagecache_action_definitions();
if (!$action = imagecache_action($actionid)) {
drupal_set_message('Unknown Action.'. $actionid, 'error');
drupal_goto('admin/build/imagecache');
}
if (!$preset = imagecache_preset($action['presetid'])) {
drupal_set_message('Unknown Preset.');
drupal_goto('admin/build/imagecache');
Darrel O'Pry
committed
}
Darrel O'Pry
committed
$form = array(
'#tree' => TRUE,
Darrel O'Pry
committed
);
Darrel O'Pry
committed
$form['actionid'] = array(
'#type' => 'value',
'#value' => $action['actionid'],
Darrel O'Pry
committed
);
Darrel O'Pry
committed
if ($definitions[$action['action']]['file']) {
require_once($definitions[$action['action']]['file']);
}
$form['data'] = call_user_func($action['action'] .'_form', $action['data']);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update Action'),
);
return $form;
Darrel O'Pry
committed
}
Darrel O'Pry
committed
function imagecache_ui_action_form_submit($form_id, $form_values) {
if ($action = imagecache_action($form_values['actionid'])) {
$action = array_merge($action, $form_values);
imagecache_action_save($action);
drupal_set_message('Action Updated');
return 'admin/build/imagecache/preset/'. $action['presetid'];
}
drupal_set_message('Unknown Action: '. $form_values['actionid']);
return 'admin/build/imagecache';
Darrel O'Pry
committed
}
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
function imagecache_ui_action_delete_form($presetid, $actionid) {
if (!$action = imagecache_action($actionid)) {
drupal_set_message('Unknown Action.'. $actionid, 'error');
drupal_goto('admin/build/imagecache');
}
if (!$preset = imagecache_preset($action['presetid'])) {
drupal_set_message('Unknown Preset.');
drupal_goto('admin/build/imagecache');
}
$form = array();
$form['actionid'] = array('#type' => 'value', '#value' => $action['actionid']);
return confirm_form(
$form,
t('Are you sure you want to delete the !action action from preset !preset?',
array('!preset' => $preset['presetname'], '!action' => $action['name'])
),
'admin/build/imagecache',
t('This action cannot be undone.'),
t('Delete'), t('Cancel')
);
}
function imagecache_ui_action_delete_form_submit($form_id, $form_values) {
$action = imagecache_action($form_values['actionid']);
imagecache_action_delete($action);
drupal_set_message(t('The action has been deleted.'));
return 'admin/build/imagecache/preset/'. $action['presetid'];
}
function imagecache_ui_action_add_form($presetid, $actionname) {
$definition = imagecache_action_definition($actionname);
$form = array(
'#tree' => TRUE,
);
$form['action'] = array(
'#type' => 'value',
'#value' => $actionname,
);
$form['presetid'] = array(
'#type' => 'value',
'#value' => $presetid,
);
$form['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
);
$form['data'] = call_user_func($actionname .'_form', $action['data']);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add Action'),
);
return $form;
}
function imagecache_ui_action_add_form_submit($form_id, $form_values) {
imagecache_action_save($form_values);
return 'admin/build/imagecache/preset/'. $form_values['presetid'];
}