diff --git a/docroot/modules/taxonomy/taxonomy.admin.inc b/docroot/modules/taxonomy/taxonomy.admin.inc index 828fde0..bf4aeda 100644 --- a/docroot/modules/taxonomy/taxonomy.admin.inc +++ b/docroot/modules/taxonomy/taxonomy.admin.inc @@ -260,87 +260,27 @@ function taxonomy_overview_terms($form, &$form_state, $vocabulary) { $page = isset($_GET['page']) ? $_GET['page'] : 0; $page_increment = variable_get('taxonomy_terms_per_page_admin', 100); // Number of terms per page. - $page_entries = 0; // Elements shown on this page. - $before_entries = 0; // Elements at the root level before this page. - $after_entries = 0; // Elements at the root level after this page. - $root_entries = 0; // Elements at the root level on this page. - // Terms from previous and next pages are shown if the term tree would have - // been cut in the middle. Keep track of how many extra terms we show on each - // page of terms. - $back_step = NULL; - $forward_step = 0; - - // An array of the terms to be displayed on this page. - $current_page = array(); - - $delta = 0; $term_deltas = array(); $tree = taxonomy_get_tree($vocabulary->vid); - $term = current($tree); - do { - // In case this tree is completely empty. - if (empty($term)) { - break; - } - $delta++; - // Count entries before the current page. - if ($page && ($page * $page_increment) > $before_entries && !isset($back_step)) { - $before_entries++; - continue; - } - // Count entries after the current page. - elseif ($page_entries > $page_increment && isset($complete_tree)) { - $after_entries++; - continue; - } - - // Do not let a term start the page that is not at the root. - if (isset($term->depth) && ($term->depth > 0) && !isset($back_step)) { - $back_step = 0; - while ($pterm = prev($tree)) { - $before_entries--; - $back_step++; - if ($pterm->depth == 0) { - prev($tree); - continue 2; // Jump back to the start of the root level parent. - } - } - } - $back_step = isset($back_step) ? $back_step : 0; - - // Continue rendering the tree until we reach the a new root item. - if ($page_entries >= $page_increment + $back_step + 1 && $term->depth == 0 && $root_entries > 1) { - $complete_tree = TRUE; - // This new item at the root level is the first item on the next page. - $after_entries++; - continue; - } - if ($page_entries >= $page_increment + $back_step) { - $forward_step++; - } - - // Finally, if we've gotten down this far, we're rendering a term on this page. - $page_entries++; + $pages = array(array()); + $page_index = 0; + foreach ($tree as $term) { $term_deltas[$term->tid] = isset($term_deltas[$term->tid]) ? $term_deltas[$term->tid] + 1 : 0; $key = 'tid:' . $term->tid . ':' . $term_deltas[$term->tid]; - - // Keep track of the first term displayed on this page. - if ($page_entries == 1) { - $form['#first_tid'] = $term->tid; + if (!$term->parents[0] && count($pages[$page_index]) >= $page_increment) { + $page_index++; } - // Keep a variable to make sure at least 2 root elements are displayed. - if ($term->parents[0] == 0) { - $root_entries++; - } - $current_page[$key] = $term; - } while ($term = next($tree)); + $pages[$page_index][$key] = $term; + } + // An array of the terms to be displayed on this page. + $current_page = isset($pages[$page]) ? $pages[$page] : array(); // Because we didn't use a pager query, set the necessary pager variables. - $total_entries = $before_entries + $page_entries + $after_entries; + $total_entries = count($tree); $pager_total_items[0] = $total_entries; $pager_page_array[0] = $page; - $pager_total[0] = ceil($total_entries / $page_increment); + $pager_total[0] = count($pages); // If this form was already submitted once, it's probably hit a validation // error. Ensure the form is rebuilt in the same order as the user submitted. @@ -349,7 +289,7 @@ function taxonomy_overview_terms($form, &$form_state, $vocabulary) { $current_page = array_merge($order, $current_page); // Update our form with the new order. foreach ($current_page as $key => $term) { // Verify this is a term for the current page and set at the current depth. - if (is_array($form_state['input'][$key]) && is_numeric($form_state['input'][$key]['tid'])) { + if (isset($form_state['input'][$key]['depth'])) { $current_page[$key]->depth = $form_state['input'][$key]['depth']; } else { @@ -386,7 +326,7 @@ function taxonomy_overview_terms($form, &$form_state, $vocabulary) { ); $form[$key]['weight'] = array( '#type' => 'weight', - '#delta' => $delta, + '#delta' => $total_entries, '#title_display' => 'invisible', '#title' => t('Weight for added term'), '#default_value' => $term->weight, @@ -397,9 +337,11 @@ function taxonomy_overview_terms($form, &$form_state, $vocabulary) { $form['#total_entries'] = $total_entries; $form['#page_increment'] = $page_increment; - $form['#page_entries'] = $page_entries; - $form['#back_step'] = $back_step; - $form['#forward_step'] = $forward_step; + if ($form['#page_entries'] = count($current_page)) { + $form['#first_tid'] = reset($current_page)->tid; + } + $form['#back_step'] = 0; + $form['#forward_step'] = 0; $form['#empty_text'] = t('No terms available. Add term.', array('@link' => url('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add'))); if ($vocabulary->hierarchy < 2 && count($tree) > 1) { @@ -445,9 +387,6 @@ function taxonomy_overview_terms_submit($form, &$form_state) { return; } - // Sort term order based on weight. - uasort($form_state['values'], 'drupal_sort_weight'); - $vocabulary = $form['#vocabulary']; $hierarchy = 0; // Update the current hierarchy type as we go. @@ -458,59 +397,24 @@ function taxonomy_overview_terms_submit($form, &$form_state) { return; } - // Build a list of all terms that need to be updated on previous pages. - $weight = 0; - $term = (array) $tree[0]; - while ($term['tid'] != $form['#first_tid']) { - if ($term['parents'][0] == 0 && $term['weight'] != $weight) { - $term['parent'] = $term['parents'][0]; - $term['weight'] = $weight; - $changed_terms[$term['tid']] = $term; - } - $weight++; - $hierarchy = $term['parents'][0] != 0 ? 1 : $hierarchy; - $term = (array) $tree[$weight]; - } - // Renumber the current page weights and assign any new parents. - $level_weights = array(); foreach ($form_state['values'] as $tid => $values) { if (isset($form[$tid]['#term'])) { $term = $form[$tid]['#term']; - // Give terms at the root level a weight in sequence with terms on previous pages. - if ($values['parent'] == 0 && $term['weight'] != $weight) { - $term['weight'] = $weight; - $changed_terms[$term['tid']] = $term; - } - // Terms not at the root level can safely start from 0 because they're all on this page. - elseif ($values['parent'] > 0) { - $level_weights[$values['parent']] = isset($level_weights[$values['parent']]) ? $level_weights[$values['parent']] + 1 : 0; - if ($level_weights[$values['parent']] != $term['weight']) { - $term['weight'] = $level_weights[$values['parent']]; - $changed_terms[$term['tid']] = $term; + $changed = FALSE; + // Update any changed parents. + foreach (array('parent', 'weight') as $key) { + if ($values[$key] != $term[$key]) { + $term[$key] = $values[$key]; + $changed = TRUE; } } - // Update any changed parents. - if ($values['parent'] != $term['parent']) { - $term['parent'] = $values['parent']; - $changed_terms[$term['tid']] = $term; + if ($changed) { + $changed_terms[] = $term; } - $hierarchy = $term['parent'] != 0 ? 1 : $hierarchy; - $weight++; } } - // Build a list of all terms that need to be updated on following pages. - for ($weight; $weight < count($tree); $weight++) { - $term = (array) $tree[$weight]; - if ($term['parents'][0] == 0 && $term['weight'] != $weight) { - $term['parent'] = $term['parents'][0]; - $term['weight'] = $weight; - $changed_terms[$term['tid']] = $term; - } - $hierarchy = $term['parents'][0] != 0 ? 1 : $hierarchy; - } - // Save all updated terms. foreach ($changed_terms as $changed) { $term = (object) $changed;