function taxonomy_patterns_validate in Patterns 7.2
Same name and namespace in other branches
- 7 patterns_components/components/taxonomy.inc \taxonomy_patterns_validate()
File
- patterns_components/
components/ taxonomy.inc, line 149 - Patterns component for taxonomy vocabularies and terms.
Code
function taxonomy_patterns_validate($action, $tag, &$data) {
$result = array();
$status = PATTERNS_SUCCESS;
$msg = '';
$vocabs = taxonomy_get_vocabularies();
// SYNTACTIC VALIDATION
if ($tag == 'term') {
if (!isset($data['vid']) && !empty($data['vocabulary_machine_name'])) {
foreach ($vocabs as $vid => $vocab) {
if (drupal_strtolower($vocab->name) == drupal_strtolower($data['vocabulary_machine_name'])) {
$data['vid'] = $vid;
break;
}
}
}
// we can't proceed without valid vocabulary ID
if (empty($data['vid'])) {
$status = PATTERNS_ERR;
$msg = t("Vocabulary %vocab doesn't exist.", array(
'%vocab' => $data['vocabulary_machine_name'],
));
}
}
else {
if ($tag == 'vocabulary') {
if ($action == PATTERNS_CREATE) {
$keys = patterns_utils_key_exists(array(
'name',
'machine_name',
), $data);
}
else {
$keys = patterns_utils_key_exists(array(
'machine_name',
'vid',
), $data);
// It is sufficient to have either vid or machine_name
if (!$keys['machine_name'] && $keys['vid']) {
$keys = array();
}
else {
if ($keys['machine_name'] && !$keys['vid']) {
$keys = array();
}
}
}
$msg = t("The following mandatory keys were missing for tag %tag: ", array(
'%tag' => $tag,
));
foreach ($keys as $key => $exist) {
if (!$exist) {
$status = PATTERNS_ERR;
$msg .= $key . ', ';
}
}
$msg = substr($msg, 0, -2);
}
else {
$status = PATTERNS_ERR;
$msg = t("Unknown tag: %tag.", array(
'%tag' => $tag,
));
}
}
if ($status == PATTERNS_ERR) {
return patterns_results($status, $msg);
}
// SEMANTIC VALIDATION
if ($tag == 'vocabulary') {
$voc_exists = _taxonomy_patterns_vocabulary_exists($data['machine_name'], $vocabs);
if ($action == PATTERNS_CREATE) {
if ($voc_exists) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The vocabulary %voc already exists in the system.', array(
'%voc' => $data['machine_name'],
)),
);
}
}
else {
if ($action == PATTERNS_MODIFY) {
if (!$voc_exists) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The vocabulary %voc was not found in the system, and cannot be modified.', array(
'%voc' => $data['machine_name'],
)),
);
}
}
else {
if ($action == PATTERNS_DELETE) {
if (!$voc_exists) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The vocabulary %voc was not found in the system, and cannot be deleted.', array(
'%voc' => $data['machine_name'],
)),
);
}
}
}
}
}
else {
if ($tag == 'term') {
$term_exists = FALSE;
if ($action == PATTERNS_CREATE) {
$terms = taxonomy_get_term_by_name($data['name']);
$term_exists = count($terms) == 1;
if ($term_exists) {
$status = PATTERNS_ERR;
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The term %term already exists in the system.', array(
'%term' => $data['name'],
)),
);
}
}
else {
if ($action == PATTERNS_MODIFY) {
$term_exists = taxonomy_term_load($data['tid']);
if (!$term_exists) {
$status = PATTERNS_ERR;
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The term %term was not found in the system, and cannot be modified.', array(
'%term' => $data['name'],
)),
);
}
}
else {
if ($action == PATTERNS_DELETE) {
$term_exists = taxonomy_term_load($data['tid']);
if (!$term_exists) {
$status = PATTERNS_ERR;
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The term %term was not found in the system, and cannot be deleted.', array(
'%term' => $data['name'],
)),
);
}
}
}
}
}
}
return patterns_results($status, $msg, $result);
}