Projet

Général

Profil

Paste
Télécharger (11,5 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / feeds / feeds.pages.inc @ a5572547

1 85ad3d82 Assos Assos
<?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
    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'))));
40
  }
41
  $header = array(
42
    t('Import'),
43
    t('Description'),
44
  );
45
  return theme('table', array('header' => $header, 'rows' => $rows));
46
}
47
48
/**
49
 * Render a feeds import form on import/[config] pages.
50
 */
51
function feeds_import_form($form, &$form_state, $importer_id) {
52
  $source = feeds_source($importer_id, empty($form['nid']['#value']) ? 0 : $form['nid']['#value']);
53
54
  $form = array();
55
  $form['#importer_id'] = $importer_id;
56
  // @todo Move this into fetcher?
57
  $form['#attributes']['enctype'] = 'multipart/form-data';
58
  $form['source_status'] = array(
59
    '#type' => 'fieldset',
60
    '#title' => t('Status'),
61
    '#tree' => TRUE,
62
    '#value' => feeds_source_status($source),
63
  );
64
65
  $source_form = $source->configForm($form_state);
66
  if (!empty($source_form)) {
67
    $form['feeds'] = array(
68
      '#type' => 'fieldset',
69
      '#title' => t('Import'),
70
      '#tree' => TRUE,
71
    ) + $source_form;
72
  }
73
74
  $form['submit'] = array(
75
    '#type' => 'submit',
76
    '#value' => t('Import'),
77
  );
78
  $progress = $source->progressImporting();
79
  if ($progress !== FEEDS_BATCH_COMPLETE) {
80
    $form['submit']['#disabled'] = TRUE;
81
    $form['submit']['#value'] =
82
      t('Importing (@progress %)', array('@progress' => number_format(100 * $progress, 0)));
83
  }
84
  return $form;
85
}
86
87
/**
88
 * Validation handler for node forms and feeds_import_form().
89
 */
90
function feeds_import_form_validate($form, &$form_state) {
91
  // @todo This may be a problem here, as we don't have a feed_nid at this point.
92
  feeds_source($form['#importer_id'])->configFormValidate($form_state['values']['feeds']);
93
}
94
95
/**
96
 * Submit handler for feeds_import_form().
97
 */
98
function feeds_import_form_submit($form, &$form_state) {
99
  // Save source and import.
100
  $source = feeds_source($form['#importer_id']);
101
102
  if (!empty($form_state['values']['feeds']) && is_array($form_state['values']['feeds'])) {
103
    $source->addConfig($form_state['values']['feeds']);
104
    $source->save();
105
  }
106
107
  // Refresh feed if import on create is selected.
108
  if ($source->importer->config['import_on_create']) {
109
    $source->startImport();
110
  }
111
112
  // Add to schedule, make sure importer is scheduled, too.
113
  $source->schedule();
114
  $source->importer->schedule();
115
}
116
117
/**
118
 * Render a feeds import form on node/id/import pages.
119
 */
120
function feeds_import_tab_form($form, &$form_state, $node) {
121
  $importer_id = feeds_get_importer_id($node->type);
122
  $source = feeds_source($importer_id, $node->nid);
123
124
  $form = array();
125
  $form['#feed_nid'] = $node->nid;
126
  $form['#importer_id'] = $importer_id;
127
  $form['#redirect'] = 'node/' . $node->nid;
128
  $form['source_status'] = array(
129
    '#type' => 'fieldset',
130
    '#title' => t('Status'),
131
    '#tree' => TRUE,
132
    '#value' => feeds_source_status($source),
133
  );
134
  $form = confirm_form($form, t('Import all content from source?'), 'node/' . $node->nid, '', t('Import'), t('Cancel'), 'confirm feeds update');
135
  $progress = $source->progressImporting();
136
  if ($progress !== FEEDS_BATCH_COMPLETE) {
137
    $form['actions']['submit']['#disabled'] = TRUE;
138
    $form['actions']['submit']['#value'] =
139
      t('Importing (@progress %)', array('@progress' => number_format(100 * $progress, 0)));
140
  }
141
  return $form;
142
}
143
144
/**
145
 * Submit handler for feeds_import_tab_form().
146
 */
