--- taxonomy.module.old 2005-07-25 05:55:37.000000000 +0100 +++ taxonomy.module.new 2005-07-28 11:56:01.879468800 +0100 @@ -136,6 +136,42 @@ function taxonomy_form_vocabulary($edit return form($form); } +function taxonomy_vocabulary_validate($edit) { + // Validate the vocabulary name: + if ($error = taxonomy_validate_name($edit['name'])) { + form_set_error('name', $error); + } + + // Validate the type: + if ($error = taxonomy_validate_type($edit['nodes'])) { + form_set_error('nodes', $error); + } + + return $edit; +} + +function taxonomy_validate_name($name) { + if (!strlen($name)) return t('You must enter a vocabulary name.'); +} + +function taxonomy_validate_type($type) { + if (!$type || !sizeof($type)) return t('You must select at least one type.'); +} + +function taxonomy_term_validate($edit) { + // Validate the term name: + if ($error = taxonomy_validate_term_name($edit['name'])) { + form_set_error('name', $error); + } + + return $edit; +} + +function taxonomy_validate_term_name($name) { + if (!strlen($name)) return t('You must enter a term name.'); +} + + function taxonomy_save_vocabulary($edit) { $edit['nodes'] = ($edit['nodes']) ? $edit['nodes'] : array(); $edit['weight'] = ($edit['weight']) ? $edit['weight'] : 0; @@ -260,7 +296,7 @@ function taxonomy_save_term($edit) { $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 +378,7 @@ function taxonomy_del_term($tid) { } cache_clear_all(); + return SAVED_DELETED; } function _taxonomy_confirm_del_term($tid) { @@ -1121,8 +1158,15 @@ function taxonomy_admin() { return $output; } else { - taxonomy_save_term($edit); - drupal_goto('admin/taxonomy'); + taxonomy_term_validate($edit); + if (!form_get_errors()) { + taxonomy_save_term($edit); + drupal_goto('admin/taxonomy'); + } + else { + $output = taxonomy_form_term($edit); + return $output; + } } } @@ -1156,35 +1200,51 @@ function taxonomy_admin() { 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)))); - break; + if (arg(3) == 'vocabulary') { + 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; - } - } - drupal_goto('admin/taxonomy'); + else { + 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'); + default: $output = taxonomy_overview(); }