imagestyleflush.module 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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('flush 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('flush image styles'),
  25. );
  26. return $items;
  27. }
  28. /**
  29. * Implements hook_permission().
  30. */
  31. function imagestyleflush_permission() {
  32. return array(
  33. 'flush image styles' => array(
  34. 'title' => t('Flush image styles'),
  35. 'description' => t('Allow users to flush image styles.'),
  36. ),
  37. );
  38. }
  39. /**
  40. * Implements hook_theme_registry_alter().
  41. */
  42. function imagestyleflush_theme_registry_alter(&$theme_registry) {
  43. $theme_registry['image_style_list']['function'] = 'imagestyleflush_image_style_list';
  44. }
  45. /**
  46. * theme_image_style_list() override function.
  47. *
  48. * @see image.admin.inc
  49. */
  50. function imagestyleflush_image_style_list($variables) {
  51. $styles = $variables['styles'];
  52. // Account for an extra column added by the image_styles_admin submodule of
  53. // the imagecache_actions module.
  54. $colspan = module_exists('image_styles_admin') ? 4 : 3;
  55. $header = array(t('Style name'), t('Settings'), array('data' => t('Operations'), 'colspan' => $colspan));
  56. $rows = array();
  57. foreach ($styles as $style) {
  58. $row = array();
  59. $row[] = l($style['label'], 'admin/config/media/image-styles/edit/' . $style['name']);
  60. $link_attributes = array(
  61. 'attributes' => array(
  62. 'class' => array('image-style-link'),
  63. ),
  64. );
  65. if ($style['storage'] == IMAGE_STORAGE_NORMAL) {
  66. $row[] = t('Custom');
  67. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  68. $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
  69. $row[] = l(t('delete'), 'admin/config/media/image-styles/delete/' . $style['name'], $link_attributes);
  70. }
  71. elseif ($style['storage'] == IMAGE_STORAGE_OVERRIDE) {
  72. $row[] = t('Overridden');
  73. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  74. $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
  75. $row[] = l(t('revert'), 'admin/config/media/image-styles/revert/' . $style['name'], $link_attributes);
  76. }
  77. else {
  78. $row[] = t('Default');
  79. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  80. $row[] = l(t('flush'), 'admin/config/media/image-styles/flush/' . $style['name'], $link_attributes);
  81. $row[] = '';
  82. }
  83. $rows[] = $row;
  84. }
  85. if (empty($rows)) {
  86. $rows[] = array(array(
  87. 'colspan' => $colspan,
  88. 'data' => t('There are currently no styles. <a href="!url">Add a new one</a>.', array('!url' => url('admin/config/media/image-styles/add'))),
  89. ));
  90. }
  91. return theme('table', array('header' => $header, 'rows' => $rows));
  92. }
  93. /**
  94. * Form constructor for the confirm form.
  95. *
  96. * @param $style
  97. * Associative array can contain a style name. Optional.
  98. *
  99. * @see imagestyleflush_form_submit()
  100. * @ingroup forms
  101. */
  102. function imagestyleflush_form($form, &$form_state, $style = NULL) {
  103. if (isset($style)) {
  104. $form = confirm_form(
  105. array(
  106. 'style_name' => array(
  107. '#type' => 'value',
  108. '#value' => $style['name'],
  109. ),
  110. ),
  111. t('Are you sure you want to flush the %style image style?', array('%style' => $style['label'])),
  112. 'admin/config/media/image-styles',
  113. '<em>' . t('Note: this will only flush the images. It will not rebuild them.')
  114. . '</em><br><br>' . t('This action cannot be undone.'),
  115. t('Flush'), t('Cancel')
  116. );
  117. }
  118. else {
  119. $form = confirm_form(
  120. NULL,
  121. t('Are you sure you want to flush all image styles?'),
  122. 'admin/config/media/image-styles',
  123. '<em>' . t('Note: this will only flush the images. It will not rebuild them.')
  124. . '</em><br><br>' . t('This action cannot be undone.'),
  125. t('Flush'), t('Cancel')
  126. );
  127. }
  128. return $form;
  129. }
  130. /**
  131. * Form submission handler for imagestyleflush_form().
  132. *
  133. * @see imagestyleflush_form()
  134. */
  135. function imagestyleflush_form_submit($form, &$form_state) {
  136. if (isset($form_state['values']['style_name'])) {
  137. $style = image_style_load($form_state['values']['style_name']);
  138. $operations[] = array('image_style_flush', array($style));
  139. }
  140. else {
  141. foreach (image_styles() as $style) {
  142. $operations[] = array('image_style_flush', array($style));
  143. }
  144. }
  145. // Redirect to the destination URL.
  146. $destination = drupal_get_destination();
  147. if ($destination['destination'] != current_path()) {
  148. $operations[] = array('imagestyleflush_batch_destination_redirect', $destination);
  149. }
  150. $batch = array(
  151. 'operations' => $operations,
  152. 'finished' => 'imagestyleflush_batch_finished',
  153. );
  154. batch_set($batch);
  155. }
  156. /**
  157. * Batch operation. Redirect the batch operation if it was called from the
  158. * admin_menu item.
  159. */
  160. function imagestyleflush_batch_destination_redirect($destination, &$context) {
  161. // Set the destination redirect.
  162. $context['results']['redirect'] = $destination;
  163. }
  164. /**
  165. * Batch message.
  166. */
  167. function imagestyleflush_batch_finished($success, $results, $operations) {
  168. if ($success) {
  169. drupal_set_message(t('Image styles were successfully flushed.'));
  170. }
  171. else {
  172. drupal_set_message(t('An error occurred while flushing the image caches.'), 'error');
  173. }
  174. if (!empty($results['redirect'])) {
  175. drupal_goto($results['redirect']);
  176. }
  177. else {
  178. // Send the user to the right place depending on their access.
  179. if (user_access('administer image styles')) {
  180. drupal_goto('admin/config/media/image-styles');
  181. }
  182. else {
  183. drupal_goto();
  184. }
  185. }
  186. }
  187. /**
  188. * Implements hook_admin_menu_output_build().
  189. */
  190. function imagestyleflush_admin_menu_output_build(&$content) {
  191. // Add link to the icon menu to flush image styles.
  192. if (isset($content['icon'])) {
  193. $styles = image_styles();
  194. $access = user_access('flush image styles');
  195. $destination = drupal_get_destination();
  196. $style_links = array();
  197. foreach ($styles as $style) {
  198. $style_links[$style['name']] = array(
  199. '#title' => $style['label'],
  200. '#href' => 'admin/config/media/image-styles/flush/' . $style['name'],
  201. '#access' => $access,
  202. '#options' => array(
  203. 'query' => $destination,
  204. ),
  205. );
  206. }
  207. $content['icon']['icon']['flush-image-styles'] = array(
  208. '#title' => t('Flush all image styles'),
  209. '#access' => $access,
  210. '#href' => 'admin/config/media/image-styles/flush',
  211. '#options' => array(
  212. 'query' => $destination,
  213. ),
  214. '#weight' => 25,
  215. ) + $style_links;
  216. }
  217. }