147
function feeds_import_tab_form_submit($form, &$form_state) {
148
  $form_state['redirect'] = $form['#redirect'];
149
  feeds_source($form['#importer_id'], $form['#feed_nid'])->startImport();
150
}
151
152
/**
153
 * Render a feeds delete form.
154
 *
155
 * Used on both node pages and configuration pages.
156
 * Therefore $node may be missing.
157
 */
158
function feeds_delete_tab_form($form, &$form_state, $importer_id, $node = NULL) {
159
  if (empty($node)) {
160
    $source = feeds_source($importer_id);
161
    $form['#redirect'] = 'import/' . $source->id;
162
  }
163
  else {
164
    $importer_id = feeds_get_importer_id($node->type);
165
    $source = feeds_source($importer_id, $node->nid);
166
    $form['#redirect'] = 'node/' . $source->feed_nid;
167
  }
168
  // Form cannot pass on source object.
169
  $form['#importer_id'] = $source->id;
170
  $form['#feed_nid'] = $source->feed_nid;
171
  $form['source_status'] = array(
172
    '#type' => 'fieldset',
173
    '#title' => t('Status'),
174
    '#tree' => TRUE,
175
    '#value' => feeds_source_status($source),
176
  );
177
  $form = confirm_form($form, t('Delete all items from source?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
178
  $progress = $source->progressClearing();
179
  if ($progress !== FEEDS_BATCH_COMPLETE) {
180
    $form['actions']['submit']['#disabled'] = TRUE;
181
    $form['actions']['submit']['#value'] =
182
      t('Deleting (@progress %)', array('@progress' => number_format(100 * $progress, 0)));
183
  }
184
  return $form;
185
}
186
187
/**
188
 * Submit handler for feeds_delete_tab_form().
189
 */
190
function feeds_delete_tab_form_submit($form, &$form_state) {
191
  $form_state['redirect'] = $form['#redirect'];
192
  $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
193
  feeds_source($form['#importer_id'], $feed_nid)->startClear();
194
}
195
196
/**
197
 * Render a feeds unlock form.
198
 *
199
 * Used on both node pages and configuration pages.
200
 * Therefore $node may be missing.
201
 */
202
function feeds_unlock_tab_form($form, &$form_state, $importer_id, $node = NULL) {
203
  if (empty($node)) {
204
    $source = feeds_source($importer_id);
205
    $form['#redirect'] = 'import/' . $source->id;
206
  }
207
  else {
208
    $importer_id = feeds_get_importer_id($node->type);
209
    $source = feeds_source($importer_id, $node->nid);
210
    $form['#redirect'] = 'node/' . $source->feed_nid;
211
  }
212
  // Form cannot pass on source object.
213
  $form['#importer_id'] = $source->id;
214
  $form['#feed_nid'] = $source->feed_nid;
215
  $form['source_status'] = array(
216
    '#type' => 'fieldset',
217
    '#title' => t('Status'),
218
    '#tree' => TRUE,
219
    '#value' => feeds_source_status($source),
220
  );
221
  $form = confirm_form($form, t('Unlock this importer?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
222
  if ($source->progressImporting() == FEEDS_BATCH_COMPLETE && $source->progressClearing() == FEEDS_BATCH_COMPLETE) {
223
    $form['source_locked'] = array(
224
      '#type' => 'markup',
225
      '#title' => t('Not Locked'),
226
      '#tree' => TRUE,
227
      '#markup' => t('This importer is not locked, therefore it cannot be unlocked.'),
228
    );
229
    $form['actions']['submit']['#disabled'] = TRUE;
230
    $form['actions']['submit']['#value'] = t('Unlock (disabled)');
231
  }
232
  else {
233
    $form['actions']['submit']['#value'] = t('Unlock');
234
  }
235
  return $form;
236
}
237
238
/**
239
 * Form submit handler. Resets all feeds state.
240
 */
241
function feeds_unlock_tab_form_submit($form, &$form_state) {
242
  drupal_set_message(t('Import Unlocked'));
243
  $form_state['redirect'] = $form['#redirect'];
244
  $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
245
  $importer_id = $form['#importer_id'];
246
247
  //Is there a more API-friendly way to set the state?
248
  db_update('feeds_source')
249
    ->condition('id', $importer_id)
250
    ->condition('feed_nid', $feed_nid)
251
    ->fields(array('state' => FALSE))
252
    ->execute();
253
}
254
255
/**
256
 * Handle a fetcher callback.
257
 */
258
function feeds_fetcher_callback($importer, $feed_nid = 0) {
259
  if ($importer instanceof FeedsImporter) {
260
    try {
261
      return $importer->fetcher->request($feed_nid);
262
    }
263
    catch (Exception $e) {
264
      // Do nothing.
265
    }
266
  }
267
  drupal_access_denied();
268
}
269
270
/**
271
 * Template generation
272
 */
273
function feeds_importer_template($importer_id) {
274
  $importer = feeds_importer($importer_id);
275
  if ($importer->parser instanceof FeedsCSVParser) {
276
    return $importer->parser->getTemplate();
277
  }
278
  return drupal_not_found();
279
}
280
281
/**
282
 * Renders a status display for a source.
283
 */
284
function feeds_source_status($source) {
285
  $progress_importing = $source->progressImporting();
286
  $v = array();
287
  if ($progress_importing != FEEDS_BATCH_COMPLETE) {
288
    $v['progress_importing'] = $progress_importing;
289
  }
290
  $progress_clearing = $source->progressClearing();
291
  if ($progress_clearing != FEEDS_BATCH_COMPLETE) {
292
    $v['progress_clearing'] = $progress_clearing;
293
  }
294
  $v['imported'] = $source->imported;
295
  $v['count'] = $source->itemCount();
296
  if (!empty($v)) {
297
    return theme('feeds_source_status', $v);
298
  }
299
}
300
301
/**
302
 * Themes a status display for a source.
303
 */
304
function theme_feeds_source_status($v) {
305
  $output = '<div class="info-box feeds-source-status">';
306
  $items = array();
307
  if ($v['progress_importing']) {
308
    $progress = number_format(100.0 * $v['progress_importing'], 0);
309
    $items[] = t('Importing - @progress % complete.', array('@progress' => $progress));
310
  }
311
  if ($v['progress_clearing']) {
312
    $progress = number_format(100.0 * $v['progress_clearing'], 0);
313
    $items[] = t('Deleting items - @progress % complete.', array('@progress' => $progress));
314
  }
315
  if (!count($items)) {
316
    if ($v['count']) {
317
      if ($v['imported']) {
318
        $items[] = t('Last import: @ago ago.', array('@ago' => format_interval(REQUEST_TIME - $v['imported'], 1)));
319
      }
320
      $items[] = t('@count imported items total.', array('@count' => $v['count']));
321
    }
322
    else {
323
      $items[] = t('No imported items.');
324
    }
325
  }
326
  $output .= theme('item_list', array('items' => $items));
327
  $output .= '</div>';
328
  return $output;
329
}
330
331
/**
332
 * Theme upload widget.
333
 */
334
function theme_feeds_upload($variables) {
335
  $element = $variables['element'];
336
  drupal_add_css(drupal_get_path('module', 'feeds') . '/feeds.css');
337
  _form_set_class($element, array('form-file'));
338
  $description = '';
339
  if (!empty($element['#file_info'])) {
340
    $file = $element['#file_info'];
341
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
342
    $description .= '<div class="file-info">';
343
    $description .= '<div class="file-name">';
344
    if ($wrapper) {
345
      $description .= l($file->filename, $wrapper->getExternalUrl());
346
    }
347
    else {
348
      $description .= t('URI scheme %scheme not available.', array('%scheme' =>  file_uri_scheme($uri)));
349
    }
350
    $description .= '</div>';
351
    $description .= '<div class="file-size">';
352
    $description .= format_size($file->filesize);
353
    $description .= '</div>';
354
    $description .= '<div class="file-mime">';
355
    $description .= check_plain($file->filemime);
356
    $description .= '</div>';
357
    $description .= '</div>';
358
  }
359
  $description .= '<div class="file-upload">';
360
  $description .= '<input type="file" name="' . $element['#name'] . '"' . ($element['#attributes'] ? ' ' . drupal_attributes($element['#attributes']) : '') . ' id="' . $element['#id'] . '" size="' . $element['#size'] . "\" />\n";
361
  $description .= '</div>';
362
  $element['#description'] = $description;
363
364
  // For some reason not unsetting #title leads to printing the title twice.
365
  unset($element['#title']);
366
  return theme('form_element', $element);
367
}