function rules_ui_import_form_validate

Validation callback for the import form.

File

ui/ui.forms.inc, line 576

Code

function rules_ui_import_form_validate($form, &$form_state) {
    if ($rules_config = rules_import($form_state['values']['import'], $error_msg)) {
        // Store the successfully imported entity in $form_state.
        $form_state['rules_config'] = $rules_config;
        // Check for existing entities with the same identifier.
        if ($existing_config = rules_config_load($rules_config->name)) {
            // Don't import and overwrite the existing configuration unless the user
            // has checked the 'overwrite' box.
            if (!$form_state['values']['overwrite']) {
                $vars = array(
                    '@entity' => t('Rules configuration'),
                    '%label' => $rules_config->label(),
                );
                form_set_error('import', t('Import of @entity %label failed, a @entity with the same machine name already exists. Check the overwrite option to replace it.', $vars));
            }
            // Don't import if the existing configuration has the status ENTITY_FIXED
            // because that means the configuration can't be modified.
            if ($existing_config->status == ENTITY_FIXED) {
                $vars = array(
                    '@entity' => t('Rules configuration'),
                    '%label' => $rules_config->label(),
                );
                form_set_error('import', t("Import of @entity %label failed, a @entity with the same machine name already exists and is marked as ENTITY_FIXED meaning it can't be mofified.", $vars));
            }
        }
        try {
            $rules_config->integrityCheck();
        } catch (RulesIntegrityException $e) {
            form_set_error('import', t('Integrity check for the imported configuration failed. Error message: %message.', array(
                '%message' => $e->getMessage(),
            )));
        }
        if (!user_access('bypass rules access') && !$rules_config->access()) {
            form_set_error('import', t('You have insufficient access permissions for importing this Rules configuration.'));
        }
    }
    else {
        form_set_error('import', t('Import failed.'));
        if ($error_msg) {
            drupal_set_message($error_msg, 'error');
        }
    }
}