public static function Rules::import in Hook Update Deploy Tools 8
Same name and namespace in other branches
- 7 src/Rules.php \HookUpdateDeployTools\Rules::import()
Imports rules using the rule_import module & template.
Parameters
array $rules: An array of hyphenated machine names of rules to be imported.
Throws
Overrides ImportInterface::import
File
- src/
Rules.php, line 17
Class
- Rules
- Public method for importing Rules.
Namespace
HookUpdateDeployToolsCode
public static function import($rules) {
$t = get_t();
$completed = array();
$rules = (array) $rules;
$total_requested = count($rules);
try {
self::canImport();
Check::canCall('rules_config_load');
Check::canCall('rules_import');
$rule_feature_path = HudtInternal::getStoragePath('rules');
foreach ($rules as $rid => $rule_file_prefix) {
$filename = HudtInternal::normalizeFileName($rule_file_prefix);
$rule_machine_name = HudtInternal::normalizeMachineName($rule_file_prefix);
// If the file is there, process it.
if (HudtInternal::canReadFile($filename, 'rules')) {
// Read the file.
$file_contents = HudtInternal::readFileToString($filename, 'rules');
$error_msg = '';
// Use the maching name to see if it exists.
$existing_rule = rules_config_load($rule_machine_name);
$imported_rule = rules_import($file_contents, $error_msg);
if (!empty($existing_rule)) {
$operation = $t('Overwrote');
$imported_rule->id = $existing_rule->id;
unset($imported_rule->is_new);
}
else {
$operation = $t('Created');
}
if ($imported_rule
->integrityCheck()) {
// Passed integrity check, save it.
$imported_rule
->save();
}
else {
// Failed integrity check.
$message = 'Rule @operation of @rule_machine_name - Failed integrity check. Not saved. Aborting update.';
$vars = array(
'@operation' => $operation,
'@rule_machine_name' => $rule_machine_name,
);
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
// Verify that the save happened by reloading the rule.
$saved_rule = rules_config_load($rule_machine_name);
if (!empty($imported_rule) && empty($error_msg) && !empty($saved_rule)) {
// Success.
$message = '@operation: @rule_machine_name - imported successfully.';
global $base_url;
$link = "{$base_url}/admin/config/workflow/rules/reaction/manage/{$rule_machine_name}";
$vars = array(
'@operation' => $operation,
'@rule_machine_name' => $rule_machine_name,
);
Message::make($message, $vars, WATCHDOG_INFO, 1, $link);
$completed[$rule_machine_name] = $t('Imported');
}
else {
// The rule import failed. Pass on the error message.
$vars = array(
'@error' => $error_msg,
'@rule_machine_name' => $rule_machine_name,
'@file_prefix' => $rule_file_prefix,
);
$message = "The requested rule import '@rule_machine_name' failed with the following error: '@error'. Adjust your @file_prefix-export.txt rule text file accordingly and re-run update.";
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
}
}
} catch (\Exception $e) {
$vars = array(
'!error' => method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage(),
);
if (!method_exists($e, 'logMessage')) {
// Not logged yet, so log it.
$message = 'Rule import denied because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
// Output a summary before shutting this down.
$done = HudtInternal::getSummary($completed, $total_requested, 'Imported');
Message::make($done, array(), FALSE, 1);
throw new HudtException('Caught Exception: Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
$done = HudtInternal::getSummary($completed, $total_requested, 'Imported');
return $done;
}