View source
<?php
namespace HookUpdateDeployTools;
class Vocabularies {
public static function add($name, $machine_name, $description = '') {
try {
Check::canUse('taxonomy');
Check::canCall('taxonomy_vocabulary_machine_name_load');
Check::canCall('taxonomy_vocabulary_save');
Check::notEmpty('$vocabulary_name', $name);
Check::notEmpty('$machine_name', $machine_name);
$vars = array(
'!name' => $name,
'!machine_name' => $machine_name,
'!description' => $description,
);
$saved_status = FALSE;
$return_msg = '';
$vocabulary = taxonomy_vocabulary_machine_name_load($machine_name);
if ($vocabulary !== FALSE) {
$vars['@saved_text'] = "exists";
$msg = 'The Vocabulary !machine_name already exists. Skipping Vocabularies::Add.';
$return_msg .= Message::make($msg, $vars, WATCHDOG_INFO, 1);
}
else {
$new_vocab = (object) array(
'name' => $name,
'description' => $description,
'machine_name' => $machine_name,
);
$saved_status = taxonomy_vocabulary_save($new_vocab);
if ($saved_status === SAVED_NEW) {
$vars['@saved_text'] = "was created";
$vocabulary = taxonomy_vocabulary_machine_name_load($machine_name);
if ($vocabulary === FALSE) {
$message = 'Creating the Vocabulary !name did no go as expected. It does not exist.';
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
}
else {
$vars['!status'] = $saved_status;
$message = 'Creating the Vocabulary !name did no go as expected. Status:!status';
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
}
if ($name !== $vocabulary->name) {
$vars['!saved_name'] = $vocabulary->name;
$message = "The Vocabulary !machine_name @saved_text, but the requested name:'!name' does not match saved name:'!saved_name'.";
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
if ($description !== $vocabulary->description) {
$vars['!saved_desc'] = $vocabulary->description;
$message = "The Vocabulary !machine_name @saved_text, but requested description:'!description' does not match saved description:'!saved_desc'.";
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'Vocabularies::add !machine_name failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
if ($saved_status === SAVED_NEW) {
self::delete($machine_name);
$vars['!error'] .= " The Vocabulary {$machine_name} created by this attempt was deleted.";
}
throw new HudtException('Caught Exception: Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
$return_msg .= Message::make("The Vocabulary !name (!machine_name) @saved_text with Description: !description", $vars, WATCHDOG_INFO, 1);
return $return_msg;
}
public static function delete($machine_name) {
try {
Check::canUse('taxonomy');
Check::canCall('taxonomy_vocabulary_machine_name_load');
Check::canCall('taxonomy_vocabulary_delete');
Check::notEmpty('$machine_name', $machine_name);
$vars = array(
'!machine_name' => $machine_name,
);
$vocabulary = taxonomy_vocabulary_machine_name_load($machine_name);
if ($vocabulary === FALSE) {
$vars['@exists_text'] = "does not exist";
$vars['@action_taken'] = "so was not deleted. Skipping Vocabularies::delete";
}
else {
$vars['@exists_text'] = "exists";
$delete_status = taxonomy_vocabulary_delete($vocabulary->vid);
$vars['@deleted_status'] = $delete_status;
if ($delete_status === SAVED_DELETED) {
$vars['@action_taken'] = "was deleted";
$vocabulary = taxonomy_vocabulary_machine_name_load($machine_name);
if ($vocabulary !== FALSE) {
$message = "Deleting the Vocabulary '!machine_name' did no go as expected. It @action_taken but still @exists_text.";
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
}
else {
$message = "Deleting the Vocabulary '!machine_name' did not go as expected. Status:'@deleted_status'";
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
}
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'Vocabularies::delete !machine_name failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Caught Exception: Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
$return_msg = Message::make("Vocabulary '!machine_name' @exists_text @action_taken.", $vars, WATCHDOG_INFO, 1);
return $return_msg;
}
public static function loadByMachineName($vocabulary_machine_name, $strict = FALSE) {
Check::canUse('taxonomy');
Check::canCall('taxonomy_vocabulary_machine_name_load');
Check::notEmpty('vocabulary_machine_name', $vocabulary_machine_name);
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
if (!empty($vocabulary)) {
return $vocabulary;
}
else {
if ($strict) {
$message = "There is no Vocabulary with machine name '@!name'. It could not be loaded.";
$variables = array(
'@name' => $vocabulary_machine_name,
);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
return FALSE;
}
}
}
public static function loadByName($vocabulary_name, $strict = FALSE) {
Check::canUse('taxonomy');
Check::canCall('taxonomy_get_vocabularies');
$vocabularies = taxonomy_get_vocabularies(NULL);
foreach ($vocabularies as $vocabulary) {
if (!empty($vocabulary) && $vocabulary->name === $vocabulary_name) {
return $vocabulary;
}
}
if ($strict) {
$message = "There is no Vocabulary '@!name'. It could not be loaded.";
$variables = array(
'@name' => $vocabulary_name,
);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
return FALSE;
}
}
}