/[drupal]/contributions/modules/simpleviews/simpleviews.pages.inc
ViewVC logotype

Contents of /contributions/modules/simpleviews/simpleviews.pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Fri Sep 26 19:28:12 2008 UTC (14 months ago) by eaton
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +4 -4 lines
File MIME type: text/x-php
Fixing a couple of minor pre-rollout bugs.
1 <?php
2 // $Id: simpleviews.pages.inc,v 1.1 2008/09/25 22:08:09 eaton Exp $
3
4 /**
5 * Build the overview form.
6 *
7 * Loads all items and builds an overview form with weight elements for each
8 * item.
9 *
10 * @ingroup forms
11 * @see _simpleviews_overview_item_field()
12 * @see simpleviews_overview_form_submit()
13 * @see theme_simpleviews_overview_form()
14 */
15 function simpleviews_overview_form(&$form_state) {
16 $results = db_query("SELECT * FROM {simpleviews}");
17 $items = array();
18 while ($item = db_fetch_array($results)) {
19 $items[$item['svid']] = $item;
20 }
21
22 $form['items']['#tree'] = TRUE;
23 foreach ($items as $svid => $item) {
24 $form['items'][$svid] = _simpleviews_overview_item_field($item);
25 }
26
27 return $form;
28 }
29
30
31 /**
32 * Build the overview form fields for a single item.
33 *
34 * @see simpleviews_overview_form()
35 */
36 function _simpleviews_overview_item_field($item) {
37 $form['svid'] = array(
38 '#type' => 'hidden',
39 '#value' => $item['svid'],
40 );
41
42 $form['title'] = array(
43 '#type' => 'markup',
44 '#value' => check_plain($item['title']),
45 );
46
47 $form['path'] = array(
48 '#type' => 'markup',
49 '#value' => l($item['path'], $item['path']),
50 );
51
52 $path = drupal_get_path('module', 'simpleviews') . '/images/';
53 $links[] = array(
54 'title' => theme('image', $path . 'text-editor.png', t('Edit')),
55 'href' => 'admin/build/simpleviews/' . $item['svid'] . '/edit',
56 'html' => TRUE,
57 );
58 $links[] = array(
59 'title' => theme('image', $path . 'edit-delete.png', t('Delete')),
60 'href' => 'admin/build/simpleviews/' . $item['svid'] . '/delete',
61 'html' => TRUE,
62 );
63
64 $form['operations'] = array(
65 '#type' => 'markup',
66 '#value' => theme('links', $links),
67 );
68
69 return $form;
70 }
71
72 /**
73 * Theme the overview form.
74 *
75 * Arranges items in a table.
76 *
77 * @ingroup themeable
78 * @ingroup forms
79 * @see simpleviews_overview_form()
80 */
81 function theme_simpleviews_overview_form($form) {
82 $rows = array();
83 foreach (element_children($form['items']) as $key) {
84 $row = array();
85
86 // Render the hidden 'item id' field and the title of the item into the
87 // same column of the row.
88 $row[] = drupal_render($form['items'][$key]['svid']) . drupal_render($form['items'][$key]['title']);
89 $row[] = drupal_render($form['items'][$key]['path']);
90 $row[] = drupal_render($form['items'][$key]['operations']);
91 $rows[] = $row;
92 }
93
94 // If there were no items found, note the fact so users don't get confused
95 // by a completely empty table.
96 if (count($rows) == 0) {
97 $rows[] = array(array('data' => t('No items have been added.'), 'colspan' => 3));
98 }
99
100 // Render a list of header titles, and our array of rows, into a table. Even
101 // we've already rendered all of our items, we always call drupal_render()
102 // on the form itself after we're done, so hidden security fields and other
103 // elements (like buttons) will appear properly at the bottom of the form.
104 $header = array(t('Title'), t('Path'), t('Operations'));
105 $output = theme('table', $header, $rows);
106 $output .= drupal_render($form);
107
108 return $output;
109 }
110
111
112 /* Individual item editing form */
113
114 /**
115 * Build the record editing form.
116 *
117 * If a record is passed in, an edit form with both Save and Delete buttons will
118 * be built. Otherwise, a blank 'add new record' form, without the Delete button,
119 * will be built.
120 *
121 * @ingroup forms
122 * @see simpleviews_form_submit()
123 * @see simpleviews_form_delete()
124 */
125 function simpleviews_form(&$form_state, $simpleview = array()) {
126 $form['simpleview'] = _simpleviews_form($simpleview);
127
128 $form['buttons']['submit'] = array(
129 '#type' => 'submit',
130 '#value' => t('Submit'),
131 );
132
133 // Only show the delete button if we already have an ID. Set the delete
134 // button's submit handler to a custom function that should only fire if
135 // this button is clicked. In all other cases, the form will fall back to
136 // the default $form_id_submit() function.
137 if (!empty($simpleview['svid'])) {
138 $form['buttons']['delete'] = array(
139 '#type' => 'submit',
140 '#value' => t('Delete'),
141 '#submit' => array('simpleviews_form_delete'),
142 );
143 }
144
145 return $form;
146 }
147
148 /**
149 * Build the record editing chunk, in a reusable fashion.
150 */
151 function _simpleviews_form($simpleview = array()) {
152 $simpleview += simpleviews_default_data();
153
154 // We theme this explicitly, rather than overriding the entire form,
155 // so that modules re-use this chunk of form to on their own pages and still
156 // get the proper formatting.
157 $form['#theme'] = 'simpleviews_edit_form';
158
159 if (!empty($simpleview['svid'])) {
160 $form['svid'] = array(
161 '#type' => 'value',
162 '#value' => $simpleview['svid'],
163 );
164 }
165
166 $form['title'] = array(
167 '#type' => 'textfield',
168 '#title' => t('Title'),
169 '#required' => TRUE,
170 '#default_value' => $simpleview['title'],
171 );
172
173 $form['path'] = array(
174 '#type' => 'textfield',
175 '#title' => t('Path'),
176 '#required' => TRUE,
177 '#default_value' => $simpleview['path'],
178 );
179
180 $form['filter'] = array(
181 '#type' => 'select',
182 '#title' => t('Display'),
183 '#options' => array(
184 'all' => t('All posts'),
185 ),
186 '#required' => TRUE,
187 '#default_value' => $simpleview['filter'],
188 );
189 foreach (node_get_types('names') as $type => $name) {
190 $form['filter']['#options']['node:'. $type] = t('!type posts', array('!type' => $name));
191 }
192
193 $form['sort'] = array(
194 '#type' => 'select',
195 '#title' => t('Sorted'),
196 '#options' => array(
197 'newest' => t('Newest first'),
198 'oldest' => t('Oldest first'),
199 'a-z' => t('By title'),
200 ),
201 '#required' => TRUE,
202 '#default_value' => $simpleview['sort'],
203 );
204 if (module_exists('statistics')) {
205 $form['sort']['#options']['popular'] = t('By number of hits');
206 }
207 if (module_exists('votingapi')) {
208 $form['sort']['#options']['top-rated'] = t('By rating');
209 }
210
211 $form['style'] = array(
212 '#type' => 'select',
213 '#title' => t('As a'),
214 '#options' => array(
215 'full' => t('List of full posts'),
216 'teasers' => t('List of teasers'),
217 'titles' => t('List of titles'),
218 //'grid' => t('Grid of thumbnails'),
219 ),
220 '#required' => TRUE,
221 '#default_value' => $simpleview['style'],
222 );
223
224 $form['argument']['#element_validate'] = array('simpleviews_preprocess_arg_element');
225 $form['argument']['arg_toggle'] = array(
226 '#type' => 'checkbox',
227 '#title' => t('Let visitors filter the page'),
228 '#default_value' => !empty($simpleview['argument']),
229 );
230
231 $form['argument']['argument'] = array(
232 '#type' => 'select',
233 '#title' => t('By'),
234 '#required' => TRUE,
235 '#options' => array(
236 'date' => t('The date posts were written'),
237 'author' => t('The author of the post'),
238 ),
239 '#default_value' => $simpleview['argument'],
240 );
241 foreach (taxonomy_get_vocabularies() as $key => $vocab) {
242 $form['argument']['argument']['#options']['term:' . $vocab->vid] = "The post's $vocab->name";
243 }
244
245 $form['rss'] = array(
246 '#type' => 'checkbox',
247 '#title' => t('Add an RSS feed'),
248 '#default_value' => $simpleview['rss'],
249 );
250
251 $form['block'] = array(
252 '#type' => 'checkbox',
253 '#title' => t('Create a sidebar widget'),
254 '#default_value' => $simpleview['rss'],
255 );
256
257 drupal_alter('simpleview_reusable_form', $form, $simpleview);
258
259 return $form;
260 }
261
262
263 /**
264 * A slightly unholy hack to preserve the behavior of the 'filter' checkbox
265 * even though we never store it explicitly.
266 */
267 function simpleviews_preprocess_arg_element(&$element, &$form_state) {
268 if (empty($element['arg_toggle']['#value'])) {
269 $key = $element['argument']['#name'];
270 $form_state['values'][$key] = '';
271 }
272 }
273
274 /**
275 * Simple theme wrapper for the simpleview edit form, adds CSS and JS.
276 */
277 function theme_simpleviews_edit_form($form) {
278 drupal_add_css(drupal_get_path('module', 'simpleviews') . '/simpleviews.css');
279 drupal_add_js(drupal_get_path('module', 'simpleviews') . '/simpleviews.js');
280
281 $extra = empty($form['argument']['argument']['#default_value']) ? ' class="js-hide"' : '';
282 $form['argument']['argument']['#prefix'] = '<div id="simpleviews-arg-wrapper"' . $extra . '>';
283 $form['argument']['argument']['#suffix'] = '</div>';
284
285 $output .= '<div id="simpleviews-elements">' . drupal_render($form) . '</div>';
286 return $output;
287 }
288
289
290 /**
291 * General submit handler for the add/edit form.
292 *
293 * Simply passes incoming form values on to the module's CRUD save function,
294 * then redirects to the overview form.
295 *
296 * @ingroup formapi
297 * @see simpleviews_form()
298 */
299 function simpleviews_form_submit($form, &$form_state) {
300 $item = $form_state['values'];
301 simpleviews_item_save($item);
302 views_flush_caches();
303 $form_state['redirect'] = 'admin/build/simpleviews';
304 }
305
306
307 /**
308 * Delete button submit handler for the add/edit form.
309 *
310 * Redirects to the 'delete item' confirmation page without performing any
311 * operations.
312 *
313 * @ingroup formapi
314 * @see simpleviews_form()
315 */
316 function simpleviews_form_delete($form, &$form_state) {
317 $form_state['redirect'] = 'admin/build/simpleviews/' . $form_state['values']['svid'] . '/delete';
318 }
319
320
321 /**
322 * Build the delete confirmation form.
323 *
324 * A simple wrapper around Drupal's core confirm_form() function. Adds a value
325 * field to store the ID of the item being deleted.
326 *
327 * @ingroup forms
328 * @see simpleviews_delete_confirm_submit()
329 * @see confirm_form()
330 */
331 function simpleviews_delete_confirm(&$form_state, $item) {
332 $form['svid'] = array(
333 '#type' => 'value',
334 '#value' => $item['svid'],
335 );
336
337 return confirm_form($form,
338 t('Are you sure you want to delete %title?', array('%title' => $item['title'])),
339 isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/simpleviews',
340 t('This action cannot be undone.'),
341 t('Delete'),
342 t('Cancel')
343 );
344 }
345
346 /**
347 * General submit handler for the delete confirmation form.
348 *
349 * Core's confirm_form() function adds the 'confirm' value element we check
350 * against to ensure the form was properly submitted. If it's there, delete
351 * the item and redirect to the overview form.
352 *
353 * @ingroup formapi
354 * @see simpleviews_form()
355 */
356
357 function simpleviews_delete_confirm_submit($form, &$form_state) {
358 if ($form_state['values']['confirm']) {
359 simpleviews_item_delete($form_state['values']['svid']);
360 drupal_set_message(t('Your item was deleted.'));
361 }
362 $form_state['redirect'] = 'admin/build/simpleviews';
363 }

  ViewVC Help
Powered by ViewVC 1.1.2