Projet

Général

Profil

Paste
Télécharger (3,76 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / feeds.rules.inc @ 87dbc3bf

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
          // Saving is handled by feeds anyway (unless the skip action is used).
48
          'skip save' => TRUE,
49
        ),
50
      ),
51
      'access callback' => 'feeds_rules_access_callback',
52
    );
53
    // Add bundle information if the node processor is used.
54
    if ($processor instanceof FeedsNodeProcessor) {
55
      $info['feeds_import_'. $importer->id]['variables'][$entity_type]['bundle'] = $processor->bundle();
56
    }
57
  }
58
  return $info;
59
}
60

    
61
/**
62
 * Implements of hook_rules_action_info().
63
 */
64
function feeds_rules_action_info() {
65
  return array(
66
    'feeds_skip_item' => array(
67
      'base' => 'feeds_action_skip_item',
68
      'label' => t('Skip import of feeds item'),
69
      'group' => t('Feeds'),
70
      'parameter' => array(
71
        'entity' => array('type' => 'entity', 'label' => t('The feeds import item to be marked as skipped')),
72
      ),
73
      'access callback' => 'feeds_rules_access_callback',
74
    ),
75
  );
76
}
77

    
78
/**
79
 * Implements hook_rules_data_info().
80
 */
81
function feeds_rules_data_info() {
82
  return array(
83
    'feeds_source' => array(
84
      'label' => t('Feeds source'),
85
      'group' => t('Feeds'),
86
      'wrap' => TRUE,
87
      'property info' => array(
88
        'id' => array(
89
          'label' => t('ID'),
90
          'type' => 'text',
91
          'description' => t("The machine readable name of the source importer."),
92
        ),
93
        'imported' => array(
94
          'label' => t('Date imported'),
95
          'type' => 'date',
96
          'description' => t("The date the source was last imported."),
97
        ),
98
        // @TODO: fetcher, parser, state ...
99
      ),
100
    ),
101
  );
102
}
103

    
104
/**
105
 * Mark feeds import item as skipped.
106
 */
107
function feeds_action_skip_item($entity_wrapper) {
108
  $entity = $entity_wrapper->value();
109
  if (isset($entity->feeds_item)) {
110
    $entity->feeds_item->skip = TRUE;
111
  }
112
}
113

    
114
/**
115
 * Help callback for the skip action.
116
 */
117
function feeds_action_skip_item_help() {
118
  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.");
119
}
120

    
121
/**
122
 * Access callback for the feeds rules integration.
123
 */
124
function feeds_rules_access_callback() {
125
  return user_access('administer feeds');
126
}