You are here

function imagecache_ui_preset_form in ImageCache 6.2

Same name and namespace in other branches
  1. 5.2 imagecache_ui.module \imagecache_ui_preset_form()
1 string reference to 'imagecache_ui_preset_form'
imagecache_ui_menu in ./imagecache_ui.module
Implementation of hook_menu().

File

./imagecache_ui.pages.inc, line 132

Code

function imagecache_ui_preset_form($form_state, $preset = array()) {
  $form = array();
  $form['presetid'] = array(
    '#type' => 'value',
    '#value' => isset($preset['presetid']) ? $preset['presetid'] : '',
  );

  // Browsers don't submit disabled form values so we've got to put two copies
  // of the name on the form: one for the submit handler and one for the user.
  if (isset($preset['storage']) && $preset['storage'] === IMAGECACHE_STORAGE_DEFAULT) {
    $form['presetname'] = array(
      '#type' => 'value',
      '#value' => $preset['presetname'],
    );
    $form['presetname_display'] = array(
      '#type' => 'textfield',
      '#size' => '64',
      '#title' => t('Preset Namespace'),
      '#default_value' => $preset['presetname'],
      '#disabled' => TRUE,
    );
  }
  else {
    $form['presetname'] = array(
      '#type' => 'textfield',
      '#size' => '64',
      '#title' => t('Preset Namespace'),
      '#default_value' => isset($preset['presetname']) ? $preset['presetname'] : '',
      '#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.'),
      '#validate' => array(
        'imagecache_element_presetname_validate' => array(),
      ),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => isset($preset['storage']) && $preset['storage'] === IMAGECACHE_STORAGE_DEFAULT ? t('Override Defaults') : t('Save Preset'),
    '#weight' => 9,
  );

  // For new presets don't show the preview or all actions to be added.
  if (empty($preset['presetid'])) {
    return $form;
  }
  $form['actions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Actions'),
    '#tree' => TRUE,
    '#theme' => 'imagecache_ui_preset_actions',
  );
  foreach ($preset['actions'] as $i => $action) {

    // skip unknown actions...
    if (!($definition = imagecache_action_definition($action['action']))) {
      continue;
    }
    $action_form['name'] = array(
      '#value' => t($definition['name']),
    );
    $action_form['action'] = array(
      '#type' => 'value',
      '#value' => $action['action'],
    );
    $action_form['actionid'] = array(
      '#type' => 'value',
      '#value' => $action['actionid'],
    );
    $action_form['presetid'] = array(
      '#type' => 'value',
      '#value' => $action['presetid'],
    );
    $action_form['settings'] = array(
      '#theme' => $action['action'],
      '#value' => $action['data'],
    );
    $action_form['data'] = array(
      '#type' => 'value',
      '#value' => $action['data'],
    );
    $action_form['weight'] = array(
      '#type' => 'weight',
      '#default_value' => $action['weight'],
      '#access' => $preset['storage'] !== IMAGECACHE_STORAGE_DEFAULT,
    );
    $action_form['configure'] = array(
      '#value' => l(t('Configure'), 'admin/build/imagecache/' . $action['presetid'] . '/' . $action['actionid']),
      '#access' => $preset['storage'] !== IMAGECACHE_STORAGE_DEFAULT,
    );
    $action_form['remove'] = array(
      '#value' => l(t('Delete'), 'admin/build/imagecache/' . $action['presetid'] . '/' . $action['actionid'] . '/delete'),
      '#access' => $preset['storage'] !== IMAGECACHE_STORAGE_DEFAULT,
    );
    $form['actions'][$i] = $action_form;
  }
  $form['actions']['new'] = array(
    '#tree' => FALSE,
    '#type' => 'fieldset',
    '#title' => t('New Actions'),
    '#collapsible' => TRUE,
    '#collapsed' => count($preset['actions']) > 0,
    '#access' => $preset['storage'] !== IMAGECACHE_STORAGE_DEFAULT,
  );
  foreach (imagecache_action_definitions() as $action => $definition) {
    $form['actions']['new'][] = array(
      '#type' => 'markup',
      '#prefix' => '<div>',
      '#suffix' => '</div>',
      '#value' => l(t('Add !action', array(
        '!action' => $definition['name'],
      )), 'admin/build/imagecache/' . $preset['presetid'] . '/add/' . $action) . ' - ' . $definition['description'],
    );
  }

  // Display a preview image for this action. Put it below the submit button so
  // users don't have to do a bunch of scrolling.
  $preview_path = file_create_path('imagecache_sample.png');
  if (!file_exists($preview_path)) {

    // Copy of the image into the files directory so rather than generating it
    // from the original so we don't end up creating subdirectories mirroring
    // the path to the this module.
    $preview_path = drupal_get_path('module', 'imagecache_ui') . '/sample.png';
    file_copy($preview_path, 'imagecache_sample.png');
  }
  imagecache_image_flush($preview_path);
  $imagecache_path = imagecache_create_url($preset['presetname'], $preview_path, TRUE);
  $form['preview'] = array(
    '#type' => 'item',
    '#title' => t('Preview'),
    '#value' => l($imagecache_path, $imagecache_path) . '<br />' . l("<img src='{$imagecache_path}' />", $imagecache_path, array(
      'html' => TRUE,
    )),
    '#weight' => 10,
  );
  return $form;
}