You are here

function taxonomy_manager_term_data_edit in Taxonomy Manager 6.2

Same name and namespace in other branches
  1. 5 taxonomy_manager.module \taxonomy_manager_term_data_edit()
  2. 6 taxonomy_manager.admin.inc \taxonomy_manager_term_data_edit()

callback handler for updating term data

Parameters

$vid:

1 string reference to 'taxonomy_manager_term_data_edit'
taxonomy_manager_menu in ./taxonomy_manager.module
Implementation of hook_menu

File

./taxonomy_manager.admin.inc, line 1580
Taxonomy Manager Admin

Code

function taxonomy_manager_term_data_edit() {
  $param = $_POST;
  if (!taxonomy_manager_valid_ajax_callback($param)) {
    return;
  }
  $msg = t("Changes successfully saved");
  $is_error_msg = FALSE;
  $tid = $param['tid'];
  if (!$tid) {
    $tid = $param['term_data']['tid'];
  }
  $vid = $param['vid'];
  if (!$vid) {
    $vid = $param['term_data']['vid'];
  }
  $values = $param['value'];
  $attr_type = $param['attr_type'];
  $op = $param['op'];
  if ($op == t("Save changes")) {
    db_query("UPDATE {term_data} SET name = '%s' WHERE tid = %d", $param['term_data']['name'], $tid);
    db_query("UPDATE {term_data} SET description = '%s' WHERE tid = %d", $param['term_data']['description'], $tid);
  }
  if ($op == 'add') {
    $typed_terms = taxonomy_manager_autocomplete_tags_get_tids($values, $vid, FALSE);
  }
  if ($op == 'add' && ($attr_type == 'parent' || $attr_type == 'related')) {

    //check for unique names
    $error_msg = "";
    if (_taxonomy_manager_check_duplicates($vid, $values, $error_msg)) {
      $msg = t("Warning: Your input matches with multiple terms, because of duplicated term names. Please enter a unique term name or the term id with 'term-id:[tid]'") . " (" . $error_msg . ").";
      $is_error_msg = TRUE;
    }
    if (!taxonomy_manager_check_language($vid, array(
      $tid,
    ), $typed_terms)) {
      $msg = t("Terms must be of the same language");
      $is_error_msg = TRUE;
    }
    if ($attr_type == 'parent') {

      //validation for consistent hierarchy
      $parents = array();
      foreach ($typed_terms as $parent_info) {
        $parents[$parent_info['tid']] = $parent_info['tid'];
      }
      $tids = array();
      $tids[$tid] = $tid;
      if (!taxonomy_manager_check_circular_hierarchy($tids, $parents)) {
        $msg = t('Invalid parent. The resulting hierarchy would contain circles, which is not allowed. A term cannot be a parent of itself.');
        $is_error_msg = TRUE;
      }
    }
  }
  else {
    if ($op == 'add' && $attr_type == 'translation') {
      $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
      preg_match_all($regexp, $values, $matches);
      $translation_terms = array_unique($matches[1]);
      if (count($translation_terms) > 1) {
        $msg = t('Please provide only one term for translation');
        $is_error_msg = TRUE;
      }
      else {
        if ($param['edit-term-data-translations-op-lang'] == $param['edit-term-data-language']) {
          $msg = t('Invalid language selection');
          $is_error_msg = TRUE;
        }
        else {
          if (empty($param['edit-term-data-translations-op-lang'])) {
            $msg = t('Missing language for new term');
            $is_error_msg = TRUE;
          }
          else {
            $translations = i18ntaxonomy_term_get_translations(array(
              'tid' => $tid,
            ), FALSE);
            foreach ($translations as $translation) {
              if ($translation->language == $param['edit-term-data-translations-op-lang']) {
                $msg = t('Invalid language selection. Translation already exists');
                $is_error_msg = TRUE;
                break;
              }
            }
          }
        }
      }
    }
    else {
      if ($attr_type == "language") {
        $translations = i18ntaxonomy_term_get_translations(array(
          'tid' => $tid,
        ), FALSE);
        foreach ($translations as $translation) {
          if ($translation->language == $values) {
            $msg = t('Invalid language selection.');
            $is_error_msg = TRUE;
            break;
          }
        }
      }
    }
  }
  if (!$is_error_msg) {
    if ($op == 'add') {
      if ($attr_type == 'synonym') {
        $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
        preg_match_all($regexp, $values, $matches);
        $synonyms = array_unique($matches[1]);
        $values = array();
        foreach ($synonyms as $syn) {
          $values[] = trim($syn);
        }
      }
      else {
        $typed_term_tids = array();
        if ($attr_type == "translation") {
          $typed_term_tids = taxonomy_manager_autocomplete_tags_get_tids($values, $vid, TRUE, $param['edit-term-data-translations-op-lang']);
        }
        else {
          $typed_term_tids = taxonomy_manager_autocomplete_tags_get_tids($values, $vid, TRUE, $param['edit-term-data-language']);
        }
        $values = array();
        foreach ($typed_term_tids as $term_info) {
          $values[] = $term_info['tid'];
        }
      }
    }
    switch ($attr_type) {
      case 'parent':
        if (!is_array($values)) {
          $values = array(
            $values,
          );
        }
        foreach ($values as $value) {
          db_query("DELETE FROM {term_hierarchy} WHERE parent = %d AND tid = %d", $value, $tid);
          if ($op == 'add') {
            db_query("INSERT INTO {term_hierarchy} (parent, tid) VALUES (%d, %d)", $value, $tid);
          }
        }
        if ($op == 'delete') {
          $parents = taxonomy_get_parents($tid);
          if (count($parents) == 0) {

            //ensure that a term has a least parent 0
            db_query("DELETE FROM {term_hierarchy} WHERE parent = 0 AND tid = %d", $tid);
            db_query("INSERT INTO {term_hierarchy} (parent, tid) VALUES (0, %d)", $tid);
          }
        }
        else {
          if ($op == 'add') {
            db_query("DELETE FROM {term_hierarchy} WHERE parent = 0 AND tid = %d", $tid);
          }
        }
        taxonomy_manager_update_voc($vid, taxonomy_get_parents($tid));
        $msg = t("Successfully updated parents");
        break;
      case 'related':
        if (!is_array($values)) {
          $values = array(
            $values,
          );
        }
        foreach ($values as $value) {
          if ($value != 0) {
            db_query("DELETE FROM {term_relation} WHERE tid1 = %d AND tid2 = %d", $tid, $value);

            //  db_query("DELETE FROM {term_relation} WHERE tid2 = %d AND tid1 = %d", $tid, $value);
            if ($op == 'add') {
              db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $tid, $value);
            }
          }
        }
        $msg = t("Successfully updated related terms");
        break;
      case 'synonym':
        if (!is_array($values)) {
          $values = array(
            $values,
          );
        }
        foreach ($values as $value) {
          db_query("DELETE FROM {term_synonym} WHERE tid = %d AND name = '%s'", $tid, $value);
          if ($op == 'add') {
            db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $tid, $value);
          }
        }
        $msg = t("Successfully updated synonyms");
        break;
      case 'weight':
        if (is_numeric($values)) {
          db_query("UPDATE {term_data} SET weight = %d WHERE tid = %d", $values, $tid);
          $msg = t("Successfully updated weight to !weight", array(
            '!weight' => $values,
          ));
        }
        break;
      case 'language':
        if (module_exists('i18ntaxonomy')) {
          _i18ntaxonomy_term_set_lang($tid, $values);
          $msg = t("Successfully updated language");
        }
        else {
          $is_error_msg = TRUE;
          $msg = t("Module i18ntaxonomy not enabled");
        }
        break;
      case 'translation':
        if (module_exists('i18ntaxonomy')) {
          if ($op == "add") {
            taxonomy_manager_add_translation($tid, $values[0]);
            $msg = t("Successfully added translation");
          }
          else {
            if ($op == "delete") {
              db_query('UPDATE {term_data} SET trid = 0 WHERE tid = %d', $values);
              if (count(i18ntaxonomy_term_get_translations(array(
                'tid' => $tid,
              ), FALSE)) == 0) {
                db_query('UPDATE {term_data} SET trid = 0 WHERE tid = %d', $tid);
              }
              $msg = t("Successfully removed translation");
            }
          }
        }
        else {
          $is_error_msg = TRUE;
          $msg = t("Module i18ntaxonomy not enabled");
        }
        break;
    }
    $term = (array) taxonomy_get_term($tid);
    $term['fields'] = $param['term_data']['fields'];
    module_invoke_all('taxonomy', 'update', 'term', $term);
    module_invoke_all('taxonomy_manager_term_data_submit', $param, $values);
  }
  drupal_json(array(
    'data' => taxonomy_manager_update_term_data_form($vid, $tid, TRUE, FALSE, $msg, $is_error_msg),
  ));

  //taxonomy_manager_update_term_data_form($vid, $tid, TRUE);
  exit;
}