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