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

Contents of /contributions/modules/tapir/tapir.module

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


Revision 1.9 - (show annotations) (download) (as text)
Sat Oct 11 16:07:55 2008 UTC (13 months, 2 weeks ago) by rszrama
Branch: MAIN
CVS Tags: DRUPAL-5--1-6, HEAD
Branch point for: DRUPAL-5
Changes since 1.8: +9 -3 lines
File MIME type: text/x-php
Patch by hanoii to toggle the display of a table's footer.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Implements an API for the dynamic configuration and display of tables.
7 *
8 * TAPIr defines a tables API that can be used for dynamic table generation.
9 * Tables are defined in their own builder functions and rendered using the
10 * function tapir_get_table
11 *
12 * Development sponsored by the Ubercart project. http://www.ubercart.org
13 */
14
15 /*******************************************************************************
16 * Hook Functions
17 ******************************************************************************/
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function tapir_menu($may_cache) {
23 if ($may_cache) {
24 $items[] = array(
25 'path' => 'admin/settings/tapir',
26 'title' => t('Configure tables'),
27 'callback' => 'tapir_master_list',
28 'access' => user_access('edit tables'),
29 'description' => t('View and configure the dynamic tables on your site.'),
30 'type' => MENU_NORMAL_ITEM,
31 );
32 }
33 else {
34 $tables = module_invoke_all('table_settings');
35 foreach ($tables as $table) {
36 $path = isset($table['path']) ? $table['path'] : 'admin/settings/tapir';
37 $items[] = array(
38 'path' => $path .'/'. $table['id'],
39 'title' => isset($table['menu title']) ? $table['menu title'] : $table['id'],
40 'callback' => 'tapir_edit',
41 'callback arguments' => array($table['id'], $path, $table['preview']),
42 'access' => isset($table['access']) ? user_access($table['access']) : user_access('edit tables'),
43 'description' => t('Configure the fields and settings for !table_id.', array('!table_id' => $table['id'])),
44 'type' => ((!isset($table['path']) && variable_get('tapir_table_menu_items', FALSE)) || $table['menu item'] === TRUE) ? MENU_NORMAL_ITEM : MENU_CALLBACK,
45 );
46 $items[] = array(
47 'path' => $path .'/'. $table['id'] .'/view',
48 'title' => t('View !table_id', array('!table_id' => $table['id'])),
49 'callback' => 'tapir_view',
50 'callback arguments' => array($table['id'], $path),
51 'access' => isset($table['access']) ? user_access($table['access']) : user_access('edit tables'),
52 'description' => t('View !table_id with the current settings.', array('!table_id' => $table['id'])),
53 'type' => MENU_CALLBACK,
54 );
55 }
56 }
57 return $items;
58 }
59
60 /**
61 * Implementation of hook_perm().
62 */
63 function tapir_perm() {
64 return array('edit tables');
65 }
66
67 /*******************************************************************************
68 * Callback Functions, Forms, and Tables
69 ******************************************************************************/
70
71 /**
72 * Display a list of all dynamic tables defined in enabled modules.
73 */
74 function tapir_master_list() {
75 $output = '<p>'. t("Click on a table's ID to configure the display of that table:") .'</p>'
76 .'<p>'. tapir_table_list() .'</p><br />'
77 .'<p>'. t('Use the form below to adjust the TAPIr settings:') .'</p>'
78 . drupal_get_form('tapir_settings_form');
79
80 return $output;
81 }
82
83 /**
84 * Build the settings form for the TAPIr module.
85 */
86 function tapir_settings_form() {
87 $form['tapir_table_menu_items'] = array(
88 '#type' => 'checkbox',
89 '#title' => t('Show table settings pages in this path as menu items.'),
90 '#default_value' => variable_get('tapir_table_menu_items', FALSE),
91 );
92
93 return system_settings_form($form);
94 }
95
96 /**
97 * Display the edit page for the table specified by $table_id.
98 */
99 function tapir_edit($table_id, $path, $preview = TRUE) {
100 if (!function_exists($table_id)) {
101 return t('Error generating settings form for table %table_id.', array('%table_id' => $table_id));
102 }
103
104 $null = NULL;
105 $fields = array_merge($table_id('fields', $null), module_invoke_all('table_alter', $table_id, 'fields', $null));
106 if (!is_array($fields)) {
107 return t('No fields have been defined for this column.');
108 }
109 for ($i = 0; $i < count($fields); $i++) {
110 $fields[$i]['weight'] = variable_get($table_id .'_'. $fields[$i]['name'] .'_weight', $fields[$i]['weight']);
111 }
112 usort($fields, '_tapir_field_sort');
113
114 $output .= '<p>'. drupal_get_form('tapir_edit_form', $table_id, $fields) .'</p>';
115
116 if ($preview !== FALSE) {
117 $output .= '<p>'. l(t('View table'), $path .'/'. $table_id .'/view') .'</p>';
118 }
119
120 if (user_access('edit tables')) {
121 $output .= '<p>'. l(t('Table master list'), 'admin/settings/tapir') .'</p>';
122 }
123
124 return $output;
125 }
126
127 /**
128 * Build the settings form for an individual table.
129 */
130 function tapir_edit_form($table_id, $fields) {
131 foreach ($fields as $field) {
132 $form['field_'. $field['name']][$table_id .'_'. $field['name'] .'_name'] = array(
133 '#value' => check_plain($field['title'])
134 );
135 if ($field['locked'] == TRUE) {
136 $form['field_'. $field['name']][$table_id .'_'. $field['name'] .'_enabled'] = array(
137 '#type' => 'checkbox',
138 '#default_value' => $field['enabled'],
139 '#disabled' => TRUE,
140 '#value' => $field['enabled'],
141 );
142 }
143 else {
144 $form['field_'. $field['name']][$table_id .'_'. $field['name'] .'_enabled'] = array(
145 '#type' => 'checkbox',
146 '#default_value' => variable_get($table_id .'_'. $field['name'] .'_enabled', $field['enabled']),
147 );
148 }
149 $form['field_'. $field['name']][$table_id .'_'. $field['name'] .'_title'] = array(
150 '#type' => 'textfield',
151 '#default_value' => variable_get($table_id .'_'. $field['name'] .'_title', $field['title']),
152 '#size' => 32,
153 );
154 $form['field_'. $field['name']][$table_id .'_'. $field['name'] .'_weight'] = array(
155 '#type' => 'weight',
156 '#default_value' => variable_get($table_id .'_'. $field['name'] .'_weight', $field['weight']),
157 );
158 }
159
160 $form[$table_id . '_footer'] = array(
161 '#title' => t('Display the table footer.'),
162 '#type' => 'checkbox',
163 '#default_value' => variable_get($table_id . '_footer', TRUE),
164 );
165
166 return system_settings_form($form);
167 }
168
169 /**
170 * Theme the table settings form to display compactly in a table.
171 */
172 function theme_tapir_edit_form($form) {
173 foreach (element_children($form) as $field) {
174 if (substr($field, 0, 6) == 'field_') {
175 $row = array();
176 foreach (array_keys($form[$field]) as $key) {
177 if (substr($key, 0, 1) != '#') {
178 $row[] = drupal_render($form[$field][$key]);
179 }
180 }
181 $rows[] = $row;
182 }
183 }
184
185 $header = array(t('Field'), t('Enable'), t('Title'), t('Weight'));
186 $output .= '<p>'. theme('table', $header, $rows) .'</p>'
187 .'<p>'. drupal_render($form) .'</p>';
188
189 return $output;
190 }
191
192 /**
193 * Preview a table with its current settings, mostly used for debugging.
194 */
195 function tapir_view($table_id, $path) {
196 $output = '<p>'. tapir_get_table($table_id, NULL) .'</p>'
197 .'<p>'. l(t('Back to settings'), $path .'/'. $table_id) .'</p>';
198
199 return $output;
200 }
201
202 /*******************************************************************************
203 * Module and Helper Functions
204 ******************************************************************************/
205
206 /**
207 * Generate a list of defined tables that link to their edit forms.
208 *
209 * When no arguments are passed, this function generates a table of every
210 * table defined by TAPIr in the enabled modules. This is how the table is
211 * generated at admin/settings/tapir.
212 *
213 * The path for a table's settings page is auto-generated by TAPIr to be
214 * admin/settings/tapir/table_id where table_id is the ID of the table as
215 * defined by TAPIr. The path may be specified as any_path/table_id in a
216 * module's implementation of TAPIr hook_table_settings(). To generate a list
217 * of tables whose path (not including the table_id) matches a certain set of
218 * paths, you may pass them in as a list of string arguments.
219 *
220 * For example, if hook_table_settings() defines the path of a few tables as
221 * module/tables, you can generate a list of these tables by calling this
222 * function with the following line:
223 *
224 * $output = tapir_table_list('module/tables');
225 *
226 * You can generate a list for as many paths as you wish in this way.
227 *
228 * @param ...
229 * A list of paths for which you want to display the tables.
230 * @return
231 * A string containing the HTML of a themed table that displays available
232 * Table IDs as links to their settings pages along with a short description
233 * of the table as defined in the module's implementation of the TAPIr
234 * hook_table_settings().
235 */
236 function tapir_table_list() {
237 $args = func_get_args();
238
239 $tables = module_invoke_all('table_settings');
240 foreach ($tables as $table) {
241 $access = isset($table['access']) ? user_access($table['access']) : user_access('edit tables');
242 if ($access && (count($args) == 0 || in_array($table['path'], $args))) {
243 $path = isset($table['path']) ? $table['path'] : 'admin/settings/tapir';
244 $rows[] = array(l($table['id'], $path .'/'. $table['id']), $table['description']);
245 }
246 }
247
248 $header = array(t('Table ID'), t('Description'));
249 $output .= theme('table', $header, $rows);
250
251 return $output;
252 }
253
254 /**
255 * Build a table based on its current configuration for display on a page.
256 *
257 * The heart and soul of TAPIr, this function is responsible for gathering all
258 * the information about a table and serving it up properly. All a developer
259 * needs to do is pass along the ID of the table they want to build and this
260 * function will create the appropriate header and row data by including only
261 * enabled fields and ordering them by their weight.
262 *
263 * @param $table_id
264 * The ID of the table you wish to build. The ID must match the name of the
265 * builder function for that table, and it must have a corresponding entry
266 * in TAPIr hook_table_settings() for a settings form to be generated for it.
267 * @param $arg
268 * Argument that is passed on to the table building function. Must pass a
269 * variable set to NULL if this is not needed to get around a PHP 5 change. :(
270 * @return
271 * A string containing the HTML of the dynamically generated table.
272 */
273 function tapir_get_table($table_id, &$arg) {
274 if (!function_exists($table_id)) {
275 return '';
276 }
277
278 $fields = array_merge($table_id('fields', $arg), _tapir_invoke_all('table_alter', $table_id, 'fields', $arg));
279 if (!is_array($fields)) {
280 return '';
281 }
282 for ($i = 0; $i < count($fields); $i++) {
283 $fields[$i]['weight'] = variable_get($table_id .'_'. $fields[$i]['name'] .'_weight', $fields[$i]['weight']);
284 }
285 usort($fields, '_tapir_field_sort');
286 $table_data = $table_id('data', $arg);
287 if (!is_array($table_data)) {
288 $table_data = array();
289 }
290 $data = array_merge($table_data, _tapir_invoke_all('table_alter', $table_id, 'data', $arg));
291 $attributes = $table_id('attributes', $arg);
292
293 foreach ($fields as $field) {
294 if (variable_get($table_id .'_'. $field['name'] .'_enabled', $field['enabled']) == TRUE) {
295 if (!is_array($field['attributes'])) {
296 $field['attributes'] = array();
297 }
298 $header[] = array_merge(array('data' => variable_get($table_id .'_'. $field['name'] .'_title', $field['title'])), $field['attributes']);
299 for ($i = 0; $i < count($data[$field['name']]); $i++) {
300 $rows[$i]['data'][] = $data[$field['name']][$i];
301 }
302 }
303 }
304
305 if (isset($data['#attributes'])) {
306 for ($i = 0; $i < count($data['#attributes']); $i++) {
307 $rows[$i] = array_merge($rows[$i], $data['#attributes'][$i]);
308 }
309 }
310
311 if (isset($data['#footer']) && variable_get($table_id . '_footer', TRUE)) {
312 $rows[] = $data['#footer'];
313 }
314
315 $output = theme('table', $header, $rows, $attributes);
316
317 return $output;
318 }
319
320 function tapir_get_header($table_id, $arg){
321 if (!function_exists($table_id)) {
322 return;
323 }
324
325 $fields = array_merge($table_id('fields', $arg), _tapir_invoke_all('table_alter', $table_id, 'fields', $arg));
326 if (!is_array($fields)) {
327 return '';
328 }
329 for ($i = 0; $i < count($fields); $i++) {
330 $fields[$i]['weight'] = variable_get($table_id .'_'. $fields[$i]['name'] .'_weight', $fields[$i]['weight']);
331 }
332 usort($fields, '_tapir_field_sort');
333
334 foreach ($fields as $field) {
335 if (variable_get($table_id .'_'. $field['name'] .'_enabled', $field['enabled']) == TRUE) {
336 if (!is_array($field['attributes'])) {
337 $field['attributes'] = array();
338 }
339 $header[] = array_merge(array('data' => variable_get($table_id .'_'. $field['name'] .'_title', $field['title'])), $field['attributes']);
340 }
341 }
342 return $header;
343 }
344
345 /**
346 * Sort the fields of a table by weight using this function with usort().
347 */
348 function _tapir_field_sort($a, $b) {
349 if ($a['weight'] == $b['weight']) {
350 return 0;
351 }
352
353 return ($a['weight'] > $b['weight']) ? 1 : -1;
354 }
355
356 /**
357 * Perform an invoke_all but pass in the $data by reference.
358 */
359 function _tapir_invoke_all($hook, $table_id, $op, &$arg) {
360 $return = array();
361 foreach (module_implements($hook) as $module) {
362 $function = $module .'_'. $hook;
363 $result = $function($table_id, $op, $arg);
364 if (isset($result) && is_array($result)) {
365 $return = array_merge($return, $result);
366 }
367 else if (isset($result)) {
368 $return[] = $result;
369 }
370 }
371
372 return $return;
373 }

  ViewVC Help
Powered by ViewVC 1.1.2