Projet

Général

Profil

Paste
Télécharger (15,8 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Menu callbacks, form callbacks and helpers.
6
 */
7

    
8
/**
9
 * Render a page of available importers.
10
 */
11
function feeds_page() {
12
  $rows = array();
13
  if ($importers = feeds_importer_load_all()) {
14
    foreach ($importers as $importer) {
15
      if ($importer->disabled) {
16
        continue;
17
      }
18
      if (!(user_access('import ' . $importer->id . ' feeds') || user_access('administer feeds'))) {
19
        continue;
20
      }
21
      if (empty($importer->config['content_type'])) {
22
        $link = 'import/' . $importer->id;
23
        $title = $importer->config['name'];
24
      }
25
      elseif (node_access('create', $importer->config['content_type'])) {
26
        $link = 'node/add/' . str_replace('_', '-', $importer->config['content_type']);
27
        $title = node_type_get_name($importer->config['content_type']);
28
      }
29
      else {
30
        continue;
31
      }
32
      $rows[] = array(
33
        l($title, $link),
34
        check_plain($importer->config['description']),
35
      );
36
    }
37
  }
38
  if (empty($rows)) {
39
    // The feeds_ui module is enabled.
40
    if (module_exists('feeds_ui') && user_access('administer feeds')) {
41
      drupal_set_message(t('There are no importers, go to <a href="@importers">Feed importers</a> to create one or enable an existing one.', array('@importers' => url('admin/structure/feeds'))));
42
    }
43
    else {
44
      // The feeds_ui module is not enabled but the current user has access to
45
      // Modules to enable it.
46
      if (user_access('administer modules')) {
47
        drupal_set_message(t('The Feeds UI Admin module is not enabled and there are no importers, go to <a href="@modules">Modules</a> and enable Feeds Admin UI. Then go to <a href="@importers">Feed importers</a> to create one or enable an existing one.', array('@modules' => url('admin/modules'), '@importers' => url('admin/structure/feeds'))));
48
      }
49
      else {
50
        // The feeds_ui module is not enabled and the current user cannot
51
        // enable it.
52
        drupal_set_message(t("The Feeds UI Admin module is not enabled. Please contact the Administrator for your site and ask them to enable it."));
53
      }
54
    }
55
  }
56
  $header = array(
57
    t('Import'),
58
    t('Description'),
59
  );
60
  return theme('table', array('header' => $header, 'rows' => $rows));
61
}
62

    
63
/**
64
 * Render a feeds import form on import/[config] pages.
65
 */
66
function feeds_import_form(array $form, array &$form_state, FeedsImporter $importer) {
67
  $source = feeds_source($importer->id);
68

    
69
  $form['#importer_id'] = $importer->id;
70
  // @todo Move this into fetcher?
71
  $form['#attributes']['enctype'] = 'multipart/form-data';
72
  $form['source_status'] = array(
73
    '#type' => 'fieldset',
74
    '#title' => t('Status'),
75
    '#tree' => TRUE,
76
    '#value' => feeds_source_status($source),
77
  );
78

    
79
  $source_form = $source->configForm($form_state);
80
  if (!empty($source_form)) {
81
    $form['feeds'] = array(
82
      '#type' => 'fieldset',
83
      '#title' => t('Import'),
84
      '#tree' => TRUE,
85
    ) + $source_form;
86
  }
87

    
88
  // Set submit button label based on settings.
89
  if ($source->importer->config['import_on_create']) {
90
    $submit = t('Import');
91
    if ($source->importer->config['process_in_background']) {
92
      // When processing the import in background, the import job is put in the
93
      // queue.
94
      $submit = t('Schedule import');
95
    }
96
  }
97
  elseif ($source->importer->config['import_period'] != FEEDS_SCHEDULE_NEVER) {
98
    // The import would be scheduled according to the periodic import setting.
99
    $submit = t('Schedule import');
100
  }
101
  else {
102
    drupal_set_message(t('For this importer both "@import_period" and "@import_on_create" are turned off. It is possible that Feeds will not import the provided source.', array(
103
      '@import_period' => t('Periodic import'),
104
      '@import_on_create' => t('Import on submission'),
105
    )), 'warning', FALSE);
106
    $submit = t('Save');
107
  }
108

    
109
  $form['actions'] = array('#type' => 'actions');
110
  $form['actions']['submit'] = array(
111
    '#type' => 'submit',
112
    '#value' => $submit,
113
  );
114

    
115
  // Disable submit button if import is initiated.
116
  $progress = $source->progressImporting();
117
  if ($progress !== FEEDS_BATCH_COMPLETE) {
118
    $form['actions']['submit']['#disabled'] = TRUE;
119
    $form['actions']['submit']['#value'] = t('Importing (@progress %)', array(
120
      '@progress' => number_format(100 * $progress, 0),
121
    ));
122

    
123
    // Check if import task is queued.
124
    if ($source->isQueued()) {
125
      $form['source_status']['#value'] .= t('Run cron to continue the import.');
126
    }
127
  }
128

    
129
  return $form;
130
}
131

    
132
/**
133
 * Validation handler for node forms and feeds_import_form().
134
 */
135
function feeds_import_form_validate($form, &$form_state) {
136
  // @todo This may be a problem here, as we don't have a feed_nid at this point.
137
  feeds_source($form['#importer_id'])->configFormValidate($form_state['values']['feeds']);
138
}
139

    
140
/**
141
 * Submit handler for feeds_import_form().
142
 */
143
function feeds_import_form_submit($form, &$form_state) {
144
  // Save source and import.
145
  $source = feeds_source($form['#importer_id']);
146

    
147
  if (!empty($form_state['values']['feeds']) && is_array($form_state['values']['feeds'])) {
148
    $source->addConfig($form_state['values']['feeds']);
149
    $source->save();
150
  }
151

    
152
  // Refresh feed if import on create is selected.
153
  if ($source->importer->config['import_on_create']) {
154
    $source->startImport();
155
    if ($source->importer->config['process_in_background']) {
156
      drupal_set_message(t('Import scheduled.'));
157
    }
158
  }
159

    
160
  // Add to schedule, make sure importer is scheduled, too.
161
  $source->ensureSchedule();
162

    
163
  // If an import is only triggered by periodic import, check if it is about to
164
  // be rescheduled so there is at least a message.
165
  if (!$source->importer->config['import_on_create']) {
166
    // Check if the importer is about to be rescheduled.
167
    $importers = feeds_reschedule();
168
    if (isset($importers[$form['#importer_id']])) {
169
      drupal_set_message(t('Rescheduling the import will happen on the next cron run.'), 'status');
170
    }
171
  }
172
}
173

    
174
/**
175
 * Render a feeds import form on node/id/import pages.
176
 */
177
function feeds_import_tab_form($form, &$form_state, $node) {
178
  $importer_id = feeds_get_importer_id($node->type);
179
  $source = feeds_source($importer_id, $node->nid);
180

    
181
  $form = array();
182
  $form['#feed_nid'] = $node->nid;
183
  $form['#importer_id'] = $importer_id;
184
  $form['#redirect'] = 'node/' . $node->nid;
185
  $form['source_status'] = array(
186
    '#type' => 'fieldset',
187
    '#title' => t('Status'),
188
    '#tree' => TRUE,
189
    '#value' => feeds_source_status($source),
190
  );
191
  $form = confirm_form($form, t('Import all content from source?'), 'node/' . $node->nid, '', t('Import'), t('Cancel'), 'confirm feeds update');
192

    
193
  // Change submit button label if processing in background.
194
  if ($source->importer->config['process_in_background']) {
195
    $form['actions']['submit']['#value'] = t('Schedule import');
196
  }
197

    
198
  // Disable submit button if import is initiated.
199
  $progress = $source->progressImporting();
200
  if ($progress !== FEEDS_BATCH_COMPLETE) {
201
    $form['actions']['submit']['#disabled'] = TRUE;
202
    $form['actions']['submit']['#value'] = t('Importing (@progress %)', array(
203
      '@progress' => number_format(100 * $progress, 0),
204
    ));
205

    
206
    // Check if import task is queued.
207
    if ($source->isQueued()) {
208
      $form['source_status']['#value'] .= t('Run cron to continue the import.');
209
    }
210
  }
211

    
212
  return $form;
213
}
214

    
215
/**
216
 * Submit handler for feeds_import_tab_form().
217
 */
218
function feeds_import_tab_form_submit($form, &$form_state) {
219
  $form_state['redirect'] = $form['#redirect'];
220
  $source = feeds_source($form['#importer_id'], $form['#feed_nid']);
221
  $source->startImport();
222
  $source->ensureSchedule();
223

    
224
  if ($source->importer->config['process_in_background']) {
225
    drupal_set_message(t('Import scheduled.'));
226
  }
227
}
228

    
229
/**
230
 * Render a feeds delete form.
231
 *
232
 * Used on both node pages and configuration pages.
233
 * Therefore $node may be missing.
234
 */
235
function feeds_delete_tab_form(array $form, array &$form_state, FeedsImporter $importer = NULL, $node = NULL) {
236
  if (empty($node)) {
237
    $source = feeds_source($importer->id);
238
    $form['#redirect'] = 'import/' . $source->id;
239
  }
240
  else {
241
    $importer_id = feeds_get_importer_id($node->type);
242
    $source = feeds_source($importer_id, $node->nid);
243
    $form['#redirect'] = 'node/' . $source->feed_nid;
244
  }
245
  // Form cannot pass on source object.
246
  $form['#importer_id'] = $source->id;
247
  $form['#feed_nid'] = $source->feed_nid;
248
  $form['source_status'] = array(
249
    '#type' => 'fieldset',
250
    '#title' => t('Status'),
251
    '#tree' => TRUE,
252
    '#value' => feeds_source_status($source),
253
  );
254
  $form = confirm_form($form, t('Delete all items from source?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
255

    
256
  // Change submit button label if processing in background.
257
  if ($source->importer->config['process_in_background']) {
258
    $form['actions']['submit']['#value'] = t('Schedule delete');
259
  }
260

    
261
  // Disable submit button if clearing is initiated.
262
  $progress = $source->progressClearing();
263
  if ($progress !== FEEDS_BATCH_COMPLETE) {
264
    $form['actions']['submit']['#disabled'] = TRUE;
265
    $form['actions']['submit']['#value'] = t('Deleting (@progress %)', array(
266
      '@progress' => number_format(100 * $progress, 0),
267
    ));
268
    $form['source_status']['#value'] .= t('Run cron to continue the deletion of items.');
269
  }
270

    
271
  return $form;
272
}
273

    
274
/**
275
 * Submit handler for feeds_delete_tab_form().
276
 */
277
function feeds_delete_tab_form_submit($form, &$form_state) {
278
  $form_state['redirect'] = $form['#redirect'];
279
  $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
280
  $source = feeds_source($form['#importer_id'], $feed_nid);
281
  $source->startClear();
282
  $source->ensureSchedule();
283

    
284
  if ($source->importer->config['process_in_background']) {
285
    drupal_set_message(t('Deletion of items scheduled.'));
286
  }
287
}
288

    
289
/**
290
 * Render a feeds unlock form.
291
 *
292
 * Used on both node pages and configuration pages.
293
 * Therefore $node may be missing.
294
 */
295
function feeds_unlock_tab_form($form, &$form_state, FeedsImporter $importer = NULL, $node = NULL) {
296
  if (empty($node)) {
297
    $source = feeds_source($importer->id);
298
    $form['#redirect'] = 'import/' . $source->id;
299
  }
300
  else {
301
    $importer_id = feeds_get_importer_id($node->type);
302
    $source = feeds_source($importer_id, $node->nid);
303
    $form['#redirect'] = 'node/' . $source->feed_nid;
304
  }
305
  // Form cannot pass on source object.
306
  $form['#importer_id'] = $source->id;
307
  $form['#feed_nid'] = $source->feed_nid;
308
  $form['source_status'] = array(
309
    '#type' => 'fieldset',
310
    '#title' => t('Status'),
311
    '#tree' => TRUE,
312
    '#value' => feeds_source_status($source),
313
  );
314
  $form = confirm_form($form, t('Unlock this importer?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
315
  if ($source->progressImporting() == FEEDS_BATCH_COMPLETE && $source->progressClearing() == FEEDS_BATCH_COMPLETE) {
316
    $form['source_locked'] = array(
317
      '#type' => 'markup',
318
      '#title' => t('Not Locked'),
319
      '#tree' => TRUE,
320
      '#markup' => t('This importer is not locked, therefore it cannot be unlocked.'),
321
    );
322
    $form['actions']['submit']['#disabled'] = TRUE;
323
    $form['actions']['submit']['#value'] = t('Unlock (disabled)');
324
  }
325
  else {
326
    $form['actions']['submit']['#value'] = t('Unlock');
327
  }
328
  return $form;
329
}
330

    
331
/**
332
 * Form submit handler. Resets all feeds state.
333
 */
334
function feeds_unlock_tab_form_submit($form, &$form_state) {
335
  $form_state['redirect'] = $form['#redirect'];
336
  $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
337
  $importer_id = $form['#importer_id'];
338

    
339
  feeds_source($importer_id, $feed_nid)
340
    ->unlock();
341
  drupal_set_message(t('Importer unlocked.'));
342
}
343

    
344
/**
345
 * Handle a fetcher callback.
346
 */
347
function feeds_fetcher_callback($importer, $feed_nid = 0) {
348
  if ($importer instanceof FeedsImporter) {
349
    try {
350
      return $importer->fetcher->request($feed_nid);
351
    }
352
    catch (Exception $e) {
353
      // Do nothing.
354
    }
355
  }
356
  drupal_access_denied();
357
}
358

    
359
/**
360
 * Template generation.
361
 */
362
function feeds_importer_template(FeedsImporter $importer) {
363
  if ($importer->parser instanceof FeedsCSVParser) {
364
    return $importer->parser->getTemplate();
365
  }
366
  return drupal_not_found();
367
}
368

    
369
/**
370
 * Renders a status display for a source.
371
 */
372
function feeds_source_status($source) {
373
  $progress_importing = $source->progressImporting();
374
  $v = array();
375
  if ($progress_importing != FEEDS_BATCH_COMPLETE) {
376
    $v['progress_importing'] = $progress_importing;
377
  }
378
  $progress_clearing = $source->progressClearing();
379
  if ($progress_clearing != FEEDS_BATCH_COMPLETE) {
380
    $v['progress_clearing'] = $progress_clearing;
381
  }
382
  $v['imported'] = $source->imported;
383
  $v['count'] = $source->itemCount();
384
  $v['next'] = $source->getNextImportTimeDetails();
385
  if (!empty($v)) {
386
    return theme('feeds_source_status', $v);
387
  }
388
}
389

    
390
/**
391
 * Themes a status display for a source.
392
 */
393
function theme_feeds_source_status($v) {
394
  $output = '<div class="info-box feeds-source-status">';
395
  $items = array();
396
  if ($v['progress_importing']) {
397
    $progress = number_format(100.0 * $v['progress_importing'], 0);
398
    $items[] = t('Importing - @progress % complete.', array(
399
      '@progress' => $progress,
400
    ));
401
  }
402
  if ($v['progress_clearing']) {
403
    $progress = number_format(100.0 * $v['progress_clearing'], 0);
404
    $items[] = t('Deleting items - @progress % complete.', array(
405
      '@progress' => $progress,
406
    ));
407
  }
408
  if (!count($items)) {
409
    if ($v['count']) {
410
      if ($v['imported']) {
411
        $items[] = t('Last import: @ago ago.', array(
412
          '@ago' => format_interval(REQUEST_TIME - $v['imported'], 1),
413
        ));
414
      }
415
      $items[] = t('@count imported items total.', array(
416
        '@count' => $v['count'],
417
      ));
418
    }
419
    else {
420
      $items[] = t('No imported items.');
421
    }
422
  }
423

    
424
  if ($v['next']) {
425
    // Check if medium date format contains hours/minutes.
426
    $date_format = variable_get('date_format_medium');
427
    $use_custom_date_format = $date_format && !strpos($date_format, 'H:i');
428

    
429
    if (!empty($v['next']['message'])) {
430
      $items[] = t('Next import: @message.', array(
431
        '@message' => $v['next']['message'],
432
      ));
433
    }
434
    elseif ($v['next']['time'] > REQUEST_TIME) {
435
      $items[] = t('Next import: @date (via @method)', array(
436
        '@date' => $use_custom_date_format ? format_date($v['next']['time'], 'custom', 'Y/m/d H:i:s') : format_date($v['next']['time']),
437
        '@method' => $v['next']['method'],
438
      ));
439
    }
440
    else {
441
      $items[] = t('Next import: on next cron run (via @method).', array(
442
        '@method' => $v['next']['method'],
443
      ));
444
    }
445
  }
446
  else {
447
    $items[] = t('Next import: not scheduled.');
448
  }
449
  $output .= theme('item_list', array('items' => $items));
450
  $output .= '</div>';
451
  return $output;
452
}
453

    
454
/**
455
 * Theme upload widget.
456
 */
457
function theme_feeds_upload($variables) {
458
  $element = $variables['element'];
459
  drupal_add_css(drupal_get_path('module', 'feeds') . '/feeds.css');
460
  _form_set_class($element, array('form-file'));
461
  $summary = '';
462
  if (!empty($element['#file_info'])) {
463
    $file = $element['#file_info'];
464
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
465
    $summary .= '<div class="feeds-file-info">';
466
    $summary .= '<div class="feeds-file-name">';
467
    if ($wrapper) {
468
      $summary .= l($file->filename, $wrapper->getExternalUrl());
469
    }
470
    else {
471
      $summary .= t('URI scheme %scheme not available.', array(
472
        '%scheme' => file_uri_scheme($uri),
473
      ));
474
    }
475
    $summary .= '</div>';
476
    $summary .= '<div class="file-size">';
477
    $summary .= format_size($file->filesize);
478
    $summary .= '</div>';
479
    $summary .= '<div class="feeds-file-mime">';
480
    $summary .= check_plain($file->filemime);
481
    $summary .= '</div>';
482
    $summary .= '</div>';
483
  }
484
  // Prepend the summary to the form field.
485
  $element['#children'] = '<div class="feeds-file">' . $summary . '<div class="feeds-file-upload">' . $element['#children'];
486
  // Render file upload field using theme_form_element().
487
  $output = theme('form_element', $element);
488
  // Close "feeds-file" and "feeds-file-upload" divs.
489
  $output .= '</div></div>';
490

    
491
  return $output;
492
}