[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/rules/rules_admin/ -> rules_admin.export.inc (source)

   1  <?php
   2  // $Id: rules_admin.export.inc,v 1.1.2.10 2010/11/25 11:14:52 fago Exp $
   3  
   4  
   5  /**
   6   * @file Rules Import/Export Admin UI
   7   */
   8  
   9  rules_include('rules_admin');
  10  module_load_include('inc', 'rules', 'rules.export');
  11  
  12  
  13  /**
  14   * Imports a configuration of rules, rule sets, ..
  15   */
  16  function rules_admin_form_import() {
  17    $form = array();
  18    $form['import'] = array(
  19      '#type' => 'textarea',
  20      '#title' => t('Configuration to import'),
  21      '#description' => t('Just paste your exported configuration here.'),
  22      '#rows' => 15,
  23      '#required' => TRUE,
  24    );
  25    $form['button'] = array('#type' => 'submit', '#weight' => 10, '#value' => t('Import'));
  26    return $form;
  27  }
  28  
  29  function rules_admin_form_import_submit($form, $form_state) {
  30    @eval('$import = '. $form_state['values']['import'] .';');
  31    if (isset($import) && is_array($import) && count($import)) {
  32      // Check whether it are workflow-ng rules
  33      if (rules_admin_import_workflow_ng($import)) {
  34        return;
  35      }
  36  
  37      foreach ($import as $item_type => $elements) {
  38        foreach ($elements as $name => $element) {
  39          //let the item type alter the data before it's saved
  40          rules_item_type_invoke($item_type, 'import', array(&$name, &$element));
  41          rules_item_save($item_type, $name, $element);
  42          drupal_set_message(t('Imported %label.', array('%label' => rules_get_element_label($element))));
  43        }
  44      }
  45      rules_clear_cache();
  46    }
  47    else {
  48      drupal_set_message(t('Import failed.'), 'error');
  49    }
  50  }
  51  
  52  /**
  53   * Item type callback: Customize to be imported rules
  54   */
  55  function rules_item_rule_import(&$name, &$rule) {
  56    $rules = rules_get_configured_items('rules');
  57  
  58    if (!isset($rule['#status']) || $rule['#status'] == 'default') {
  59      if (!isset($rules[$name])) {
  60        // This default rule doesn't exist on this system, so we make it custom
  61        $rule['#status'] = 'custom';
  62      }
  63    }
  64    if (isset($rule['#status']) && $rule['#status'] == 'custom' && (!isset($rules[$name]) || $rules[$name]['#label'] != $rule['#label'] || $rules[$name]['#set'] != $rule['#set'])) {
  65      $rule['#status'] = 'custom';
  66    }
  67    rules_import_hook($rule);
  68  }
  69  
  70  /**
  71   * Item type callback: Customize to be imported rule sets
  72   */
  73  function rules_item_rule_set_import(&$name, &$rule_set) {
  74  
  75    if (!isset($rule_set['status']) || $rule_set['status'] == 'default') {
  76      $sets = rules_get_configured_items('rule_sets');
  77  
  78      if (!isset($sets[$name])) {
  79        // This default rule set doesn't exist on this system, so we make it custom
  80        $rule_set['status'] = 'custom';
  81      }
  82    }
  83  }
  84  
  85  /**
  86   * Tries to import workflow-ng rules.
  87   *
  88   * @return TRUE, only if workflow-ng rules are detected.
  89   */
  90  function rules_admin_import_workflow_ng($import) {
  91    foreach ($import as $name => $cfg) {
  92      if (count(element_children($cfg)) == count($cfg)) {
  93        // This is no workflow-ng rule, so exit
  94        return FALSE;
  95      }
  96      rules_include('rules');
  97  
  98      // Load the install and form include files, as there modules
  99      // may provide upgrade information.
 100      module_load_all_includes('install');
 101      rules_include('rules_forms');
 102  
 103      $rule = rules_import_workflow_ng_rule($name, $cfg);
 104      if ($rule) {
 105        // Handle rule format upgrades as the workflow-ng import just returns rules of the initial format
 106        $rule = rules_rule_format_upgrade($rule);
 107  
 108        rules_item_save('rules', $name, $rule);
 109        drupal_set_message(t('Successfully imported the workflow-ng rule %label.', array('%label' => $rule['#label'])));
 110      }
 111      else {
 112        drupal_set_message(t('Failed importing the workflow-ng rule %label.', array('%label' => $rule['#label'])), 'error');
 113      }
 114    }
 115    rules_clear_cache();
 116    return TRUE;
 117  }
 118  
 119  /**
 120   * Exports one or more configurations
 121   */
 122  function rules_admin_form_export($form_state) {
 123    $form = array();
 124    if (!isset($form_state['export'])) {
 125      $form['export'] = array('#tree' => TRUE);
 126      foreach (rules_get_items() as $name => $info) {
 127        $items = rules_get_configured_items($name);
 128        if ($name === 'rules') {
 129          // Filter out rules within rule sets, we only want event-triggered,
 130          // standalone rules.
 131          $items = array_filter($items, 'rules_admin_is_event_rule');
 132          $info['label'] = t('Triggered rules');
 133        }
 134        $items = rules_admin_get_grouped_labels($items);
 135        if (count($items)) {
 136          $form['export'][$name] = array(
 137            '#type' => 'select',
 138            '#title' => t('Select the %label to export', array('%label' => $info['label'])),
 139            '#options' => array(0 => '-') + $items,
 140            '#multiple' => TRUE,
 141            '#default_value' => 0,
 142          );
 143        }
 144        else {
 145          $msg = t('There are no %label to be exported.', array('%label' => $info['label']));
 146          $form['export'][$name] = array('#value' => '<p>'. $msg .'</p>');
 147        }
 148      }
 149      // Add the possibility to export by category
 150      if ($tags = rules_admin_get_categories('rules') + rules_admin_get_categories('rule_sets')) {
 151        $form['export_by_tag'] = array(
 152          '#type' => 'select',
 153          '#title' => t('Export by category'),
 154          '#options' => array(0 => '-') + $tags,
 155          '#multiple' => TRUE,
 156          '#default_value' => 0,
 157        );
 158      }
 159      $form['button'] = array('#type' => 'submit', '#weight' => 10, '#value' => t('Export'));
 160    }
 161    else {
 162      //show a textarea containg the exported configs
 163      $form['result'] = array(
 164        '#type' => 'textarea',
 165        '#title' => t('Exported rule configurations'),
 166        '#description' => t('Copy these data and paste them into the import page, to import.'),
 167        '#rows' => 30,
 168        '#default_value' => var_export($form_state['export'], TRUE),
 169      );
 170    }
 171    return $form;
 172  }
 173  
 174  function rules_admin_form_export_submit($form, &$form_state) {
 175    $export = array();
 176    foreach (array_filter($form_state['values']['export']) as $item_type => $item_names) {
 177      $items = rules_get_configured_items($item_type);
 178      $export[$item_type] = array_intersect_key($items, array_filter($item_names));
 179    }
 180    if (isset($form_state['values']['export_by_tag']) && $tags_to_export = array_filter($form_state['values']['export_by_tag'])) {
 181      rules_admin_export_by_category($export, $tags_to_export);
 182    }
 183    if ($export = array_filter($export)) {
 184      $form_state['export'] = rules_export_items($export);
 185    }
 186    else {
 187      drupal_set_message(t('Please select the items to export.'), 'error');
 188    }
 189    $form_state['rebuild'] = TRUE;
 190  }
 191  
 192  /**
 193   * Adds in the rule items for the given tags.
 194   */
 195  function rules_admin_export_by_category(&$export, $tags_to_export = array()) {
 196    // Search all items for the given tag.
 197    foreach (rules_get_items() as $item_type => $info) {
 198      $items = array_filter(rules_get_configured_items($item_type), 'rules_admin_element_filter');
 199      foreach ($items as $name => $item) {
 200        $categories = isset($item['categories']) ? $item['categories'] : (isset($item['#categories']) ? $item['#categories'] : array());
 201        if (array_intersect($tags_to_export, $categories)) {
 202          $export[$item_type][$name] = $item;
 203        }
 204      }
 205    }
 206  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7