Projet

Général

Profil

Paste
Télécharger (12,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / feeds.pages.inc @ 41cc1b08

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
  $form['submit'] = array(
89
    '#type' => 'submit',
90
    '#value' => t('Import'),
91
  );
92
  $progress = $source->progressImporting();
93
  if ($progress !== FEEDS_BATCH_COMPLETE) {
94
    $form['submit']['#disabled'] = TRUE;
95
    $form['submit']['#value'] =
96
      t('Importing (@progress %)', array('@progress' => number_format(100 * $progress, 0)));
97
  }
98
  return $form;
99
}
100

    
101
/**
102
 * Validation handler for node forms and feeds_import_form().
103
 */
104
function feeds_import_form_validate($form, &$form_state) {
105
  // @todo This may be a problem here, as we don't have a feed_nid at this point.
106
  feeds_source($form['#importer_id'])->configFormValidate($form_state['values']['feeds']);
107
}
108

    
109
/**
110
 * Submit handler for feeds_import_form().
111
 */
112
function feeds_import_form_submit($form, &$form_state) {
113
  // Save source and import.
114
  $source = feeds_source($form['#importer_id']);
115

    
116
  if (!empty($form_state['values']['feeds']) && is_array($form_state['values']['feeds'])) {
117
    $source->addConfig($form_state['values']['feeds']);
118
    $source->save();
119
  }
120

    
121
  // Refresh feed if import on create is selected.
122
  if ($source->importer->config['import_on_create']) {
123
    $source->startImport();
124
  }
125

    
126
  // Add to schedule, make sure importer is scheduled, too.
127
  $source->schedule();
128
}
129

    
130
/**
131
 * Render a feeds import form on node/id/import pages.
132
 */
133
function feeds_import_tab_form($form, &$form_state, $node) {
134
  $importer_id = feeds_get_importer_id($node->type);
135
  $source = feeds_source($importer_id, $node->nid);
136

    
137
  $form = array();
138
  $form['#feed_nid'] = $node->nid;
139
  $form['#importer_id'] = $importer_id;
140
  $form['#redirect'] = 'node/' . $node->nid;
141
  $form['source_status'] = array(
142
    '#type' => 'fieldset',
143
    '#title' => t('Status'),
144
    '#tree' => TRUE,
145
    '#value' => feeds_source_status($source),
146
  );
147
  $form = confirm_form($form, t('Import all content from source?'), 'node/' . $node->nid, '', t('Import'), t('Cancel'), 'confirm feeds update');
148
  $progress = $source->progressImporting();
149
  if ($progress !== FEEDS_BATCH_COMPLETE) {
150
    $form['actions']['submit']['#disabled'] = TRUE;
151
    $form['actions']['submit']['#value'] =
152
      t('Importing (@progress %)', array('@progress' => number_format(100 * $progress, 0)));
153
  }
154
  return $form;
155
}
156

    
157
/**
158
 * Submit handler for feeds_import_tab_form().
159
 */
160
function feeds_import_tab_form_submit($form, &$form_state) {
161
  $form_state['redirect'] = $form['#redirect'];
162
  feeds_source($form['#importer_id'], $form['#feed_nid'])->startImport();
163
}
164

    
165
/**
166
 * Render a feeds delete form.
167
 *
168
 * Used on both node pages and configuration pages.
169
 * Therefore $node may be missing.
170
 */
