feeds.rules.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration.
  5. */
  6. /**
  7. * Implements hook_rules_event_info().
  8. */
  9. function feeds_rules_event_info() {
  10. // General events definitions.
  11. $info = array(
  12. 'feeds_before_import' => array(
  13. 'label' => t('Before importing feed'),
  14. 'group' => t('Feeds'),
  15. 'variables' => array('source' => array('type' => 'feeds_source', 'label' => 'Feeds source')),
  16. ),
  17. 'feeds_after_import' => array(
  18. 'label' => t('After importing feed'),
  19. 'group' => t('Feeds'),
  20. 'variables' => array('source' => array('type' => 'feeds_source', 'label' => 'Feeds source')),
  21. ),
  22. );
  23. // Per importer events definitions.
  24. $entity_info = entity_get_info();
  25. foreach (feeds_importer_load_all() as $importer) {
  26. $config = $importer->getConfig();
  27. $processor = feeds_plugin($config['processor']['plugin_key'], $importer->id);
  28. // It's possible to get FeedsMissingPlugin here which will break things
  29. // since it doesn't implement FeedsProcessor::entityType().
  30. if (!$processor instanceof FeedsProcessor) {
  31. continue;
  32. }
  33. $entity_type = $processor->entityType();
  34. $label = isset($entity_info[$entity_type]['label']) ? $entity_info[$entity_type]['label'] : $entity_type;
  35. $info['feeds_import_'. $importer->id] = array(
  36. 'label' => t('Before saving an item imported via @name.', array('@name' => $importer->config['name'])),
  37. 'group' => t('Feeds'),
  38. 'variables' => array(
  39. $entity_type => array(
  40. 'label' => t('Imported @label', array('@label' => $label)),
  41. 'type' => $entity_type,
  42. // Saving is handled by feeds anyway (unless the skip action is used).
  43. 'skip save' => TRUE,
  44. ),
  45. ),
  46. 'access callback' => 'feeds_rules_access_callback',
  47. );
  48. // Add bundle information if the node processor is used.
  49. if ($processor instanceof FeedsNodeProcessor) {
  50. $info['feeds_import_'. $importer->id]['variables'][$entity_type]['bundle'] = $processor->bundle();
  51. }
  52. }
  53. return $info;
  54. }
  55. /**
  56. * Implements of hook_rules_action_info().
  57. */
  58. function feeds_rules_action_info() {
  59. return array(
  60. 'feeds_skip_item' => array(
  61. 'base' => 'feeds_action_skip_item',
  62. 'label' => t('Skip import of feeds item'),
  63. 'group' => t('Feeds'),
  64. 'parameter' => array(
  65. 'entity' => array('type' => 'entity', 'label' => t('The feeds import item to be marked as skipped')),
  66. ),
  67. 'access callback' => 'feeds_rules_access_callback',
  68. ),
  69. );
  70. }
  71. /**
  72. * Implements hook_rules_data_info().
  73. */
  74. function feeds_rules_data_info() {
  75. return array(
  76. 'feeds_source' => array(
  77. 'label' => t('Feeds source'),
  78. 'group' => t('Feeds'),
  79. 'wrap' => TRUE,
  80. 'property info' => array(
  81. 'id' => array(
  82. 'label' => t('ID'),
  83. 'type' => 'text',
  84. 'description' => t("The machine readable name of the source importer."),
  85. ),
  86. 'imported' => array(
  87. 'label' => t('Date imported'),
  88. 'type' => 'date',
  89. 'description' => t("The date the source was last imported."),
  90. ),
  91. // @TODO: fetcher, parser, state ...
  92. ),
  93. ),
  94. );
  95. }
  96. /**
  97. * Mark feeds import item as skipped.
  98. */
  99. function feeds_action_skip_item($entity_wrapper) {
  100. $entity = $entity_wrapper->value();
  101. if (isset($entity->feeds_item)) {
  102. $entity->feeds_item->skip = TRUE;
  103. }
  104. }
  105. /**
  106. * Help callback for the skip action.
  107. */
  108. function feeds_action_skip_item_help() {
  109. 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.");
  110. }
  111. /**
  112. * Access callback for the feeds rules integration.
  113. */
  114. function feeds_rules_access_callback() {
  115. return user_access('administer feeds');
  116. }