Upgrade file handling.
[project/feeds.git] / feeds.pages.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Menu callbacks, form callbacks and helpers.
7 */
8
9 /**
10 * Render a page of available importers.
11 */
12 function feeds_page() {
13 $rows = array();
14 if ($importers = feeds_importer_load_all()) {
15 foreach ($importers as $importer) {
16 if ($importer->disabled) {
17 continue;
18 }
19 if (!(user_access('import '. $importer->id .' feeds') || user_access('administer feeds'))) {
20 continue;
21 }
22 if (empty($importer->config['content_type'])) {
23 $link = 'import/'. $importer->id;
24 $title = $importer->config['name'];
25 }
26 elseif (user_access('create '. $importer->config['content_type'] .' content')) {
27 $link = 'node/add/'. str_replace('_', '-', $importer->config['content_type']);
28 $title = node_type_get_name($importer->config['content_type']);
29 }
30 $rows[] = array(
31 l($title, $link),
32 check_plain($importer->config['description']),
33 );
34 }
35 }
36 $header = array(
37 t('Import'),
38 t('Description'),
39 );
40 return theme('table', array('header' => $header, 'rows' => $rows));
41 }
42
43 /**
44 * Render a feeds import form on import/[config] pages.
45 */
46 function feeds_import_form($form, &$form_state, $importer_id) {
47 $source = feeds_source($importer_id, empty($form['nid']['#value']) ? 0 : $form['nid']['#value']);
48
49 $form = array();
50 $form['#importer_id'] = $importer_id;
51 // @todo Move this into fetcher?
52 $form['#attributes']['enctype'] = 'multipart/form-data';
53 $form['feeds'] = array(
54 '#type' => 'fieldset',
55 '#tree' => TRUE,
56 );
57 $form['feeds'] += $source->configForm($form_state);
58 $form['submit'] = array(
59 '#type' => 'submit',
60 '#value' => t('Import'),
61 );
62 return $form;
63 }
64
65 /**
66 * Validation handler for node forms and feeds_import_form().
67 */
68 function feeds_import_form_validate($form, &$form_state) {
69 // @todo This may be a problem here, as we don't have a feed_nid at this point.
70 feeds_source($form['#importer_id'])->configFormValidate($form_state['values']['feeds']);
71 }
72
73 /**
74 * Submit handler for feeds_import_form().
75 */
76 function feeds_import_form_submit($form, &$form_state) {
77
78 // Save source and import.
79 $source = feeds_source($form['#importer_id']);
80 $source->addConfig($form_state['values']['feeds']);
81 $source->save();
82
83 // Refresh feed if import on create is selected.
84 if ($source->importer->config['import_on_create']) {
85 feeds_batch_set(t('Importing'), 'import', $form['#importer_id']);
86 }
87
88 // Add to schedule, make sure importer is scheduled, too.
89 $source->schedule();
90 $source->importer->schedule();
91 }
92
93 /**
94 * Render a feeds import form on node/id/import pages.
95 */
96 function feeds_import_tab_form($form, &$form_state, $node) {
97 $importer_id = feeds_get_importer_id($node->type);
98
99 $form = array();
100 $form['#feed_nid'] = $node->nid;
101 $form['#importer_id'] = $importer_id;
102 $form['#redirect'] = 'node/'. $node->nid;
103 return confirm_form($form, t('Import all content from feed?'), 'node/'. $node->nid, '', t('Import'), t('Cancel'), 'confirm feeds update');
104 }
105
106 /**
107 * Submit handler for feeds_import_tab_form().
108 */
109 function feeds_import_tab_form_submit($form, &$form_state) {
110 $form_state['redirect'] = $form['#redirect'];
111 feeds_batch_set(t('Importing'), 'import', $form['#importer_id'], $form['#feed_nid']);
112 }
113
114 /**
115 * Render a feeds delete form.
116 *
117 * Used on both node pages and configuration pages.
118 * Therefore $node may be missing.
119 */
120 function feeds_delete_tab_form($form, &$form_state, $importer_id, $node = NULL) {
121 if (empty($node)) {
122 $form['#redirect'] = 'import/'. $importer_id;
123 }
124 else {
125 $importer_id = feeds_get_importer_id($node->type);
126 $form['#feed_nid'] = $node->nid;
127 $form['#redirect'] = 'node/'. $node->nid;
128 }
129 // Form cannot pass on feed object.
130 $form['#importer_id'] = $importer_id;
131 return confirm_form($form, t('Delete all items from feed?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
132 }
133
134 /**
135 * Submit handler for feeds_delete_tab_form().
136 */
137 function feeds_delete_tab_form_submit($form, &$form_state) {
138 $form_state['redirect'] = $form['#redirect'];
139 $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
140 feeds_batch_set(t('Deleting'), 'clear', $form['#importer_id'], $feed_nid);
141 }
142
143 /**
144 * Handle a fetcher callback.
145 */
146 function feeds_fetcher_callback($importer, $feed_nid = 0) {
147 if ($importer instanceof FeedsImporter) {
148 return $importer->fetcher->request($feed_nid);
149 }
150 drupal_access_denied();
151 }
152
153 /**
154 * Theme upload widget.
155 */
156 function theme_feeds_upload($variables) {
157 $element = $variables['element'];
158 drupal_add_css(drupal_get_path('module', 'feeds') .'/feeds.css');
159 _form_set_class($element, array('form-file'));
160 $description = '';
161 if (!empty($element['#file_info'])) {
162 $info = $element['#file_info'];
163 $description .= '<div class="file-info">';
164 $description .= '<div class="file-name">';
165 $description .= l(basename($info['path']), $info['path']);
166 $description .= '</div>';
167 $description .= '<div class="file-size">';
168 $description .= format_size($info['size']);
169 $description .= '</div>';
170 if (isset($info['mime'])) {
171 $description .= '<div class="file-mime">';
172 $description .= check_plain($info['mime']);
173 $description .= '</div>';
174 }
175 $description .= '</div>';
176 }
177 $description .= '<div class="file-upload">';
178 $description .= '<input type="file" name="'. $element['#name'] .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '') .' id="'. $element['#id'] .'" size="'. $element['#size'] ."\" />\n";
179 $description .= '</div>';
180 $element['#description'] = $description;
181 return theme('form_element', $element);
182 }