/[drupal]/contributions/modules/views_argument_api/views_argument_api.module
ViewVC logotype

Contents of /contributions/modules/views_argument_api/views_argument_api.module

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


Revision 1.1 - (show annotations) (download) (as text)
Mon Jun 11 20:39:32 2007 UTC (2 years, 5 months ago) by morrissinger
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of views_argument_api module. This module allows modules to implement and use methods other than the URI to specify arguments in block and embedded views.
1 <?php
2 // $Id$
3
4 /*********************************************************************
5 * Drupal Hooks
6 */
7
8 /**
9 * Implementation of hook_menu().
10 */
11 function views_argument_api_menu($may_cache) {
12 //Include functionality on behalf of core modules.
13 _views_argument_api_include_core_modules();
14
15 if(!$may_cache && $_POST['form_id'] == 'views_edit_view') {
16 $form_values = $_POST['views_argument_api_configure_form'];
17 $form_values['name'] = $_POST['name'];
18 $form_values = _views_argument_api_check_plain($form_values);
19 views_argument_api_form_submit($_POST['form_id'], $form_values);
20 }
21 }
22
23 /**
24 * Implementation of hook_xmlrpc().
25 */
26 function views_argument_api_xmlrpc() {
27
28 //Include functionality on behalf of core modules.
29 _views_argument_api_include_core_modules();
30 }
31
32 /**
33 * Implementation of hook_cron().
34 */
35 function views_argument_api_cron() {
36 //Include functionality on behalf of core modules.
37 _views_argument_api_include_core_modules();
38 }
39
40 /**
41 * Implementation of hook_form_alter().
42 */
43 function views_argument_api_form_alter($form_id, &$form) {
44 switch($form_id) {
45 case 'views_edit_view':
46 $name = $form['basic-info']['name']['#default_value'];
47
48 //Get all available elements
49 $elements = views_argument_api_get_elements();
50
51 foreach($elements as $argid => $element_set) {
52 foreach($element_set as $key => $element) {
53 $options[$argid][$key] = $element['label'];
54 }
55 }
56
57 if(!empty($name)) {
58 //Get default values
59 $default_elements = views_argument_api_get_element_names_by_view($name);
60 }
61
62 $f = 'views_argument_api_configure_form';
63 $form[$f] = array(
64 '#description' => t('Select the argument form elements you wish to use with this form when passing arguments to block or embedded views.'),
65 '#type' => 'fieldset',
66 '#title' => t('Argument API'),
67 '#collapsible' => TRUE,
68 '#collapsed' => TRUE,
69 '#weight' => -1,
70 '#tree' => TRUE,
71 '#theme' => 'views_argument_api_configure_form',
72 );
73 foreach($form['argument'] as $key => $element) {
74 if(is_numeric($key)) {
75 $argid = $element['id']['#value'];
76 $title = $form['argument']['add']['id']['#options'][$argid];
77 if(isset($options[$argid])) {
78 $form[$f][$argid] = array(
79 '#type' => 'select',
80 '#title' => $title,
81 '#options' => $options[$argid],
82 '#default_value' => $default_elements[$argid],
83 );
84 }
85 }
86 }
87 }
88 }
89
90 /*********************************************************************
91 * Helper Functions
92 */
93
94 /**
95 * Handles submission of additional information on the views_edit_view form.
96 *
97 * @param $form_id
98 * The form ID of the form being submitted.
99 */
100 function views_argument_api_form_submit($form_id, $form_values) {
101 //Get the view name;
102 $name = $form_values['name'];
103 unset($form_values['name']);
104 //Delete old query results
105 db_query("DELETE FROM {views_argument_api} WHERE view = '%s'", $name);
106
107 foreach($form_values as $argid => $element) {
108 db_query("INSERT INTO {views_argument_api} (view, argid, element) VALUES ('%s', '%s', '%s')", $name, $argid, $element);
109 }
110 }
111
112 /**
113 * Recursively runs check_plain() on arrays
114 *
115 * @param $form_values
116 * A single value, an array of values, or a multi-dimensional array of values.
117 * @return
118 * The array, having each value been run through check_plain().
119 */
120 function _views_argument_api_check_plain($form_values) {
121 if(is_array($form_values)) {
122 foreach($form_values as $key => $value) {
123 $form_values[$key] = _views_argument_api_check_plain($value);
124 }
125 return $form_values;
126 } else {
127 return check_plain($form_values);
128 }
129 }
130
131 /**
132 * Includes views argument api functionality for core modules.
133 */
134 function _views_argument_api_include_core_modules() {
135 $path = drupal_get_path('module', 'views_argument_api');
136 $dirpath = "$path/modules";
137 $dh = opendir($dirpath);
138 while (false !== ($file = readdir($dh))) {
139 if (!is_dir("$dirpath/$file")) {
140 if(substr($file, -4, 4) == ".inc") {
141 require_once("$dirpath/$file");
142 }
143 }
144 }
145 closedir($dh);
146 }
147
148 /*********************************************************************
149 * Theme Functions
150 */
151 function theme_views_argument_api_configure_form(&$form) {
152 foreach (element_children($form) as $key) {
153 $row = array();
154 $row['title'] = $form[$key]['#title'];
155 $form[$key]['#title'] = NULL;
156
157 $row['element'] = drupal_render($form[$key]);
158
159 $rows[] = $row;
160 $row = array();
161 }
162
163 if(!empty($rows)) {
164 return theme('table', array(t('Argument'), t('Form element to use')), $rows);
165 } else {
166 return drupal_render($form);
167 }
168
169 }
170
171 /*********************************************************************
172 *********************************************************************
173 * API ***************************************************************
174 *********************************************************************
175 *********************************************************************/
176
177
178 /*********************************************************************
179 * Functions for finding out about provided elements.
180 */
181
182 /**
183 * @file
184 * Provides the API for the views_argument_api module. This file is
185 * supposed to be a collection of useful public access functions for
186 * other modules that want to lean on this module.
187 */
188
189 /**
190 * Returns an array of form element names for a particular view.
191 *
192 * @param $name
193 * A view name
194 * @return
195 * An array of form element names.
196 */
197 function views_argument_api_get_element_names_by_view($name) {
198 $qs = db_query("SELECT * FROM {views_argument_api} v WHERE v.view = '%s'", $name);
199 while($obj = db_fetch_object($qs)) {
200 $elements[$obj->argid] = $obj->element;
201 }
202
203 return $elements;
204 }
205
206 /**
207 * Get all elements currently assigned to a specific view.
208 *
209 * @param $name
210 * A view name.
211 * @return
212 * An array of elements, keyed by argid.
213 */
214 function views_argument_api_get_elements_by_view($name) {
215 $elements = views_argument_api_get_elements();
216 $view_elements_chosen = views_argument_api_get_element_names_by_view($name);
217
218 foreach($view_elements_chosen as $key => $chosen_element) {
219 $return[$key] = $elements[$key][$chosen_element];
220 }
221
222 return $return;
223 }
224
225 /**
226 * Returns a set of form values related to the views argument API, which
227 * are a subset of a larger set of form values.
228 *
229 * Use this function if you are processing a form that includes form elements
230 * generated by this API. This function accepts a set of form values and
231 * returns just the form values related to the API, keyed by argument ID.
232 *
233 * @param $view_name
234 * A unique view name.
235 * @param $form_values
236 * An array of form values, as would be passed to a form validation or
237 * form submission function.
238 * @return
239 * An array of form values that come from form elements generated by this
240 * API.
241 */
242 function views_argument_api_get_api_form_values($view_name, $form_values) {
243 $elements = views_argument_api_get_elements_by_view($view_name);
244 foreach($elements as $argid => $element) {
245 $return[$argid] = $form_values[$argid];
246 }
247
248 return $return;
249 }
250 /**
251 * Call all modules to get their exposed form element information.
252 *
253 * @return
254 * An array of elements.
255 */
256 function views_argument_api_get_elements() {
257 foreach(module_implements('views_argument_api_argument_elements') as $module) {
258 $module_elements = array();
259 $func = $module . '_views_argument_api_argument_elements';
260 $module_elements = $func();
261 foreach($module_elements as $argid => $set) {
262 foreach($set as $element) {
263 $elements[$argid][$module . '_' . $element['name']] = array(
264 'name' => $module . '_' . $element['name'],
265 'label' => $element['label'],
266 'handler' => $element['handler'],
267 'callback arguments' => $element['callback arguments'],
268 'element' => $element['element'],
269 );
270 }
271 }
272
273 }
274
275 return $elements;
276 }
277
278 /*********************************************************************
279 * Functions for building and processing forms.
280 */
281
282 /**
283 * Builds a form for handling views arguments for a specific view.
284 *
285 * @param $name
286 * A view name.
287 * @return
288 * An array of form elements.
289 */
290 function views_argument_api_build_argument_form($name) {
291 $elements = views_argument_api_get_elements_by_view($name);
292 foreach($elements as $argid => $element) {
293 $form[$argid] = $element['element'];
294 }
295
296 return $form;
297 }
298
299 /**
300 * Handles processing of a argument form element generated by this API.
301 *
302 * @param $view_name
303 * A unique view name.
304 * @param $arg_id
305 * An argument id (e.g. 'taxid').
306 * @value
307 * The value of a views_argument_api form element.
308 * @return
309 * A processed value for a view argument, given by a specific element's handler.
310 */
311 function views_argument_api_process_argument_element($view_name, $arg_id, $value) {
312 $elements = views_argument_api_get_elements_by_view($view_name);
313 $func = $elements[$arg_id]['handler'];
314 return call_user_func_array($func, array_merge(array($value), $elements[$arg_id]['callback arguments']));
315 }
316
317 /**
318 * Handles processing of all argument form elements for a particular view.
319 *
320 * Rather than looping through views_argument_api_process_elements_by_view,
321 * if you want to process an entire form, you should use this function.
322 * This will save on performance by performing some queries once, as opposed
323 * to one time for each element.
324 *
325 * @param $view_name
326 * A unique view name.
327 * @param $form_values
328 * A set of form values for processing, keyed by argument ID.
329 * @return
330 * An array of processed values for view arguments, keyed by argid.
331 */
332 function views_argument_api_process_argument_elements_by_view($view_name, $form_values) {
333 $elements = views_argument_api_get_elements_by_view($view_name);
334 foreach($elements as $argid => $element) {
335 $func = $elements[$argid]['handler'];
336 $processed[$argid] = call_user_func_array($func, array_merge(array($form_values[$argid]), $elements[$argid]['callback arguments']));
337 }
338
339 return $processed;
340 }
341
342 /**
343 * Saves an entire set of argument form element values to a specific
344 * API set ID. If a view name is provided, the function will first
345 * cross-check the form values against the argument IDs in the view,
346 * to ensure that it is working only with views_argument_api form
347 * values.
348 *
349 * @param $api_id
350 * An API set ID (of your choosing) to save the form values against.
351 * Think of this as the ID you would provide in a call to
352 * variable_set() as the first argument.
353 * @param $edit
354 * A set of form values, which may or may not be exclusively the values
355 * provided by a form generated by this API.
356 * @param $view_name
357 * A view name against which to cross-check the keys of $edit. (optional).
358 * @return
359 * TRUE if successful. FALSE if there was a problem.
360 */
361 function views_argument_api_save_arguments($api_id, $edit, $view_name = NULL) {
362 if(!empty($view_name)) {
363 $api_values = views_argument_api_get_api_form_values($view_name, $edit);
364 } else {
365 $api_values = $edit;
366 }
367
368 db_query("DELETE FROM {views_argument_api_args} WHERE api_id = '%s'", $api_id);
369 $r = db_query("INSERT INTO {views_argument_api_args} (api_id, api_values) VALUES ('%s', '%s')", $api_id, serialize($api_values));
370 return $r;
371 }
372
373 /**
374 * Returns an entire set of argument form element values matching
375 * a specific API set ID, or $default if none is found.
376 *
377 * @param $api_id
378 * An API set ID for a set of form values.
379 * @param $default
380 * A value to return if no values have been set for the API set ID
381 * provided.
382 * @return
383 * An entire set of argument form element values matching a
384 * specific API set ID, or $default if none is found.
385 */
386 function views_argument_api_get($api_id, $default) {
387 $values = db_result(db_query("SELECT v.api_values FROM {views_argument_api_args} v WHERE v.api_id = '%s'", $api_id));
388 return (!empty($values) ? unserialize($values) : $default);
389 }
390
391 /**
392 * Returns an entire set of argument values, processed by the handlers
393 * specified with the particular view. This function constructs an entire
394 * set of arguments to pass to a view from an API set ID and a view name.
395 *
396 * @param $api_id
397 * An API set ID for a set of form values.
398 * @param $default
399 * A value to return if no values have been set for the API set ID
400 * provided.
401 * @param $view_name
402 * A unique view name.
403 * @param $for_view
404 * If this paramater is set, the function will return a numerically-keyed
405 * view, as opposed to an associative array. This is suitable to pass
406 * to views_build_view.
407 * @return
408 * An entire set of arguments that may be passed to a view, if
409 * the specific API set ID returns a match. $default, otherwise.
410 */
411 function views_argument_api_get_processed($api_id, $default, $view_name, $for_view = FALSE) {
412 $values = views_argument_api_get($api_id, $default);
413
414 if(empty($values)) {
415 return $default;
416 } else {
417 $processed_values = views_argument_api_process_argument_elements_by_view($view_name, $values);
418
419 if($for_view) {
420 foreach($processed_values as $value) {
421 $arg_set[] = $value;
422 }
423 } else {
424 $arg_set = $processed_values;
425 }
426 return $arg_set;
427 }
428 }
429 /*********************************************************************
430 * Some default processing handlers that your API elements can use.
431 */
432
433 /**
434 * Returns the value of the form element.
435 *
436 * This is the most simple of all handlers. While your API elements
437 * may wish to take a form value and run it through additional
438 * functions to come up with an argument value, it is likely that
439 * many modules will want to simply return the form value.
440 *
441 * This would certainly be the case for many textfield form elements,
442 * and perhaps for some select elements as well.
443 */
444 function views_argument_api_handler_default_form_value($value) {
445 return check_plain($value);
446 }
447
448 /*********************************************************************
449 * Some default themes for the argument form that you can use.
450 */
451
452 /**
453 * Tabular theme for argument form.
454 */
455 function theme_views_argument_api_form_tabular(&$form) {
456 foreach (element_children($form) as $key) {
457 $row1 = array();
458 $row2 = array();
459
460 $row1['title'] = $form[$key]['#title'];
461 $form[$key]['#title'] = NULL;
462 if(isset($form[$key]['#description'])) {
463 $row2[] = array(
464 'data' => $form[$key]['#description'],
465 'colspan' => 2,
466 );
467 }
468
469 $form[$key]['#description'] = NULL;
470
471 $row1['element'] = drupal_render($form[$key]);
472
473 $rows[] = $row1;
474 if(!empty($row2)) {
475 $rows[] = $row2;
476 }
477 }
478
479 if(!empty($rows)) {
480 return theme('table', array(t('Argument'), t('Value')), $rows);
481 } else {
482 return drupal_render($form);
483 }
484
485 }

  ViewVC Help
Powered by ViewVC 1.1.2