[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/image/ -> image.admin.inc (source)

   1  <?php
   2  // $Id: image.admin.inc,v 1.9.2.2 2010/08/03 17:43:00 sun Exp $
   3  
   4  /**
   5   * Menu callback; Form builder function for image settings.
   6   */
   7  function image_admin_settings() {
   8    $form['image_updated'] = array(
   9      '#type' => 'hidden',
  10      '#value' => variable_get('image_updated', time()),
  11    );
  12  
  13    $form['files'] = array(
  14      '#type' => 'fieldset',
  15      '#title' => t('Image file settings'),
  16    );
  17    $form['files']['image_default_path'] = array(
  18      '#type' => 'textfield',
  19      '#title' => t('Default image path'),
  20      '#default_value' => variable_get('image_default_path', 'images'),
  21      '#description' => t('Subdirectory in the directory %dir where pictures will be stored. Do not include trailing slash.', array('%dir' => file_directory_path())),
  22    );
  23  
  24    $form['files']['image_max_upload_size'] = array(
  25      '#type' => 'textfield',
  26      '#title' => t('Maximum upload size'),
  27      '#default_value' => variable_get('image_max_upload_size', 800),
  28      '#field_suffix' => t('KB'),
  29      '#size' => 12,
  30      '#description' => t('Maximum file size for image uploads. When a maximum image dimensions is specified for original images the size is checked after resizing.'),
  31    );
  32  
  33    $form['image_sizes'] = array(
  34      '#type' => 'fieldset',
  35      '#title' => t('Image sizes'),
  36      '#tree' => TRUE,
  37      '#theme' => 'image_settings_sizes_form',
  38      '#description' => '<p>'. t("The <em>Scale image</em> operation resizes images so that they fit with in the given dimensions. If only one dimension is specified the other dimension will be computed based on the image's aspect ratio. The <em>Scale and crop image</em> operation resizes images to be exactly the given dimensions. If only one dimension is specified the image will not be cropped, making this is equivalent to <em>Scale image</em>.") .'</p>'
  39        .'<p>'. t("Note: 'Original' dimensions will only be used to resize images when they are first uploaded. Existing originals will not be modified.") .'</p>',
  40    );
  41  
  42    $link_options = array(
  43      IMAGE_LINK_HIDDEN => t('Hidden'),
  44      IMAGE_LINK_SHOWN => t('Same window'),
  45      IMAGE_LINK_NEW => t('New window'),
  46    );
  47  
  48    $sizes = image_get_sizes();
  49  
  50    // Add some empty rows for user defined sizes.
  51    $num_sizes = count($sizes);
  52    for ($i = $num_sizes; $i < ($num_sizes + 3); $i++) {
  53      $sizes['new' . $i] = array(
  54        'label' => '',
  55        'operation' => 'scale',
  56        'width' => '',
  57        'height' => '',
  58        'link' => IMAGE_LINK_SHOWN,
  59        'new' => TRUE,
  60      );
  61    }
  62  
  63    foreach ($sizes as $key => $size) {
  64      $form['image_sizes'][$key]['label'] = array(
  65        '#type' => 'textfield',
  66        '#default_value' => $size['label'],
  67        '#size' => 25,
  68        '#maxlength' => 32,
  69      );
  70  
  71      // For required sizes, set the value and disable the field.
  72      if (_image_is_required_size($key)) {
  73        $form['image_sizes'][$key]['label']['#disabled'] = TRUE;
  74        $form['image_sizes'][$key]['label']['#value'] = $size['label'];
  75        $form['image_sizes'][$key]['label']['#required'] = TRUE;
  76      }
  77      $form['image_sizes'][$key]['operation'] = array(
  78        '#type' => 'select',
  79        '#default_value' => $size['operation'],
  80        '#options' => array('scale' => t('Scale image'), 'scale_crop' => t('Scale and crop image')),
  81      );
  82      $form['image_sizes'][$key]['width'] = array(
  83        '#type' => 'textfield',
  84        '#default_value' => $size['width'],
  85        '#size' => 5,
  86        '#maxlength' => 5,
  87      );
  88      $form['image_sizes'][$key]['height'] = array(
  89        '#type' => 'textfield',
  90        '#default_value' => $size['height'],
  91        '#size' => 5,
  92        '#maxlength' => 5,
  93      );
  94      $form['image_sizes'][$key]['link'] = array(
  95        '#type' => 'select',
  96        '#default_value' => $size['link'],
  97        '#options' => $link_options,
  98      );
  99    }
 100  
 101    // Make changes to the settings before passing them off to
 102    // system_settings_form_submit().
 103    $form['#submit'][] = 'image_admin_settings_submit';
 104  
 105    return system_settings_form($form);
 106  }
 107  
 108  /**
 109   * Form validation handler for image admin settings form.
 110   */
 111  function image_admin_settings_validate($form, &$form_state) {
 112    // Check that the sizes provided have the required amount of information.
 113    $image_sizes = $form_state['values']['image_sizes'];
 114    foreach (element_children($image_sizes) as $key) {
 115      // If there's a label they must provide at either a height or width.
 116      if ($key != IMAGE_ORIGINAL && !empty($image_sizes[$key]['label'])) {
 117        if (empty($image_sizes[$key]['width']) && empty($image_sizes[$key]['height'])) {
 118          form_set_error("image_sizes][$key][width", t('You must specify width, height or both dimensions.'));
 119        }
 120      }
 121    }
 122  
 123    // Try to create directories and warn the user of errors.
 124    $image_default_path = $form_state['values']['image_default_path'];
 125    $image_path = file_create_path(file_directory_path() . '/' . $image_default_path);
 126    $temp_path = $image_path . '/temp';
 127  
 128    if (!file_check_directory($image_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS, 'image_default_path')) {
 129      form_set_error('image_default_path', t('You have specified an invalid directory.'));
 130    }
 131    if (!file_check_directory($temp_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS, 'image_default_path')) {
 132      form_set_error('image_default_path', t('You have specified an invalid directory.'));
 133    }
 134  }
 135  
 136  /**
 137   * Form submit handler for image admin settings form.
 138   */
 139  function image_admin_settings_submit($form, &$form_state) {
 140    // Ensure that 'image_default_path' variable contains no trailing slash.
 141    $form_state['values']['image_default_path'] = rtrim($form_state['values']['image_default_path'], '/');
 142  
 143    // Remove deleted sizes, and use the label as indexes for new sizes.
 144    $old_sizes = image_get_sizes();
 145    // If the size's operation, or dimensions change we need to rebuild.
 146    $rebuild = FALSE;
 147  
 148    foreach ($form_state['values']['image_sizes'] as $key => $size) {
 149      // Changed to the original setting only affect new images and they
 150      // shouldn't be able to add or remove it.
 151      if ($key == IMAGE_ORIGINAL) {
 152        continue;
 153      }
 154  
 155      // Remove sizes without labels.
 156      if (empty($size['label'])) {
 157        unset($form_state['values']['image_sizes'][$key]);
 158      }
 159  
 160      // Check if only one is set, indicating an addition or removal.
 161      if (isset($form_state['values']['image_sizes'][$key]) ^ isset($old_sizes[$key])) {
 162        $rebuild |= TRUE;
 163  
 164        // When adding a new size, we need to assign a key.
 165        if (isset($form_state['values']['image_sizes'][$key])) {
 166          unset($form_state['values']['image_sizes'][$key]);
 167          $new_key = drupal_strtolower(drupal_substr($size['label'], 0, 32));
 168          $form_state['values']['image_sizes'][$new_key] = $size;
 169        }
 170      }
 171      // Check for changes.
 172      else if (isset($form_state['values']['image_sizes'][$key]) && isset($old_sizes[$key])) {
 173        // Did the operation, height or width change?
 174        foreach (array('operation', 'height', 'width') as $field) {
 175          $rebuild |= ($form_state['values']['image_sizes'][$key][$field] != $old_sizes[$key][$field]);
 176        }
 177      }
 178    }
 179  
 180    // If we've changed anything update the image_update variable so the
 181    // derivative images are rebuilt.
 182    if ($rebuild) {
 183      drupal_set_message(t('Changes to the images sizes mean that the derivative images will need to be regenerated.'));
 184      $form_state['values']['image_updated'] = time();
 185    }
 186  }
 187  
 188  function theme_image_settings_sizes_form($form) {
 189    $header = array(t('Label'), t('Operation'), t('Width'), t('Height'), t('Link'));
 190    foreach (element_children($form) as $key) {
 191      $row = array();
 192      $row[] = drupal_render($form[$key]['label']);
 193      $row[] = drupal_render($form[$key]['operation']);
 194      $row[] = drupal_render($form[$key]['width']);
 195      $row[] = drupal_render($form[$key]['height']);
 196      $row[] = drupal_render($form[$key]['link']);
 197      $rows[] = $row;
 198    }
 199    $output = theme('table', $header, $rows);
 200    $output .= drupal_render($form);
 201  
 202    return $output;
 203  }
 204  


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