[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/imagecache/ -> imagecache_ui.pages.inc (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7