You are here

function dynamic_background_admin_images in Dynamic Background 7.2

Same name and namespace in other branches
  1. 6 includes/backgrounds.admin.inc \dynamic_background_admin_images()
  2. 7 includes/backgrounds.admin.inc \dynamic_background_admin_images()

The image administation form.

1 string reference to 'dynamic_background_admin_images'
dynamic_background_menu in ./dynamic_background.module
Implements hook_menu().

File

includes/backgrounds.admin.inc, line 11
Implementation of the administration image upload and selecton form.

Code

function dynamic_background_admin_images($form, &$form_state) {
  $form = array();

  // Load settings
  $settings = variable_get('dynamic_background_setting', array());
  if (empty($settings)) {
    drupal_set_message(t('You have not yet configured "Dynamic background". Click <a href="@link">here</a> to configure it.', array(
      '@link' => url('admin/config/user-interface/backgrounds/default'),
    )), 'warning');
    return;
  }

  // File upload form encoded.
  $form['#attributes'] = array(
    'enctype' => 'multipart/form-data',
  );
  $form['dynamic_background_images'] = array(
    '#type' => 'fieldset',
    '#title' => t('Upload images'),
    '#collapsed' => FALSE,
    '#collapsible' => FALSE,
    '#prefix' => '<div id="dynamic-background-upload-wrapper">',
    '#suffix' => '</div>',
  );

  // Load images
  $images = dynamic_background_load_images('default');
  $active_fid = dynamic_background_active_image('default', '-1');
  if ($active_fid) {
    $active_fid = $active_fid->fid;
  }

  // Build images form elements.
  $i = 0;
  foreach ($images as $fid => $uri) {
    $i++;
    $default = array(
      'picture' => $uri,
      'fid' => $fid,
      'picture_use' => $fid == $active_fid ? 1 : 0,
      'picture_delete' => 0,
    );
    $form['dynamic_background_images']['image_' . $fid] = array(
      '#type' => 'background_upload_form',
      '#title' => t('Background image %num', array(
        '%num' => $i,
      )),
      '#default_value' => $default,
    );
  }

  // Add empty upload slots, if any left and unlimited not selected.
  if (is_numeric($settings['num_of_pictures'])) {
    for ($i; $settings['num_of_pictures'] > $i; $i++) {
      $form['dynamic_background_images']['image_' . $i] = array(
        '#type' => 'background_upload_form',
        '#title' => t('Background image %num', array(
          '%num' => $i + 1,
        )),
        '#default_value' => array(),
      );
    }
  }
  else {

    // Add logic to handle add-one-more button.
    if (!isset($form_state['upload_slots'])) {
      $form_state['upload_slots'] = 1;
    }

    // Add empty upload slots.
    for ($slots = 0; $slots < $form_state['upload_slots']; $slots++) {
      $i++;
      $form['dynamic_background_images']['image_' . $i] = array(
        '#type' => 'background_upload_form',
        '#title' => t('Background image %num', array(
          '%num' => $i,
        )),
        '#default_value' => array(),
      );
    }

    // Create add one more button.
    $form['dynamic_background_images']['add_image'] = array(
      '#type' => 'submit',
      '#value' => t('Add one more'),
      '#submit' => array(
        'dynamic_background_admin_images_add_one',
      ),
      '#ajax' => array(
        'callback' => 'dynamic_background_admin_images_add_one_callback',
        'wrapper' => 'dynamic-background-upload-wrapper',
      ),
    );
  }

  // Give it system setting form style.
  $form['#theme'] = array(
    'system_settings_form',
  );

  // Set validators and submit handler(s).
  $form['#validate'][] = 'dynamic_background_admin_images_validate';
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  return $form;
}