imagestyleflush.module 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @file
  4. * functionality for flushing image styles.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function imagestyleflush_menu() {
  10. $items['admin/config/media/image-styles/flush'] = array(
  11. 'title' => 'Flush all styles',
  12. 'description' => 'Flush all image styles.',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('imagestyleflush_form'),
  15. 'access arguments' => array('administer image styles'),
  16. 'type' => MENU_LOCAL_ACTION,
  17. 'weight' => 3,
  18. );
  19. $items['admin/config/media/image-styles/flush/%image_style'] = array(
  20. 'title' => 'Flush style',
  21. 'description' => 'Flush an image style.',
  22. 'page callback' => 'drupal_get_form',
  23. 'page arguments' => array('imagestyleflush_form', 5),
  24. 'access arguments' => array('administer image styles'),
  25. );
  26. return $items;
  27. }
  28. /**
  29. * Implements hook_theme_registry_alter().
  30. */
  31. function imagestyleflush_theme_registry_alter(&$theme_registry) {
  32. $theme_registry['image_style_list']['function'] = 'imagestyleflush_image_style_list';
  33. }
  34. /**
  35. * theme_image_style_list() override function.
  36. *
  37. * @see image.admin.inc
  38. */
  39. function imagestyleflush_image_style_list($variables) {
  40. $styles = $variables['styles'];
  41. $header = array(t('Style name'), t('Settings'), array('data' => t('Operations'), 'colspan' => 3));
  42. $rows = array();
  43. foreach ($styles as $style) {
  44. $row = array();
  45. $row[] = l($style['label'], 'admin/config/media/image-styles/edit/' . $style['name']);
  46. $link_attributes = array(
  47. 'attributes' => array(
  48. 'class' => array('image-style-link'),
  49. ),
  50. );
  51. if ($style['storage'] == IMAGE_STORAGE_NORMAL) {
  52. $row[] = t('Custom');
  53. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  54. $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
  55. $row[] = l(t('delete'), 'admin/config/media/image-styles/delete/' . $style['name'], $link_attributes);
  56. }
  57. elseif ($style['storage'] == IMAGE_STORAGE_OVERRIDE) {
  58. $row[] = t('Overridden');
  59. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  60. $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
  61. $row[] = l(t('revert'), 'admin/config/media/image-styles/revert/' . $style['name'], $link_attributes);
  62. }
  63. else {
  64. $row[] = t('Default');
  65. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  66. $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
  67. $row[] = '';
  68. }
  69. $rows[] = $row;
  70. }
  71. if (empty($rows)) {
  72. $rows[] = array(array(
  73. 'colspan' => 4,
  74. 'data' => t('There are currently no styles. <a href="!url">Add a new one</a>.', array('!url' => url('admin/config/media/image-styles/add'))),
  75. ));
  76. }
  77. return theme('table', array('header' => $header, 'rows' => $rows));
  78. }
  79. /**
  80. * Form constructor for the confirm form.
  81. *
  82. * @param $style
  83. * Associative array can contain a style name. Optional.
  84. *
  85. * @see imagestyleflush_form_submit()
  86. * @ingroup forms
  87. */
  88. function imagestyleflush_form($form, &$form_state, $style = NULL) {
  89. if (isset($style)) {
  90. $form = confirm_form(
  91. array(
  92. 'style_name' => array(
  93. '#type' => 'value',
  94. '#value' => $style['name'],
  95. ),
  96. ),
  97. t('Are you sure you want to flush the %style image style?', array('%style' => $style['label'])),
  98. 'admin/config/media/image-styles',
  99. t('This action cannot be undone.'),
  100. t('Flush'), t('Cancel')
  101. );
  102. }
  103. else {
  104. $form = confirm_form(
  105. NULL,
  106. t('Are you sure you want to flush all image styles?'),
  107. 'admin/config/media/image-styles',
  108. t('This action cannot be undone.'),
  109. t('Flush'), t('Cancel')
  110. );
  111. }
  112. return $form;
  113. }
  114. /**
  115. * Form submission handler for imagestyleflush_form().
  116. *
  117. * @see imagestyleflush_form()
  118. */
  119. function imagestyleflush_form_submit($form, &$form_state) {
  120. if (isset($form_state['values']['style_name'])) {
  121. $style = image_style_load($form_state['values']['style_name']);
  122. $operations[] = array('image_style_flush', array($style));
  123. }
  124. else {
  125. foreach (image_styles() as $style) {
  126. $operations[] = array('image_style_flush', array($style));
  127. }
  128. }
  129. $batch = array(
  130. 'operations' => $operations,
  131. 'finished' => 'imagestyleflush_batch_finished',
  132. );
  133. batch_set($batch);
  134. }
  135. /**
  136. * Batch message.
  137. */
  138. function imagestyleflush_batch_finished($success, $results, $operations) {
  139. if ($success) {
  140. drupal_set_message(t('Image styles were successfully flushed.'));
  141. }
  142. else {
  143. drupal_set_message(t('An error occurred while flushing the image caches.'), 'error');
  144. }
  145. drupal_goto('admin/config/media/image-styles');
  146. }