/[drupal]/contributions/modules/taxonomy_theme/taxonomy_theme_admin.inc
ViewVC logotype

Contents of /contributions/modules/taxonomy_theme/taxonomy_theme_admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.10 - (show annotations) (download) (as text)
Mon Jan 12 19:58:32 2009 UTC (10 months, 1 week ago) by profix898
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +36 -26 lines
File MIME type: text/x-php
- bugfix: do not store properties for default theme
- bugfix: entries with conditions are not removed on selection method change
- task: change taxonomy_theme to support new themekey namespaces
1 <?php
2 // $Id: taxonomy_theme_admin.inc,v 1.9 2008/06/08 15:21:03 profix898 Exp $
3
4 require_once(drupal_get_path('module', 'taxonomy_theme') .'/taxonomy_theme_helper.inc');
5
6 define('TAXONOMY_THEME_SKIP', 0);
7 define('TAXONOMY_THEME_TERM', 1);
8 define('TAXONOMY_THEME_VOCAB', 2);
9 define('TAXONOMY_THEME_ALLTAXO', 3);
10 define('TAXONOMY_THEME_NODETYPE', 4);
11
12 /**
13 * Function taxonomy_theme_admin_form().
14 */
15 function taxonomy_theme_admin_form() {
16 // TaxonomyTheme selection method
17 $options = array();
18 $options[TAXONOMY_THEME_SKIP] = t('Disabled');
19 $properties = array_keys(variable_get('themekey_properties', array()));
20 if (in_array('taxonomy:tid', $properties)) {
21 $options[TAXONOMY_THEME_TERM] = t('Term-based');
22 $options[TAXONOMY_THEME_ALLTAXO] = t('All Taxonomy');
23 }
24 if (in_array('taxonomy:vid', $properties)) {
25 $options[TAXONOMY_THEME_VOCAB] = t('Vocab-based');
26 }
27 ksort($options);
28 //
29 $method = variable_get('taxonomy_theme_method', TAXONOMY_THEME_SKIP);
30 if (count($options) < 4) {
31 $message = t('Property \'Taxonomy: Term (taxonomy:tid)\' or \'Taxonomy: Vocabulary (taxonomy:vid)\' is not
32 enabled!<br />You dont get all available options unless you enable both of these properties
33 (visit the <a href="@url">ThemeKey settings</a> page).',
34 array('@url' => url('admin/settings/themekey/settings', array('query' => drupal_get_destination())))
35 );
36 drupal_set_message($message, 'error');
37 $method = TAXONOMY_THEME_SKIP;
38 }
39 //
40 $form['taxonomy_theme_method'] = array(
41 '#type' => 'select',
42 '#title' => t('Template selection'),
43 '#default_value' => $method,
44 '#options' => $options,
45 '#description' => t('This value determines the method used for template selection.'),
46 );
47
48 //
49 switch($method) {
50 case TAXONOMY_THEME_TERM:
51 $form[] = _taxonomy_theme_admin_term_form();
52 break;
53 case TAXONOMY_THEME_VOCAB:
54 $form[] = _taxonomy_theme_admin_vocab_form();
55 break;
56 case TAXONOMY_THEME_ALLTAXO:
57 $form[] = _taxonomy_theme_admin_alltaxo_form();
58 break;
59 default:
60 }
61
62 $form['buttons']['submit'] = array(
63 '#type' => 'submit',
64 '#value' => t('Save configuration'),
65 '#submit' => array('_taxonomy_theme_admin_form_submit')
66 );
67 $form['buttons']['reset'] = array(
68 '#type' => 'submit',
69 '#value' => t('Reset to defaults'),
70 '#submit' => array('_taxonomy_theme_admin_form_reset')
71 );
72
73 return $form;
74 }
75
76 /**
77 * Function _taxonomy_theme_admin_term_form().
78 * (term-based administration form)
79 */
80 function _taxonomy_theme_admin_term_form() {
81 $form['taxonomy_theme_select'] = array(
82 '#type' => 'fieldset',
83 '#title' => t('Terms => Themes'),
84 '#collapsible' => FALSE,
85 '#collapsed' => FALSE,
86 );
87
88 $options = array();
89 $selector = variable_get('taxonomy_theme_selector', '');
90 if (!$selector) {
91 $options[0] = t('Choose a category');
92 }
93 $vocabs = taxonomy_get_vocabularies();
94 foreach ($vocabs as $vocab) {
95 $options[$vocab->vid] = $vocab->name;
96 }
97 if (!empty($vocabs)) {
98 $form['taxonomy_theme_select']['taxonomy_theme_selector'] = array(
99 '#type' => 'select',
100 '#title' => t('Category to use for template selection'),
101 '#default_value' => $selector,
102 '#options' => $options,
103 '#description' => t('The value of this category will be used to determine the template to apply.'),
104 );
105 }
106 else {
107 $form['taxonomy_theme_select']['text'] = array(
108 '#value' => t('<br /><strong>Your site currently has no categories.</strong><br /><br />Please create at least one
109 category, add the terms you will map to themes and return to this page. For example, you might create
110 a category called Sections that has two terms, <em>blue</em> to map to the bluebeach theme and
111 <em>grey</em> to map to the <em>box_grey</em> theme.')
112 );
113 }
114
115 if ($selector) {
116 $terms = taxonomy_get_tree($selector);
117 $conditions = array(array('property' => 'taxonomy:vid', 'operator' => '=', 'value' => $selector));
118 $form['taxonomy_theme_select'][] = _taxonomy_theme_admin_table_builder('tid', t('Term'), $terms, $conditions);
119 }
120
121 return $form;
122 }
123
124 /**
125 * Function _taxonomy_theme_admin_vocab_form().
126 * (vocabulary-based administration form)
127 */
128 function _taxonomy_theme_admin_vocab_form() {
129 $form['taxonomy_theme_select'] = array(
130 '#type' => 'fieldset',
131 '#title' => t('Vocabularies => Themes'),
132 '#collapsible' => FALSE,
133 '#collapsed' => FALSE,
134 );
135
136 $vocabs = taxonomy_get_vocabularies();
137 $form['taxonomy_theme_select'][] = _taxonomy_theme_admin_table_builder('vid', t('Vocabulary'), $vocabs);
138
139 return $form;
140 }
141
142 /**
143 * Function _taxonomy_theme_admin_alltaxo_form().
144 * (all-taxonomy administration form)
145 */
146 function _taxonomy_theme_admin_alltaxo_form() {
147 $form['taxonomy_theme_select'] = array(
148 '#type' => 'fieldset',
149 '#title' => t('All Taxonomy => Themes'),
150 '#collapsible' => FALSE,
151 '#collapsed' => FALSE,
152 );
153
154 $vocabs = taxonomy_get_vocabularies();
155 if (count($vocabs)) {
156 foreach ($vocabs as $vocab) {
157 $rows = array();
158 $form['taxonomy_theme_select'][$vocab->vid] = array(
159 '#type' => 'fieldset',
160 '#title' => t('Vocabulary: ') . $vocab->name,
161 '#collapsible' => TRUE,
162 '#collapsed' => TRUE,
163 );
164 $terms = taxonomy_get_tree($vocab->vid);
165 $form['taxonomy_theme_select'][$vocab->vid][] = _taxonomy_theme_admin_table_builder('tid', t('Term'), $terms);
166 }
167 }
168 else {
169 $form['taxonomy_theme_select']['text'] = array('#value' => t('<strong>Your site currently has no vocabularies.</strong>'));
170 }
171
172 return $form;
173 }
174
175 /**
176 * Function taxonomy_theme_admin_form_submit().
177 */
178 function _taxonomy_theme_admin_form_submit($form, &$form_state) {
179 //
180 $selector = variable_get('taxonomy_theme_selector', '');
181 $method = variable_get('taxonomy_theme_method', TAXONOMY_THEME_SKIP);
182 if ($form_state['values']['taxonomy_theme_method'] != $method) {
183 $form_state['redirect'] = 'admin/settings/themekey/taxonomy/confirm/'. $method;
184 }
185 if (isset($form_state['values']['taxonomy_theme_selector'])) {
186 if (!empty($form_state['redirect']) || $form_state['values']['taxonomy_theme_selector'] != $selector) {
187 $form_state['redirect'] = 'admin/settings/themekey/taxonomy/confirm/'. $method .'/'. $selector;
188 }
189 $selector = $form_state['values']['taxonomy_theme_selector'];
190 }
191 //
192 if (empty($form_state['redirect'])) {
193 foreach ($form_state['values']['table'] as $key => $details) {
194 switch ($method) {
195 case TAXONOMY_THEME_TERM:
196 $conditions = array(array('property' => 'taxonomy:vid', 'operator' => '=', 'value' => $selector));
197 taxonomy_theme_set_theme('taxonomy:tid', $key, $details['theme'], $conditions);
198 break;
199 case TAXONOMY_THEME_VOCAB:
200 taxonomy_theme_set_theme('taxonomy:vid', $key, $details['theme']);
201 break;
202 case TAXONOMY_THEME_ALLTAXO:
203 taxonomy_theme_set_theme('taxonomy:tid', $key, $details['theme']);
204 break;
205 default:
206 }
207 }
208 }
209 unset($form_state['values']['table']);
210 //
211 foreach ($form_state['values'] as $key => $value) {
212 $pos = strpos($key, 'taxonomy_theme_');
213 if ($pos !== FALSE && $pos == 0) {
214 variable_set($key, $value);
215 }
216 }
217 }
218
219 /**
220 * Function taxonomy_theme_admin_form_reset().
221 */
222 function _taxonomy_theme_admin_form_reset($form, &$form_state) {
223 switch ($form_state['values']['taxonomy_theme_method']) {
224 case TAXONOMY_THEME_VOCAB:
225 taxonomy_theme_delall_theme('taxonomy:vid');
226 break;
227 case TAXONOMY_THEME_TERM:
228 case TAXONOMY_THEME_ALLTAXO:
229 taxonomy_theme_delall_theme('taxonomy:tid');
230 break;
231 default:
232 }
233 //
234 foreach ($form_state['values'] as $key => $value) {
235 $pos = strpos($key, 'taxonomy_theme_');
236 if ($pos !== FALSE && $pos == 0) {
237 variable_del($key);
238 }
239 }
240 }
241
242 /**
243 * Function _taxonomy_theme_admin_form_confirm().
244 */
245 function _taxonomy_theme_admin_form_confirm() {
246 if (!($method = arg(5))) {
247 drupal_goto('admin/settings/themekey/taxonomy');
248 }
249
250 //
251 $vid = arg(6);
252 if (is_numeric($vid) && $method == TAXONOMY_THEME_TERM
253 && variable_get('taxonomy_theme_method', TAXONOMY_THEME_SKIP) == TAXONOMY_THEME_TERM) {
254 $selector = variable_get('taxonomy_theme_selector', '');
255 $vocab_from = taxonomy_vocabulary_load($vid);
256 $vocab_to = taxonomy_vocabulary_load($selector);
257 $description = t('<p>The category used for template selection has changed (from %from to %to).
258 Items associated with the previous selected category should be removed to
259 prevent interference with the new settings.<br />
260 Click \'Remove\' to continue removing obsolete items.</p>',
261 array('%from' => $vocab_from->name, '%to' => $vocab_to->name)
262 );
263 }
264 else {
265 $description = t('<p>The template selection method has changed. Items associated with the previous
266 method should be removed to prevent interference with the new settings.<br />
267 Click \'Remove\' to continue removing obsolete items.</p>');
268 }
269
270 $form = array();
271 $params = array(
272 'key' => ($method == TAXONOMY_THEME_VOCAB) ? 'taxonomy:vid' : 'taxonomy:tid',
273 'vid' => isset($vid) ? $vid : NULL
274 );
275 $form['taxonomy_theme_params'] = array('#type' => 'value', '#value' => $params);
276 $form['#submit'] = array('_taxonomy_theme_admin_form_confirm_submit');
277
278 return confirm_form(
279 $form,
280 t('Do you want to remove obsolete items?'),
281 'admin/settings/themekey/taxonomy',
282 $description,
283 t('Remove'),
284 t('Keep')
285 );
286 }
287
288 /**
289 * Function _taxonomy_theme_admin_form_confirm_submit().
290 */
291 function _taxonomy_theme_admin_form_confirm_submit($form, &$form_state) {
292 if (isset($form_state['values']['taxonomy_theme_params'])) {
293 $conditions = array();
294 if (isset($form_state['values']['taxonomy_theme_params']['vid'])) {
295 $vid = $form_state['values']['taxonomy_theme_params']['vid'];
296 $conditions = array(array('property' => 'taxonomy:vid', 'operator' => '=', 'value' => $vid));
297 }
298 taxonomy_theme_delall_theme($form_state['values']['taxonomy_theme_params']['key'], $conditions);
299 }
300 $form_state['redirect'] = 'admin/settings/themekey/taxonomy';
301 }
302
303 /**
304 * Function _taxonomy_theme_admin_table_builder().
305 * (build list/table of taxonomy vocabs/terms)
306 */
307 function _taxonomy_theme_admin_table_builder($key, $label, $data, $conditions = array()) {
308 $form['table'] = array(
309 '#theme' => 'taxonomy_theme_table',
310 '#header' => array($label, t('Assigned to Theme')),
311 '#tree' => TRUE
312 );
313 //
314 $rows = array();
315 $themes = _themekey_theme_options();
316 foreach ($data as $item) {
317 $form['table'][$item->$key]['name'] = array('#value' => $item->name);
318 $form['table'][$item->$key]['theme'] = array(
319 '#type' => 'select',
320 '#default_value' => taxonomy_theme_get_theme('taxonomy:'. $key, $item->$key, $conditions),
321 '#options' => $themes,
322 );
323 }
324
325 return $form;
326 }
327
328 /**
329 * Function theme_taxonomy_theme_table().
330 */
331 function theme_taxonomy_theme_table($form) {
332 $header = isset($form['#header']) ? $form['#header'] : array();
333 $attributes = isset($form['#attributes']) ? $form['#attributes'] : array();
334
335 $rows = array();
336 foreach (element_children($form) as $key) {
337 $row = array();
338 foreach (element_children($form[$key]) as $item) {
339 $row[] = drupal_render($form[$key][$item]);
340 }
341 $rows[] = $row;
342 }
343
344 if (empty($rows)) {
345 $message = check_plain(isset($form['#empty']) ? $form['#empty'] : t('There are no items in the table.'));
346 $rows[] = array(array('data' => $message, 'colspan' => count($header), 'align' => 'center', 'class' => 'message'));
347 }
348
349 return count($rows) ? theme('table', $header, $rows, $attributes) : '';
350 }
351
352 /**
353 * Function _taxonomy_theme_form_alter().
354 */
355 function _taxonomy_theme_form_alter(&$form, $form_state, $form_id) {
356 $themes = _themekey_theme_options();
357 $method = variable_get('taxonomy_theme_method', TAXONOMY_THEME_SKIP);
358 // Alter taxonomy term page
359 if ($form_id == 'taxonomy_form_term' && ($method == TAXONOMY_THEME_TERM || $method == TAXONOMY_THEME_ALLTAXO)) {
360 $selector = variable_get('taxonomy_theme_selector', '');
361 if ($method == TAXONOMY_THEME_TERM && $selector && $form['#vocabulary']['vid'] != $selector) {
362 return;
363 }
364 $form_theme['taxonomy_theme_select'] = array(
365 '#type' => 'fieldset',
366 '#title' => t('Theme'),
367 '#collapsible' => TRUE,
368 '#collapsed' => FALSE
369 );
370 $conditions = array(array('property' => 'taxonomy:vid', 'operator' => '=', 'value' => $selector));
371 $conditions = ($method == TAXONOMY_THEME_TERM) ? $conditions : array();
372 $form_theme['taxonomy_theme_select']['theme'] = array(
373 '#type' => 'select',
374 '#default_value' => taxonomy_theme_get_theme('taxonomy:tid', $form['tid']['#value'], $conditions),
375 '#options' => $themes,
376 '#description' => t('Theme to be assigned to current term')
377 );
378 array_splice($form, 4, 0, $form_theme);
379 $form['#submit'][] = 'taxonomy_theme_form_alter_submit';
380 }
381 // Alter taxonomy vocabulary page
382 else if (($form_id == 'taxonomy_form_vocabulary') && ($method == TAXONOMY_THEME_VOCAB)) {
383 $form_theme['taxonomy_theme_select'] = array(
384 '#type' => 'fieldset',
385 '#title' => t('Theme'),
386 '#collapsible' => TRUE,
387 '#collapsed' => FALSE
388 );
389 $form_theme['taxonomy_theme_select']['theme'] = array(
390 '#type' => 'select',
391 '#default_value' => taxonomy_theme_get_theme('taxonomy:vid', $form['vid']['#value']),
392 '#options' => $themes,
393 '#description' => t('Theme to be assigned to current vocabulary')
394 );
395 array_splice($form, 3, 0, $form_theme);
396 $form['#submit'][] = 'taxonomy_theme_form_alter_submit';
397 }
398 }
399
400 /**
401 * function _taxonomy_theme_alter_submit().
402 */
403 function _taxonomy_theme_form_alter_submit($form, &$form_state) {
404 // Submit taxonomy term page
405 if ($form_state['values']['form_id'] == 'taxonomy_form_term') {
406 taxonomy_theme_set_theme('taxonomy:tid', $form_state['values']['tid'], $form_state['values']['theme']);
407 }
408 // Submit taxonomy vocabulary page
409 else if ($form_state['values']['form_id'] == 'taxonomy_form_vocabulary') {
410 taxonomy_theme_set_theme('taxonomy:vid', $form_state['values']['vid'], $form_state['values']['theme']);
411 }
412 }

  ViewVC Help
Powered by ViewVC 1.1.2