source: sipes/cord/modules/taxonomy/taxonomy.admin.inc @ 52861f4

stableversion-3.0
Last change on this file since 52861f4 was b354002, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio del cord

  • Propiedad mode establecida a 100755
File size: 35.6 KB
Línea 
1<?php
2
3/**
4 * @file
5 * Administrative page callbacks for the taxonomy module.
6 */
7
8/**
9 * Form builder to list and manage vocabularies.
10 *
11 * @ingroup forms
12 * @see taxonomy_overview_vocabularies_submit()
13 * @see theme_taxonomy_overview_vocabularies()
14 */
15function taxonomy_overview_vocabularies() {
16  $vocabularies = taxonomy_get_vocabularies();
17  $form = array('#tree' => TRUE);
18  foreach ($vocabularies as $vocabulary) {
19    $types = array();
20    foreach ($vocabulary->nodes as $type) {
21      $node_type = node_get_types('name', $type);
22      $types[] = $node_type ? check_plain($node_type) : check_plain($type);
23    }
24    $form[$vocabulary->vid]['#vocabulary'] = (array)$vocabulary;
25    $form[$vocabulary->vid]['name'] = array('#value' => check_plain($vocabulary->name));
26    $form[$vocabulary->vid]['types'] = array('#value' => implode(', ', $types));
27    $form[$vocabulary->vid]['weight'] = array('#type' => 'weight', '#delta' => 10, '#default_value' => $vocabulary->weight);
28    $form[$vocabulary->vid]['edit'] = array('#value' => l(t('edit vocabulary'), "admin/content/taxonomy/edit/vocabulary/$vocabulary->vid"));
29    $form[$vocabulary->vid]['list'] = array('#value' => l(t('list terms'), "admin/content/taxonomy/$vocabulary->vid"));
30    $form[$vocabulary->vid]['add'] = array('#value' => l(t('add terms'), "admin/content/taxonomy/$vocabulary->vid/add/term"));
31  }
32
33  // Only make this form include a submit button and weight if more than one
34  // vocabulary exists.
35  if (count($vocabularies) > 1) {
36    $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
37  }
38  elseif (isset($vocabulary)) {
39    unset($form[$vocabulary->vid]['weight']);
40  }
41  return $form;
42}
43
44/**
45 * Submit handler for vocabularies overview. Updates changed vocabulary weights.
46 *
47 * @see taxonomy_overview_vocabularies()
48 */
49function taxonomy_overview_vocabularies_submit($form, &$form_state) {
50  foreach ($form_state['values'] as $vid => $vocabulary) {
51    if (is_numeric($vid) && $form[$vid]['#vocabulary']['weight'] != $form_state['values'][$vid]['weight']) {
52      $form[$vid]['#vocabulary']['weight'] = $form_state['values'][$vid]['weight'];
53      taxonomy_save_vocabulary($form[$vid]['#vocabulary']);
54    }
55  }
56}
57
58/**
59 * Theme the vocabulary overview as a sortable list of vocabularies.
60 *
61 * @ingroup themeable
62 * @see taxonomy_overview_vocabularies()
63 */
64function theme_taxonomy_overview_vocabularies($form) {
65  $rows = array();
66  foreach (element_children($form) as $key) {
67    if (isset($form[$key]['name'])) {
68      $vocabulary = &$form[$key];
69
70      $row = array();
71      $row[] = drupal_render($vocabulary['name']);
72      $row[] = drupal_render($vocabulary['types']);
73      if (isset($vocabulary['weight'])) {
74        $vocabulary['weight']['#attributes']['class'] = 'vocabulary-weight';
75        $row[] = drupal_render($vocabulary['weight']);
76      }
77      $row[] = drupal_render($vocabulary['edit']);
78      $row[] = drupal_render($vocabulary['list']);
79      $row[] = drupal_render($vocabulary['add']);
80      $rows[] = array('data' => $row, 'class' => 'draggable');
81    }
82  }
83  if (empty($rows)) {
84    $rows[] = array(array('data' => t('No vocabularies available.'), 'colspan' => '5'));
85  }
86
87  $header = array(t('Name'), t('Type'));
88  if (isset($form['submit'])) {
89    $header[] = t('Weight');
90    drupal_add_tabledrag('taxonomy', 'order', 'sibling', 'vocabulary-weight');
91  }
92  $header[] = array('data' => t('Operations'), 'colspan' => '3');
93  return theme('table', $header, $rows, array('id' => 'taxonomy')) . drupal_render($form);
94}
95
96/**
97 * Display form for adding and editing vocabularies.
98 *
99 * @ingroup forms
100 * @see taxonomy_form_vocabulary_submit()
101 */
102function taxonomy_form_vocabulary(&$form_state, $edit = array()) {
103  $edit += array(
104    'name' => '',
105    'description' => '',
106    'help' => '',
107    'nodes' => array(),
108    'hierarchy' => 0,
109    'relations' => 0,
110    'tags' => 0,
111    'multiple' => 0,
112    'required' => 0,
113    'weight' => 0,
114  );
115  $form['identification'] = array(
116    '#type' => 'fieldset',
117    '#title' => t('Identification'),
118    '#collapsible' => TRUE,
119  );
120  $form['identification']['name'] = array('#type' => 'textfield',
121    '#title' => t('Vocabulary name'),
122    '#default_value' => $edit['name'],
123    '#maxlength' => 255,
124    '#description' => t('The name for this vocabulary, e.g., <em>"Tags"</em>.'),
125    '#required' => TRUE,
126  );
127  $form['identification']['description'] = array('#type' => 'textarea',
128    '#title' => t('Description'),
129    '#default_value' => $edit['description'],
130    '#description' => t('Description of the vocabulary; can be used by modules.'),
131  );
132  $form['identification']['help'] = array('#type' => 'textfield',
133    '#title' => t('Help text'),
134    '#maxlength' => 255,
135    '#default_value' => $edit['help'],
136    '#description' => t('Instructions to present to the user when selecting terms, e.g., <em>"Enter a comma separated list of words"</em>.'),
137  );
138  $form['content_types'] = array(
139    '#type' => 'fieldset',
140    '#title' => t('Content types'),
141    '#collapsible' => TRUE,
142  );
143  $form['content_types']['nodes'] = array('#type' => 'checkboxes',
144    '#title' => t('Content types'),
145    '#default_value' => $edit['nodes'],
146    '#options' => array_map('check_plain', node_get_types('names')),
147    '#description' => t('Select content types to categorize using this vocabulary.'),
148  );
149  $form['settings'] = array(
150    '#type' => 'fieldset',
151    '#title' => t('Settings'),
152    '#collapsible' => TRUE,
153  );
154  $form['settings']['tags'] = array('#type' => 'checkbox',
155    '#title' => t('Tags'),
156    '#default_value' => $edit['tags'],
157    '#description' => t('Terms are created by users when submitting posts by typing a comma separated list.'),
158  );
159  $form['settings']['multiple'] = array('#type' => 'checkbox',
160    '#title' => t('Multiple select'),
161    '#default_value' => $edit['multiple'],
162    '#description' => t('Allows posts to have more than one term from this vocabulary (always true for tags).'),
163  );
164  $form['settings']['required'] = array('#type' => 'checkbox',
165    '#title' => t('Required'),
166    '#default_value' => $edit['required'],
167    '#description' => t('At least one term in this vocabulary must be selected when submitting a post.'),
168  );
169  $form['settings']['weight'] = array('#type' => 'weight',
170    '#title' => t('Weight'),
171    '#default_value' => $edit['weight'],
172    '#description' => t('Vocabularies are displayed in ascending order by weight.'),
173  );
174  // Set the hierarchy to "multiple parents" by default. This simplifies the
175  // vocabulary form and standardizes the term form.
176  $form['hierarchy'] = array('#type' => 'value',
177    '#value' => '0',
178  );
179  // Enable "related terms" by default.
180  $form['relations'] = array('#type' => 'value',
181    '#value' => '1',
182  );
183
184  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
185  if (isset($edit['vid'])) {
186    $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
187    $form['vid'] = array('#type' => 'value', '#value' => $edit['vid']);
188    $form['module'] = array('#type' => 'value', '#value' => $edit['module']);
189  }
190  return $form;
191}
192
193/**
194 * Accept the form submission for a vocabulary and save the results.
195 */
196function taxonomy_form_vocabulary_submit($form, &$form_state) {
197  // Fix up the nodes array to remove unchecked nodes.
198  $form_state['values']['nodes'] = array_filter($form_state['values']['nodes']);
199  switch (taxonomy_save_vocabulary($form_state['values'])) {
200    case SAVED_NEW:
201      drupal_set_message(t('Created new vocabulary %name.', array('%name' => $form_state['values']['name'])));
202      watchdog('taxonomy', 'Created new vocabulary %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/vocabulary/'. $form_state['values']['vid']));
203      break;
204    case SAVED_UPDATED:
205      drupal_set_message(t('Updated vocabulary %name.', array('%name' => $form_state['values']['name'])));
206      watchdog('taxonomy', 'Updated vocabulary %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/vocabulary/'. $form_state['values']['vid']));
207      break;
208  }
209
210  $form_state['vid'] = $form_state['values']['vid'];
211  $form_state['redirect'] = 'admin/content/taxonomy';
212  return;
213}
214
215/**
216 * Page to edit a vocabulary.
217 */
218function taxonomy_admin_vocabulary_edit($vocabulary) {
219  if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || isset($_POST['confirm'])) {
220    return drupal_get_form('taxonomy_vocabulary_confirm_delete', $vocabulary->vid);
221  }
222  return drupal_get_form('taxonomy_form_vocabulary', (array)$vocabulary);
223}
224
225/**
226 * Page to edit a vocabulary term.
227 */
228function taxonomy_admin_term_edit($tid) {
229  if ($term = (array)taxonomy_get_term($tid)) {
230    return drupal_get_form('taxonomy_form_term', taxonomy_vocabulary_load($term['vid']), $term);
231  }
232  return drupal_not_found();
233}
234
235/**
236 * Form builder for the taxonomy terms overview.
237 *
238 * Display a tree of all the terms in a vocabulary, with options to edit
239 * each one. The form is made drag and drop by the theme function.
240 *
241 * @ingroup forms
242 * @see taxonomy_overview_terms_submit()
243 * @see theme_taxonomy_overview_terms()
244 */
245function taxonomy_overview_terms(&$form_state, $vocabulary) {
246  global $pager_page_array, $pager_total, $pager_total_items;
247
248  // Check for confirmation forms.
249  if (isset($form_state['confirm_reset_alphabetical'])) {
250    return taxonomy_vocabulary_confirm_reset_alphabetical($form_state, $vocabulary->vid);
251  }
252
253  drupal_set_title(t('Terms in %vocabulary', array('%vocabulary' => $vocabulary->name)));
254  $form = array(
255    '#vocabulary' => (array)$vocabulary,
256    '#tree' => TRUE,
257    '#parent_fields' => FALSE,
258  );
259
260  $page            = isset($_GET['page']) ? $_GET['page'] : 0;
261  $page_increment  = variable_get('taxonomy_terms_per_page_admin', 100);  // Number of terms per page.
262  $page_entries    = 0;   // Elements shown on this page.
263  $before_entries  = 0;   // Elements at the root level before this page.
264  $after_entries   = 0;   // Elements at the root level after this page.
265  $root_entries    = 0;   // Elements at the root level on this page.
266
267  // Terms from previous and next pages are shown if the term tree would have
268  // been cut in the middle. Keep track of how many extra terms we show on each
269  // page of terms.
270  $back_peddle    = NULL;
271  $forward_peddle = 0;
272
273  // An array of the terms to be displayed on this page.
274  $current_page = array();
275
276  // Case for free tagging.
277  if ($vocabulary->tags) {
278    // We are not calling taxonomy_get_tree because that might fail with a big
279    // number of tags in the freetagging vocabulary.
280    $results = pager_query(db_rewrite_sql('SELECT t.*, h.parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $page_increment, 0, NULL, $vocabulary->vid);
281    $total_entries = db_query(db_rewrite_sql('SELECT count(*) FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d', 't', 'tid'), $page_increment, 0, NULL, $vocabulary->vid);
282    while ($term = db_fetch_object($results)) {
283      $key = 'tid:'. $term->tid .':0';
284      $current_page[$key] = $term;
285      $page_entries++;
286    }
287  }
288  // Case for restricted vocabulary.
289  else {
290    $term_deltas = array();
291    $tree = taxonomy_get_tree($vocabulary->vid);
292    $term = current($tree);
293    do {
294      // In case this tree is completely empty.
295      if (empty($term)) {
296        break;
297      }
298      // Count entries before the current page.
299      if ($page && ($page * $page_increment) > $before_entries && !isset($back_peddle)) {
300        $before_entries++;
301        continue;
302      }
303      // Count entries after the current page.
304      elseif ($page_entries > $page_increment && isset($complete_tree)) {
305        $after_entries++;
306        continue;
307      }
308
309      // Do not let a term start the page that is not at the root.
310      if (isset($term->depth) && ($term->depth > 0) && !isset($back_peddle)) {
311        $back_peddle = 0;
312        while ($pterm = prev($tree)) {
313          $before_entries--;
314          $back_peddle++;
315          if ($pterm->depth == 0) {
316            prev($tree);
317            continue 2; // Jump back to the start of the root level parent.
318          }
319        }
320      }
321      $back_peddle = isset($back_peddle) ? $back_peddle : 0;
322
323      // Continue rendering the tree until we reach the a new root item.
324      if ($page_entries >= $page_increment + $back_peddle + 1 && $term->depth == 0 && $root_entries > 1) {
325        $complete_tree = TRUE;
326        // This new item at the root level is the first item on the next page.
327        $after_entries++;
328        continue;
329      }
330      if ($page_entries >= $page_increment + $back_peddle) {
331        $forward_peddle++;
332      }
333
334      // Finally, if we've gotten down this far, we're rendering a term on this page.
335      $page_entries++;
336      $term_deltas[$term->tid] = isset($term_deltas[$term->tid]) ? $term_deltas[$term->tid] + 1 : 0;
337      $key = 'tid:'. $term->tid .':'. $term_deltas[$term->tid];
338
339      // Keep track of the first term displayed on this page.
340      if ($page_entries == 1) {
341        $form['#first_tid'] = $term->tid;
342      }
343      // Keep a variable to make sure at least 2 root elements are displayed.
344      if ($term->parents[0] == 0) {
345        $root_entries++;
346      }
347      $current_page[$key] = $term;
348    } while ($term = next($tree));
349
350    // Because we didn't use a pager query, set the necessary pager variables.
351    $total_entries = $before_entries + $page_entries + $after_entries;
352    $pager_total_items[0] = $total_entries;
353    $pager_page_array[0] = $page;
354    $pager_total[0] = ceil($total_entries / $page_increment);
355  }
356
357  // If this form was already submitted once, it's probably hit a validation
358  // error. Ensure the form is rebuilt in the same order as the user submitted.
359  if (!empty($form_state['post'])) {
360    $order = array_flip(array_keys($form_state['post'])); // Get the $_POST order.
361    $current_page = array_merge($order, $current_page); // Update our form with the new order.
362    foreach ($current_page as $key => $term) {
363      // Verify this is a term for the current page and set at the current depth.
364      if (is_array($form_state['post'][$key]) && is_numeric($form_state['post'][$key]['tid'])) {
365        $current_page[$key]->depth = $form_state['post'][$key]['depth'];
366      }
367      else {
368        unset($current_page[$key]);
369      }
370    }
371  }
372
373  // Build the actual form.
374  foreach ($current_page as $key => $term) {
375    // Save the term for the current page so we don't have to load it a second time.
376    $form[$key]['#term'] = (array)$term;
377    if (isset($term->parents)) {
378      $form[$key]['#term']['parent'] = $term->parent = $term->parents[0];
379      unset($form[$key]['#term']['parents'], $term->parents);
380    }
381
382    $form[$key]['view'] = array('#value' => l($term->name, "taxonomy/term/$term->tid"));
383    if (!$vocabulary->tags && $vocabulary->hierarchy < 2 && count($tree) > 1) {
384      $form['#parent_fields'] = TRUE;
385      $form[$key]['tid'] = array(
386        '#type' => 'hidden',
387        '#value' => $term->tid
388      );
389      $form[$key]['parent'] = array(
390        '#type' => 'hidden',
391        // Yes, default_value on a hidden. It needs to be changeable by the javascript.
392        '#default_value' => $term->parent,
393      );
394      $form[$key]['depth'] = array(
395        '#type' => 'hidden',
396        // Same as above, the depth is modified by javascript, so it's a default_value.
397        '#default_value' => $term->depth,
398      );
399    }
400    $form[$key]['edit'] = array('#value' => l(t('edit'), "admin/content/taxonomy/edit/term/$term->tid", array('query' => drupal_get_destination())));
401  }
402
403  $form['#total_entries'] = $total_entries;
404  $form['#page_increment'] = $page_increment;
405  $form['#page_entries'] = $page_entries;
406  $form['#back_peddle'] = $back_peddle;
407  $form['#forward_peddle'] = $forward_peddle;
408  $form['#empty_text'] = t('No terms available.');
409
410  if (!$vocabulary->tags && $vocabulary->hierarchy < 2 && count($tree) > 1) {
411    $form['submit'] = array(
412      '#type' => 'submit',
413      '#value' => t('Save')
414    );
415    $form['reset_alphabetical'] = array(
416      '#type' => 'submit',
417      '#value' => t('Reset to alphabetical')
418    );
419    $form['destination'] = array(
420      '#type' => 'hidden',
421      '#value' => $_GET['q'] . (isset($_GET['page']) ? '?page='. $_GET['page'] : '')
422    );
423  }
424
425  return $form;
426}
427
428/**
429 * Submit handler for terms overview form.
430 *
431 * Rather than using a textfield or weight field, this form depends entirely
432 * upon the order of form elements on the page to determine new weights.
433 *
434 * Because there might be hundreds or thousands of taxonomy terms that need to
435 * be ordered, terms are weighted from 0 to the number of terms in the
436 * vocabulary, rather than the standard -10 to 10 scale. Numbers are sorted
437 * lowest to highest, but are not necessarily sequential. Numbers may be skipped
438 * when a term has children so that reordering is minimal when a child is
439 * added or removed from a term.
440 *
441 * @see taxonomy_overview_terms()
442 */
443function taxonomy_overview_terms_submit($form, &$form_state) {
444  if ($form_state['clicked_button']['#value'] == t('Reset to alphabetical')) {
445    // Execute the reset action.
446    if ($form_state['values']['reset_alphabetical'] === TRUE) {
447      return taxonomy_vocabulary_confirm_reset_alphabetical_submit($form, $form_state);
448    }
449    // Rebuild the form to confirm the reset action.
450    $form_state['rebuild'] = TRUE;
451    $form_state['confirm_reset_alphabetical'] = TRUE;
452    return;
453  }
454
455  $order = array_flip(array_keys($form['#post'])); // Get the $_POST order.
456  $form_state['values'] = array_merge($order, $form_state['values']); // Update our original form with the new order.
457
458  $vocabulary = $form['#vocabulary'];
459  $hierarchy = 0; // Update the current hierarchy type as we go.
460
461  $changed_terms = array();
462  $tree = taxonomy_get_tree($vocabulary['vid']);
463
464  if (empty($tree)) {
465    return;
466  }
467
468  // Build a list of all terms that need to be updated on previous pages.
469  $weight = 0;
470  $term = (array)$tree[0];
471  while ($term['tid'] != $form['#first_tid']) {
472    if ($term['parents'][0] == 0 && $term['weight'] != $weight) {
473      $term['parent'] = $term['parents'][0];
474      $term['weight'] = $weight;
475      $changed_terms[$term['tid']] = $term;
476    }
477    $weight++;
478    $hierarchy = $term['parents'][0] != 0 ? 1 : $hierarchy;
479    $term = (array)$tree[$weight];
480  }
481
482  // Renumber the current page weights and assign any new parents.
483  $level_weights = array();
484  foreach ($form_state['values'] as $tid => $values) {
485    if (isset($form[$tid]['#term'])) {
486      $term = $form[$tid]['#term'];
487      // Give terms at the root level a weight in sequence with terms on previous pages.
488      if ($values['parent'] == 0 && $term['weight'] != $weight) {
489        $term['weight'] = $weight;
490        $changed_terms[$term['tid']] = $term;
491      }
492      // Terms not at the root level can safely start from 0 because they're all on this page.
493      elseif ($values['parent'] > 0) {
494        $level_weights[$values['parent']] = isset($level_weights[$values['parent']]) ? $level_weights[$values['parent']] + 1 : 0;
495        if ($level_weights[$values['parent']] != $term['weight']) {
496          $term['weight'] = $level_weights[$values['parent']];
497          $changed_terms[$term['tid']] = $term;
498        }
499      }
500      // Update any changed parents.
501      if ($values['parent'] != $term['parent']) {
502        $term['parent'] = $values['parent'];
503        $changed_terms[$term['tid']] = $term;
504      }
505      $hierarchy = $term['parent'] != 0 ? 1 : $hierarchy;
506      $weight++;
507    }
508  }
509
510  // Build a list of all terms that need to be updated on following pages.
511  for ($weight; $weight < count($tree); $weight++) {
512    $term = (array)$tree[$weight];
513    if ($term['parents'][0] == 0 && $term['weight'] != $weight) {
514      $term['parent'] = $term['parents'][0];
515      $term['weight'] = $weight;
516      $changed_terms[$term['tid']] = $term;
517    }
518    $hierarchy = $term['parents'][0] != 0 ? 1 : $hierarchy;
519  }
520
521  // Save all updated terms.
522  foreach ($changed_terms as $term) {
523    taxonomy_save_term($term);
524  }
525
526  // Update the vocabulary hierarchy to flat or single hierarchy.
527  if ($vocabulary['hierarchy'] != $hierarchy) {
528    $vocabulary['hierarchy'] = $hierarchy;
529    taxonomy_save_vocabulary($vocabulary);
530  }
531}
532
533/**
534 * Theme the terms overview as a sortable list of terms.
535 *
536 * @ingroup themeable
537 * @see taxonomy_overview_terms()
538 */
539function theme_taxonomy_overview_terms($form) {
540  $page_increment  = $form['#page_increment'];
541  $page_entries    = $form['#page_entries'];
542  $back_peddle     = $form['#back_peddle'];
543  $forward_peddle  = $form['#forward_peddle'];
544
545  // Add drag and drop if parent fields are present in the form.
546  if ($form['#parent_fields']) {
547    drupal_add_tabledrag('taxonomy', 'match', 'parent', 'term-parent', 'term-parent', 'term-id', FALSE);
548    drupal_add_tabledrag('taxonomy', 'depth', 'group', 'term-depth', NULL, NULL, FALSE);
549    drupal_add_js(drupal_get_path('module', 'taxonomy') .'/taxonomy.js');
550    drupal_add_js(array('taxonomy' => array('backPeddle' => $back_peddle, 'forwardPeddle' => $forward_peddle)), 'setting');
551    drupal_add_css(drupal_get_path('module', 'taxonomy') .'/taxonomy.css');
552  }
553
554  $errors = form_get_errors() != FALSE ? form_get_errors() : array();
555  $rows = array();
556  foreach (element_children($form) as $key) {
557    if (isset($form[$key]['#term'])) {
558      $term = &$form[$key];
559
560      $row = array();
561      $row[] = (isset($term['#term']['depth']) && $term['#term']['depth'] > 0 ? theme('indentation', $term['#term']['depth']) : '') . drupal_render($term['view']);
562      if ($form['#parent_fields']) {
563        $term['tid']['#attributes']['class'] = 'term-id';
564        $term['parent']['#attributes']['class'] = 'term-parent';
565        $term['depth']['#attributes']['class'] = 'term-depth';
566        $row[0] .= drupal_render($term['parent']) . drupal_render($term['tid']) . drupal_render($term['depth']);
567      }
568      $row[] = drupal_render($term['edit']);
569
570      $row = array('data' => $row);
571      $rows[$key] = $row;
572    }
573  }
574
575  // Add necessary classes to rows.
576  $row_position = 0;
577  foreach ($rows as $key => $row) {
578    $classes = array();
579    if (isset($form['#parent_fields'])) {
580      $classes[] = 'draggable';
581    }
582
583    // Add classes that mark which terms belong to previous and next pages.
584    if ($row_position < $back_peddle || $row_position >= $page_entries - $forward_peddle) {
585      $classes[] = 'taxonomy-term-preview';
586    }
587
588    if ($row_position !== 0 && $row_position !== count($rows) - 1) {
589      if ($row_position == $back_peddle - 1 || $row_position == $page_entries - $forward_peddle - 1) {
590        $classes[] = 'taxonomy-term-divider-top';
591      }
592      elseif ($row_position == $back_peddle || $row_position == $page_entries - $forward_peddle) {
593        $classes[] = 'taxonomy-term-divider-bottom';
594      }
595    }
596
597    // Add an error class if this row contains a form error.
598    foreach ($errors as $error_key => $error) {
599      if (strpos($error_key, $key) === 0) {
600        $classes[] = 'error';
601      }
602    }
603    $rows[$key]['class'] = implode(' ', $classes);
604    $row_position++;
605  }
606
607  if (empty($rows)) {
608    $rows[] = array(array('data' => $form['#empty_text'], 'colspan' => '2'));
609  }
610
611  $header = array(t('Name'), t('Operations'));
612  $output = theme('table', $header, $rows, array('id' => 'taxonomy'));
613  $output .= drupal_render($form);
614  $output .= theme('pager', NULL, $page_increment);
615
616  return $output;
617}
618
619/**
620 * Menu callback; return the edit form for a new term after setting the title.
621 */
622function taxonomy_add_term_page($vocabulary) {
623  drupal_set_title(t('Add term to %vocabulary', array('%vocabulary' => $vocabulary->name)));
624  return drupal_get_form('taxonomy_form_term' , $vocabulary);
625}
626
627/**
628 * Form function for the term edit form.
629 *
630 * @ingroup forms
631 * @see taxonomy_form_term_submit()
632 */
633function taxonomy_form_term(&$form_state, $vocabulary, $edit = array()) {
634  $edit += array(
635    'name' => '',
636    'description' => '',
637    'tid' => NULL,
638    'weight' => 0,
639  );
640
641  $parent = array_keys(taxonomy_get_parents($edit['tid']));
642  $form['#term'] = $edit;
643  $form['#term']['parent'] = $parent;
644  $form['#vocabulary'] = (array)$vocabulary;
645  $form['#vocabulary']['nodes'] = drupal_map_assoc($vocabulary->nodes);;
646
647  // Check for confirmation forms.
648  if (isset($form_state['confirm_delete'])) {
649    return array_merge($form, taxonomy_term_confirm_delete($form_state, $edit['tid']));
650  }
651  elseif (isset($form_state['confirm_parents'])) {
652    return array_merge($form, taxonomy_term_confirm_parents($form_state, $vocabulary));
653  }
654
655  $form['identification'] = array(
656    '#type' => 'fieldset',
657    '#title' => t('Identification'),
658    '#collapsible' => TRUE,
659  );
660  $form['identification']['name'] = array(
661    '#type' => 'textfield',
662    '#title' => t('Term name'),
663    '#default_value' => $edit['name'],
664    '#maxlength' => 255,
665    '#description' => t('The name of this term.'),
666    '#required' => TRUE);
667  $form['identification']['description'] = array(
668    '#type' => 'textarea',
669    '#title' => t('Description'),
670    '#default_value' => $edit['description'],
671    '#description' => t('A description of the term. To be displayed on taxonomy/term pages and RSS feeds.'));
672
673  $form['advanced'] = array(
674    '#type' => 'fieldset',
675    '#title' => t('Advanced options'),
676    '#collapsible' => TRUE,
677    '#collapsed' => $vocabulary->hierarchy > 1 ? FALSE : TRUE,
678  );
679
680  // taxonomy_get_tree and taxonomy_get_parents may contain large numbers of
681  // items so we check for taxonomy_override_selector before loading the
682  // full vocabulary. Contrib modules can then intercept before
683  // hook_form_alter to provide scalable alternatives.
684  if (!variable_get('taxonomy_override_selector', FALSE)) {
685    $parent = array_keys(taxonomy_get_parents($edit['tid']));
686    $children = taxonomy_get_tree($vocabulary->vid, $edit['tid']);
687
688    // A term can't be the child of itself, nor of its children.
689    foreach ($children as $child) {
690      $exclude[] = $child->tid;
691    }
692    $exclude[] = $edit['tid'];
693
694    $form['advanced']['parent'] = _taxonomy_term_select(t('Parents'), 'parent', $parent, $vocabulary->vid, t('Parent terms') .'.', 1, '<'. t('root') .'>', $exclude);
695    $form['advanced']['relations'] = _taxonomy_term_select(t('Related terms'), 'relations', array_keys(taxonomy_get_related($edit['tid'])), $vocabulary->vid, NULL, 1, '<'. t('none') .'>', array($edit['tid']));
696  }
697  $form['advanced']['synonyms'] = array(
698    '#type' => 'textarea',
699    '#title' => t('Synonyms'),
700    '#default_value' => implode("\n", taxonomy_get_synonyms($edit['tid'])),
701    '#description' => t('Synonyms of this term, one synonym per line.'));
702  $form['advanced']['weight'] = array(
703    '#type' => 'textfield',
704    '#title' => t('Weight'),
705    '#size' => 6,
706    '#default_value' => $edit['weight'],
707    '#description' => t('Terms are displayed in ascending order by weight.'),
708    '#required' => TRUE);
709  $form['vid'] = array(
710    '#type' => 'value',
711    '#value' => $vocabulary->vid);
712  $form['submit'] = array(
713    '#type' => 'submit',
714    '#value' => t('Save'));
715
716  if ($edit['tid']) {
717    $form['delete'] = array(
718      '#type' => 'submit',
719      '#value' => t('Delete'));
720    $form['tid'] = array(
721      '#type' => 'value',
722      '#value' => $edit['tid']);
723  }
724  else {
725    $form['destination'] = array('#type' => 'hidden', '#value' => $_GET['q']);
726  }
727
728  return $form;
729}
730
731/**
732 * Validation handler for the term edit form. Ensure numeric weight values.
733 *
734 * @see taxonomy_form_term()
735 */
736function taxonomy_form_term_validate($form, &$form_state) {
737  if (isset($form_state['values']['weight']) && !is_numeric($form_state['values']['weight'])) {
738    form_set_error('weight', t('Weight value must be numeric.'));
739  }
740}
741
742/**
743 * Submit handler to insert or update a term.
744 *
745 * @see taxonomy_form_term()
746 */
747function taxonomy_form_term_submit($form, &$form_state) {
748  if ($form_state['clicked_button']['#value'] == t('Delete')) {
749    // Execute the term deletion.
750    if ($form_state['values']['delete'] === TRUE) {
751      return taxonomy_term_confirm_delete_submit($form, $form_state);
752    }
753    // Rebuild the form to confirm term deletion.
754    $form_state['rebuild'] = TRUE;
755    $form_state['confirm_delete'] = TRUE;
756    return;
757  }
758  // Rebuild the form to confirm enabling multiple parents.
759  elseif ($form_state['clicked_button']['#value'] == t('Save') && !$form['#vocabulary']['tags'] && count($form_state['values']['parent']) > 1 && $form['#vocabulary']['hierarchy'] < 2) {
760    $form_state['rebuild'] = TRUE;
761    $form_state['confirm_parents'] = TRUE;
762    return;
763  }
764
765  switch (taxonomy_save_term($form_state['values'])) {
766    case SAVED_NEW:
767      drupal_set_message(t('Created new term %term.', array('%term' => $form_state['values']['name'])));
768      watchdog('taxonomy', 'Created new term %term.', array('%term' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/term/'. $form_state['values']['tid']));
769      break;
770    case SAVED_UPDATED:
771      drupal_set_message(t('Updated term %term.', array('%term' => $form_state['values']['name'])));
772      watchdog('taxonomy', 'Updated term %term.', array('%term' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/term/'. $form_state['values']['tid']));
773      break;
774  }
775
776  if (!$form['#vocabulary']['tags']) {
777    $current_parent_count = count($form_state['values']['parent']);
778    $previous_parent_count = count($form['#term']['parent']);
779    // Root doesn't count if it's the only parent.
780    if ($current_parent_count == 1 && isset($form_state['values']['parent'][''])) {
781      $current_parent_count = 0;
782      $form_state['values']['parent'] = array();
783    }
784
785    // If the number of parents has been reduced to one or none, do a check on the
786    // parents of every term in the vocabulary value.
787    if ($current_parent_count < $previous_parent_count && $current_parent_count < 2) {
788      taxonomy_check_vocabulary_hierarchy($form['#vocabulary'], $form_state['values']);
789    }
790    // If we've increased the number of parents and this is a single or flat
791    // hierarchy, update the vocabulary immediately.
792    elseif ($current_parent_count > $previous_parent_count && $form['#vocabulary']['hierarchy'] < 2) {
793      $form['#vocabulary']['hierarchy'] = $current_parent_count == 1 ? 1 : 2;
794      taxonomy_save_vocabulary($form['#vocabulary']);
795    }
796  }
797
798  $form_state['tid'] = $form_state['values']['tid'];
799  $form_state['redirect'] = 'admin/content/taxonomy';
800  return;
801}
802
803/**
804 * Form builder for the confirmation of multiple term parents.
805 *
806 * @ingroup forms
807 * @see taxonomy_form_term()
808 */
809function taxonomy_term_confirm_parents(&$form_state, $vocabulary) {
810  $form = array();
811  foreach (element_children($form_state['values']) as $key) {
812    $form[$key] = array(
813      '#type' => 'value',
814      '#value' => $form_state['values'][$key],
815    );
816  }
817  $question = t('Set multiple term parents?');
818  $description = '<p>'. t("Adding multiple parents to a term will cause the %vocabulary vocabulary to look for multiple parents on every term. Because multiple parents are not supported when using the drag and drop outline interface, drag and drop will be disabled if you enable this option. If you choose to have multiple parents, you will only be able to set parents by using the term edit form.", array('%vocabulary' => $vocabulary->name)) .'</p>';
819  $description .= '<p>'. t("You may re-enable the drag and drop interface at any time by reducing multiple parents to a single parent for the terms in this vocabulary.") .'</p>';
820  return confirm_form($form, $question, drupal_get_destination(), $description, t('Set multiple parents'));
821}
822
823/**
824 * Form builder for the term delete form.
825 *
826 * @ingroup forms
827 * @see taxonomy_term_confirm_delete_submit()
828 */
829function taxonomy_term_confirm_delete(&$form_state, $tid) {
830  $term = taxonomy_get_term($tid);
831
832  $form['type'] = array('#type' => 'value', '#value' => 'term');
833  $form['name'] = array('#type' => 'value', '#value' => $term->name);
834  $form['tid'] = array('#type' => 'value', '#value' => $tid);
835  $form['delete'] = array('#type' => 'value', '#value' => TRUE);
836  return confirm_form($form,
837                  t('Are you sure you want to delete the term %title?',
838                  array('%title' => $term->name)),
839                  'admin/content/taxonomy',
840                  t('Deleting a term will delete all its children if there are any. This action cannot be undone.'),
841                  t('Delete'),
842                  t('Cancel'));
843}
844
845/**
846 * Submit handler to delete a term after confirmation.
847 *
848 * @see taxonomy_term_confirm_delete()
849 */
850function taxonomy_term_confirm_delete_submit($form, &$form_state) {
851  taxonomy_del_term($form_state['values']['tid']);
852  taxonomy_check_vocabulary_hierarchy($form['#vocabulary'], $form_state['values']);
853  drupal_set_message(t('Deleted term %name.', array('%name' => $form_state['values']['name'])));
854  watchdog('taxonomy', 'Deleted term %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
855  $form_state['redirect'] = 'admin/content/taxonomy';
856  return;
857}
858
859/**
860 * Form builder for the vocabulary delete confirmation form.
861 *
862 * @ingroup forms
863 * @see taxonomy_vocabulary_confirm_delete_submit()
864 */
865function taxonomy_vocabulary_confirm_delete(&$form_state, $vid) {
866  $vocabulary = taxonomy_vocabulary_load($vid);
867
868  $form['type'] = array('#type' => 'value', '#value' => 'vocabulary');
869  $form['vid'] = array('#type' => 'value', '#value' => $vid);
870  $form['name'] = array('#type' => 'value', '#value' => $vocabulary->name);
871  return confirm_form($form,
872                  t('Are you sure you want to delete the vocabulary %title?',
873                  array('%title' => $vocabulary->name)),
874                  'admin/content/taxonomy',
875                  t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'),
876                  t('Delete'),
877                  t('Cancel'));
878}
879
880/**
881 * Submit handler to delete a vocabulary after confirmation.
882 *
883 * @see taxonomy_vocabulary_confirm_delete()
884 */
885function taxonomy_vocabulary_confirm_delete_submit($form, &$form_state) {
886  $status = taxonomy_del_vocabulary($form_state['values']['vid']);
887  drupal_set_message(t('Deleted vocabulary %name.', array('%name' => $form_state['values']['name'])));
888  watchdog('taxonomy', 'Deleted vocabulary %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
889  $form_state['redirect'] = 'admin/content/taxonomy';
890  return;
891}
892
893/**
894 * Form builder to confirm reseting a vocabulary to alphabetical order.
895 *
896 * @ingroup forms
897 * @see taxonomy_vocabulary_confirm_reset_alphabetical_submit()
898 */
899function taxonomy_vocabulary_confirm_reset_alphabetical(&$form_state, $vid) {
900  $vocabulary = taxonomy_vocabulary_load($vid);
901
902  $form['type'] = array('#type' => 'value', '#value' => 'vocabulary');
903  $form['vid'] = array('#type' => 'value', '#value' => $vid);
904  $form['name'] = array('#type' => 'value', '#value' => $vocabulary->name);
905  $form['reset_alphabetical'] = array('#type' => 'value', '#value' => TRUE);
906  return confirm_form($form,
907                  t('Are you sure you want to reset the vocabulary %title to alphabetical order?',
908                  array('%title' => $vocabulary->name)),
909                  'admin/content/taxonomy/'. $vid,
910                  t('Resetting a vocabulary will discard all custom ordering and sort items alphabetically.'),
911                  t('Reset to alphabetical'),
912                  t('Cancel'));
913}
914
915/**
916 * Submit handler to reset a vocabulary to alphabetical order after confirmation.
917 *
918 * @see taxonomy_vocabulary_confirm_reset_alphabetical()
919 */
920function taxonomy_vocabulary_confirm_reset_alphabetical_submit($form, &$form_state) {
921  db_query('UPDATE {term_data} SET weight = 0 WHERE vid = %d', $form_state['values']['vid']);
922  drupal_set_message(t('Reset vocabulary %name to alphabetical order.', array('%name' => $form_state['values']['name'])));
923  watchdog('taxonomy', 'Reset vocabulary %name to alphabetical order.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
924  $form_state['redirect'] = 'admin/content/taxonomy/'. $form_state['values']['vid'];
925}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.