123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844 |
- <?php
- function term_merge_form($form, $form_state, $vocabulary = NULL, $term = NULL) {
- if (is_null($vocabulary)) {
- $vocabulary = taxonomy_vocabulary_load($term->vid);
- }
-
- $form['#vocabulary'] = $vocabulary;
- if (!isset($form_state['storage']['confirm'])) {
-
- $tree = taxonomy_get_tree($vocabulary->vid);
- $term_branch_value = is_null($term) ? NULL : array($term->tid);
- if (variable_get('taxonomy_override_selector', FALSE) && module_exists('hs_taxonomy')) {
-
-
- $form['term_branch'] = array(
- '#type' => 'hierarchical_select',
-
-
-
-
- '#config' => array(
- 'module' => 'hs_taxonomy',
- 'params' => array(
- 'vid' => $vocabulary->vid,
- 'exclude_tid' => NULL,
- 'root_term' => FALSE,
- ),
- 'enforce_deepest' => 0,
- 'entity_count' => 0,
- 'require_entity' => 0,
- 'save_lineage' => 0,
- 'level_labels' => array(
- 'status' => 0,
- ),
- 'dropbox' => array(
- 'status' => 1,
- 'limit' => 0,
- ),
- 'editability' => array(
- 'status' => 0,
- ),
- 'resizable' => TRUE,
- 'render_flat_select' => 0,
- ),
- );
- }
- else {
-
- $options = array();
- foreach ($tree as $v) {
- $options[$v->tid] = str_repeat('-', $v->depth) . $v->name . ' [tid: ' . $v->tid . ']';
- }
- $form['term_branch'] = array(
- '#type' => 'select',
- '#required' => TRUE,
- '#multiple' => TRUE,
- '#options' => $options,
- '#size' => 8,
- );
- }
- $form['term_branch'] = array(
- '#title' => t('Terms to Merge'),
- '#description' => t('Please, choose the terms you want to merge into another term.'),
- '#ajax' => array(
- 'callback' => 'term_merge_form_term_trunk',
- 'wrapper' => 'term-merge-form-term-trunk',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- '#default_value' => $term_branch_value,
- ) + $form['term_branch'];
- if (is_null($form['term_branch']['#default_value'])) {
- unset($form['term_branch']['#default_value']);
- }
- $form['term_trunk'] = array(
- '#type' => 'fieldset',
- '#title' => t('Merge Into'),
- '#prefix' => '<div id="term-merge-form-term-trunk">',
- '#suffix' => '</div>',
- '#tree' => TRUE,
- );
-
- $term_trunk_widget_options = array(
- 'autocomplete' => 'Autocomplete',
- );
- if (variable_get('taxonomy_override_selector', FALSE) && module_exists('hs_taxonomy')) {
- $term_trunk_widget_options['hs_taxonomy'] = t('Hierarchical Select');
- $term_trunk_widget = 'hs_taxonomy';
- }
- else {
- $term_trunk_widget_options['select'] = t('Select');
- $term_trunk_widget = 'select';
- }
-
-
- if (count($tree) > 200) {
- $term_trunk_widget = 'autocomplete';
- }
-
- if (isset($form_state['values']['term_trunk']['widget']) && in_array($form_state['values']['term_trunk']['widget'], array_keys($term_trunk_widget_options))) {
- $term_trunk_widget = $form_state['values']['term_trunk']['widget'];
- }
- $form['term_trunk']['widget'] = array(
- '#type' => 'radios',
- '#title' => t('Widget'),
- '#required' => TRUE,
- '#options' => $term_trunk_widget_options,
- '#default_value' => $term_trunk_widget,
- '#description' => t('Choose what widget you prefer for entering the term trunk.'),
- '#ajax' => array(
- 'callback' => 'term_merge_form_term_trunk',
- 'wrapper' => 'term-merge-form-term-trunk',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
-
-
-
-
-
-
-
-
- $function = 'term_merge_form_term_trunk_widget_' . $term_trunk_widget;
- $function($form, $form_state, $vocabulary);
-
-
- $form['term_trunk']['tid']['#title'] = t('Merge into');
-
- $form += term_merge_merge_options_elements($vocabulary);
- $form['actions'] = array(
- '#type' => 'actions',
- );
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit'),
- );
- }
- else {
-
- $count = count($form_state['values']['term_branch']);
- $question = format_plural($count, 'Are you sure want to merge 1 term?', 'Are you sure want to merge @count terms?');
- $form = confirm_form($form, $question, 'admin/structure/taxonomy/' . $vocabulary->machine_name);
- }
- return $form;
- }
- function term_merge_form_validate($form, &$form_state) {
- if (!isset($form_state['storage']['confirm'])) {
-
- $prohibited_trunks = array();
- foreach ($form_state['values']['term_branch'] as $term_branch) {
- $children = taxonomy_get_tree($form['#vocabulary']->vid, $term_branch);
- $prohibited_trunks[] = $term_branch;
- foreach ($children as $child) {
- $prohibited_trunks[] = $child->tid;
- }
- }
- if (in_array($form_state['values']['term_trunk']['tid'], $prohibited_trunks)) {
- form_error($form['term_trunk']['tid'], t('Trunk term cannot be one of the selected branch terms or their children.'));
- }
- }
- }
- function term_merge_form_submit($form, &$form_state) {
- if (!isset($form_state['storage']['confirm'])) {
-
-
- $form_state['storage']['confirm'] = 0;
- $form_state['rebuild'] = TRUE;
-
-
-
- if (isset($form_state['values']['relations'])) {
- $form_state['values'] += $form_state['values']['relations'];
- }
- $form_state['storage']['info'] = $form_state['values'];
- $form_state['storage']['merge_settings'] = term_merge_merge_options_submit($form, $form_state, $form);
- $form_state['storage']['old_form'] = $form;
- }
- else {
-
- $form_state['values'] = $form_state['storage']['info'];
-
- if ($form_state['values']['term_trunk']['tid'] == TERM_MERGE_NEW_TERM_TRUNK) {
-
- module_load_include('inc', 'taxonomy', 'taxonomy.admin');
- taxonomy_form_term_submit($form_state['storage']['old_form']['term_trunk']['term_create'], $form_state);
- $term_trunk = $form_state['term'];
- }
- else {
- $term_trunk = taxonomy_term_load($form_state['values']['term_trunk']['tid']);
- }
- term_merge(array_values($form_state['values']['term_branch']), $term_trunk->tid, $form_state['storage']['merge_settings']);
- $form_state['redirect'] = array('taxonomy/term/' . $term_trunk->tid);
- }
- }
- function term_merge_form_term_trunk_widget_select(&$form, &$form_state, $vocabulary) {
- $tree = taxonomy_get_tree($vocabulary->vid);
- $options = array();
- foreach ($tree as $v) {
- $options[$v->tid] = str_repeat('-', $v->depth) . $v->name . ' [tid: ' . $v->tid . ']';
- }
- $term_branch_value = array();
-
-
- if (isset($form['term_branch']['#default_value']) && is_array($form['term_branch']['#default_value'])) {
- $term_branch_value = $form['term_branch']['#default_value'];
- }
- if (isset($form_state['values']['term_branch']) && is_array($form_state['values']['term_branch'])) {
- $term_branch_value = $form_state['values']['term_branch'];
- }
- if (!empty($term_branch_value)) {
-
-
- foreach ($term_branch_value as $v) {
- unset($options[$v]);
- foreach (taxonomy_get_tree($vocabulary->vid, $v) as $child) {
- unset($options[$child->tid]);
- }
- }
- $options = array(TERM_MERGE_NEW_TERM_TRUNK => 'New Term') + $options;
- }
- else {
-
- $options = array();
- }
- $form['term_trunk']['tid'] = array(
- '#type' => 'select',
- '#required' => TRUE,
- '#description' => t('Choose into what term you want to merge.'),
- '#options' => $options,
- '#ajax' => array(
- 'callback' => 'term_merge_form_term_trunk_term_create',
- 'wrapper' => 'term-merge-form-term-trunk-term-create',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
- $form['term_trunk']['term_create'] = array(
- '#prefix' => '<div id="term-merge-form-term-trunk-term-create">',
- '#suffix' => '</div>',
- );
-
-
- if (isset($form_state['values']['term_trunk']['tid']) && $form_state['values']['term_trunk']['tid'] == TERM_MERGE_NEW_TERM_TRUNK) {
- module_load_include('inc', 'taxonomy', 'taxonomy.admin');
- $form['term_trunk']['term_create'] += array(
- '#type' => 'fieldset',
- '#title' => t('Create New Term'),
- );
- $form['term_trunk']['term_create'] += taxonomy_form_term($form['term_trunk']['term_create'], $form_state, array(), $vocabulary);
-
-
- unset($form['term_trunk']['term_create']['actions']);
-
-
-
-
-
-
-
-
- unset($options[TERM_MERGE_NEW_TERM_TRUNK]);
- if (is_array($form['term_trunk']['term_create']['relations']['parent']['#options'])) {
- $form['term_trunk']['term_create']['relations']['parent']['#options'] = array_slice($form['term_trunk']['term_create']['relations']['parent']['#options'], 0, 1, TRUE) + $options;
- }
-
-
-
- $instances = field_info_instances($form['term_trunk']['term_create']['#entity_type'], $form['term_trunk']['term_create']['#bundle']);
- foreach ($instances as $instance) {
- $field = field_info_field($instance['field_name']);
- if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
- if (isset($form['term_trunk']['term_create'][$field['field_name']][LANGUAGE_NONE]['add_more']['#limit_validation_errors'])) {
- $form['term_trunk']['term_create'][$field['field_name']][LANGUAGE_NONE]['add_more']['#limit_validation_errors'] = array(array('term_branch'), array('term_trunk'));
- }
- }
- }
- }
- }
- function term_merge_form_term_trunk_widget_hs_taxonomy(&$form, &$form_state, $vocabulary) {
- $form['term_trunk']['tid'] = array(
- '#type' => 'hierarchical_select',
- '#description' => t('Please select a term to merge into.'),
- '#required' => TRUE,
- '#element_validate' => array('term_merge_form_trunk_term_widget_hs_taxonomy_validate'),
- '#config' => array(
- 'module' => 'hs_taxonomy',
- 'params' => array(
- 'vid' => $vocabulary->vid,
- 'exclude_tid' => NULL,
- 'root_term' => FALSE,
- ),
- 'enforce_deepest' => 0,
- 'entity_count' => 0,
- 'require_entity' => 0,
- 'save_lineage' => 0,
- 'level_labels' => array(
- 'status' => 0,
- ),
- 'dropbox' => array(
- 'status' => 0,
- ),
- 'editability' => array(
- 'status' => 0,
- ),
- 'resizable' => TRUE,
- 'render_flat_select' => 0,
- ),
- );
- }
- function term_merge_form_term_trunk_widget_autocomplete(&$form, &$form_state, $vocabulary) {
- $form['term_trunk']['tid'] = array(
- '#type' => 'textfield',
- '#description' => t("Start typing in a term's name in order to get some suggestions."),
- '#required' => TRUE,
- '#autocomplete_path' => 'term-merge/autocomplete/term-trunk/' . $vocabulary->machine_name,
- '#element_validate' => array('term_merge_form_trunk_term_widget_autocomplete_validate'),
- );
- }
- function term_merge_form_trunk_term_widget_autocomplete_validate($element, &$form_state, $form) {
- $term = taxonomy_get_term_by_name($element['#value'], $form['#vocabulary']->machine_name);
- if (!is_array($term) || empty($term)) {
-
-
- form_error($element, t('There are no terms with name %name in the %vocabulary vocabulary.', array(
- '%name' => $element['#value'],
- '%vocabulary' => $form['#vocabulary']->name,
- )));
- }
- else {
-
-
- $term = array_pop($term);
- form_set_value($element, $term->tid, $form_state);
- }
- }
- function term_merge_form_trunk_term_widget_hs_taxonomy_validate($element, &$form_state, $form) {
- $tid = 0;
- if (is_array($element['#value']) && !empty($element['#value'])) {
- $tid = (int) array_pop($element['#value']);
- }
- form_set_value($element, $tid, $form_state);
- }
- function term_merge_form_term_trunk_widget_autocomplete_autocomplete($vocabulary) {
-
-
- $args = func_get_args();
-
- array_shift($args);
- $tags_typed = implode('/', $args);
-
- $query = db_select('taxonomy_term_data', 't');
- $tags_return = $query->addTag('translatable')
- ->addTag('term_access')
- ->fields('t', array('tid', 'name'))
- ->condition('t.vid', $vocabulary->vid)
- ->condition('t.name', '%' . db_like($tags_typed) . '%', 'LIKE')
- ->range(0, 10)
- ->execute()
- ->fetchAllKeyed();
- $term_matches = array();
- foreach ($tags_return as $tid => $name) {
- $n = $name;
-
- if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
- $n = '"' . str_replace('"', '""', $name) . '"';
- }
- $term_matches[$n] = check_plain($name . ' [tid: ' . $tid . ']');
- }
- drupal_json_output($term_matches);
- }
- function term_merge_form_term_trunk($form, $form_state) {
- return $form['term_trunk'];
- }
- function term_merge_form_term_trunk_term_create($form, $form_state) {
- return $form['term_trunk']['term_create'];
- }
- function term_merge_duplicates_form($form, &$form_state, $vocabulary = NULL, $parent_term = NULL) {
-
- drupal_add_js(drupal_get_path('module', 'term_merge') . '/js/duplicate.form.js');
-
-
- if (!is_null($parent_term) && is_null($vocabulary)) {
- $vocabulary = taxonomy_vocabulary_load($parent_term->vid);
- }
- $tree = taxonomy_get_tree($vocabulary->vid, is_null($parent_term) ? 0 : $parent_term->tid);
-
-
- $form['help'] = array(
- '#markup' => '<p>' . t('Here you can merge terms with the same names. It is a useful tool against term-duplicates. If this tool is invoked on a term (not on the entire vocabulary), duplicate terms will be sought only among children of that term. The terms are grouped by names. Term into which the merging will occur is selected manually by user, however you must know that it is impossible to merge a parent term into any of its children.') . '</p>',
- );
- $form['scaling'] = array(
- '#type' => 'fieldset',
- '#title' => t('Scaling for large vocabularies'),
- '#description' => t('Adjust these settings if your vocabulary is very large.'),
- '#tree' => TRUE,
- '#collapsible' => TRUE,
- );
- $form['scaling']['help'] = array(
- '#markup' => '<p>' . format_plural(count($tree), 'Vocabulary %vocabulary has only 1 term. It is very unlikely you will merge anything here.', 'Vocabulary %vocabulary has @count terms. If this tool works slow, you may instruct the duplicate finder tool to terminate its work after it has found a specific number of possible duplicates.', array(
- '%vocabulary' => $vocabulary->name,
- )) . '</p>',
- );
- $form['scaling']['max_duplicates'] = array(
- '#type' => 'textfield',
- '#title' => t('Show N duplicates'),
- '#description' => t('Input an integer here - this many duplicates will be show on the form. Once this amount of possible duplicates is found, the search process terminates.'),
- '#required' => TRUE,
- '#default_value' => isset($form_state['values']['scaling']['max_duplicates']) ? $form_state['values']['scaling']['max_duplicates'] : 300,
- '#element_validate' => array('element_validate_integer_positive'),
- );
- $form['scaling']['update'] = array(
- '#type' => 'button',
- '#value' => t('Re-run duplicate search'),
- '#ajax' => array(
- 'callback' => 'term_merge_duplicates_form_scaling',
- 'wrapper' => 'term-merge-duplicate-wrapper',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
-
- $count = 0;
-
-
- $groups = array();
- foreach ($tree as $term) {
- if ($count >= $form['scaling']['max_duplicates']['#default_value']) {
-
- break;
- }
- $name = term_merge_duplicates_process_name($term->name);
- if (!isset($groups[$name])) {
- $groups[$name] = array();
- }
- else {
-
-
-
- $count++;
- if (count($groups[$name]) == 1) {
- $count++;
- }
- }
- $groups[$name][$term->tid] = $term;
- }
- $form['wrapper'] = array(
- '#prefix' => '<div id="term-merge-duplicate-wrapper">',
- '#suffix' => '</div>',
- );
- if ($count > 0) {
- $form['wrapper']['global_switch'] = array(
- '#type' => 'checkbox',
- '#title' => t('Select All Terms'),
- '#description' => t('Checking here will select for merging all the encountered duplicate terms.'),
- '#attributes' => array(
- 'class' => array('term-merge-duplicate-general-switch'),
- ),
- );
- }
- $form['wrapper']['group'] = array(
- '#tree' => TRUE,
- );
- foreach ($groups as $i => $group) {
- if (count($group) > 1) {
-
- ksort($group);
- $first_term = reset($group);
- $options = array();
- foreach ($group as $term) {
- $parents = array();
-
- $parents[] = t('Vocabulary Root');
- foreach (taxonomy_get_parents_all($term->tid) as $parent) {
-
- if ($parent->tid != $term->tid) {
- $parents[] = $parent->name;
- }
- }
- $language = isset($term->language) ? $term->language : LANGUAGE_NONE;
- if ($language == LANGUAGE_NONE) {
- $language = t('Not Specified');
- }
- $options[$term->tid] = array(
- 'id' => $term->tid,
- 'title' => l($term->name, 'taxonomy/term/' . $term->tid),
- 'language' => $language,
- 'description' => check_markup($term->description, $term->format),
- 'parents' => implode(' » ', $parents),
- );
- }
- $form['wrapper']['group'][$i] = array(
- '#type' => 'fieldset',
- '#title' => check_plain($first_term->name),
- '#collapsible' => TRUE,
- '#pre_render' => array('term_merge_duplicates_fieldset_preprocess'),
- '#element_validate' => array('term_merge_duplicates_fieldset_validate'),
- );
- $form['wrapper']['group'][$i]['duplicates'] = array(
- '#type' => 'tableselect',
- '#title' => 'Duplicates',
- '#header' => array(
- 'id' => t('ID'),
- 'title' => t('Title'),
- 'description' => t('Description'),
- 'language' => t('Language'),
- 'parents' => t('Parents'),
- ),
- '#options' => $options,
- );
- $options = array();
- foreach ($group as $term) {
- $options[$term->tid] = $term->name;
- }
- $form['wrapper']['group'][$i]['trunk_tid'] = array(
- '#type' => 'radios',
- '#title' => t('Merge Into'),
- '#options' => $options,
- '#attributes' => array(
- 'class' => array('term-merge-duplicate-trunk'),
- ),
- );
- }
- }
- if ($count > 0) {
-
- $form += term_merge_merge_options_elements($vocabulary);
- $form['actions'] = array(
- '#type' => 'actions',
- );
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit'),
- );
- }
- else {
- if (is_null($parent_term)) {
- $no_match_text = t('Sorry, seems like we were not able to find any possible duplicate terms in %vocabulary vocabulary.', array(
- '%vocabulary' => $vocabulary->name,
- ));
- }
- else {
- $no_match_text = t('Sorry, seems like we were not able to find any possible duplicate terms among children of %term term. You may want to search for duplicates through the entire <a href="!url">vocabulary</a>.', array(
- '%term' => $parent_term->name,
- '!url' => url('admin/structure/taxonomy/' . $vocabulary->machine_name . '/merge/duplicates'),
- ));
- }
- $form['nothing_found'] = array(
- '#markup' => '<p><b>' . $no_match_text . '</b></p>',
- );
- }
- return $form;
- }
- function term_merge_duplicates_form_submit($form, &$form_state) {
- $batch = array(
- 'title' => t('Merging terms'),
- 'operations' => array(),
- 'finished' => 'term_merge_batch_finished',
- 'file' => drupal_get_path('module', 'term_merge') . '/term_merge.batch.inc',
- );
-
- $merge_settings = term_merge_merge_options_submit($form, $form_state, $form);
- if (isset($form_state['values']['group'])) {
- foreach ($form_state['values']['group'] as $values) {
-
- $term_branches = array_filter($values['duplicates']);
-
- unset($term_branches[$values['trunk_tid']]);
- if (!empty($term_branches)) {
-
- $batch['operations'][] = array('_term_merge_batch_process', array(
- $term_branches,
- $values['trunk_tid'],
- $merge_settings,
- ));
- }
- }
- }
- if (empty($batch['operations'])) {
- drupal_set_message(t('No merging has been made, because you have not selected any duplicate term to merge.'));
- }
- else {
- batch_set($batch);
- }
- }
- function term_merge_duplicates_process_name($name) {
-
- $name = drupal_strtoupper($name);
-
- if (module_exists('transliteration')) {
- $name = transliteration_get($name);
-
- $name = preg_replace('#\W#', '', $name);
- }
- return $name;
- }
- function term_merge_duplicates_fieldset_preprocess($element) {
- $options = &$element['duplicates']['#options'];
- foreach ($options as $tid => $row) {
- $element['trunk_tid'][$tid]['#title_display'] = 'invisible';
- $options[$tid] = array(
- 'trunk' => drupal_render($element['trunk_tid'][$tid]),
- ) + $options[$tid];
- }
- $element['trunk_tid']['#title_display'] = 'invisible';
- $element['duplicates']['#header'] = array(
- 'trunk' => $element['trunk_tid']['#title'],
- ) + $element['duplicates']['#header'];
- return $element;
- }
- function term_merge_duplicates_fieldset_validate($element, &$form_state, $form) {
- if (!empty($element['duplicates']['#value']) && !is_numeric($element['trunk_tid']['#value'])) {
- form_error($element, t('Please, choose %trunk_tid_label for the group %group_label', array(
- '%trunk_tid_label' => $element['trunk_tid']['#title'],
- '%group_label' => $element['#title'],
- )));
- }
- }
- function term_merge_duplicates_form_scaling($form, &$form_state) {
- return $form['wrapper'];
- }
|