Skip to content
gallery_block_admin.inc 11.8 KiB
Newer Older
<?php
// $Id$

require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');

/**
 * gallery.module : gallery_block_admin.inc
 * Block settings functions
 */

/**
 * Function _gallery_block_admin_navigation().
 */
function _gallery_block_admin_navigation() {
  $form['gallery_block_navigation_cache'] = array(
    '#type' => 'select',
    '#title' => t('Block cache'),
    '#default_value' => variable_get('gallery_block_navigation_cache', BLOCK_CACHE_PER_USER),
    '#options' => array(
      BLOCK_NO_CACHE => t('Cache disabled'),
      BLOCK_CACHE_PER_USER => t('Cache per user')
    ),
    '#description' => t('Configure cache settings for this block. It\'s usually safe to set this to \'Cache per user\'.')
  );

  return $form;
}

/**
 * Function _gallery_block_admin_block().
 */
function _gallery_block_admin_block($delta) {
  $block_map = _gallery_block_map(TRUE);
  $block_type = variable_get('gallery_block_'. $delta .'_type', 'imageblock');
  $plugin = $block_map[$block_type]['plugin'];
  $plugin_block = $block_map[$block_type]['block'];
  
  // General block settings (common for all G2 block types)
  $form['gallery_block_'. $delta .'_blockid'] = array(
    '#type' => 'textfield',
    '#title' => t('Block Identifier [Block @id]', array('@id' => $delta)),
    '#default_value' => variable_get('gallery_block_'. $delta .'_blockid', ''),
    '#size' => 30,
    '#maxlength' => 255,
    '#description' => t('A short identifier to distinguish blocks of the same type. (Blocks are numbered by default)')
  );
  $form['gallery_block_'. $delta .'_cache'] = array(
    '#type' => 'select',
    '#title' => t('Block cache'),
    '#default_value' => variable_get('gallery_block_'. $delta .'_cache', BLOCK_CACHE_PER_USER),
    '#options' => array(
      BLOCK_NO_CACHE => t('Cache disabled'),
      BLOCK_CACHE_PER_USER => t('Cache per user'),
      BLOCK_CACHE_PER_ROLE => t('Cache per role')
    ),
    '#description' => _gallery_block_admin_description($block_type .'_cache'),
  );
  
  // Block type selector
  $block_list = array();
  foreach ($block_map as $type => $details) {
    $block_list[$type] = $details['block'];
  }
  $form['gallery_block_'. $delta .'_type'] = array(
    '#type' => 'select',
    '#title' => t('Block type'),
    '#default_value' => $block_type,
    '#options' => $block_list,
    '#description' => t('Select the Gallery2 block to be used in this Drupal block. In previous versions only
                         \'ImageBlock\' was available, but now you can embed any Gallery2 block in Drupal.'),
  );
  
  // Block specific settings
  $form[$block_type] = array(
    '#type' => 'fieldset',
    '#title' => t('%block settings', array('%block' => $plugin_block)),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form[$block_type][] = _gallery_block_admin_block_params($delta, $plugin, $plugin_block);
  
  return $form;
}

/**
 * Function _gallery_block_admin_description().
 */
function _gallery_block_admin_description($element) {
  // TODO
  switch($element) {
    case 'imageblock_cache':
      return t('Configure cache settings for this block. Unless random images are used you can safely keep this to
                \'Cache per user\' (default). If you dont have per-user permissions configured in your gallery you
                can improve performance by setting to \'Cache per role\'.');
    default:
      return '';
  }
}

/**
 * Function _gallery_block_admin_block_params().
 */
function _gallery_block_admin_block_params($delta, $plugin, $plugin_block) {
  $g2_blocks = gallery_get_blocks();
  if ($plugin_block == 'ImageBlock') {
    // Image block specific settings
    _gallery_block_admin_imageblock($delta, $plugin, $plugin_block, $g2_blocks, $form);
  }
  else {
    // Generate a Drupal form element for each G2 block variable
    foreach ($g2_blocks[$plugin][$plugin_block]['vars'] as $parameter => $options) {
      $block = strtolower($plugin_block .'_'. $parameter);
      $element = 'gallery_block_'. $delta .'_'. $block;
      // Common form element properties
      $form[$element] = array(
        '#title' => $options['description'],
        '#default_value' => variable_get($element, $options['default']),
        '#description' => _gallery_block_admin_description($block),
      );
      // Form element type specific properties
      switch ($options['type']) {
        case 'boolean':
          $form[$element]['#type'] = 'checkbox';
          break;
        case 'text':
          $form[$element]['#type'] = 'textfield';
          $form[$element]['#size'] = max(10, min(80, strlen($options['default'])));
          $form[$element]['#maxlength'] = 255;
          break;
        case 'choice':
          $form[$element]['#type'] = 'select';
          $form[$element]['#options'] = $options['choices'];
          break;
        default:
      }
    }
  }

  return $form;
}

/**
 * Function _gallery_block_admin_imageblock().
 */
function _gallery_block_admin_imageblock($delta, $plugin, $plugin_block, $g2_blocks, &$form) {
  $plugin_status =  gallery_plugin_status(array('imageblock', 'imageframe'));
  $form['imageblock']['#description'] = t('The Gallery Image/Grid Block requires the Gallery2 Image Block plugin
                                           (!imageblock_status) and optionally the Gallery2 Image Frame plugin
                                           (!imageframe_status).', array(
                      '!imageblock_status' => theme('gallery_plugin_status_message', $plugin_status['imageblock']),
                      '!imageframe_status' => theme('gallery_plugin_status_message', $plugin_status['imageframe']))
  );
  
  $element = 'gallery_block_'. $delta .'_'. strtolower($plugin_block);
  $num_cols = variable_get($element .'_num_cols', 2);
  $num_rows = variable_get($element .'_num_rows', 2);
  $numimages = $num_cols * $num_rows;
  
  $form[$element .'_num_cols'] = array(
    '#type' => 'select',
    '#title' => t('Number of columns'),
    '#default_value' => variable_get($element .'_num_cols', 2),
    '#options' => _gallery_range_array(1, 10),
    '#description' => t('Select the number of columns in the grid.'),
  );
  
  $form[$element .'_num_rows'] = array(
    '#type' => 'select',
    '#title' => t('Number of rows'),
    '#default_value' => variable_get($element .'_num_rows', 2),
    '#options' => _gallery_range_array(1, 10),
    '#description' => t('Select the number of rows in the grid.'),
  );
  
  $form[$element .'_block_block'] = array(
    '#type' => 'fieldset',
    '#title' => t('Image types'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
    '#description' => t('Pick the type of images you would like to see. You can select the same type more than once.'),
  );
  $gallery_block_block = variable_get($element .'_block_block', array('randomImage'));
  for ($i=0; $i<$numimages; $i++) {
    $form[$element .'_block_block'][$i] = array(
      '#type' => 'select',
      '#title' => '',
      '#default_value' => isset($gallery_block_block[$i]) ? $gallery_block_block[$i] : NULL,
      '#options' => array_merge(array(NULL => t('None')), $g2_blocks[$plugin][$plugin_block]['vars']['blocks']['choices']),
    );
  }
  
  $form[$element .'_item_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Album or Item ID'),
    '#default_value' => variable_get($element .'_item_id', ''),
    '#size' => 20,
    '#maxlength' => 20,
    '#description' => t('Enter the Gallery image or album ID (or blank). If an album or item ID is specified, random
                         images will only be selected from that album and its sub-albums. If \'user\' (or \'user:123\')
                         is entered, items will be taken from the current (or specified) user\'s useralbum.')
  );
  
  $form[$element .'_block_show'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Image data'),
    '#default_value' => variable_get($element .'_block_show', array('title', 'heading')),
    '#options' => array(
      'title'   => t('Title'),
      'date'    => t('Date'),
      'views'   => t('View Count'),
      'owner'   => t('Item owner'),
      'heading' => t('Heading')
    ),
    '#description' => t('Choose the item metadata you would like to display.'),
  );
  
  $form[$element .'_size_method'] = array(
    '#type' => 'select',
    '#title' => t('Image size method'),
    '#default_value' => variable_get($element .'_size_method', GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT),
    '#options' => array(
      'maxsize' => t('Max Size'),
      'exactsize' => t('Exact Size'),
    ),
    '#description' => t('\'Max Size\' gives faster image downloading, but the image size
                         may be smaller than the size defined below. <br />\'Exact Size\' may be slower
                         (as a larger image is downloaded and then scaled by the browser) but the image
                         will be guaranteed to be the size defined below.'),
  );
  
  $form[$element .'_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Image size'),
    '#default_value' => variable_get($element .'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT),
    '#size' => 10,
    '#maxlength' => 10,
    '#description' => t('Sets the size (in pixels) of the longest side of the image according to the method defined above.'),
  );
  
  $imageframe_desc = ($plugin_status['imageframe'] != GALLERY_PLUGIN_ENABLED) ?
      t('Requires the Gallery2 Image Frame plugin (!imageframe_status).',
      array('!imageframe_status' => theme('gallery_plugin_status_message', $plugin_status['imageframe']))) : '';

  $image_frames = gallery_get_image_frames();
  
  $form[$element .'_album_frame'] = array(
    '#type' => 'select',
    '#title' => t('Album frame'),
    '#default_value' => variable_get($element .'_album_frame', 'none'),
    '#options' => $image_frames,
    '#description' => $imageframe_desc,
  );
  
  $form[$element .'_item_frame'] = array(
    '#type' => 'select',
    '#title' => t('Item frame'),
    '#default_value' => variable_get($element .'_item_frame', 'none'),
    '#options' => $image_frames,
    '#description' => $imageframe_desc,
  );
  
  $form[$element .'_link_target'] = array(
    '#type' => 'textfield',
    '#title' => t('Link target'),
    '#default_value' => variable_get($element .'_link_target', ''),
    '#size' => 20,
    '#maxlength' => 20,
    '#description' => t('Enter a link target (e.g. \'_blank\' to open in a new window).'),
  );
  
  $form[$element .'_link'] = array(
    '#type' => 'textfield',
    '#title' => t('Image Link'),
    '#default_value' => variable_get($element .'_link', ''),
    '#size' => 60,
    '#maxlength' => 255,
    '#description' => t('By default the image has a link to the item in the Gallery. This
      can be overridden here (or leave empty for the default). Use \'none\' for no link, or a URL
      to link somewhere else instead.'),
  );
  
  $form['element'] = array('#type' => 'value', '#value' => $element);
  $form['update_btn'] = array(
    '#type' => 'submit',
    '#value' => t('Save and Edit'),
    '#submit' => array('_gallery_block_admin_update')
  );
  return $form;
}

/**
 * Function _gallery_block_admin_update().
 */
function _gallery_block_admin_update($form, &$form_state) {
  _gallery_block_admin_save(0, $form_state['values']);
}

/**
 * Function _gallery_block_admin_save().
 */
function _gallery_block_admin_save($delta, $values) {
  $element = $values['element'];
  unset($values['element']);
  // Validate variable values
  if (isset($values[$element .'_size'])) {
    if (!is_numeric($values[$element .'_size']) || $values[$element .'_size'] < 10) {
      $values[$element .'_size'] = 10;
      drupal_set_message(t('Image size must be a number greater than ten pixels.
                            (The value has been updated to \'10\' for your convenience.)'), 'error');
    }
  }
  // Save variables
  foreach ($values as $key => $value) {
    if (strpos($key, $element) == 0) {
      if (is_array($value)) {
        $value = array_values(array_filter($value));
      }
      variable_set($key, $value);
    }
  }
}