Projet

Général

Profil

Paste
Télécharger (9,03 ko) Statistiques
| Branche: | Révision:

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

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
    // Total hack to get around iCalcreator's mistreatment of UID "0".
38
    if (empty($calendar->components[0]->uid) || empty($calendar->components[0]->uid['value'])) {
39
      $calendar->components[0]->uid = array('value' => 'zero', 'params' => NULL);
40
    }
41
    
42
    // Allow modules to alter the vcalendar object before we interpret it.
43
    $context = array(
44
      'source' => $source,
45
      'fetcher_result' => $fetcher_result,
46
    );
47
    drupal_alter('date_ical_import_vcalendar', $calendar, $context);
48
    
49
    // We've got a vcalendar object created from the feed data. Now we need to
50
    // convert that vcalendar into an array of Feeds-compatible data arrays.
51
    // ParserVcalendar->parse() does that.
52
    require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_ical') . '/libraries/ParserVcalendar.inc';
53
    $parser = new ParserVcalendar($calendar, $source, $fetcher_result, $source->getConfigFor($this));
54
    
55
    // Using the stored progress pointer (or 0 if it's not set),
56
    // determine which section of the feed to parse, then parse it.
57
    $offset = isset($state->pointer) ? $state->pointer : 0;
58
    $limit = $source->importer->getLimit();
59
    $rows = $parser->parse($offset, $limit);
60
    
61
    // Report progress.
62
    $state->total = $parser->getTotalComponents();
63
    // We need to add 1 to the index of the last parsed componenent so that
64
    // the subsequent batch starts on the first unparsed component.
65
    $state->pointer = $parser->getLastComponentParsed() + 1;
66
    $state->progress($state->total, $state->pointer);
67
    
68
    return new FeedsParserResult($rows);
69
  }
70
  
71
  /**
72
   * Defines the default configuration settings for an actual import.
73
   */
74
  public function sourceDefaults() {
75
    return array(
76
      'indefinite_count' => $this->config['indefinite_count'],
77
      'until_not_utc' => $this->config['until_not_utc'],
78
      'skip_days' => $this->config['skip_days'],
79
    );
80
  }
81
  
82
  /**
83
   * Defines the default settings shown on the configuration form.
84
   */
85
  public function configDefaults() {
86
    return array(
87
      'indefinite_count' => '52',
88
      'indefinite_message_display' => TRUE,
89
      'until_not_utc' => FALSE,
90
      'skip_days' => NULL,
91
    );
92
  }
93
  
94
  /**
95
   * Builds the configuration form.
96
   */
97
  public function configForm(&$form_state) {
98
    $form = array();
99
    $form['indefinite_count'] = array(
100
      '#title' => t('Indefinite COUNT'),
101
      '#type' => 'select',
102
      '#options' => array(
103
        '31' => '31',
104
        '52' => '52',
105
        '90' => '90',
106
        '365' => '365',
107
      ),
108
      '#description' => t('Indefinitely repeating events are not supported. The repeat count will instead be set to this number.'),
109
      '#default_value' => $this->config['indefinite_count'],
110
    );
111
    $form['indefinite_message_display'] = array(
112
      '#title' => t('Display message when RRULE is missing COUNT'),
113
      '#type' => 'checkbox',
114
      '#default_value' => $this->config['indefinite_message_display'],
115
      '#description' => t('Display a message when an indefinitely repeating rule is adjusted by the "Indefinite COUNT" setting above.'),
116
    );
117
    $form['until_not_utc'] = array(
118
      '#title' => t('RRULE UNTILs are not in UTC'),
119
      '#type' => 'checkbox',
120
      '#description' => t('Enable this setting if your reccuring events are not repeating the correct number of times. ' .
121
          'The iCal spec requires that the UNTIL value in an RRULE almost always be specified in UTC, but some iCal feed creators fail to follow that rule ' .
122
          '(the UNTIL values in those feeds\' RRULEs don\'t end is "Z"). This causes the UNTIL value to be off by several hours, ' .
123
          'which can cause the repeat calculator to miss or add repeats.'),
124
      '#default_value' => $this->config['until_not_utc'],
125
    );
126
    $form['skip_days'] = array(
127
      '#title' => t('Skip events more than X days old'),
128
      '#type' => 'textfield',
129
      '#size' => 5,
130
      '#description' => t('Set this value to any positive integer (or 0) to skip events which ended more than that many days before the import. ' .
131
        'Leave it blank to import all events.'),
132
      '#default_value' => $this->config['skip_days'],
133
    );
134
    return $form;
135
  }
