Index: taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy.module,v
retrieving revision 1.217
diff -u -r1.217 taxonomy.module
--- taxonomy.module 25 Jul 2005 04:55:37 -0000 1.217
+++ taxonomy.module 31 Jul 2005 11:37:26 -0000
@@ -136,12 +136,33 @@
return form($form);
}
+function taxonomy_vocabulary_validate($edit) {
+ // Validate the vocabulary name:
+ if (!strlen($edit['name'])) {
+ form_set_error('name', t('You must enter a vocabulary name.'));
+ }
+
+ // Validate the type:
+ if (!$edit['nodes'] || !count($edit['nodes'])) {
+ form_set_error('nodes', t('You must select at least one type.'));
+ }
+ return $edit;
+}
+
+function taxonomy_term_validate($edit) {
+ // Validate the term name:
+ if (!strlen($edit['name'])) {
+ form_set_error('name', t('You must enter a term name.'));
+ }
+ return $edit;
+}
+
function taxonomy_save_vocabulary($edit) {
$edit['nodes'] = ($edit['nodes']) ? $edit['nodes'] : array();
$edit['weight'] = ($edit['weight']) ? $edit['weight'] : 0;
$data = array('name' => $edit['name'], 'description' => $edit['description'], 'help' => $edit['help'], 'multiple' => $edit['multiple'], 'required' => $edit['required'], 'hierarchy' => $edit['hierarchy'], 'relations' => $edit['relations'], 'tags' => $edit['tags'], 'weight' => $edit['weight'], 'module' => isset($edit['module']) ? $edit['module'] : 'taxonomy');
- if ($edit['vid'] && $edit['name']) {
+ if ($edit['vid'] && is_string($edit['name'])) {
db_query('UPDATE {vocabulary} SET '. _taxonomy_prepare_update($data) .' WHERE vid = %d', $edit['vid']);
db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $edit['vid']);
foreach ($edit['nodes'] as $type) {
@@ -233,7 +254,8 @@
$form .= _taxonomy_term_select(t('Related terms'), 'relations', array_keys(taxonomy_get_related($edit['tid'])), $vocabulary_id, NULL, 1, '<'. t('none') .'>', array($edit['tid']));
}
- $form .= form_textarea(t('Synonyms'), 'synonyms', implode("\n", taxonomy_get_synonyms($edit['tid'])), 60, 5, t('Synonyms of this term, one synonym per line.', array('%help-url' => url('admin/help/taxonomy', NULL, NULL, 'synonyms'))));
+ $synonyms = (isset($edit['synonyms'])) ? $edit['synonyms'] : implode("\n",taxonomy_get_synonyms($edit['tid']));
+ $form .= form_textarea(t('Synonyms'), 'synonyms', $synonyms, 60, 5, t('Synonyms of this term, one synonym per line.', array('%help-url' => url('admin/help/taxonomy', NULL, NULL, 'synonyms'))));
$form .= form_weight(t('Weight'), 'weight', $edit['weight'], 10, t('In listings, the heavier terms will sink and the lighter terms will be positioned nearer the top.'));
// Append extra term form elements.
$form .= implode('', module_invoke_all('taxonomy', 'form post', 'term', $edit));
@@ -252,7 +274,7 @@
}
function taxonomy_save_term($edit) {
- if ($edit['tid'] && $edit['name']) {
+ if ($edit['tid'] && is_string($edit['name'])) {
$data = array('name' => $edit['name'], 'description' => $edit['description'], 'weight' => $edit['weight']);
db_query('UPDATE {term_data} SET '. _taxonomy_prepare_update($data) .' WHERE tid = %d', $edit['tid']);
@@ -260,7 +282,7 @@
$status = SAVED_UPDATED;
}
else if ($edit['tid']) {
- return taxonomy_del_term($edit['tid']);
+ $status = taxonomy_del_term($edit['tid']);
}
else {
$edit['tid'] = db_next_id('{term_data}_tid');
@@ -342,6 +364,7 @@
}
cache_clear_all();
+ return SAVED_DELETED;
}
function _taxonomy_confirm_del_term($tid) {
@@ -1121,8 +1144,16 @@
return $output;
}
else {
- taxonomy_save_term($edit);
- drupal_goto('admin/taxonomy');
+ taxonomy_term_validate($edit);
+ if (!form_get_errors()) {
+ taxonomy_save_term($edit);
+ drupal_set_message(t('Created new term %term.', array('%term' => theme('placeholder', $edit['name']))));
+ drupal_goto('admin/taxonomy');
+ }
+ else {
+ $output = taxonomy_form_term($edit);
+ return $output;
+ }
}
}
@@ -1156,32 +1187,47 @@
else {
$deleted_name = $edit['name'];
$edit['name'] = 0;
+ $edit['nodes'] = array(0);
// fall through:
}
case t('Submit'):
if (arg(3) == 'vocabulary') {
- list($status, $object) = array_values(taxonomy_save_vocabulary($edit));
- switch ($status) {
- case SAVED_NEW:
- drupal_set_message(t('Created new vocabulary %name.', array('%name' => theme('placeholder', $edit['name']))));
- break;
- case SAVED_UPDATED:
- drupal_set_message(t('Updated vocabulary %name.', array('%name' => theme('placeholder', $edit['name']))));
- break;
- case SAVED_DELETED:
- drupal_set_message(t('Deleted vocabulary %name.', array('%name' => theme('placeholder', $deleted_name))));
+ taxonomy_vocabulary_validate($edit);
+ if (!form_get_errors()) {
+ list($status, $object) = array_values(taxonomy_save_vocabulary($edit));
+ switch ($status) {
+ case SAVED_NEW:
+ drupal_set_message(t('Created new vocabulary %name.', array('%name' => theme('placeholder', $edit['name']))));
+ break;
+ case SAVED_UPDATED:
+ drupal_set_message(t('Updated vocabulary %name.', array('%name' => theme('placeholder', $edit['name']))));
+ break;
+ case SAVED_DELETED:
+ drupal_set_message(t('Deleted vocabulary %name.', array('%name' => theme('placeholder', $deleted_name))));
+ break;
+ }
+ }
+ else {
+ $output = taxonomy_form_vocabulary($edit);
break;
- }
+ }
}
else {
- list($status, $object) = array_values(taxonomy_save_term($edit));
- switch ($status) {
- case SAVED_NEW:
- drupal_set_message(t('Created new term %term.', array('%term' => theme('placeholder', $edit['name']))));
- break;
- case SAVED_UPDATED:
- drupal_set_message(t('The term %term has been updated.', array('%term' => theme('placeholder', $edit['name']))));
- break;
+ taxonomy_term_validate($edit);
+ if (!form_get_errors()) {
+ list($status, $object) = array_values(taxonomy_save_term($edit));
+ switch ($status) {
+ case SAVED_NEW:
+ drupal_set_message(t('Created new term %term.', array('%term' => theme('placeholder', $edit['name']))));
+ break;
+ case SAVED_UPDATED:
+ drupal_set_message(t('The term %term has been updated.', array('%term' => theme('placeholder', $edit['name']))));
+ break;
+ }
+ }
+ else {
+ $output = taxonomy_form_term($edit);
+ break;
}
}
drupal_goto('admin/taxonomy');