You are here

node_gallery.admin.inc in Node Gallery 6

Same filename and directory in other branches
  1. 6.3 node_gallery.admin.inc
  2. 6.2 node_gallery.admin.inc

Node gallery admin file.

File

node_gallery.admin.inc
View source
<?php

/**
 * @file
 * Node gallery admin file. 
 *
 */
function node_gallery_config_list() {
  $items = gallery_config_gateway::get_types('gallery');
  $node_types = node_get_types();
  if (!empty($items)) {
    $headers = array(
      t('Gallery Name'),
      t('Gallery type'),
      t('Image type'),
      t('Operations'),
    );
    foreach ($items as $item) {
      $rows[] = array(
        $item->name,
        $node_types[$item->gallery_type]->name,
        $node_types[$item->image_type]->name,
        theme('links', node_gallery_config_operations($item)),
      );
    }
    return theme('table', $headers, $rows, array(
      'class' => 'node-gallery-config-list',
    ));
  }
  else {
    return t("There's no gallery type now.");
  }
}
function node_gallery_config_operations($config) {
  $items[] = array(
    'title' => t('Edit'),
    'href' => 'admin/build/node_gallery/edit/' . $config->gallery_type,
  );
  $items[] = array(
    'title' => t('Delete'),
    'href' => 'admin/build/node_gallery/delete/' . $config->gallery_type,
  );
  return $items;
}
function node_gallery_config_form($form_state, $gallery_config = NULL) {
  $form = array();
  static $node_types, $image_types, $imagecaches;
  if (empty($imagecaches)) {
    foreach (imagecache_presets() as $id => $imagecache) {
      $imagecaches[$imagecache['presetname']] = $imagecache['presetname'];
    }
  }
  if (empty($imagecaches)) {
    form_set_error('Node gallery config', t('No imagecache preset now. You must set at least one.'));
  }
  if (empty($gallery_config)) {
    drupal_set_title(t('Add Gallery Config'));
  }
  else {
    drupal_set_title(t('Edit %name', array(
      '%name' => $gallery_config->name,
    )));
  }
  $gallery_config = empty($gallery_config) ? array() : (array) $gallery_config;
  $gallery_config = empty($form_state['storage']) ? $gallery_config : array_merge($gallery_config, $form_state['storage']);
  $gallery_config = empty($form_state['values']) ? $gallery_config : array_merge($gallery_config, $form_state['values']);
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Gallery Type Name'),
    '#description' => t('The human-readable name of this content type. This text will be displayed as part of the list on the create content page. It is recommended that this name begin with a capital letter and contain only letters, numbers, and <strong>spaces</strong>.'),
    '#maxlength' => 32,
    '#required' => TRUE,
    '#default_value' => $gallery_config['name'],
  );
  $step = empty($form_state['values']['step']) ? 1 : $form_state['values']['step'] + 1;
  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => $step,
  );
  if ($step == 1) {
    if (empty($node_types)) {
      foreach (node_get_types() as $node_type) {
        $node_types[$node_type->type] = $node_type->name;
      }
    }
    $form['gallery_type'] = array(
      '#type' => 'radios',
      '#title' => t('Gallery Type'),
      '#options' => $node_types,
      '#description' => t('Select which content type should be used as gallery type.'),
      '#default_value' => $gallery_config['gallery_type'],
      '#required' => TRUE,
    );
    $form['image_type'] = array(
      '#type' => 'radios',
      '#title' => t('Image Type'),
      '#options' => $node_types,
      '#description' => t('Select which content type should be used as image type.'),
      '#default_value' => $gallery_config['image_type'],
      '#required' => TRUE,
    );
    $form['next'] = array(
      '#type' => 'submit',
      '#value' => t('Next'),
      '#submit' => array(
        'node_gallery_config_form_next_submit',
      ),
    );
  }
  else {

    /*cck support*/
    if (module_exists('content')) {
      content_clear_type_cache();
      $image_type = content_types($form_state['storage']['image_type']);

      //non-cck fields
      foreach ($image_type['extra'] as $name => $field) {
        if ($name != 'menu' && $name != 'attachments') {
          $image_type_fields[$name] = $field['label'];
        }
      }

      //cck fields
      foreach ($image_type['fields'] as $name => $field) {
        $image_type_fields[$name] = $field['widget']['label'];
      }
    }
    else {
      $node_fields = node_gallery_get_type_fields($form_state['storage']['image_type']);
      foreach ($node_fields as $name => $field) {
        $image_type_fields[$name] = $field['label'];
      }
    }

    //print_r($gallery_config);die;
    $lightbox2 = module_exists('lightbox2');
    drupal_add_js(drupal_get_path('module', 'node_gallery') . '/node_gallery.admin.js', 'module');
    $form['data'] = array(
      '#tree' => TRUE,
    );
    $form['data']['display_fields'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Fields'),
      '#options' => $image_type_fields,
      '#description' => t('Specify which fields should be displayed.'),
      '#required' => TRUE,
      '#default_value' => empty($gallery_config['display_fields']) ? array() : $gallery_config['display_fields'],
    );
    $form['data']['gallery_directory'] = array(
      '#type' => 'textfield',
      '#title' => t('Gallery Directory'),
      '#description' => t('Specify a subdirectory of the file system to save the images of your gallery . The document root is \'files\'.
      Use %uid for user_id, %username for username, %gid for gallery_id, %gallery_name for gallery_name.'),
      '#default_value' => $gallery_config['gallery_directory'],
    );
    $form['data']['default_cover'] = array(
      '#type' => 'textfield',
      '#title' => t('Default Cover Image'),
      '#description' => t('Specify a default cover image to show when there is not an image in gallery. The path begins with your drupal root. <strong>Do not include the leading slash /.</strong>'),
      '#default_value' => $gallery_config['default_cover'],
    );
    $form['data']['image_size'] = array(
      '#type' => 'fieldset',
      '#title' => t('Image Sizes'),
      '#description' => t('Specify which imagecache preset you wish to use to display the image.'),
      '#tree' => TRUE,
    );
    $form['data']['image_size']['cover'] = array(
      '#type' => 'select',
      '#title' => t('Cover'),
      '#options' => $imagecaches,
      '#default_value' => $gallery_config['image_size']['cover'],
    );
    $form['data']['image_size']['thumbnail'] = array(
      '#type' => 'select',
      '#title' => t('Thumbnail'),
      '#options' => $imagecaches,
      '#default_value' => $gallery_config['image_size']['thumbnail'],
    );
    $form['data']['image_size']['preview'] = array(
      '#type' => 'select',
      '#title' => t('Preview (This will be your Display--A Preview of the Full-Size Image)'),
      '#options' => $imagecaches,
      '#default_value' => $gallery_config['image_size']['preview'],
    );
    $form['data']['teaser'] = array(
      '#type' => 'fieldset',
      '#title' => t('Teaser Setting'),
      '#description' => t('Specify how to display the gallery and image when viewing the node as a teaser.'),
    );
    $display_extra_options = array();
    if ($lightbox2) {
      $display_extra_options['lightbox2_gallery'] = t('Thumbnails that open a Lightbox2 Gallery');
    }
    $form['data']['teaser']['gallery_display_type'] = array(
      '#type' => 'radios',
      '#title' => t('Display Type'),
      '#attributes' => array(
        'class' => 'teaser-display-type-radios',
      ),
      '#options' => array_merge(array(
        'cover' => t('Cover'),
        'thumbnails' => t('Thumbnails'),
      ), $display_extra_options),
      '#default_value' => empty($gallery_config['teaser']['gallery_display_type']) ? 'cover' : $gallery_config['teaser']['gallery_display_type'],
    );
    $form['data']['teaser']['thumbnails_num'] = array(
      '#type' => 'textfield',
      '#title' => t('Number of Thumbnails to Display'),
      '#description' => t('If you select the gallery display type as thumbnails above, enter the number of thumbnails you want to display when viewing the teaser.'),
      '#default_value' => $gallery_config['teaser']['thumbnails_num'],
    );
    if ($lightbox2) {
      $form['data']['teaser']['lightbox2_gallery'] = array(
        '#type' => 'select',
        '#title' => t('Image Size for Lightbox2 Gallery'),
        '#options' => $imagecaches,
        '#description' => t('Select an Imagecache preset that will be used with the Lightbox2 popup.'),
        '#default_value' => $gallery_config['teaser']['lightbox2_gallery'],
        '#prefix' => '<div class="lightbox2-gallery-preset">',
        '#suffix' => '</div>',
      );
    }
    $form['data']['teaser']['image'] = array(
      '#type' => 'select',
      '#title' => t('Image Size for Teaser Display Type'),
      '#options' => $imagecaches,
      '#default_value' => $gallery_config['teaser']['image'],
    );

    /**
     * Gallery Landing page
     */
    $form['data']['gallery'] = array(
      '#type' => 'fieldset',
      '#title' => t('Gallery Landing Page Setting'),
      '#description' => t('Specify how to display the gallery landing page and image when viewing the node as a teaser.'),
    );
    $gallery_display_extra_options = array();
    $gallery_display_default_value = $gallery_config['gallery']['gallery_display'] == '' ? 'thumbnails' : $gallery_config['gallery']['gallery_display'];
    if ($lightbox2) {
      $gallery_display_extra_options['lightbox2_gallery'] = t('Thumbnails that open a Lightbox2 Gallery');
    }
    else {
      if ($gallery_display_default_value == 'lightbox2_gallery') {

        // We don't want the default to be a radio that doesn't exist
        $gallery_display_default_value = 'thumbnails';
      }
    }
    $form['data']['gallery']['gallery_display'] = array(
      '#type' => 'radios',
      '#title' => t('Gallery Display Type'),
      '#attributes' => array(
        'class' => 'gallery-display-type-radios',
      ),
      '#options' => array_merge(array(
        'cover' => t('Cover'),
        'thumbnails' => t('Thumbnails'),
      ), $gallery_display_extra_options),
      '#default_value' => $gallery_display_default_value,
    );
    if ($lightbox2) {
      $form['data']['gallery']['lightbox2_gallery_preset'] = array(
        '#type' => 'select',
        '#title' => t('Image Size for Lightbox2 Gallery'),
        '#options' => $imagecaches,
        '#description' => t('Select an Imagecache preset that will be used with the Lightbox2 popup.'),
        '#default_value' => $gallery_config['gallery']['lightbox2_gallery_preset'],
        '#prefix' => '<div class="gallery-lightbox2-gallery-preset">',
        '#suffix' => '</div>',
      );
    }
    $form['data']['gallery']['image'] = array(
      '#type' => 'select',
      '#title' => t('Image Size for Gallery Display Type'),
      '#options' => $imagecaches,
      '#default_value' => $gallery_config['gallery']['image'],
    );
    $extra_options = array();
    $extra_description = '';
    if ($lightbox2) {
      $extra_options['lightbox2'] = t('Enable as Lightbox2 Popup');
    }
    else {
      $extra_description = '<br />' . t('If you install and enable the <a href="http://drupal.org/project/lightbox2">Lightbox2</a> module, you can use Lightbox to display a larger version of the photo.');
    }
    $form['data']['view_original'] = array(
      '#type' => 'radios',
      '#title' => t('View Larger Image'),
      '#attributes' => array(
        'class' => 'view-original-radios',
      ),
      '#options' => array_merge(array(
        '0' => t('Disable'),
        'default' => t('Enable as a Link on the Displayed Image'),
        'text' => t('Enable as Text Link Below the Displayed Image'),
      ), $extra_options),
      '#description' => t('Specify whether allow or not viewing the original image.') . $extra_description,
      '#default_value' => empty($gallery_config['view_original']) ? 0 : $gallery_config['view_original'],
    );
    $form['data']['view_original_text'] = array(
      '#type' => 'textfield',
      '#title' => t('View Original Text Link'),
      '#description' => t('If you have selected the "Enable Text Link Below Displayed Image" option, you should provide some text for the link.'),
      '#default_value' => empty($gallery_config['view_original_text']) ? t('Download the Original Image') : $gallery_config['view_original_text'],
      '#prefix' => '<div class="view-original-text-value">',
      '#suffix' => '</div>',
    );

    // Force the user to select a imagecache preset for lightbox
    if ($lightbox2) {
      $form['data']['lightbox2'] = array(
        '#type' => 'select',
        '#title' => t('Imagecache Preset for Lightbox2'),
        '#options' => $imagecaches,
        '#description' => t('Select an Imagecache preset that will be used with the Lightbox2 popup.  Please note that this preset should be larger than your Display preset.'),
        '#default_value' => $gallery_config['lightbox2'],
        '#prefix' => '<div class="lightbox2-preset">',
        '#suffix' => '</div>',
      );
    }
    $form['data']['content_display'] = array(
      '#type' => 'radios',
      '#title' => t('Choose the content you want to display'),
      '#options' => array(
        'gallery' => t('Gallery'),
        'image' => t('Images'),
      ),
      '#description' => t("If you select 'Gallery', the body text of the Gallery node will be used. If you select 'Images', the \n      body text of the Image node will be used. This allows you choose if you want the content display with each image to be different or not."),
      '#default_value' => empty($gallery_config['content_display']) ? 'image' : $gallery_config['content_display'],
    );
    $form['data']['image_comment'] = array(
      '#type' => 'radios',
      '#title' => t('Comment to image'),
      '#options' => array(
        t('Disabled'),
        t('Read only'),
        t('Read/Write'),
      ),
      '#description' => t('Choose whether or not to allow comment to images.'),
      '#default_value' => $gallery_config['image_comment'] == '' ? 2 : $gallery_config['image_comment'],
    );
    $form['data']['upload_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Image Upload Settings'),
      '#description' => t('This allows you to configure the bulk upload page.'),
    );
    $form['data']['upload_settings']['number_uploads'] = array(
      '#type' => 'select',
      '#title' => t('Number of Uploads'),
      '#options' => array(
        '1' => '1',
        '2' => '2',
        '3' => '3',
        '4' => '4',
        '5' => '5',
        '6' => '6',
        '7' => '7',
        '8' => '8',
        '9' => '9',
        '10' => '10',
        '11' => '11',
        '12' => '12',
        '13' => '13',
        '14' => '14',
        '15' => '15',
        '16' => '16',
        '17' => '17',
        '18' => '18',
        '19' => '19',
        '20' => '20',
      ),
      '#multiple' => FALSE,
      '#description' => t('This allows you to specify how many file upload fields appear on the image upload form.  By default, only 5 fields are provided.  We recommend you do not have more than 5 fields if you are on a shared host or have limited resourced.  If you incress this to more than 5, we highly recommend you test this by uploading lots of large files before putting your site into production.'),
      '#default_value' => empty($gallery_config['upload_settings']['number_uploads']) ? '5' : $gallery_config['upload_settings']['number_uploads'],
    );
    $form['previous'] = array(
      '#type' => 'submit',
      '#value' => t('Previous'),
      '#submit' => array(
        'node_gallery_config_form_pre_submit',
      ),
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
  }
  return $form;
}
function node_gallery_config_form_validate($form, &$form_state) {
  if ($form_state['values']['next'] == t('Next')) {
    if ($form_state['values']['gallery_type'] == $form_state['values']['image_type']) {
      unset($form_state['values']['step']);
      form_set_error('Type', t('The gallery type and image type can\'t be the same.'));
    }
  }
}
function node_gallery_config_form_next_submit($form, &$form_state) {
  $form_state['storage'] = empty($form_state['storage']) ? $form_state['values'] : array_merge($form_state['storage'], $form_state['values']);
}
function node_gallery_config_form_pre_submit($form, &$form_state) {
  $form_state['storage'] = empty($form_state['storage']) ? $form_state['values'] : array_merge($form_state['storage'], $form_state['values']);
  unset($form_state['values']['step']);
}
function node_gallery_config_form_submit($form, &$form_state) {
  $submit_values = array_merge($form_state['storage'], $form_state['values']);
  $gallery_config = new gallery_config($submit_values);
  $gallery_config
    ->save();
  unset($form_state['storage']);
  $form_state['rebuild'] = FALSE;
  $form_state['redirect'] = 'admin/build/node_gallery';
}
function node_gallery_config_delete_form($form_state, $gallery_config) {
  $form['gallery_type'] = array(
    '#type' => 'value',
    '#value' => $gallery_config->gallery_type,
  );
  return confirm_form($form, t('Are you sure you want to delete gallery type config %name?', array(
    '%name' => $gallery_config->gallery_type,
  )), 'admin/build/node_gallery', NULL, t('Delete'), t('Back'));
}
function node_gallery_config_delete_form_submit($form, &$form_state) {
  $gallery_config = new gallery_config($form_state['values']);
  if ($gallery_config
    ->delete()) {
    drupal_set_message(t('Gallery type config %name has been deleted.', array(
      '%name' => $gallery_config->gallery_type,
    )));
    watchdog('node_gallery', t('Image gallery delete form: %name deleted.', array(
      '%name' => $gallery_config->gallery_type,
    )), WATCHDOG_NOTICE);
    $form_state['redirect'] = 'admin/build/node_gallery';
  }
}

/**
 * copy from cck module.
 *
 * @param unknown_type $type_name
 * @return unknown
 */
function node_gallery_get_type_fields($type_name) {
  $type = node_get_types('type', $type_name);
  $extra = array();
  if ($type->has_title) {
    $extra['title'] = array(
      'label' => $type->title_label,
      'description' => t('Node module form.'),
      'weight' => -5,
    );
  }
  if ($type->has_body) {
    $extra['body_field'] = array(
      'label' => $type->body_label,
      'description' => t('Node module form.'),
      'weight' => 0,
      'view' => 'body',
    );
  }
  if (module_exists('locale') && variable_get("language_content_type_{$type_name}", 0)) {
    $extra['language'] = array(
      'label' => t('Language'),
      'description' => t('Locale module form.'),
      'weight' => 0,
    );
  }
  if (module_exists('taxonomy') && taxonomy_get_vocabularies($type_name)) {
    $extra['taxonomy'] = array(
      'label' => t('Taxonomy'),
      'description' => t('Taxonomy module form.'),
      'weight' => -3,
    );
  }
  return $extra;
}