136
  
137
  /**
138
   * Validation handler for configForm.
139
   */
140
  public function configFormValidate(&$source_config) {
141
    if (!preg_match('/^\d+$/', $source_config['skip_days']) && $source_config['skip_days'] !== '') {
142
      form_set_error('skip_days', 'You must enter a positive integer.');
143
    }
144
    if ($source_config['skip_days'] === '') {
145
      $source_config['skip_days'] = NULL;
146
    }
147
  }
148
  
149
  /**
150
   * Creates the list of mapping sources offered by DateiCalFeedsParser.
151
   */
152
  public static function getiCalMappingSources() {
153
    $sources = array();
154
    $sources['SUMMARY'] = array(
155
      'name' => t('Summary/Title'),
156
      'description' => t('The SUMMARY property. A short summary (usually the title) of the event.
157
        A title is required for every node, so you need to include this source and have it mapped to the node title, except under unusual circumstances.'
158
      ),
159
      'date_ical_parse_handler' => 'parseTextProperty',
160
    );
161
    $sources['COMMENT'] = array(
162
      'name' => t('Comment'),
163
      'description' => t('The COMMENT property. A text comment is allowed on most components.'),
164
      'date_ical_parse_handler' => 'parseTextProperty',
165
    );
166
    $sources['DESCRIPTION'] = array(
167
      'name' => t('Description'),
168
      'description' => t('The DESCRIPTION property. A more complete description of the event than what is provided by the Summary.'),
169
      'date_ical_parse_handler' => 'parseTextProperty',
170
    );
171
    $sources['DTSTART'] = array(
172
      'name' => t('Date: Start'),
173
      'description' => t('The DTSTART property. The start time of each event in the feed.'),
174
      'date_ical_parse_handler' => 'parseDateTimeProperty',
175
    );
176
    $sources['DTEND'] = array(
177
      'name' => t('Date: End'),
178
      'description' => t('THE DTEND or DURATION property. The end time (or duration) of each event in the feed.'),
179
      'date_ical_parse_handler' => 'parseDateTimeProperty',
180
    );
181
    $sources['RRULE'] = array(
182
      'name' => t('Date: Repeat Rule'),
183
      'description' => t('The RRULE property. Describes when and how often this event should repeat.
184
        The date field for the target node must be configured to support repeating dates, using the Date Repeat Field module (a submodule of Date).'),
185
      'date_ical_parse_handler' => 'parseRepeatProperty',
186
    );
187
    $sources['UID'] = array(
188
      'name' => 'UID',
189
      '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.
190
        If used, this field MUST be set to Unique.'),
191
      'date_ical_parse_handler' => 'parseTextProperty',
192
    );
193
    $sources['URL'] = array(
194
      'name' => 'URL',
195
      'description' => t('The URL property. Some feeds specify a URL for the event using this property.'),
196
      'date_ical_parse_handler' => 'parseTextProperty',
197
    );
198
    $sources['LOCATION'] = array(
199
      'name' => t('Location'),
200
      'description' => t('The LOCATION property. Can be mapped to a text field, or the title of a referenced node.'),
201
      'date_ical_parse_handler' => 'parseTextProperty',
202
    );
203
    $sources['LOCATION:ALTREP'] = array(
204
      'name' => t('Location: ALTREP'),
205
      'description' => t('The ALTREP value of the LOCATION property. Additional location information, usually a URL to a page with more info.'),
206
      'date_ical_parse_handler' => 'parsePropertyParameter',
207
    );
208
    $sources['CATEGORIES'] = array(
209
      'name' => t('Categories'),
210
      'description' => t('The CATEGORIES property. Catagories that describe the event, which can be imported into taxonomy terms.'),
211
      'date_ical_parse_handler' => 'parseMultivalueProperty',
212
    );
213
    
214
    // Allow other modules to add custom source fields.
215
    drupal_alter('date_ical_mapping_sources', $sources);
216
    
217
    return $sources;
218
  }
219
}