theme-settings.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. require_once dirname(__FILE__) . '/includes/alpha.inc';
  3. require_once dirname(__FILE__) . '/includes/base.inc';
  4. require_once dirname(__FILE__) . '/includes/theme-settings-general.inc';
  5. require_once dirname(__FILE__) . '/includes/theme-settings-structure.inc';
  6. /**
  7. * Implements hook_form_system_theme_settings_alter()
  8. */
  9. function alpha_form_system_theme_settings_alter(&$form, &$form_state) {
  10. drupal_add_css(drupal_get_path('theme', 'alpha') . '/css/alpha-theme-settings.css', array('group' => CSS_THEME, 'weight' => 100));
  11. $theme = alpha_get_theme();
  12. $form_state['theme'] = $theme->theme;
  13. $form_state['regions'] = $theme->regions;
  14. $form_state['zones'] = $theme->zones;
  15. $form['alpha_settings'] = array(
  16. '#type' => 'vertical_tabs',
  17. '#weight' => -10,
  18. '#prefix' => t('<h3>Layout configuration</h3>'),
  19. );
  20. alpha_theme_settings_general($form, $form_state);
  21. alpha_theme_settings_structure($form, $form_state);
  22. $form['#validate'][] = 'alpha_theme_settings_form_validate';
  23. $form['#submit'][] = 'alpha_theme_settings_form_submit';
  24. }
  25. /**
  26. * Form element validation handler for replacing the value "_none" with NULL.
  27. */
  28. function alpha_theme_settings_validate_not_empty(&$element, &$form_state) {
  29. if ($element['#value'] == '_none') {
  30. form_set_value($element, NULL, $form_state);
  31. }
  32. }
  33. /**
  34. * Form element validation handler for validating the primary region setting for zones.
  35. */
  36. function alpha_theme_settings_validate_primary(&$element, &$form_state) {
  37. if ($element['#value'] != '_none') {
  38. $values = $form_state['values'];
  39. if ($values['alpha_region_' . $element['#value'] . '_zone'] != $element['#zone']) {
  40. form_set_value($element, NULL, $form_state);
  41. }
  42. else {
  43. $sum = 0;
  44. foreach ($form_state['regions'] as $region => $item) {
  45. if ($values['alpha_region_' . $region . '_zone'] == $element['#zone']) {
  46. $sum += $values['alpha_region_' . $region . '_columns'];
  47. $sum += $values['alpha_region_' . $region . '_prefix'];
  48. $sum += $values['alpha_region_' . $region . '_suffix'];
  49. }
  50. }
  51. if ($sum > $values['alpha_zone_' . $element['#zone'] . '_columns']) {
  52. form_error($element, t('You have specified the %region region as the primary region for the %zone zone but the summed region width is greater than the number of available columns for that zone.', array('%region' => $form_state['regions'][$element['#value']]['name'], '%zone' => $form_state['zones'][$element['#zone']]['name'])));
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * Form element validation handler for validating the region order manipulation setting for zones.
  59. */
  60. function alpha_theme_settings_validate_order(&$element, &$form_state) {
  61. if ($element['#value']) {
  62. $values = $form_state['values'];
  63. $sum = 0;
  64. foreach ($form_state['regions'] as $region => $item) {
  65. if ($values['alpha_region_' . $region . '_zone'] == $element['#zone']) {
  66. $sum += $values['alpha_region_' . $region . '_columns'];
  67. $sum += $values['alpha_region_' . $region . '_prefix'];
  68. $sum += $values['alpha_region_' . $region . '_suffix'];
  69. }
  70. }
  71. if ($sum > $values['alpha_zone_' . $element['#zone'] . '_columns']) {
  72. form_error($element, t('You have chosen to manipulate the region positioning of the %zone zone but the summed region width is greater than the number of available columns for that zone.', array('%zone' => $form_state['zones'][$element['#zone']]['name'])));
  73. }
  74. }
  75. }
  76. /**
  77. * Removes the vertical tab active tab from the values. It really doesn't
  78. * belong there!
  79. */
  80. function alpha_theme_settings_form_validate($form, &$form_state) {
  81. unset($form_state['values']['alpha_settings__active_tab']);
  82. }
  83. /**
  84. * Clears the cache for the theme settings upon form submission.
  85. */
  86. function alpha_theme_settings_form_submit($form, &$form_state) {
  87. alpha_cache_clear($form_state['theme'], (isset($form_state['delta']) ? $form_state['delta'] : NULL));
  88. }
  89. /**
  90. * A helper function to return a proper options array for a form.
  91. *
  92. * @param $start
  93. * The number to start with.
  94. *
  95. * @param $end
  96. * The number to end with.
  97. *
  98. * @param $step
  99. * The size of a step.
  100. *
  101. * @return
  102. * An array of scale options.
  103. */
  104. function alpha_scale_options($start, $end, $step) {
  105. $options = array();
  106. foreach (range($start, $end, $step) as $number) {
  107. // Format the value to display with one decimal.
  108. $options[(string) $number] = number_format($number, 1);
  109. }
  110. return $options;
  111. }
  112. /**
  113. * A helper function to return a proper options array for a form.
  114. *
  115. * @return
  116. * An array of optional or responsive stylesheet options.
  117. */
  118. function alpha_css_options($css) {
  119. $output = array();
  120. foreach ($css as $key => $info) {
  121. $output[$key] = '<strong>' . check_plain($info['name']) . '</strong> (' . (isset($info['options']['media']) ? $info['options']['media'] : 'all') . ') - ' . $info['file'] . '<div class="description">' . $info['description'] . '</div>';
  122. }
  123. return $output;
  124. }
  125. /**
  126. * A helper function to return a proper options array for a form.
  127. *
  128. * @param $theme
  129. * The key (machin-readable name) of a theme.
  130. *
  131. * @see
  132. * hook_css_alter().
  133. *
  134. * @return
  135. * An array of stylesheets that can be disabled / excluded with
  136. * hook_css_alter().
  137. */
  138. function alpha_exclude_options($theme) {
  139. $output = array();
  140. foreach (alpha_retrieve_excludes($theme) as $key => $info) {
  141. if ($info['type'] == 'exclude') {
  142. $output[$key] = '<strong>' . basename($key) . '</strong> - ' . t('Defined by') . ' ' . $info['name'] . '<div class="description">' . $info['description'] . '</div>';
  143. }
  144. else {
  145. $output[$key] = '<strong>' . basename($key) . '</strong> (' . $info['media'] . ') - ' . t('Belongs to') . ' ' . $info['name'] . '<div class="description">' . $info['description'] . '</div>';
  146. }
  147. }
  148. return $output;
  149. }
  150. /**
  151. * A helper function to return a proper options array for a form.
  152. *
  153. * @return
  154. * An array of available grids.
  155. */
  156. function alpha_grid_options($grids) {
  157. $output = array();
  158. foreach ($grids as $key => $info) {
  159. $output[$key] = check_plain($info['name']);
  160. }
  161. return $output;
  162. }
  163. /**
  164. * A helper function to return a proper options array for a form.
  165. *
  166. * @param $grid
  167. * The name of a grid.
  168. *
  169. * @return
  170. * An array of available layouts.
  171. */
  172. function alpha_grid_layouts_options($grid) {
  173. $output = array();
  174. if (!empty($grid['layouts'])) {
  175. foreach ($grid['layouts'] as $key => $info) {
  176. $output[$key] = check_plain($info['name']);
  177. }
  178. }
  179. return $output;
  180. }
  181. /**
  182. * A helper function to return a proper options array for a form.
  183. *
  184. * @return
  185. * An array of available libraries.
  186. */
  187. function alpha_library_options($libraries) {
  188. $output = array();
  189. foreach ($libraries as $key => $info) {
  190. $output[$key] = check_plain($info['name']) . '<div class="description">' . $info['description'] . '</div>';
  191. }
  192. return $output;
  193. }
  194. /**
  195. * A helper function to return a proper options array for a form.
  196. *
  197. * @param $grid
  198. * The grid that you want to fetch the available containers for.
  199. *
  200. * @return
  201. * An array of available containers.
  202. */
  203. function alpha_container_options($grid) {
  204. $output = array();
  205. if (!empty($grid['columns'])) {
  206. foreach ($grid['columns'] as $count => $title) {
  207. $output[$count] = t('@count Columns', array('@count' => $count));
  208. }
  209. }
  210. return $output;
  211. }
  212. /**
  213. * A helper function to return a proper options array for a form.
  214. *
  215. * @param $max
  216. * The maximum number of columns that you want to cover.
  217. *
  218. * @return
  219. * An array of available columns counts.
  220. */
  221. function alpha_column_options($max = NULL) {
  222. $output = array();
  223. if (isset($max)) {
  224. foreach (range(0, $max) as $width) {
  225. $output[$width] = t('@width Columns', array('@width' => $width));
  226. }
  227. }
  228. return $output;
  229. }
  230. /**
  231. * A helper function to return a proper options array for a form.
  232. *
  233. * @return
  234. * An array of available zones.
  235. */
  236. function alpha_zone_options($zones) {
  237. $output = array();
  238. foreach ($zones as $key => $info) {
  239. $output[$key] = check_plain($info['name']);
  240. }
  241. return $output;
  242. }
  243. /**
  244. * A helper function to return a proper options array for a form.
  245. *
  246. * @return
  247. * An array of available regions.
  248. */
  249. function alpha_region_options($regions) {
  250. $output = array();
  251. foreach ($regions as $region => $item) {
  252. $output[$region] = $item['name'];
  253. }
  254. return $output;
  255. }
  256. /**
  257. * A helper function to return a proper options array for a form.
  258. *
  259. * @return
  260. * An array of available regions for a zone.
  261. */
  262. function alpha_zone_regions($zone, $regions) {
  263. $matches = array();
  264. foreach ($regions as $region => $info) {
  265. if ($zone == $info['zone']) {
  266. $matches[$region] = $info;
  267. }
  268. }
  269. return alpha_region_options($matches);
  270. }