<?php

/**
 * @file
 * functionality for flushing image styles.
 */

/**
 * Implements hook_menu().
 */
function imagestyleflush_menu() {
  $items['admin/config/media/image-styles/flush'] = array(
    'title' => 'Flush all styles',
    'description' => 'Flush all image styles.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('imagestyleflush_form'),
    'access arguments' => array('administer image styles'),
    'type' => MENU_LOCAL_ACTION,
    'weight' => 3,
  );
  $items['admin/config/media/image-styles/flush/%image_style'] = array(
    'title' => 'Flush style',
    'description' => 'Flush an image style.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('imagestyleflush_form', 5),
    'access arguments' => array('administer image styles'),
  );

  return $items;
}

/**
 * Implements hook_theme_registry_alter().
 */
function imagestyleflush_theme_registry_alter(&$theme_registry) {
  $theme_registry['image_style_list']['function'] = 'imagestyleflush_image_style_list';
}

/**
 * theme_image_style_list() override function.
 *
 * @see image.admin.inc
 */
function imagestyleflush_image_style_list($variables) {
  $styles = $variables['styles'];

  $header = array(t('Style name'), t('Settings'), array('data' => t('Operations'), 'colspan' => 3));
  $rows = array();
  foreach ($styles as $style) {
    $row = array();
    $row[] = l($style['name'], 'admin/config/media/image-styles/edit/' . $style['name']);
    $link_attributes = array(
      'attributes' => array(
        'class' => array('image-style-link'),
      ),
    );
    if ($style['storage'] == IMAGE_STORAGE_NORMAL) {
      $row[] = t('Custom');
      $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
      $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
      $row[] = l(t('delete'), 'admin/config/media/image-styles/delete/' . $style['name'], $link_attributes);
    }
    elseif ($style['storage'] == IMAGE_STORAGE_OVERRIDE) {
      $row[] = t('Overridden');
      $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
      $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
      $row[] = l(t('revert'), 'admin/config/media/image-styles/revert/' . $style['name'], $link_attributes);
    }
    else {
      $row[] = t('Default');
      $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
      $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
      $row[] = '';
    }
    $rows[] = $row;
  }

  if (empty($rows)) {
    $rows[] = array(array(
      'colspan' => 4,
      'data' => t('There are currently no styles. <a href="!url">Add a new one</a>.', array('!url' => url('admin/config/media/image-styles/add'))),
      ));
  }

  return theme('table', array('header' => $header, 'rows' => $rows));
}

/**
 * Form constructor for the confirm form.
 *
 * @param $style
 *   Associative array can contain a style name. Optional.
 *
 * @see imagestyleflush_form_submit()
 * @ingroup forms
 */
function imagestyleflush_form($form, &$form_state, $style = NULL) {
  if (isset($style)) {
    $form = confirm_form(
      array(
        'style_name' => array(
          '#type' => 'value',
          '#value' => $style['name'],
        ),
      ),
      t('Are you sure you want to flush @style image style?', array('@style' => $style['name'])),
      'admin/config/media/image-styles',
       t('This action cannot be undone.'),
      t('Flush'), t('Cancel')
    );
  }
  else {
    $form = confirm_form(
      NULL,
      t('Are you sure you want to flush all image styles?'),
      'admin/config/media/image-styles',
       t('This action cannot be undone.'),
      t('Flush'), t('Cancel')
    );
  }

  return $form;
}

/**
 * Form submission handler for imagestyleflush_form().
 *
 * @see imagestyleflush_form()
 */
function imagestyleflush_form_submit($form, &$form_state) {
  if (isset($form_state['values']['style_name'])) {
    $style = image_style_load($form_state['values']['style_name']);
    $operations[] = array('image_style_flush', array($style));
  }
  else {
    foreach (image_styles() as $style) {
      $operations[] = array('image_style_flush', array($style));
    }
  }

  $batch = array(
    'operations' => $operations,
    'finished' => 'imagestyleflush_batch_finished',
  );

  batch_set($batch);
}

/**
 * Batch message.
 */
function imagestyleflush_batch_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('Image styles was successfully flushed.'));
  }
  else {
    drupal_set_message(t('An error occurred while flushing the image caches.', 'error'));
  }
  drupal_goto('admin/config/media/image-styles');
}
