styles_ui.admin.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the Styles UI module.
  5. */
  6. /**
  7. * Page callback for various styles overview listing pages.
  8. */
  9. function styles_ui_containers_overview($field_type) {
  10. // Get the full list of styles.
  11. $styles = styles_default_styles();
  12. // Get the containers for this field type.
  13. $styles_containers = styles_default_containers();
  14. $field_containers = $styles_containers[$field_type];
  15. // Build our table listing of styles.
  16. $header = array(t('Styles'), array('data' => t('Operations'), 'colspan' => 2));
  17. $rows = array();
  18. foreach ($styles[$field_type]['styles'] as $style_name => $style) {
  19. // What's the path to edit this style?
  20. $edit_path = $field_containers['admin']['path'] . '/edit/' . $style_name;
  21. $row = array(l(t('@style', array('@style' => $style_name)), $edit_path));
  22. // Add our ops.
  23. if ($style['storage'] == STYLES_STORAGE_NORMAL) {
  24. $row[] = array('data' => l(t('edit'), $edit_path));
  25. $row[] = array('data' => l(t('delete'), $field_containers['admin']['path'] . '/delete/' . $style_name));
  26. }
  27. else if ($style['storage'] == STYLES_STORAGE_OVERRIDE) {
  28. $row[] = array('data' => l(t('edit'), $edit_path));
  29. $row[] = array('data' => l(t('revert'), $field_containers['admin']['path'] . '/delete/' . $style_name));
  30. }
  31. else {
  32. $row[] = array('data' => l(t('edit'), $edit_path), 'colspan' => 2);
  33. }
  34. $rows[] = $row;
  35. }
  36. // Add a new style link to the empty table.
  37. // Note that the link at the top of the page is placed in hook_menu.
  38. $field_info = field_info_field_types($field_type);
  39. $field_label = $field_info['label'];
  40. $title = 'Add ' . $field_label . ' style';
  41. $build['styles_table'] = array(
  42. '#theme' => 'table',
  43. '#header' => $header,
  44. '#rows' => $rows,
  45. '#empty' => t('No styles available. <a href="@link">@add</a>.', array('@add' => t($title), '@link' => url($field_containers['admin']['path'] . '/add'))),
  46. );
  47. return $build;
  48. }
  49. /**
  50. * Page callback for adding a style.
  51. */
  52. function styles_ui_style_add_form($form, $form_state, $field_type) {
  53. $form = array();
  54. $form['field_type'] = array(
  55. '#type' => 'value',
  56. '#value' => $field_type,
  57. );
  58. $form['name'] = array(
  59. '#type' => 'textfield',
  60. '#title' => t('Style name'),
  61. '#description' => t('Enter the name of your desired style, which must be a unique name containing only alphanumeric characters and underscores.'),
  62. );
  63. $form['submit'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Save'),
  66. );
  67. return $form;
  68. }
  69. /**
  70. * Validation handler for the add style form.
  71. */
  72. function styles_ui_style_add_form_validate($form, $form_state) {
  73. $field_type = $form_state['values']['field_type'];
  74. $style_name = $form_state['values']['name'];
  75. $styles = styles_default_styles();
  76. if (!preg_match('!^[a-z0-9_]+$!', $style_name)) {
  77. form_set_error('style_name', t('The machine-readable style name must contain only lowercase letters, numbers, and underscores.'));
  78. }
  79. else if (!empty($styles[$field_type]['styles'][$style_name])) {
  80. form_set_error('style_name', t('The machine-readable style name %style_name is already taken.', array('%style_name' => $style_name)));
  81. }
  82. }
  83. /**
  84. * Submission handler for the add style form.
  85. */
  86. function styles_ui_style_add_form_submit(&$form, &$form_state) {
  87. $field_type = $form_state['values']['field_type'];
  88. $style_name = $form_state['values']['name'];
  89. $style = array(
  90. 'field_type' => $field_type,
  91. 'name' => $style_name,
  92. 'description' => '',
  93. );
  94. // Save the new style.
  95. styles_style_save($style);
  96. // Get the containers for this field type.
  97. $styles_containers = styles_default_containers();
  98. $field_containers = $styles_containers[$field_type];
  99. // Redirect to this style's edit page.
  100. $form_state['redirect'] = $field_containers['admin']['path'] . '/edit/' . $style_name;
  101. }
  102. /**
  103. * Callback for the style edit form.
  104. */
  105. function styles_ui_style_edit_form($form, $form_state, $field_type, $style_name) {
  106. $style = styles_style_load($field_type, $style_name);
  107. $form['field_type'] = array(
  108. '#type' => 'value',
  109. '#value' => $field_type,
  110. );
  111. $form['old_style_name'] = array(
  112. '#type' => 'value',
  113. '#value' => $style['name'],
  114. );
  115. $form['name'] = array(
  116. '#type' => 'textfield',
  117. '#title' => t('Name'),
  118. '#description' => t('The name of this style, which must be a unique name containing only alphanumeric characters and underscores.'),
  119. '#default_value' => $style['name'],
  120. '#required' => TRUE,
  121. );
  122. $form['description'] = array(
  123. '#type' => 'textarea',
  124. '#title' => t('Description'),
  125. '#description' => t('The description of the style.'),
  126. '#default_value' => $style['description'],
  127. );
  128. $form['containers'] = array(
  129. '#type' => 'vertical_tabs',
  130. '#tree' => TRUE,
  131. );
  132. $containers = styles_default_containers($field_type);
  133. $presets = styles_default_presets($field_type);
  134. foreach ($containers['containers'] as $container_name => $container) {
  135. $style = $presets['containers'][$container_name]['styles'][$style_name];
  136. $container_label = isset($container['label']) ? $container['label'] : $container_name;
  137. $form['containers'][$container_name] = array(
  138. '#type' => 'fieldset',
  139. '#title' => t('@container', array('@container' => $container_label)),
  140. );
  141. $options = array();
  142. foreach ($presets['containers'][$container_name]['presets'] as $preset_name => $preset) {
  143. $options[$preset_name] = t('@preset', array('@preset' => $preset_name));
  144. }
  145. $default_value = isset($presets['containers'][$container_name]['styles'][$style_name]['preset']) ? $presets['containers'][$container_name]['styles'][$style_name]['preset'] : (isset($presets['containers'][$container_name]['styles'][$style_name]['default preset']) ? $presets['containers'][$container_name]['styles'][$style_name]['default preset'] : $presets['containers'][$container_name]['default preset']);
  146. $form['containers'][$container_name]['preset'] = array(
  147. '#type' => 'select',
  148. '#title' => t('Preset'),
  149. '#options' => $options,
  150. '#default_value' => $default_value,
  151. );
  152. $form['containers'][$container_name]['default_preset'] = array(
  153. '#type' => 'item',
  154. '#title' => t('Default preset'),
  155. '#markup' => t('%preset', array('%preset' => $style['default preset'])),
  156. );
  157. if (isset($container['preview'])) {
  158. $form['containers'][$container_name]['preview'] = array(
  159. '#type' => 'item',
  160. '#title' => t('Preview'),
  161. '#markup' => theme($container['preview'], array('style_name' => $style_name)),
  162. );
  163. }
  164. }
  165. $form['submit'] = array(
  166. '#type' => 'submit',
  167. '#value' => t('Save'),
  168. );
  169. return $form;
  170. // $field_info = field_info_field_types($field_type);
  171. // $presets = styles_default_presets();
  172. // $preset = $presets[$field_type]['containers'][$preset_name];
  173. // $styles_containers = styles_default_containers();
  174. // $containers = $styles_containers[$field_type]['containers'];
  175. // drupal_set_title(t('Edit @field_type style preset: @preset', array('@field_type' => $field_info['label'], '@preset' => $preset_name)));
  176. // $form = array();
  177. // $form['containers'] = array(
  178. // '#type' => 'vertical_tabs',
  179. // );
  180. // $styles = styles_default_styles($field_type);
  181. // dpm($styles);
  182. //
  183. // // Begin the settings array to send to jQuery.
  184. // $settings = array(
  185. // 'stylesUI' => array(
  186. // 'url' => url('styles-ui/preview'),
  187. // 'fieldType' => check_plain($field_type),
  188. // ),
  189. // );
  190. // foreach ($styles['styles'] as $style_name => $style) {
  191. // $form['containers'][$style_name] = array(
  192. // '#type' => 'fieldset',
  193. // '#title' => $style['label'],
  194. // );
  195. // if (isset($preset['styles'][$style_name]['current preset'])) {
  196. // $this_preset = $preset['styles'][$style_name]['current preset'];
  197. // }
  198. // else {
  199. // $this_preset = $preset['styles'][$style_name]['default preset'];
  200. // }
  201. // $options = array();
  202. // foreach ($preset['presets'] as $preset_style_name => $effects) {
  203. // $options[$preset_style_name] = $preset_style_name;
  204. // }
  205. //
  206. // // Store the container in the 'rel' attribute for later AJAX previews.
  207. // $rel = check_plain($style_name);
  208. // $form['containers'][$style_name]['preset_' . $style_name] = array(
  209. // '#type' => 'radios',
  210. // '#title' => t('Style effects preset'),
  211. // '#default_value' => $this_preset,
  212. // '#options' => $options,
  213. // '#attributes' => array('class' => array('styles-ui-preset'), 'rel' => $rel),
  214. // );
  215. // // Add a preview.
  216. // if (isset($styles_containers[$field_type]['themes']) && isset($styles_containers[$field_type]['themes']['preview'])) {
  217. // $preview = '<div id="styles-ui-preview-wrapper-' . $rel . '" class="styles-ui-preview-wrapper">' . theme($styles_containers[$field_type]['themes']['preview'], array('field_type' => $field_type, 'container_name' => $style_name, 'style_name' => $this_preset)) . '</div>';
  218. // $form['containers'][$style_name]['preview_' . $style_name] = array(
  219. // '#type' => 'item',
  220. // '#title' => t('Style preview'),
  221. // '#markup' => $preview,
  222. // );
  223. // }
  224. // }
  225. // // Add the javascript for live previews on radio select.
  226. // $form['#attached'] = array(
  227. // 'js' => array(drupal_get_path('module', 'styles_ui') . '/styles_ui.js'),
  228. // );
  229. // drupal_add_js($settings, array('type' => 'setting'));
  230. // return $form;
  231. }
  232. /**
  233. * Validation handler for the edit style form.
  234. */
  235. function styles_ui_style_edit_form_validate($form, $form_state) {
  236. $field_type = $form_state['values']['field_type'];
  237. $style_name = $form_state['values']['name'];
  238. $old_style_name = $form_state['values']['old_style_name'];
  239. $styles = styles_default_styles();
  240. if (!preg_match('!^[a-z0-9_]+$!', $style_name)) {
  241. form_set_error('style_name', t('The machine-readable style name must contain only lowercase letters, numbers, and underscores.'));
  242. }
  243. else if (($style_name != $old_style_name) && !empty($styles[$field_type]['styles'][$style_name])) {
  244. form_set_error('style_name', t('The machine-readable style name %style_name is already taken.', array('%style_name' => $style_name)));
  245. }
  246. }
  247. /**
  248. * Submission handler for the add style form.
  249. */
  250. function styles_ui_style_edit_form_submit(&$form, &$form_state) {
  251. $field_type = $form_state['values']['field_type'];
  252. $style_name = $form_state['values']['name'];
  253. $style = array(
  254. 'field_type' => $field_type,
  255. 'name' => $style_name,
  256. 'description' => $form_state['values']['description'],
  257. );
  258. // Save the new style.
  259. styles_style_save($style);
  260. // Get the containers for this field type.
  261. $styles = styles_default_presets($field_type);
  262. $containers = styles_default_containers($field_type);
  263. foreach ($form_state['values']['containers'] as $container_name => $container) {
  264. if (is_array($container)) {
  265. $default_preset = isset($styles['containers'][$container_name]['styles'][$style_name]['default preset']) ? $styles['containers'][$container_name]['styles'][$style_name]['default preset'] : $styles['containers'][$container_name]['default preset'];
  266. $delete_only = ($default_preset == $container['preset']);
  267. styles_style_save_preset($field_type, $container_name, $style_name, $container['preset'], $delete_only);
  268. }
  269. }
  270. // Redirect to this style's edit page.
  271. $form_state['redirect'] = $containers['admin']['path'] . '/edit/' . $style_name;
  272. }
  273. function styles_ui_preview_ajax($field_type, $container_name, $preset_name) {
  274. $styles_containers = styles_default_containers();
  275. $containers = $styles_containers[$field_type]['containers'];
  276. drupal_json_output(array(
  277. 'id' => '#styles-ui-preview-wrapper-' . check_plain($container_name),
  278. 'preview' => theme($containers[$container_name]['themes']['preview'], array('field_type' => $field_type, 'container_name' => $container_name, 'style_name' => $preset_name)),
  279. ));
  280. die();
  281. }
  282. /**
  283. * Menu callback; delete a style.
  284. */
  285. function styles_ui_delete_confirm($form, &$form_state, $field_type, $style_name) {
  286. $form['field_type'] = array(
  287. '#type' => 'value',
  288. '#value' => $field_type,
  289. );
  290. $form['style_name'] = array(
  291. '#type' => 'value',
  292. '#value' => $style_name,
  293. );
  294. $styles = styles_default_styles($field_type);
  295. $style = $styles['styles'][$style_name];
  296. if ($style['storage'] == STYLES_STORAGE_OVERRIDE) {
  297. $delete = 'revert';
  298. }
  299. else {
  300. $delete = 'delete';
  301. }
  302. $caption = '<p>' . t('This action cannot be undone.') . '</p>';
  303. $containers = styles_default_containers($field_type);
  304. $cancel_path = $containers['admin']['path'];
  305. $form['containers'] = array(
  306. '#type' => 'value',
  307. '#value' => $containers,
  308. );
  309. $message = t('Are you sure you want to @delete the %field_type style %style_name?', array('@delete' => $delete, '%field_type' => $field_type, '%style_name' => $style_name));
  310. return confirm_form($form, $message, $cancel_path, $caption, t('@delete', array('@delete' => ucfirst($delete))));
  311. }
  312. /**
  313. * Process style delete confirm submissions.
  314. */
  315. function styles_ui_delete_confirm_submit($form, &$form_state) {
  316. $field_type = $form_state['values']['field_type'];
  317. $style_name = $form_state['values']['style_name'];
  318. $containers = styles_default_containers($field_type);
  319. styles_style_delete($field_type, $style_name);
  320. // Redirect to this style's edit page.
  321. $form_state['redirect'] = $containers['admin']['path'];
  322. }