file_styles.admin.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the File: Styles module.
  5. */
  6. /**
  7. * Menu callback; Listing of all current File styles.
  8. */
  9. function file_style_list() {
  10. $page = array();
  11. $styles = file_styles();
  12. $page['file_style_list'] = array(
  13. '#markup' => theme('file_style_list', array('styles' => $styles)),
  14. '#attached' => array(
  15. 'css' => array(drupal_get_path('module', 'file_styles') . '/file_styles.admin.css' => array('preprocess' => FALSE)),
  16. ),
  17. );
  18. return $page;
  19. }
  20. /**
  21. * Form builder; Form for adding a new file style.
  22. *
  23. * @ingroup forms
  24. * @see file_style_add_form_submit()
  25. * @see file_style_name_validate()
  26. */
  27. function file_style_add_form($form, &$form_state) {
  28. $form['name'] = array(
  29. '#type' => 'textfield',
  30. '#size' => '64',
  31. '#title' => t('Style name'),
  32. '#default_value' => '',
  33. '#description' => t('The name is used in URLs for generated file. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
  34. '#element_validate' => array('file_style_name_validate'),
  35. '#required' => TRUE,
  36. );
  37. $form['submit'] = array(
  38. '#type' => 'submit',
  39. '#value' => t('Create new style'),
  40. );
  41. return $form;
  42. }
  43. /**
  44. * Submit handler for adding a new file style.
  45. */
  46. function file_style_add_form_submit($form, &$form_state) {
  47. $style = array('name' => $form_state['values']['name']);
  48. $style = file_style_save($style);
  49. drupal_set_message(t('Style %name was created.', array('%name' => $style['name'])));
  50. $form_state['redirect'] = 'admin/config/file/file-styles/edit/' . $style['name'];
  51. }
  52. /**
  53. * Element validate function to ensure unique, URL safe style names.
  54. */
  55. function file_style_name_validate($element, $form_state) {
  56. // Check for duplicates.
  57. $styles = file_styles();
  58. if (isset($styles[$element['#value']]) && (!isset($form_state['file_style']['msid']) || $styles[$element['#value']]['msid'] != $form_state['file_style']['msid'])) {
  59. form_set_error($element['#name'], t('The file style name %name is already in use.', array('%name' => $element['#value'])));
  60. }
  61. // Check for illegal characters in file style names.
  62. if (preg_match('/[^0-9a-z_\-]/', $element['#value'])) {
  63. form_set_error($element['#name'], t('Please only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for style names.'));
  64. }
  65. }
  66. /**
  67. * Form builder; Edit a file style name and effects order.
  68. *
  69. * @param $form_state
  70. * An associative array containing the current state of the form.
  71. * @param $style
  72. * An file style array.
  73. * @ingroup forms
  74. * @see file_style_form_submit()
  75. * @see file_style_name_validate()
  76. */
  77. function file_style_form($form, &$form_state, $file_style) {
  78. $title = t('Edit %name style', array('%name' => $file_style['name']));
  79. drupal_set_title($title, PASS_THROUGH);
  80. // Adjust this form for styles that must be overridden to edit.
  81. $editable = (bool) ($file_style['storage'] & IMAGE_STORAGE_EDITABLE);
  82. if (!$editable && empty($form_state['input'])) {
  83. drupal_set_message(t('This file style is currently being provided by a module. Click the "Override defaults" button to change its settings.'), 'warning');
  84. }
  85. $form_state['file_style'] = $file_style;
  86. $form['#tree'] = TRUE;
  87. $form['#attached']['css'][drupal_get_path('module', 'file') . '/file.admin.css'] = array('preprocess' => FALSE);
  88. $form['#attached']['css'][drupal_get_path('module', 'image') . '/image.admin.css'] = array('preprocess' => FALSE);
  89. // // Show the thumbnail preview.
  90. // $form['preview'] = array(
  91. // '#type' => 'item',
  92. // '#title' => t('Preview'),
  93. // '#markup' => theme('file_style_preview', array('style' => $style)),
  94. // );
  95. // Allow the name of the style to be changed, unless this style is
  96. // provided by a module's hook_default_file_styles().
  97. if ($file_style['storage'] & IMAGE_STORAGE_MODULE) {
  98. $form['name'] = array(
  99. '#type' => 'item',
  100. '#title' => t('File style name'),
  101. '#markup' => $file_style['name'],
  102. '#description' => t('This file style is being provided by %module module and may not be renamed.', array('%module' => $style['module'])),
  103. );
  104. }
  105. else {
  106. $form['name'] = array(
  107. '#type' => 'textfield',
  108. '#size' => '64',
  109. '#title' => t('File style name'),
  110. '#default_value' => $file_style['name'],
  111. '#description' => t('The name is used in URLs for generated file. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
  112. '#element_validate' => array('file_style_name_validate'),
  113. '#required' => TRUE,
  114. );
  115. }
  116. // Build the list of existing file effects for this file style.
  117. $form['effects'] = array(
  118. '#theme' => 'file_style_effects',
  119. );
  120. // foreach ($style['effects'] as $meid => $effect) {
  121. // $form['effects'][$meid]['#weight'] = isset($form_state['input']['effects']) ? $form_state['input']['effects'][$meid]['weight'] : NULL;
  122. // $form['effects'][$meid]['label'] = array(
  123. // '#markup' => $effect['label'],
  124. // );
  125. // $form['effects'][$meid]['summary'] = array(
  126. // '#markup' => isset($effect['summary theme']) ? theme($effect['summary theme'], array('data' => $effect['data'])) : '',
  127. // );
  128. // $form['effects'][$meid]['weight'] = array(
  129. // '#type' => 'weight',
  130. // '#default_value' => $effect['weight'],
  131. // '#access' => $editable,
  132. // );
  133. // $form['effects'][$meid]['configure'] = array(
  134. // '#type' => 'link',
  135. // '#title' => t('edit'),
  136. // '#href' => 'admin/config/file/file-styles/edit/' . $style['name'] . '/effects/' . $effect['meid'],
  137. // '#access' => $editable && isset($effect['form callback']),
  138. // );
  139. // $form['effects'][$meid]['remove'] = array(
  140. // '#type' => 'link',
  141. // '#title' => t('delete'),
  142. // '#href' => 'admin/config/file/file-styles/edit/' . $style['name'] . '/effects/' . $effect['meid'] . '/delete',
  143. // '#access' => $editable,
  144. // );
  145. // }
  146. $form['effects']['tabs'] = array(
  147. '#type' => 'vertical_tabs',
  148. );
  149. // Build the new file effect addition form and add it to the effect list.
  150. foreach (file_effect_definitions() as $definition_name => $definition) {
  151. $form['effects']['tabs'][$definition_name] = array(
  152. '#type' => 'fieldset',
  153. '#collapsible' => TRUE,
  154. '#title' => check_plain($definition['label']),
  155. '#description' => t('@label will be applied to the following:<br />Streams: %streams<br />File types: %types', array('@label' => $definition['label'], '%streams' => implode(', ', $definition['streams']), '%types' => implode(', ', $definition['mimetypes']))),
  156. );
  157. $new_effect_options = array(' ' => t('Select a file style to apply'));
  158. foreach ($definition['styles'] as $style_name => $style) {
  159. $effects = array();
  160. foreach ($style['effects'] as $effect) {
  161. $data = array();
  162. foreach ($effect['data'] as $type => $value) {
  163. $data[] = t('@type = @value', array('@type' => $type, '@value' => $value));
  164. }
  165. $effects[] = t('@effect: (@data)', array('@effect' => $effect['label'], '@data' => implode(', ', $data)));
  166. }
  167. $new_effect_options[$style_name] = t('@name - %effects', array('@name' => $style['name'], '%effects' => implode('; ', $effects))); // check_plain($style['name'] . ' ' . implode(', ', $effects));
  168. }
  169. $form['effects']['tabs'][$definition_name]['new'] = array(
  170. '#type' => 'radios',
  171. '#title' => t('Please select a style from the following options:'),
  172. '#options' => $new_effect_options,
  173. '#default_value' => isset($file_style['styles'][$definition_name]) ? $file_style['styles'][$definition_name] : ' ',
  174. );
  175. if (isset($definition['add-new-link'])) {
  176. $form['effects']['tabs'][$definition_name]['new']['#description'] = filter_xss($definition['add-new-link']);
  177. }
  178. if (isset($definition['preview-theme'])) {
  179. // theme_image_style_preview is not properly registered.
  180. module_load_include('inc', 'image', 'image.admin');
  181. // Show the thumbnail preview.
  182. $form['effects']['tabs'][$definition_name]['preview'] = array(
  183. '#type' => 'item',
  184. '#title' => t('Preview'),
  185. '#markup' => theme($definition['preview-theme'], array('style' => $definition['styles']['thumbnail'])),// $file_style['styles'][$definition_name])),
  186. );
  187. }
  188. // $form['effects']['tabs'][$definition_name]['weight'] = array(
  189. // '#type' => 'weight',
  190. // '#default_value' => count($form['effects']['tabs'][$definition_name]) - 1,
  191. // );
  192. }
  193. // $new_effect_options = array('' => t('Select a new effect'));
  194. // foreach (file_effect_definitions() as $effect => $definition) {
  195. // $new_effect_options[$effect] = check_plain($definition['name']);
  196. // }
  197. // $form['effects']['new'] = array(
  198. // '#tree' => FALSE,
  199. // '#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL,
  200. // '#access' => $editable,
  201. // );
  202. // $form['effects']['new']['new'] = array(
  203. // '#type' => 'radios',
  204. // '#options' => $new_effect_options,
  205. // );
  206. // $form['effects']['new']['add'] = array(
  207. // '#type' => 'submit',
  208. // '#value' => t('Add'),
  209. // '#validate' => array('file_style_form_add_validate'),
  210. // '#submit' => array('file_style_form_submit', 'file_style_form_add_submit'),
  211. // );
  212. // Show the Override or Submit button for this style.
  213. $form['override'] = array(
  214. '#type' => 'submit',
  215. '#value' => t('Override defaults'),
  216. '#validate' => array(),
  217. '#submit' => array('file_style_form_override_submit'),
  218. '#access' => !$editable,
  219. );
  220. $form['submit'] = array(
  221. '#type' => 'submit',
  222. '#value' => t('Update style'),
  223. '#access' => $editable,
  224. );
  225. return $form;
  226. }
  227. /**
  228. * Validate handler for adding a new file effect to a file style.
  229. */
  230. function file_style_form_add_validate($form, &$form_state) {
  231. if (!$form_state['values']['new']) {
  232. form_error($form['effects']['new']['new'], t('Select an effect to add.'));
  233. $form_state['rebuild'] = TRUE;
  234. }
  235. }
  236. /**
  237. * Submit handler for adding a new file effect to a file style.
  238. */
  239. function file_style_form_add_submit($form, &$form_state) {
  240. $style = $form_state['file_style'];
  241. // Check if this field has any configuration options.
  242. $effect = file_effect_definition_load($form_state['values']['new']);
  243. // Load the configuration form for this option.
  244. if (isset($effect['form callback'])) {
  245. $path = 'admin/config/file/file-styles/edit/' . $form_state['file_style']['name'] . '/add/' . $form_state['values']['new'];
  246. $form_state['redirect'] = array($path, array('query' => array('weight' => $form_state['values']['weight'])));
  247. }
  248. // If there's no form, imfiletely add the file effect.
  249. else {
  250. $effect['isid'] = $style['isid'];
  251. $effect['weight'] = $form_state['values']['weight'];
  252. file_effect_save($effect);
  253. drupal_set_message(t('The file effect was successfully applied.'));
  254. }
  255. }
  256. /**
  257. * Submit handler for overriding a module-defined style.
  258. */
  259. function file_style_form_override_submit($form, &$form_state) {
  260. drupal_set_message(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $form_state['file_style']['name'])));
  261. file_default_style_save($form_state['file_style']);
  262. }
  263. /**
  264. * Submit handler for saving a file style.
  265. */
  266. function file_style_form_submit($form, &$form_state) {
  267. // Update the file style name if it has changed.
  268. $style = $form_state['file_style'];
  269. if (isset($form_state['values']['name']) && $style['name'] != $form_state['values']['name']) {
  270. $style['name'] = $form_state['values']['name'];
  271. }
  272. // Update file effect weights.
  273. if (!empty($form_state['values']['effects'])) {
  274. foreach ($form_state['values']['effects'] as $meid => $effect_data) {
  275. if (isset($style['effects'][$meid])) {
  276. $effect = $style['effects'][$meid];
  277. $effect['weight'] = $effect_data['weight'];
  278. file_effect_save($effect);
  279. }
  280. }
  281. }
  282. file_style_save($style);
  283. if ($form_state['values']['op'] == t('Update style')) {
  284. drupal_set_message('Changes to the style have been saved.');
  285. }
  286. $form_state['redirect'] = 'admin/config/file/file-styles/edit/' . $style['name'];
  287. }
  288. function file_style_delete_form() {
  289. }
  290. function file_style_revert_form() {
  291. }
  292. function file_effect_form() {
  293. }
  294. function file_effect_delete_form() {
  295. }
  296. // function file_effect_form() {
  297. // }
  298. /**
  299. * Display the page containing the list of file styles.
  300. *
  301. * @param $variables
  302. * An associative array containing:
  303. * - styles: An array of all the file styles returned by file_get_styles().
  304. *
  305. * @see file_get_styles()
  306. * @ingroup themeable
  307. */
  308. function theme_file_style_list($variables) {
  309. $styles = $variables['styles'];
  310. $header = array(t('Style name'), t('Settings'), array('data' => t('Operations'), 'colspan' => 3));
  311. $rows = array();
  312. foreach ($styles as $style) {
  313. $row = array();
  314. $row[] = l($style['name'], 'admin/config/file/file-styles/edit/' . $style['name']);
  315. $link_attributes = array(
  316. 'attributes' => array(
  317. 'class' => array('file-style-link'),
  318. ),
  319. );
  320. if ($style['storage'] == IMAGE_STORAGE_NORMAL) {
  321. $row[] = t('Custom');
  322. $row[] = l(t('edit'), 'admin/config/file/file-styles/edit/' . $style['name'], $link_attributes);
  323. $row[] = l(t('delete'), 'admin/config/file/file-styles/delete/' . $style['name'], $link_attributes);
  324. }
  325. elseif ($style['storage'] == IMAGE_STORAGE_OVERRIDE) {
  326. $row[] = t('Overridden');
  327. $row[] = l(t('edit'), 'admin/config/file/file-styles/edit/' . $style['name'], $link_attributes);
  328. $row[] = l(t('revert'), 'admin/config/file/file-styles/revert/' . $style['name'], $link_attributes);
  329. }
  330. else {
  331. $row[] = t('Default');
  332. $row[] = l(t('edit'), 'admin/config/file/file-styles/edit/' . $style['name'], $link_attributes);
  333. $row[] = '';
  334. }
  335. $rows[] = $row;
  336. }
  337. if (empty($rows)) {
  338. $rows[] = array(array(
  339. 'colspan' => 4,
  340. 'data' => t('There are currently no styles. <a href="!url">Add a new one</a>.', array('!url' => url('admin/config/file/file-styles/add'))),
  341. ));
  342. }
  343. return theme('table', array('header' => $header, 'rows' => $rows));
  344. }