Projet

Général

Profil

Paste
Télécharger (6,29 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date_ical / includes / DateiCalFeedsParser.inc @ 74f6bef0

1
<?php
2
/**
3
 * @file
4
 * DateiCalFeedsParser is Date iCal's Feeds parser plugin.
5
 */
6

    
7
class DateiCalFeedsParser extends FeedsParser {
8
  
9
  /**
10
   * Implements FeedsParser::getMappingSources().
11
   */
12
  public function getMappingSources() {
13
    return parent::getMappingSources() + self::getiCalMappingSources();
14
  }
15
  
16
  /**
17
   * Implements FeedsParser::parse().
18
   */
19
  public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
20
    $library = libraries_load('iCalcreator');
21
    if (!$library['loaded']) {
22
      throw new DateIcalException(t('Unable to load the iCalcreator library. Please ensure that it is properly installed.'));
23
    }
24
    $state = $source->state(FEEDS_PARSE);
25
    
26
    // Read the iCal feed into memory.
27
    $ical_feed_contents = $fetcher_result->getRaw();
28
    
29
    // Parse the feed into an iCalcreator vcalendar object.
30
    $calendar = new vcalendar();
31
    if ($calendar->parse($ical_feed_contents) === FALSE) {
32
      $plugin = $source->importer->config['fetcher']['plugin_key'];
33
      $url = $source->config[$plugin]['source'];
34
      throw new DateIcalException(t('Parsing the data from %url failed. Please ensure that this URL leads to a valid iCal feed.', array('%url' => $url)));
35
    }
36
    
37
    // Allow modules to alter the vcalendar object before we interpret it.
38
    $context = array(
39
      'source' => $source,
40
      'fetcher_result' => $fetcher_result,
41
    );
42
    drupal_alter('date_ical_import_vcalendar', $calendar, $context);
43
    
44
    // We've got a vcalendar object created from the feed data. Now we need to
45
    // convert that vcalendar into an array of Feeds-compatible data arrays.
46
    // ParserVcalendar->parse() does that.
47
    require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_ical') . '/libraries/ParserVcalendar.inc';
48
    $parser = new ParserVcalendar($calendar, $source, $fetcher_result, $source->getConfigFor($this));
49
    
50
    // Using the stored progress pointer (or 0 if it's not set),
51
    // determine which section of the feed to parse, then parse it.
52
    $offset = isset($state->pointer) ? $state->pointer : 0;
53
    $limit = $source->importer->getLimit();
54
    $rows = $parser->parse($offset, $limit);
55
    
56
    // Report progress.
57
    $state->total = $parser->getTotalComponents();
58
    // We need to add 1 to the index of the last parsed componenent so that
59
    // the subsequent batch starts on the first unparsed component.
60
    $state->pointer = $parser->getLastComponentParsed() + 1;
61
    $state->progress($state->total, $state->pointer);
62
    
63
    return new FeedsParserResult($rows);
64
  }
65
  
66
  /**
67
   * Defines the default configuration settings for an actual import.
68
   */
69
  public function sourceDefaults() {
70
    return array(
71
      'indefinite_count' => $this->config['indefinite_count'],
72
    );
73
  }
74
  
75
  /**
76
   * Defines the default settings shown on the configuration form.
77
   */
78
  public function configDefaults() {
79
    return array(
80
      'indefinite_count' => '52',
81
    );
82
  }
83
  
84
  /**
85
   * Builds the configuration form.
86
   */
87
  public function configForm(&$form_state) {
88
    $form = array();
89
    $form['indefinite_count'] = array(
90
      '#title' => t('Indefinite COUNT'),
91
      '#type' => 'select',
92
      '#options' => array(
93
        '31' => '31',
94
        '52' => '52',
95
        '90' => '90',
96
        '365' => '365',
97
      ),
98
      '#description' => t('Indefinitely repeating events are not supported. The repeat count will instead be set to this number.'),
99
      '#default_value' => $this->config['indefinite_count'],
100
    );
101
    return $form;
102
  }
103
  
104
  /**
105
   * Creates the list of mapping sources offered by DateiCalFeedsParser.
106
   */
107
  public static function getiCalMappingSources() {
108
    $sources = array();
109
    $sources['SUMMARY'] = array(
110
      'name' => t('Summary/Title'),
111
      'description' => t('The SUMMARY property. A short summary (usually the title) of the event.'),
112
      'date_ical_parse_handler' => 'parseTextProperty',
113
    );
114
    $sources['DESCRIPTION'] = array(
115
      'name' => t('Description'),
116
      'description' => t('The DESCRIPTION property. A more complete description of the event than what is provided by the Summary.'),
117
      'date_ical_parse_handler' => 'parseTextProperty',
118
    );
119
    $sources['DTSTART'] = array(
120
      'name' => t('Date: Start'),
121
      'description' => t('The DTSTART property. The start time of each event in the feed.'),
122
      'date_ical_parse_handler' => 'parseDateTimeProperty',
123
    );
124
    $sources['DTEND'] = array(
125
      'name' => t('Date: End'),
126
      'description' => t('THE DTEND or DURATION property. The end time (or duration) of each event in the feed.'),
127
      'date_ical_parse_handler' => 'parseDateTimeProperty',
128
    );
129
    $sources['RRULE'] = array(
130
      'name' => t('Repeat Rule'),
131
      'description' => t('The RRULE property. Describes when and how often this event should repeat.
132
        The date field for the target node must be configured to support repeating dates, using the Date Repeat Field module (a submodule of Date).'),
133
      'date_ical_parse_handler' => 'parseRepeatProperty',
134
    );
135
    $sources['UID'] = array(
136
      'name' => 'UID',
137
      'description' => t('The UID property. Each event must have a UID if you wish for the import process to be able to update previously-imported nodes.
138
        If used, this field MUST be set to Unique.'),
139
      'date_ical_parse_handler' => 'parseTextProperty',
140
    );
141
    $sources['URL'] = array(
142
      'name' => 'URL',
143
      'description' => t('The URL property. Some feeds specify a URL for the event using this property.'),
144
      'date_ical_parse_handler' => 'parseTextProperty',
145
    );
146
    $sources['LOCATION'] = array(
147
      'name' => t('Location'),
148
      'description' => t('The LOCATION property. Can be mapped to a text field, or the title of a referenced node.'),
149
      'date_ical_parse_handler' => 'parseTextProperty',
150
    );
151
    $sources['LOCATION:ALTREP'] = array(
152
      'name' => t('Location: ALTREP'),
153
      'description' => t('The ALTREP value of the LOCATION property. Additional location information, usually a URL to a page with more info.'),
154
      'date_ical_parse_handler' => 'parsePropertyParameter',
155
    );
156
    $sources['CATEGORIES'] = array(
157
      'name' => t('Categories'),
158
      'description' => t('The CATEGORIES property. Catagories that describe the event, which can be imported into taxonomy terms.'),
159
      'date_ical_parse_handler' => 'parseMultivalueProperty',
160
    );
161
    return $sources;
162
  }
163
}