diff options
author | Steven Wittens <steven@10.no-reply.drupal.org> | 2007-04-05 03:08:48 +0000 |
---|---|---|
committer | Steven Wittens <steven@10.no-reply.drupal.org> | 2007-04-05 03:08:48 +0000 |
commit | c504141f457401cd26adc6ea47f251e99cf930c9 (patch) | |
tree | 222357842e3b1712aab1919188c78d73d50efae3 /modules/taxonomy/taxonomy.module | |
parent | 8fd0fdbd54e17e176959592d9ee43e3b3ba60f97 (diff) | |
download | brdo-c504141f457401cd26adc6ea47f251e99cf930c9.tar.gz brdo-c504141f457401cd26adc6ea47f251e99cf930c9.tar.bz2 |
#133858: Code cleanup and e_notices in taxonomy.module for tag parsing.
Diffstat (limited to 'modules/taxonomy/taxonomy.module')
-rw-r--r-- | modules/taxonomy/taxonomy.module | 83 |
1 files changed, 52 insertions, 31 deletions
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index a3f5f34e3..c1b7b10fe 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -482,6 +482,11 @@ function taxonomy_form_term_submit($form_id, $form_values) { * Status constant indicating if term was inserted or updated. */ function taxonomy_save_term(&$form_values) { + $form_values += array( + 'description' => '', + 'weight' => 0 + ); + if (!empty($form_values['tid']) && $form_values['name']) { db_query("UPDATE {term_data} SET name = '%s', description = '%s', weight = %d WHERE tid = %d", $form_values['name'], $form_values['description'], $form_values['weight'], $form_values['tid']); $hook = 'update'; @@ -688,20 +693,7 @@ function taxonomy_form_alter(&$form, $form_id) { while ($vocabulary = db_fetch_object($c)) { if ($vocabulary->tags) { - $typed_terms = array(); - foreach ($terms as $term) { - // Extract terms belonging to the vocabulary in question. - if ($term->vid == $vocabulary->vid) { - - // Commas and quotes in terms are special cases, so encode 'em. - if (strpos($term->name, ',') !== FALSE || strpos($term->name, '"') !== FALSE) { - $term->name = '"'.str_replace('"', '""', $term->name).'"'; - } - - $typed_terms[] = $term->name; - } - } - $typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL); + $typed_string = taxonomy_implode_tags($terms, $vocabulary->vid) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL); if ($vocabulary->help) { $help = $vocabulary->help; @@ -808,21 +800,10 @@ function taxonomy_node_save($node, $terms) { unset($terms['tags']); foreach ($typed_input as $vid => $vid_value) { - // This regexp allows the following types of user input: - // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar - $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x'; - preg_match_all($regexp, $vid_value, $matches); - $typed_terms = array_unique($matches[1]); + $typed_terms = taxonomy_explode_tags($vid_value); $inserted = array(); foreach ($typed_terms as $typed_term) { - // If a user has escaped a term (to demonstrate that it is a group, - // or includes a comma or quote character), we remove the escape - // formatting so to save the term into the database as the user intends. - $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term)); - $typed_term = trim($typed_term); - if ($typed_term == "") { continue; } - // See if the term exists in the chosen vocabulary // and return the tid; otherwise, add a new record. $possibilities = taxonomy_get_term_by_name($typed_term); @@ -1492,11 +1473,7 @@ function _taxonomy_get_tid_from_term($term) { */ function taxonomy_autocomplete($vid, $string = '') { // The user enters a comma-separated list of tags. We only autocomplete the last tag. - // This regexp allows the following types of user input: - // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar - $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x'; - preg_match_all($regexp, $string, $matches); - $array = $matches[1]; + $array = taxonomy_explode_tags($string); // Fetch last tag $last_string = trim(array_pop($array)); @@ -1518,3 +1495,47 @@ function taxonomy_autocomplete($vid, $string = '') { exit(); } } + +/** + * Explode a string of given tags into an array. + */ +function taxonomy_explode_tags($tags) { + // This regexp allows the following types of user input: + // this, "somecompany, llc", "and ""this"" w,o.rks", foo bar + $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x'; + preg_match_all($regexp, $tags, $matches); + $typed_tags = array_unique($matches[1]); + + $tags = array(); + foreach ($typed_tags as $tag) { + // If a user has escaped a term (to demonstrate that it is a group, + // or includes a comma or quote character), we remove the escape + // formatting so to save the term into the database as the user intends. + $tag = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $tag))); + if ($tag != "") { + $tags[] = $tag; + } + } + + return $tags; +} + +/** + * Implode a list of tags of a certain vocabulary into a string. + */ +function taxonomy_implode_tags($tags, $vid = NULL) { + $typed_tags = array(); + foreach ($tags as $tag) { + // Extract terms belonging to the vocabulary in question. + if (is_null($vid) || $term->vid == $vid) { + + // Commas and quotes in terms are special cases, so encode 'em. + if (strpos($term->name, ',') !== FALSE || strpos($term->name, '"') !== FALSE) { + $term->name = '"'. str_replace('"', '""', $term->name) .'"'; + } + + $typed_tags[] = $term->name; + } + } + return implode(', ', $typed_tags); +} |