171
function feeds_delete_tab_form(array $form, array &$form_state, FeedsImporter $importer = NULL, $node = NULL) {
172
  if (empty($node)) {
173
    $source = feeds_source($importer->id);
174
    $form['#redirect'] = 'import/' . $source->id;
175
  }
176
  else {
177
    $importer_id = feeds_get_importer_id($node->type);
178
    $source = feeds_source($importer_id, $node->nid);
179
    $form['#redirect'] = 'node/' . $source->feed_nid;
180
  }
181
  // Form cannot pass on source object.
182
  $form['#importer_id'] = $source->id;
183
  $form['#feed_nid'] = $source->feed_nid;
184
  $form['source_status'] = array(
185
    '#type' => 'fieldset',
186
    '#title' => t('Status'),
187
    '#tree' => TRUE,
188
    '#value' => feeds_source_status($source),
189
  );
190
  $form = confirm_form($form, t('Delete all items from source?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
191
  $progress = $source->progressClearing();
192
  if ($progress !== FEEDS_BATCH_COMPLETE) {
193
    $form['actions']['submit']['#disabled'] = TRUE;
194
    $form['actions']['submit']['#value'] =
195
      t('Deleting (@progress %)', array('@progress' => number_format(100 * $progress, 0)));
196
  }
197
  return $form;
198
}
199

    
200
/**
201
 * Submit handler for feeds_delete_tab_form().
202
 */
203
function feeds_delete_tab_form_submit($form, &$form_state) {
204
  $form_state['redirect'] = $form['#redirect'];
205
  $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
206
  feeds_source($form['#importer_id'], $feed_nid)->startClear();
207
}
208

    
209
/**
210
 * Render a feeds unlock form.
211
 *
212
 * Used on both node pages and configuration pages.
213
 * Therefore $node may be missing.
214
 */
215
function feeds_unlock_tab_form($form, &$form_state, FeedsImporter $importer = NULL, $node = NULL) {
216
  if (empty($node)) {
217
    $source = feeds_source($importer->id);
218
    $form['#redirect'] = 'import/' . $source->id;
219
  }
220
  else {
221
    $importer_id = feeds_get_importer_id($node->type);
222
    $source = feeds_source($importer_id, $node->nid);
223
    $form['#redirect'] = 'node/' . $source->feed_nid;
224
  }
225
  // Form cannot pass on source object.
226
  $form['#importer_id'] = $source->id;
227
  $form['#feed_nid'] = $source->feed_nid;
228
  $form['source_status'] = array(
229
    '#type' => 'fieldset',
230
    '#title' => t('Status'),
231
    '#tree' => TRUE,
232
    '#value' => feeds_source_status($source),
233
  );
234
  $form = confirm_form($form, t('Unlock this importer?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
235
  if ($source->progressImporting() == FEEDS_BATCH_COMPLETE && $source->progressClearing() == FEEDS_BATCH_COMPLETE) {
236
    $form['source_locked'] = array(
237
      '#type' => 'markup',
238
      '#title' => t('Not Locked'),
239
      '#tree' => TRUE,
240
      '#markup' => t('This importer is not locked, therefore it cannot be unlocked.'),
241
    );
242
    $form['actions']['submit']['#disabled'] = TRUE;
243
    $form['actions']['submit']['#value'] = t('Unlock (disabled)');
244
  }
245
  else {
246
    $form['actions']['submit']['#value'] = t('Unlock');
247
  }
248
  return $form;
249
}
250

    
251
/**
252
 * Form submit handler. Resets all feeds state.
253
 */
254
function feeds_unlock_tab_form_submit($form, &$form_state) {
255
  drupal_set_message(t('Import Unlocked'));
256
  $form_state['redirect'] = $form['#redirect'];
257
  $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
258
  $importer_id = $form['#importer_id'];
259

    
260
  //Is there a more API-friendly way to set the state?
261
  db_update('feeds_source')
262
    ->condition('id', $importer_id)
263
    ->condition('feed_nid', $feed_nid)
264
    ->fields(array('state' => FALSE))
265
    ->execute();
266
}
267

    
268
/**
269
 * Handle a fetcher callback.
270
 */
271
function feeds_fetcher_callback($importer, $feed_nid = 0) {
272
  if ($importer instanceof FeedsImporter) {
273
    try {
274
      return $importer->fetcher->request($feed_nid);
275
    }
276
    catch (Exception $e) {
277
      // Do nothing.
278
    }
279
  }
280
  drupal_access_denied();
281
}
282

    
283
/**
284
 * Template generation
285
 */
286
function feeds_importer_template(FeedsImporter $importer) {
287
  if ($importer->parser instanceof FeedsCSVParser) {
288
    return $importer->parser->getTemplate();
289
  }
290
  return drupal_not_found();
291
}
292

    
293
/**
294
 * Renders a status display for a source.
295
 */
296
function feeds_source_status($source) {
297
  $progress_importing = $source->progressImporting();
298
  $v = array();
299
  if ($progress_importing != FEEDS_BATCH_COMPLETE) {
300
    $v['progress_importing'] = $progress_importing;
301
  }
302
  $progress_clearing = $source->progressClearing();
303
  if ($progress_clearing != FEEDS_BATCH_COMPLETE) {
304
    $v['progress_clearing'] = $progress_clearing;
305
  }
306
  $v['imported'] = $source->imported;
307
  $v['count'] = $source->itemCount();
308
  if (!empty($v)) {
309
    return theme('feeds_source_status', $v);
310
  }
311
}
312

    
313
/**
314
 * Themes a status display for a source.
315
 */
316
function theme_feeds_source_status($v) {
317
  $output = '<div class="info-box feeds-source-status">';
318
  $items = array();
319
  if ($v['progress_importing']) {
320
    $progress = number_format(100.0 * $v['progress_importing'], 0);
321
    $items[] = t('Importing - @progress % complete.', array('@progress' => $progress));
322
  }
323
  if ($v['progress_clearing']) {
324
    $progress = number_format(100.0 * $v['progress_clearing'], 0);
325
    $items[] = t('Deleting items - @progress % complete.', array('@progress' => $progress));
326
  }
327
  if (!count($items)) {
328
    if ($v['count']) {
329
      if ($v['imported']) {
330
        $items[] = t('Last import: @ago ago.', array('@ago' => format_interval(REQUEST_TIME - $v['imported'], 1)));
331
      }
332
      $items[] = t('@count imported items total.', array('@count' => $v['count']));
333
    }
334
    else {
335
      $items[] = t('No imported items.');
336
    }
337
  }
338
  $output .= theme('item_list', array('items' => $items));
339
  $output .= '</div>';
340
  return $output;
341
}
342

    
343
/**
344
 * Theme upload widget.
345
 */
346
function theme_feeds_upload($variables) {
347
  $element = $variables['element'];
348
  drupal_add_css(drupal_get_path('module', 'feeds') . '/feeds.css');
349
  _form_set_class($element, array('form-file'));
350
  $summary = '';
351
  if (!empty($element['#file_info'])) {
352
    $file = $element['#file_info'];
353
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
354
    $summary .= '<div class="feeds-file-info">';
355
    $summary .= '<div class="feeds-file-name">';
356
    if ($wrapper) {
357
      $summary .= l(check_plain($file->filename), $wrapper->getExternalUrl());
358
    }
359
    else {
360
      $summary .= t('URI scheme %scheme not available.', array('%scheme' =>  file_uri_scheme($uri)));
361
    }
362
    $summary .= '</div>';
363
    $summary .= '<div class="file-size">';
364
    $summary .= format_size($file->filesize);
365
    $summary .= '</div>';
366
    $summary .= '<div class="feeds-file-mime">';
367
    $summary .= check_plain($file->filemime);
368
    $summary .= '</div>';
369
    $summary .= '</div>';
370
  }
371
  // Prepend the summary to the form field.
372
  $element['#children'] = '<div class="feeds-file">' . $summary . '<div class="feeds-file-upload">' . $element['#children'];
373
  // Render file upload field using theme_form_element().
374
  $output = theme('form_element', $element);
375
  // Close "feeds-file" and "feeds-file-upload" divs.
376
  $output .= '</div></div>';
377

    
378
  return $output;
379
}