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

Contents of /contributions/modules/record/record.module

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


Revision 1.3 - (show annotations) (download) (as text)
Tue Nov 27 06:42:04 2007 UTC (2 years ago) by recidive
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +1 -1 lines
File MIME type: text/x-php
Lots of improvements:
 - Changed how arguments are processed.
 - Improved JOIN clause generation.
 - Fetched data are now the same (reference) for the main object or a sub-object, no object copy/processing is done when fetching results.
 - Adding field labels to users table schema.
 - And more.
1 <?php
2 /**
3 * Implementation of hook_init().
4 */
5 function record_init() {
6 $path = drupal_get_path('module', 'record');
7
8 // Include record extension.
9 require_once($path .'/record.inc');
10
11 // Include modules definition extensions.
12 // TODO: Cache included files list to improve performance(?).
13 foreach (file_scan_directory($path .'/modules', '\.inc$') as $file) {
14 if (module_exists($file->name)) {
15 require_once($file->filename);
16 }
17 }
18 }
19
20 /**
21 * Implementation of hook_forms().
22 *
23 * All record forms share the same form handler
24 */
25 function record_forms() {
26 foreach (array_keys(drupal_get_schema()) as $type) {
27 $forms[$type .'_record_form']['callback'] = 'record_form';
28 }
29 return $forms;
30 }
31
32 /**
33 * Generate forms based on schema information.
34 */
35 function record_form(&$form_state, $type, $object = NULL) {
36 $schema = drupal_get_schema($type);
37 $form['#id'] = 'record-form';
38 $form['#object'] = $object;
39 $form['#record_type'] = $type;
40 foreach ($schema['fields'] as $field_name => $field) {
41 switch ($field['type']) {
42 case 'serial':
43 $type = 'value';
44 break;
45 case 'varchar':
46 case 'int':
47 case 'float':
48 case 'numeric':
49 $type = 'textfield';
50 break;
51 case 'text':
52 case 'blob':
53 $type = 'textarea';
54 break;
55 case 'date':
56 $type = 'date';
57 break;
58 }
59
60 $form[$field_name] = array(
61 '#type' => $type,
62 '#title' => $field['label'],
63 '#default value' => (isset($object->$field_name) ? $object->$field_name : NULL),
64 );
65 }
66 // Add the buttons.
67 $form['buttons'] = array();
68 $form['buttons']['submit'] = array(
69 '#type' => 'submit',
70 '#value' => t('Save'),
71 '#weight' => 10,
72 '#submit' => array('record_form_submit'),
73 );
74 $form['#validate'][] = 'record_form_validate';
75 return $form;
76 }
77
78 /**
79 * Validates a record form.
80 */
81 function record_form_validate($form, &$form_state) {
82 $record = record_new($form['#record_type'], $form_state['values']);
83 if (!record_validate($record)) {
84 foreach(record_get_errors($record) as $field_name => $errors) {
85 foreach($errors as $error) {
86 form_set_error($field_name, $error);
87 }
88 }
89 }
90 }
91
92 /**
93 * Submits a record form. Save record data.
94 */
95 function record_form_submit($form, &$form_state) {
96 $record = record_new($form['#record_type'], $form_state['values']);
97 record_save($record);
98 }

  ViewVC Help
Powered by ViewVC 1.1.2