Projet

Général

Profil

Paste
Télécharger (5,51 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / feeds.rules.inc @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules integration.
6
 */
7

    
8
/**
9
 * Implements hook_rules_event_info().
10
 */
11
function feeds_rules_event_info() {
12
  // General events definitions.
13
  $info = array(
14
    'feeds_before_import' => array(
15
      'label' => t('Before importing feed'),
16
      'group' => t('Feeds'),
17
      'variables' => array('source' => array('type' => 'feeds_source', 'label' => 'Feeds source')),
18
    ),
19
    'feeds_after_import' => array(
20
      'label' => t('After importing feed'),
21
      'group' => t('Feeds'),
22
      'variables' => array('source' => array('type' => 'feeds_source', 'label' => 'Feeds source')),
23
    ),
24
  );
25
  // Per importer events definitions.
26
  $entity_info = entity_get_info();
27
  foreach (feeds_importer_load_all() as $importer) {
28
    $config = $importer->getConfig();
29
    $processor = feeds_plugin($config['processor']['plugin_key'], $importer->id);
30

    
31
    // It's possible to get FeedsMissingPlugin here which will break things
32
    // since it doesn't implement FeedsProcessor::entityType().
33
    if (!$processor instanceof FeedsProcessor) {
34
      continue;
35
    }
36

    
37
    $entity_type = $processor->entityType();
38
    $label = isset($entity_info[$entity_type]['label']) ? $entity_info[$entity_type]['label'] : $entity_type;
39

    
40
    $info['feeds_import_' . $importer->id] = array(
41
      'label' => t('Before saving an item imported via @name.', array('@name' => $importer->config['name'])),
42
      'group' => t('Feeds'),
43
      'variables' => array(
44
        $entity_type => array(
45
          'label' => t('Imported @label', array('@label' => $label)),
46
          'type' => $entity_type,
47
          'bundle' => $processor->bundle(),
48
          // Saving is handled by feeds anyway (unless the skip action is used).
49
          'skip save' => TRUE,
50
        ),
51
      ),
52
      'access callback' => 'feeds_rules_access_callback',
53
    );
54
  }
55
  return $info;
56
}
57

    
58
/**
59
 * Implements hook_rules_action_info().
60
 */
61
function feeds_rules_action_info() {
62
  return array(
63
    'feeds_import_feed' => array(
64
      'base' => 'feeds_action_import_feed',
65
      'label' => t('Execute feeds importer'),
66
      'parameter' => array(
67
        'importer' => array(
68
          'type' => 'text',
69
          'label' => t('Feeds importer'),
70
          'options list' => 'feeds_importer_list',
71
          'default mode' => 'input',
72
        ),
73
        'feed_nid' => array(
74
          'type' => 'node',
75
          'label' => t('Feed node'),
76
          'default mode' => 'input',
77
          'description' => t("The feed node, if the importer is attached to a content type. Put in '0' if the importer is not attached to a content type."),
78
        ),
79
      ),
80
      'group' => t('Feeds'),
81
      'access callback' => 'feeds_rules_access_callback',
82
    ),
83
    'feeds_skip_item' => array(
84
      'base' => 'feeds_action_skip_item',
85
      'label' => t('Skip import of feeds item'),
86
      'group' => t('Feeds'),
87
      'parameter' => array(
88
        'entity' => array('type' => 'entity', 'label' => t('The feeds import item to be marked as skipped')),
89
      ),
90
      'access callback' => 'feeds_rules_access_callback',
91
    ),
92
  );
93
}
94

    
95
/**
96
 * Implements hook_rules_data_info().
97
 */
98
function feeds_rules_data_info() {
99
  return array(
100
    'feeds_source' => array(
101
      'label' => t('Feeds source'),
102
      'group' => t('Feeds'),
103
      'wrap' => TRUE,
104
      'property info' => array(
105
        'id' => array(
106
          'label' => t('ID'),
107
          'type' => 'text',
108
          'description' => t("The machine readable name of the source importer."),
109
        ),
110
        'imported' => array(
111
          'label' => t('Date imported'),
112
          'type' => 'date',
113
          'description' => t("The date the source was last imported."),
114
        ),
115
        // @TODO: fetcher, parser, state ...
116
      ),
117
    ),
118
  );
119
}
120

    
121
/**
122
 * Rules action callback for "feeds_import_feed" action.
123
 *
124
 * @param string $importer_id
125
 *   ID of the importer.
126
 * @param object|null $feed_node
127
 *   The feed node, if found. Null otherwise.
128
 * @param array $params
129
 *   The raw parameters.
130
 */
131
function feeds_action_import_feed($importer_id, $feed_node, array $params) {
132
  $source = feeds_source($importer_id, $params['feed_nid']);
133
  try {
134
    $source->existing()->startImport();
135

    
136
    // Execute batch, if there is any. Set 'progressive' to false to prevent
137
    // batch from triggering a drupal_goto().
138
    $batch =& batch_get();
139
    if (!empty($batch)) {
140
      $batch['progressive'] = FALSE;
141
      batch_process();
142
    }
143
  }
144
  catch (FeedsNotExistingException $e) {
145
    // Ignore this kind of exception.
146
  }
147
  catch (Exception $e) {
148
    $source->log('import', $e->getMessage(), array(), WATCHDOG_ERROR);
149
  }
150
}
151

    
152
/**
153
 * Mark feeds import item as skipped.
154
 */
155
function feeds_action_skip_item($entity_wrapper) {
156
  $entity = $entity_wrapper->value();
157
  if (isset($entity->feeds_item)) {
158
    $entity->feeds_item->skip = TRUE;
159
  }
160
}
161

    
162
/**
163
 * Help callback for the skip action.
164
 */
165
function feeds_action_skip_item_help() {
166
  return t("This action allows skipping certain feed items during feeds processing, i.e. before an imported item is saved. Once this action is used on a item, the changes to the entity of the feed item are not saved.");
167
}
168

    
169
/**
170
 * List callback for selecting a Feeds importer.
171
 */
172
function feeds_importer_list() {
173
  $list = array();
174
  ctools_include('export');
175
  $configs = ctools_export_load_object('feeds_importer', 'all');
176
  foreach ($configs as $id => $config) {
177
    if (empty($config->disabled)) {
178
      $list[$id] = $config->config['name'];
179
    }
180
  }
181
  return $list;
182
}
183

    
184
/**
185
 * Access callback for the feeds rules integration.
186
 */
187
function feeds_rules_access_callback() {
188
  return user_access('administer feeds');
189
}