You are here

public function RulesEntityController::import in Rules 7.2

Overridden to work with Rules' custom export format.

Parameters

string $export: A serialized string in JSON format as produced by the RulesPlugin::export() method, or the PHP export as usual PHP array.

string $error_msg: The error message.

Overrides EntityAPIController::import

File

includes/rules.core.inc, line 113
Rules base classes and interfaces needed for any rule evaluation.

Class

RulesEntityController
Make sure loaded rule configs are instantiated right.

Code

public function import($export, &$error_msg = '') {
  $export = is_array($export) ? $export : drupal_json_decode($export);
  if (!is_array($export)) {
    $error_msg = t('Unable to parse the pasted export.');
    return FALSE;
  }

  // The key is the configuration name and the value the actual export.
  $name = key($export);
  $export = current($export);
  if (!isset($export['PLUGIN'])) {
    $error_msg = t('Export misses plugin information.');
    return FALSE;
  }

  // Create an empty configuration, re-set basic keys and import.
  $config = rules_plugin_factory($export['PLUGIN']);
  $config->name = $name;
  foreach (array(
    'label',
    'active',
    'weight',
    'tags',
    'access_exposed',
    'owner',
  ) as $key) {
    if (isset($export[strtoupper($key)])) {
      $config->{$key} = $export[strtoupper($key)];
    }
  }
  if (!empty($export['REQUIRES'])) {
    foreach ($export['REQUIRES'] as $module) {
      if (!module_exists($module)) {
        $error_msg = t('Missing the required module %module.', array(
          '%module' => $module,
        ));
        return FALSE;
      }
    }
    $config->dependencies = $export['REQUIRES'];
  }
  $config
    ->import($export);
  return $config;
}