image_styles_admin.module 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @file Hook and callback implementations that must be available at all times.
  4. */
  5. /**
  6. * Implements hook_permission().
  7. */
  8. function image_styles_admin_permission() {
  9. return array(
  10. 'import image styles' => array(
  11. 'title' => t('Import image styles.') ,
  12. 'restrict access' => TRUE,
  13. ),
  14. );
  15. }
  16. /**
  17. * Determines access to the import image style form for the current user.
  18. */
  19. function image_styles_admin_access() {
  20. return user_access('import image styles') && user_access('administer image styles');
  21. }
  22. /**
  23. * Implements hook_menu().
  24. */
  25. function image_styles_admin_menu() {
  26. $items = array();
  27. $items['admin/config/media/image-styles/duplicate/%image_style'] = array(
  28. 'title' => 'Duplicate style',
  29. 'description' => 'Make a copy of an image style.',
  30. 'page callback' => 'image_styles_admin_duplicate_page_callback',
  31. 'page arguments' => array(5),
  32. 'access arguments' => array('administer image styles'),
  33. 'file' => 'image_styles_admin.inc',
  34. );
  35. $items['admin/config/media/image-styles/export/%image_style'] = array(
  36. 'title' => 'Export style',
  37. 'description' => 'Export an image style.',
  38. 'page callback' => 'drupal_get_form',
  39. 'page arguments' => array('image_styles_admin_export_form', 5),
  40. 'access arguments' => array('administer image styles'),
  41. 'file' => 'image_styles_admin.inc',
  42. );
  43. $items['admin/config/media/image-styles/import'] = array(
  44. 'title' => 'Import style',
  45. 'description' => 'Import an image style.',
  46. 'page callback' => 'drupal_get_form',
  47. 'page arguments' => array('image_styles_admin_import_form'),
  48. 'access callback' => 'image_styles_admin_access',
  49. 'type' => MENU_LOCAL_ACTION,
  50. 'weight' => 3,
  51. 'file' => 'image_styles_admin.inc',
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Implements hook_preprocess_HOOK for theme image_style_list.
  57. */
  58. function image_styles_admin_preprocess_image_style_list(&$variables) {
  59. // Sort the image styles by name.
  60. uasort($variables['styles'], function ($a, $b) {
  61. return strcasecmp($a['label'], $b['label']);
  62. });
  63. // Tell imagecache_actions_preprocess_table to preprocess the next call to
  64. // theme_table().
  65. $image_styles = array_values($variables['styles']);
  66. image_styles_admin_preprocess_table($image_styles);
  67. // Add CSS and JS files.
  68. drupal_add_css(drupal_get_path('module', 'image_styles_admin') . '/image_styles_admin.css');
  69. if (base_path() !== '/') {
  70. $base_path = base_path();
  71. drupal_add_css("
  72. #image-styles .expand.inner { background-image: url($base_path/misc/menu-collapsed.png) }
  73. #image-styles .expanded.expand.inner { background-image: url($base_path/misc/menu-expanded.png) }",
  74. array('type' => 'inline'));
  75. }
  76. drupal_add_js(drupal_get_path('module', 'image_styles_admin') . '/image_styles_admin.js');
  77. }
  78. /**
  79. * Implements hook_preprocess_HOOK for theme table.
  80. */
  81. function image_styles_admin_preprocess_table(&$variables) {
  82. static $image_styles = NULL;
  83. // If called from image_styles_admin_preprocess_image_style_list(), the
  84. // parameter will be a sequential array.
  85. if (key($variables) === 0) {
  86. $image_styles = $variables;
  87. }
  88. else if (!empty($image_styles)) {
  89. // Normal preprocess hook call: we only process if theme('table', ...) has
  90. // been called via theme_image_style_list() and we have a non empty list of
  91. // styles;
  92. // Set an ID on the table so it can be targeted by our CSS.
  93. $variables['attributes']['id'] = 'image-styles';
  94. // Add a class to the Style name and Settings columns for styling.
  95. foreach ($variables['header'] as &$cell) {
  96. $temp_cell = is_string($cell) ? array('data' => $cell) : $cell;
  97. $class_names = array(
  98. 'style-name' => t('Style name'),
  99. 'settings' => t('Settings'),
  100. );
  101. foreach ($class_names as $class => $name) {
  102. if ($temp_cell['data'] == $name) {
  103. $temp_cell['class'][] = $class;
  104. $cell = $temp_cell;
  105. }
  106. }
  107. }
  108. // Add the effects column header.
  109. array_splice($variables['header'], 1, 0, array(array(
  110. 'data' => t('Effects') . ' <span class="description expand" role="button">(' . t('expand all') . ')</span>',
  111. 'class' => array('effects expand-all')
  112. )));
  113. // Add a column with a summary of all effects to each row.
  114. foreach ($variables['rows'] as $i => &$row) {
  115. $style = $image_styles[$i];
  116. $effects_list = array();
  117. foreach ($style['effects'] as $key => $effect) {
  118. $definition = image_effect_definition_load($effect['name']);
  119. $effect = array_merge($definition, $effect);
  120. $style['effects'][$key] = $effect;
  121. $effect_details = isset($effect['summary theme']) ? theme($effect['summary theme'], array('data' => $effect['data'])) : '';
  122. $effects_list[] = '<span class="details">' . $effect['label'] . ' ' . $effect_details . '</span>';
  123. }
  124. // Add the effects summary column to the row.
  125. $effects_summary = array(
  126. 'data' => '<div class="inner expand" role="button">' . implode('<span class="separator">, </span>', $effects_list) . '</div>',
  127. 'class' => 'description'
  128. );
  129. array_splice($row, 1, 0, array($effects_summary));
  130. }
  131. // Find the column with the edit link in it.
  132. $i = 0;
  133. $first_row = reset($variables['rows']);
  134. foreach ($first_row as $i => &$cell) {
  135. $cell_data = is_array($cell) ? $cell['data'] : $cell;
  136. if (strpos($cell_data, '>' . t('edit') . '<') !== FALSE) {
  137. break;
  138. }
  139. }
  140. // Increase the colspan for the column with the edit link to include the
  141. // duplicate and export links as well. This *should* be 2, but Drupal core
  142. // specifies 1 more than should be there.
  143. $variables['header'][$i]['colspan'] += 1;
  144. // Add the 2 links to each row by duplicating the edit link and then
  145. // changing the text and the link.
  146. $edit_column = $i;
  147. foreach ($variables['rows'] as &$row) {
  148. $i = $edit_column;
  149. // Duplicate the edit link twice.
  150. array_splice($row, $i + 1, 0, array($row[$i], $row[$i]));
  151. // Replace edit with duplicate in text and href
  152. $i++;
  153. $row[$i] = str_replace('>' . t('edit') . '<', '>' . t('duplicate') . '<', $row[$i]);
  154. $row[$i] = preg_replace('#/admin/config/media/image-styles/edit/#', '/admin/config/media/image-styles/duplicate/', $row[$i]);
  155. // Replace edit with export in text and href
  156. $i++;
  157. $row[$i] = str_replace('>' . t('edit') . '<', '>' . t('export') . '<', $row[$i]);
  158. $row[$i] = preg_replace('#/admin/config/media/image-styles/edit/#', '/admin/config/media/image-styles/export/', $row[$i]);
  159. }
  160. // Don't preprocess subsequent calls to theme_table().
  161. $image_styles = NULL;
  162. }
  163. }