<?php
// $Id: gallery_assist.admin.inc,v 1.1.2.13 2010/03/18 05:25:10 jcmc Exp $

/**
 * @file
 * Administration page from Gallery Assist.
 */

/**
 * Settings form.
 * 
 * @ingroup forms
 */ 
function gallery_assist_settings($form_state, $type = 'gallery_assist', $type_name = '') {
  global $base_path;

  drupal_add_css(drupal_get_path('module', 'gallery_assist') .'/css/gallery_assist.css', 'module', 'all', FALSE);

  $form = array();

  $assigned = variable_get('gallery_assist_'. $type, 0) == 1 ? ' ('. t('enabled') .')' : ' ('. t('disabled') .')';

  $fieldname[$type] = $type == 'gallery_assist' ? t('Gallery Assist') : t('Gallery Assist on @typename', array('@typename' => $type_name));
  if ($fieldname[$type] != 'gallery_assist') {
    $breadcrumb = drupal_get_breadcrumb();
    $breadcrumb[] = l($fieldname[$type], 'admin/settings/gallery_assist/'. $type);
    drupal_set_breadcrumb($breadcrumb);
  }

  $settings_type = variable_get('gallery_assist_'. $type .'_data', FALSE);

  if (variable_get('gallery_assist_use_imagecache', 0) == 1) {
    $settings_ic = variable_get('gallery_assist_'. $type .'_ic_data', FALSE);
  }

  $settings = is_array($settings_ic) ? array_merge($settings_type, $settings_ic) : $settings_type;

  $form['gallery_assist_'. $type .'_settings'][$type] = array(
    '#type' => 'fieldset',
    '#title' => $fieldname[$type],
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => $type == 'gallery_assist' ? 0 : 1,
    '#weight' => 0,
  );

  if (module_exists('imagecache') && variable_get('gallery_assist_use_imagecache', 0) == 1) {
    $form['gallery_assist_'. $type .'_settings'][$type] += _gallery_assist_get_ic_settings_form($type, $settings);
  }
  else {
    $form['gallery_assist_'. $type .'_settings'][$type] += _gallery_assist_sizes_settings_form($type, $settings);
  }

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_upload_settings'] = array();
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_upload_settings'] = gallery_assist_upload_settings($type, $settings);

  // Layout.
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout'] = array(
    '#type' => 'fieldset',
    '#title' => t('Layout'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['gallery_assist_'. $type .'_items_per_row'] = array(
    '#type' => 'select',
    '#title' => t('Thumbnails per row'),
    '#options' => drupal_map_assoc(array(t('unlimited'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
    '#default_value' => $settings['items_per_row'],
    '#prefix' => '<div class="gallery-assist-settings-left-half">',
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['gallery_assist_'. $type .'_rows_per_page'] = array(
    '#title' => t('Rows per page'),
    '#default_value' => $settings['rows_per_page'],
    '#type' => 'select',
    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
    '#suffix' => '</div>',
  );
  
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['gallery_assist_'. $type .'_t_items_per_row'] = array(
    '#type' => 'select',
    '#title' => t('Thumbnails per row for the teaser'),
    '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
    '#default_value' => $settings['t_items_per_row'],
    '#description' => t('Default is 0. Use this value to overwrite the teaser nummber of thumbnails pro row.'),
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['thumbnails'] = array(
    '#type' => 'fieldset',
    '#title' => t('Thumbnails'),
    '#prefix' => '<div class="ga-clear-both">',
    '#suffix' => '</div>',
    '#class' => 'ga-clear-both',
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['thumbnails']['gallery_assist_'. $type .'_item_margin'] = array(
    '#type' => 'select',
    '#title' => t('Items margin'),
    '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
    '#default_value' => $settings['item_margin'],
    '#prefix' => '<div class="gallery-assist-settings-left-half">',
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['thumbnails']['gallery_assist_'. $type .'_item_padding'] = array(
    '#type' => 'select',
    '#title' => t('Items padding'),
    '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
    '#default_value' => $settings['item_padding'],
    '#suffix' => '</div>',
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['thumbnails']['gallery_assist_'. $type .'_item_border'] = array(
    '#type' => 'select',
    '#title' => t('Items border'),
    '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
    '#default_value' => $settings['item_border'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['thumbnails']['gallery_assist_'. $type .'_item_border_color'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#class' => 'farbtastic-picker',
    '#title' => t('Items border color'),
    '#default_value' => $settings['item_border_color'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['help_links'] = array(
    '#type' => 'fieldset',
    '#title' => t('Titles and Help links'),
  );

  $desc['layout']['show_backlink'] = t('Enable this parameter to display a back link if the breadcrumb is disabled.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['help_links']['gallery_assist_'. $type .'_show_backlink'] = array(
    '#type' => 'select',
    '#title' => t('Back link'),
    '#options' => array(0 => t('off'), 1 => t('on')),
    '#default_value' => $settings['show_backlink'],
    '#description' => $desc['layout']['show_backlink'],
  );

  $desc['layout']['show_title'] = t('With creation of the article are images-titles shown.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['help_links']['gallery_assist_'. $type .'_show_title'] = array(
    '#type' => 'select',
    '#title' => t('Display titles'),
    '#options' => array(0 => t('off'), 1 => t('on')),
    '#default_value' => $settings['show_title'] ? $settings['show_title'] : 0,
    '#description' => $desc['layout']['show_title'],
  );

  $desc['layout']['title_substr'] = t('Adjust the length of the titles to the width of the miniatures.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['help_links']['gallery_assist_'. $type .'_title_substr'] = array(
    '#title' => t('Cut item title text for display'),
    '#default_value' => $settings['title_substr'],
    '#type' => 'textfield',
    '#size' => 10,
    '#description' => $desc['layout']['title_substr'],
  );

  $desc['layout']['show_toggle'] = t('Toggle to show or hide the images titles.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['help_links']['gallery_assist_'. $type .'_show_toggle'] = array(
    '#type' => 'select',
    '#title' => t('Title links toggle'),
    '#options' => array(0 => t('off'), 1 => t('on')),
    '#default_value' => $settings['show_toggle'],
    '#description' => $desc['layout']['show_toggle'],
  );

  $desc['layout']['position_toggle'] = t('The link can be possisioned acord your goal bellow or above of the gallery  container.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['help_links']['gallery_assist_'. $type .'_position_toggle'] = array(
    '#type' => 'select',
    '#title' => t('Title links position'),
    '#options' => array(0 => t('Above of the gallery container'), 1 => t('Below of the gallery container')),
    '#default_value' => $settings['position_toggle'],
    '#description' => $desc['layout']['position_toggle'],
  );

  // Default values to the positioning and behavior from GA gallery container.. 
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['teaser'] = array(
    '#type' => 'fieldset',
    '#title' => t('Teaser container'),
    '#options' => array('none' => 'none', 'left' => 'left', 'center' => 'center', 'right' => 'right'),
    '#prefix' => '<div class="gallery-assist-settings-left-half">',
    '#suffix' => '</div>',
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['pager'] = array(
    '#type' => 'fieldset',
    '#title' => t('Pager container'),
    '#options' => array('none' => 'none', 'left' => 'left', 'center' => 'center', 'right' => 'right'),
    '#prefix' => '<div class="gallery-assist-settings-left-half">',
    '#suffix' => '</div>',
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['teaser']['gallery_assist_'. $type .'_ga_align'] = array(
    '#type' => 'select',
    '#title' => t('Teaser container align'),
    '#options' => array('none' => t('none'), 'left' => t('left'), 'center' => t('center'), 'right' => t('right')),
    '#default_value' => $settings['layout']['ga_align'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['teaser']['gallery_assist_'. $type .'_t_ga_float'] = array(
    '#type' => 'select',
    '#title' => t('Teaser container float'),
    '#options' => array('none' => t('none'), 'left' => t('left'), 'right' => t('right')),
    '#default_value' => $settings['layout']['t_ga_float'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['pager']['gallery_assist_'. $type .'_gap_align'] = array(
    '#type' => 'select',
    '#title' => t('Page container align'),
    '#options' => array('none' => t('none'), 'left' => t('left'), 'center' => t('center'), 'right' => t('right')),
    '#default_value' => $settings['layout']['gap_align'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_layout']['pager']['gallery_assist_'. $type .'_p_ga_float'] = array(
    '#type' => 'select',
    '#title' => t('Page container float'),
    '#options' => array('none' => t('none'), 'left' => t('left'), 'right' => t('right')),
    '#default_value' => $settings['layout']['p_ga_float'],
  );

  // Pager.
  // $desc['pager'][''] = t('');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_pager'] = array(
    '#type' => 'fieldset',
    '#title' => t('Pager'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    //'#description' => t(''),
  );

  $desc['pager']['type'] = t('The pager can be numeric or graphic. <br />If graphic enabled it shows mini thumnails. The Gallery Assist Pager is customizable through a template.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_pager']['gallery_assist_'. $type .'_pager_symbol'] = array(
    '#title' => t('Type'),
    '#default_value' => $settings['pager_symbol'],
    '#type' => 'select',
    '#options' => array('0' => t('Numeric'), '1' => t('Icons')),
    '#description' => $desc['pager']['type'],
    '#prefix' => '<div class="gallery-assist-settings-left-half">',
  );

  $options['pager']['layout'] = gallery_assist_pager_options();
  $desc['pager']['layout'] = count($options['pager']['layout']) > 1 ? t('Choose a layout.') : t('To obtain the list of avaiable skins create a folder ga_pager in your theme folder and copy there the wanted skins from your_contributed_modules/gallery_assist/ga_pager to.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_pager']['gallery_assist_'. $type .'_pager_layout'] = array(
    '#title' => t('Layout'),
    '#default_value' => $settings['pager_layout'],
    '#type' => 'select',
    '#options' => $options['pager']['layout'],
    '#description' => $desc['pager']['layout'],
  );

  $desc['pager']['position'] = t('Weight of the gallery pager in the GA Image View relative to the image-container.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_pager']['gallery_assist_'. $type .'_pager_position'] = array(
    '#title' => t('Position'),
    '#default_value' => $settings['pager_position'],
    '#type' => 'select',
    '#options' => array('top' => t('top'), 'bottom' => t('bottom'), 'both' => t('both')),
    '#description' => $desc['pager']['position'],
    '#suffix' => '</div>',
  );

  $desc['pager']['visibles'] = t('Choose how many items should having the GA-pager on this node type.');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_pager']['gallery_assist_'. $type .'_pager_visibles'] = array(
    '#title' => t('Quantity of visible links'),
    '#default_value' => $settings['pager_visibles'],
    '#type' => 'textfield',
    '#size' => 10,
    '#description' => $desc['pager']['visibles'],
  );

  $desc['pager']['pager_t_height'] = t('');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_pager']['gallery_assist_'. $type .'_pager_t_height'] = array(
    '#title' => t('Thumbnail height'),
    '#default_value' => $settings['pager_t_height'],
    '#type' => 'textfield',
    '#size' => 10,
    '#description' => $desc['pager']['pager_t_height'],
    '#access' => is_numeric($settings['pager_presetid']) ? FALSE : TRUE,
  );

  $desc['pager']['pager_t_active_height'] = t('');
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_pager']['gallery_assist_'. $type .'_pager_t_active_height'] = array(
    '#title' => t('Thumbnail height: active'),
    '#default_value' => $settings['pager_t_active_height'],
    '#type' => 'textfield',
    '#size' => 10,
    '#description' => $desc['pager']['pager_t_active_height'],
    '#access' => is_numeric($settings['pager_presetid']) ? FALSE : TRUE,
  );

  if (module_exists('imagecache')) {
    $presets = imagecache_presets();
    $options['pager']['thm_presets'] = array('none' => 'none');
    foreach ($presets as $presetid => $value) {
      $options['pager']['thm_presets'][$presetid] = $value['presetname'];
    }
  
    $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_pager']['gallery_assist_'. $type .'_pager_presetid'] = array(
      '#type' => 'select',
      '#title' => t('ImageCache preset'),
      '#default_value' => $settings['pager_presetid'],
      //'#options' => _gallery_assist_get_ic_presets(),
      '#options' => $options['pager']['thm_presets'],
      '#description' => t('Current used setting: @pager_thm_presetname', array('@pager_thm_presetname' => $options['pager']['thm_presets'][$settings['pager_presetid']])),
    );
  }

  // Others.
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_others'] = array(
    '#type' => 'fieldset',
    '#title' => t('Others'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    //'#access' => FALSE,
  );

  if (empty($settings['show_download_link'])) {
    //$settings['show_download_link'] = 1;
  }
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_others']['gallery_assist_'. $type .'_show_download_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display the download link'),
    '#default_value' => $settings['show_download_link'],
    '#description' => t('If cheked the download link is displayed. The value can be setted for each node and for each gallery item.'),
    '#prefix' => '<div class="gallery-assist-settings-left-half">',
  );

  $h_li = array(
    t('privat: <em>Only the owner can see the gallery.</em>'),
    t('public: <em>Public for all authenticated users.</em>'),
    t('public for all: <em>Public for all users and anonymous.</em>'),
  );
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_others']['gallery_assist_'. $type .'_public_status'] = array(
    '#type' => 'select',
    '#title' => t('Gallery Public Status'),
    '#default_value' => $settings['public_status'],
    '#options' => array(0 => t('privat'), 1 => t('public'), 2 => t('public for all')),
    '#description' => theme('item_list', $h_li, NULL, 'ul'),
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_others']['gallery_assist_'. $type .'_max_items'] = array(
    '#type' => 'textfield',
    '#title' => t('Images per node'),
    '#default_value' => $settings['max_items'],
    '#size' => 8,
    '#description' => t('Allowed amount of images per article (node)'),
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_others']['gallery_assist_'. $type .'_max_form_items'] = array(
    '#type' => 'select',
    '#title' => t('Amount of uploads fields'),
    '#default_value' => $settings['max_form_items'],
    '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
    '#description' => t('Allowed amount of form upload fields per article (node)'),
  );

  if (module_exists('gallery_assist_comments')) {
    $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_others']['gallery_assist_'. $type .'_comments'] = array(
      '#type' => 'checkbox',
      '#title' => t('Comments'),
      '#default_value' => $settings['comments'],
      '#description' => t('If cheked, comment functionalty for gallery items is enable.'),
    );
  }

  if (module_exists('gallery_assist_item')) {
    $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_item'] = array(
      '#type' => 'fieldset',
      '#title' => t('Display-Item Effects'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      //'#description' => t(''),
    );
  }

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings permissions.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Allow gallery owners the configuration of some settings on their nodes with gallerie functionality.'),
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_common_settings_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('<strong>Settings to this Gallery::Common</strong>'),
    '#default_value' => $settings['common_settings_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_gallery_public_status_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Common::Gallery Public Status'),
    '#default_value' => $settings['gallery_public_status_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_container_format_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Common::Display this gallery as'),
    '#default_value' => $settings['container_format_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_gallery_container_weight_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Common::Gallery container weight'),
    '#default_value' => $settings['gallery_container_weight_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_show_in_homepage_block_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Common::Show in block'),
    '#default_value' => $settings['show_in_homepage_block_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_show_in_userprofile_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Common::Show in user profile'),
    '#default_value' => $settings['show_in_userprofile_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_show_titles_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Common::Show titles'),
    '#default_value' => $settings['show_titles_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_gallery_items_shadow_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Common::Shadow'),
    '#default_value' => $settings['gallery_items_shadow_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_extra_layout_settings_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('<strong>Settings to this Gallery::Extra Layout</strong>'),
    '#default_value' => $settings['extra_layout_settings_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_hide_teaser_container_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Extra-layout-Teaser::Hide gallery container'),
    '#default_value' => $settings['hide_teaser_container_allowed'],
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_users_perms']['gallery_assist_'. $type .'_display_download_link_allowed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Settings to this Gallery::Extra-layout-Preview::Display download link'),
    '#default_value' => $settings['display_download_link_allowed'],
  );

  // Hidden values.
  $form['gallery_assist_'. $type .'_settings'][$type]['gallery_assist_'. $type .'_change'] = array(
    '#type' => 'hidden',
    '#value' => TRUE,
  );

  $form['gallery_assist_'. $type .'_settings'][$type]['node_type'] = array(
    '#type' => 'hidden',
    '#value' => $type,
  );

  $form['submit']['save'] = array(
    '#type' => 'submit',
    '#name' => 'save',
    '#value' => t('Save'),
  );
  
  $form['gallery_assist_'. $type .'_settings']['#suffix'] = '<div id="ga-devel-by">'. gallery_assist_copyright() .'</div>';
  $form['#submit'][] = 'gallery_assist_settings_submit';
  
  return $form;
}

/**
 * List all existing content types for the assigment of the gallery functionality.
 * 
 * @ingroup forms
 */
function _gallery_assist_assigments_form() {
  $form = array();

  $types = node_get_types();
  unset($types['gallery_assist']);
  foreach ($types as $typ => $v) {
    $default_value = variable_get('gallery_assist_'. $v->type, 0);

    $form['gallery_assist_assigments']['gallery_assist_'. $v->type] = array(
      '#type' => 'checkbox',
      '#title' => $v->name,
      '#default_value' => $default_value,
    );

    $form['gallery_assist_assigments']['gallery_assist_'. $v->type .'_changed'] = array(
      '#type' => 'hidden',
      '#value' => $default_value,
    );
  }

  return $form;
}

/**
 * Build the tab with extra settings for Gallery Assist.
 * It is used to display some general settings and the forms from the submodules.
 * 
 * @ingroup forms
 */
function gallery_assist_settings_extras() {
  global $user, $base_path;

  $import_path = file_directory_path() .'/u'. $user->uid .'/import';
  $dscan1 = file_scan_directory($import_path, '.', array(), 0, FALSE);
  $paths1 = array_keys($dscan1);

  $form = array();
  $form['gallery_assist_extras'] = array(
    '#type' => 'fieldset',
    '#title' => t('Gallery Assist Extras'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  //
  $form['gallery_assist_extras']['gallery_assist_upload_conf'] = array(
    '#type' => 'fieldset',
    '#title' => t('Upload proccess'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    //'#description' => t('Better only for experts.'),
    '#weight' => -50,
  );
  $form['gallery_assist_extras']['gallery_assist_upload_conf']['gallery_assist_upload_method'] = array(
    '#type' => 'select',
    '#title' => t('Files save method'),
    '#default_value' => variable_get('gallery_assist_upload_method', 1),
    '#options' => array( 0 => t('Rename files'), 1 => t('Replace files')),
    '#description' => 'If you choose "Replace files" existing files will be replaced by uploading files through the new file with the same name.<br>If you choose "Rename files" new files with same name as existing files will be renamed with a counter at the end.',
  );

  // Assigments
  if (user_access('administer gallery_assist')) {
    $form['gallery_assist_extras']['gallery_assist_assigments'] = array(
      '#type' => 'fieldset',
      '#title' => t('Gallery functionality assigments'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => -50,
      '#description' => t('Assign the gallery functionality to other content types.'),
    );

    $form['gallery_assist_extras']['gallery_assist_assigments'] += _gallery_assist_assigments_form();
  }

  if (module_exists('imce')) {
    $form['gallery_assist_extras']['gallery_assist_import_directories'] = array(
      '#type' => 'radios',
      //'#title' => '',
      '#options' => $paths1,
    );
  }

  $form['gallery_assist_extras']['others'] = array(
    '#type' => 'fieldset',
    '#title' => t('Others'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => 40,
  );

  $form['gallery_assist_extras']['others']['gallery_assist_editform_pager_limit'] = array(
    '#type' => 'select',
    '#title' => t('Pager limit in the node edit form'),
    '#description' => t('Limit of the number of listed images in the edit form.'),
    '#default_value' => variable_get('gallery_assist_editform_pager_limit', 'none'),
    '#options' => drupal_map_assoc(array('none', 10, 15, 20, 25, 30, 35, 40, 45, 50)),
  );

  $form['gallery_assist_extras']['others']['gallery_assist_forms_possition'] = array(
    '#type' => 'textfield',
    '#title' => t('Gallery Assist form fiedsets weight'),
    '#description' => t('A little help tu set the weight of fieldsets.'),
    '#default_value' => variable_get('gallery_assist_forms_possition', -2),
    '#size' => 8
  );
  
  $form['gallery_assist_extras']['clear_cached_data'] = array(
    '#type' => 'fieldset',
    '#title' => t('Clear cached data from Gallery Assist'),
    '#weight' => 50,
    '#description' => t('Warning: high-traffic sites with many galleries will experience performance slowdowns while cached data is rebuilt.'),

  );

  $form['gallery_assist_extras']['clear_cached_data']['gallery_assist_clearthecache'] = array(
    '#type' => 'submit',
    '#title' => t('Clear the Gallery Assist cached data'),
    '#name' => 'clearthecache',
    '#value' => t('Clear the Gallery Assist cached data'),
    '#submit' => array('gallery_assist_clearthecache'),
  );

  $form['submit']['save'] = array(
    '#type' => 'submit',
    '#name' => 'save',
    '#value' => t('Save'),
  );

  $form['gallery_assist_extras']['#suffix'] = '<div id="ga-devel-by">'. gallery_assist_copyright() .'</div>';
  $form['#submit'][] = 'gallery_assist_nodetypes_submit';
  $form['#validate'][] = 'gallery_assist_nodetypes_submit_validate';

  return $form;
}

/**
 * List the aviable Gallery Assist Pager layouts.
 * 
 * @ingroup forms
 */
function gallery_assist_pager_options() {
  $pager_path_theme = drupal_get_path('theme', variable_get('theme_default', FALSE)) .'/ga_pager';
  $scan['theme'] = file_scan_directory($pager_path_theme, '.', $nomask = array('.', '..', 'README.txt'), 0, FALSE);
  $pager_options = array('default' => 'default');

  foreach ($scan as $dscan) { 

    foreach ($dscan as $p => $d) {
      if (!is_file($d->basename)) {
        $pager_options[$d->basename] = $d->basename;
      }
    }

  }

  return $pager_options;
}

/**
 * The Gallery Assist default form to customize the upload sizes and the display sizes.
 * The two separated size-settings are necessary to obtain a little of flexibility by setup the  layout ( in cases where ImageCache is not installed).
 * 
 * @ingroup forms
 */
function _gallery_assist_sizes_settings_form($type, $settings = array()) {

  $form = array();
  // Upload.
  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Sizes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_upload'] = array(
    '#type' => 'fieldset',
    '#title' => t('Upload'),
    //'#collapsible' => TRUE,
    //'#collapsed' => TRUE,
    '#prefix' => '<div class="gallery-assist-settings-left-half">',
    '#suffix' => '</div>',
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_upload']['gallery_assist_'. $type .'_upload_thm'] = array(
    '#type' => 'textfield',
    '#title' => t('Thumbnail upload size'),
    '#size' => 5,
    '#default_value' =>  $settings['upload_thm'],
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_upload']['gallery_assist_'. $type .'_upload_prev'] = array(
    '#type' => 'textfield',
    '#title' => t('Preview upload size'),
    '#size' => 5,
    '#default_value' => $settings['upload_prev'],
  );

  // Layout.
  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_lsizes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display'),
    //'#collapsible' => TRUE,
    //'#collapsed' => TRUE,
    '#prefix' => '<div class="gallery-assist-settings-left-half">',
    '#suffix' => '</div>',
  );

  if ($settings['thumbnail_size'] == '' && $settings['thumbnail_size'] == 0) {
    $settings['thumbnail_size'] = $settings['thumbnail_size'] = 100;
  }

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_lsizes']['gallery_assist_'. $type .'_thumbnail_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Thumbnail display size'),
    '#size' => 5,
    '#default_value' => $settings['thumbnail_size'],
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_lsizes']['gallery_assist_'. $type .'_preview_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Preview display size'),
    '#size' => 5,
    '#default_value' => $settings['preview_size'],
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_lsizes']['gallery_assist_'. $type .'_thumbnail_size_plus'] = array(
    '#type' => 'textfield',
    '#title' => t('Extra thumbnail custom size'),
    '#size' => 5,
    '#default_value' => $settings['thumbnail_size_plus'],
    '#description' => t('Extend the width of thumbnails needed for layout.'),
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_lsizes']['gallery_assist_'. $type .'_force_image_height'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force image height'),
    '#size' => 5,
    '#default_value' => $settings['force_image_height'],
    '#access' => module_exists('imagecache') && variable_get('gallery_assist_use_imagecache', 0) == 1 ? TRUE : FALSE,
  );

  return $form;
}

/**
 * This form replace the sizes form and shows the ImageCache presets.
 * 
 * @ingroup forms
 */
function _gallery_assist_get_ic_settings_form($type, $settings) {
  $form = array();

  // Upload.
  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes'] = array(
    '#type' => 'fieldset',
    '#title' => t('ImagaCache presets'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  $presets = imagecache_presets();
  foreach ($presets as $presetid => $value) {
    $options[$presetid] = $value['presetname'];
  }

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_thm_presetid'] = array(
    '#type' => 'select',
    '#title' => t('Thumbnail preset'),
    '#default_value' => $settings['thm_presetid'],
    '#options' => $options,
    '#description' => t('Current used setting: @thm_presetname', array('@thm_presetname' => $settings['thm_presetname'])),
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_prev_presetid'] = array(
    '#type' => 'select',
    '#title' => t('Preview preset'),
    '#default_value' => $settings['prev_presetid'],
    '#options' => $options,
    '#description' => t('Current used setting: @prev_presetname', array('@prev_presetname' => $settings['prev_presetname'])),
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_force_image_height'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force image height'),
    '#size' => 5,
    '#default_value' => $settings['force_image_height'],
  );

  $form['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_sizes']['gallery_assist_'. $type .'_thumbnail_size_plus'] = array(
    '#type' => 'textfield',
    '#title' => t('Extra thumbnail custom size'),
    '#size' => 5,
    '#default_value' => $settings['thumbnail_size_plus'],
    '#description' => t('Extend the width of thumbnails needed for layout.'),
  );

  return $form;
}

/**
 * This function print the copyright of this module at the back-end interface.
 * 
 * @ingroup forms
 */
function gallery_assist_copyright() {
  return t('Module developed by <a target="_blank" href="@ga_creator_link">Juan Morejón</a> from the <a target="_blank" href="@qtg_link">Quiptime Group</a>', array(
    '@ga_creator_link' => url('http://simple.puntolatinoclub.de'), 
    '@qtg_link' => url('http://www.quiptime.com'),
  ));
}

/**
 * Menu callback for the upload settings form.
 * A takeover from upload module.
 */
function gallery_assist_upload_settings($type, $settings) {
  if (!empty($settings['validate_upload']['resolution'])) {
    $sizes = explode('x', $settings['validate_upload']['resolution']);
    $compare1 = $sizes[0] > $sizes[1] ? $sizes[0] : $sizes[1];
    $compare2 = module_exists('imagecache') && variable_set('gallery_assist_use_imagecache', 0) == 1 ? $settings['preview_size'] : $settings['upload_prev'];
    if ($compare1 < $compare2) {
      $settings['validate_upload']['resolution'] = $compare2 .'x'. $compare2;
    }
  }

  $form['ga_upload_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node type upload settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#size' => -10,
  );

  $desc['upload']['resolution'] = t('The maximum allowed image size (e.g. 640x480). Set to 0 for no restriction. If an <a href="!image-toolkit-link">image toolkit</a> is installed, files exceeding this value will be scaled down to fit.', array('!image-toolkit-link' => url('admin/settings/image-toolkit')));
  $form['ga_upload_settings'][$type]['gallery_assist_'. $type .'_resolution'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum resolution for uploaded images'),
    '#default_value' => empty($settings['validate_upload']['resolution']) ? 0 : $settings['validate_upload']['resolution'],
    '#size' => 15,
    '#maxlength' => 10,
    '#description' => $desc['upload']['resolution'],
    '#field_suffix' => '<kbd>'. t('WIDTHxHEIGHT') .'</kbd>'
  );

  $desc['upload']['extensions'] = t('Extensions that users can upload. Separate extensions with a space and do not include the leading dot. Alloved are jpg jpeg gif png.');
  $form['ga_upload_settings'][$type]['gallery_assist_'. $type .'_extensions'] = array(
    '#type' => 'textfield',
    '#title' => t('Default permitted file extensions'),
    '#default_value' => $settings['validate_upload']['extensions'],
    '#maxlength' => 255,
    '#description' => $desc['upload']['extensions'],
  );

  $desc['upload']['file_size'] = t('The default maximum file size a user can upload. If an image is uploaded and a maximum resolution is set, the size will be checked after the file has been resized. Use dot and number (.5) for an file size smoll as 1MB.');
  $form['ga_upload_settings'][$type]['gallery_assist_'. $type .'_file_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Default maximum file size per upload'),
    '#default_value' => ($settings['validate_upload']['file_size'] / 1024) / 1024,
    '#size' => 5,
    '#maxlength' => 5,
    '#description' => $desc['upload']['file_size'],
    '#field_suffix' => t('MB'),
  );

  $desc['upload']['user_size'] = t('The default maximum size of all files a user can have on the site.');
  $form['ga_upload_settings'][$type]['gallery_assist_'. $type .'_user_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Default total file size per user'),
    '#default_value' => ($settings['validate_upload']['user_size'] / 1024) / 1024,
    '#size' => 5,
    '#maxlength' => 5,
    '#description' => $desc['upload']['user_size'],
    '#field_suffix' => t('MB'),
  );

  $form['ga_upload_settings']['upload_max_size'] = array('#value' => '<p>'. t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))) .'</p>');

  return $form;
}
