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

Contents of /contributions/modules/tablemanager/tablemanager.module

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


Revision 1.91 - (show annotations) (download) (as text)
Sun Apr 20 17:39:29 2008 UTC (19 months, 1 week ago) by pobster
Branch: MAIN
CVS Tags: HEAD
Changes since 1.90: +15 -2 lines
File MIME type: text/x-php
Quite a few structural changes, just makes more sense to me when programming.
Fixed a couple of bugs when you try to do things without actually having any tables present.
Did a little more to the css module.
Fixed the watchdog links to the old /tablemanager path (now /tables/nn) - might have to rethink this as the list page is now an optional module.  Am thinking it'd be an idea to redirect to the admin screen with a GET tid in the url to display an administrative version of the table?
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * This module allows sufficiently permissioned users to create and maintain
7 * Drupal generated themeable tables.
8 *
9 */
10
11 /**
12 * Core Drupal hooks:
13 */
14
15 /**
16 * Implementation of hook_access().
17 */
18 function tablemanager_access($op, $node, $account) {
19 switch ($node) {
20 case 'tablemanager':
21 foreach (tablemanager_tables() as $no => $table) {
22 if ($op == 'create') {
23 if (user_access('administer tables') || user_access('create '. $table['name'] .' content')) {
24 return TRUE;
25 }
26 elseif (user_access('administer/ create own tables') && tablemanager_nodeadd()) {
27 return TRUE;
28 }
29 }
30 }
31 break;
32 case 'table':
33 if ($op == 'create') {
34 if (user_access('administer tables') || user_access('administer/ create own tables')) {
35 return TRUE;
36 }
37 }
38 break;
39 }
40 } // tablemanager_access
41
42 /**
43 * Implementation of hook_filter().
44 */
45 function tablemanager_filter($op, $delta = 0, $format = -1, $text = '') {
46 switch ($op) {
47 case 'list':
48 return (array(0 => t('Tablemanager filter')));
49 break;
50 case 'name':
51 return t('tablemanager filter');
52 break;
53 case 'description':
54 return t('Substitutes [tablemanager: ...] tags with the corresponding table');
55 break;
56 case 'no cache':
57 return TRUE;
58 break;
59 case 'prepare':
60 return $text;
61 break;
62 case 'process':
63 return _tablemanager_process_text($text);
64 break;
65 } // end switch
66 } // tablemanager_filter
67
68 /**
69 * Implementation of hook_filter_tips().
70 */
71 function tablemanager_filter_tips($delta, $format, $long = FALSE) {
72 if ($long) {
73 return t('You can embed tablemanager tables within your nodes using the following syntax:<br/>
74 [tablemanager:table_id,pagination,admin_links,column=?|start=?|end=?,attribute=?|attribute=?|...]<br />
75 All arguments bar table_id are optional:
76 <ul>
77 <li><strong>table_id</strong> = table number</li>
78 <li><strong>pagination</strong> = list length (numeric or NULL for all results)</li>
79 <li><strong>admin_links</strong> = TRUE or FALSE to enable/ disable administrative links</li>
80 <li><strong>column=?|start=?|end=?</strong> = can be in any order:<ul><li><strong>column</strong> = column to search on (numeric)</li><li><strong>start</strong> = match from</li><li><strong>end</strong> = match end (optional)</li></ul></li>
81 <li><strong>attributes</strong> = add as many attributes as you like separated by \'|\', for example border=2|bgcolor=yellow</li>
82 </ul>');
83 }
84 else {
85 return t('You can embed tablemanager tables within your nodes using the following syntax:<br/>[tablemanager:table_id,pagination,admin_links,column=?|start=?|end=?,attribute=?|attribute=?|...]');
86 }
87 } // tablemanager_filter_tips
88
89 /**
90 * Implementation of hook_help().
91 */
92 function tablemanager_help($path, $arg) {
93 switch ($path) {
94 case 'node/add/tablemanager': // Shown when you click through 'table entry' on the create content screen
95 return t('Choose from the following available tables:');
96 case 'node/add/tablemanager/%': // Shown when you add a new row
97 return t("<p>!desc</p>Add a new entry to table %table. Fill in your data and click submit.",
98 array(
99 '%table' => tablemanager_table_load($arg[3]),
100 '!desc' => tablemanager_desc(tablemanager_table_load($arg[3])),
101 )
102 );
103 }
104 } // tablemanager_help
105
106 /**
107 * Implementation of hook_menu().
108 */
109 function tablemanager_menu() {
110 $items = array();
111 // main configuration page of the basic tablemanager module
112 $items['admin/settings/tablemanager'] = array(
113 'title' => 'Tablemanager Settings',
114 'description' => 'Administer Tablemanagers tables/ users.',
115 'file' => 'includes/settings.inc',
116 'page callback' => 'drupal_get_form',
117 'page arguments' => array('tablemanager_instructions'),
118 'access arguments' => array('administer tablemanager settings'),
119 'type' => MENU_NORMAL_ITEM,
120 );
121 // the default local task (needed when other modules want to offer
122 // alternative tablemanager types and their own configuration page as local task)
123 $items['admin/settings/tablemanager/settings'] = array(
124 'title' => 'Instructions',
125 'type' => MENU_DEFAULT_LOCAL_TASK,
126 'weight' => -20,
127 );
128 $items['admin/settings/tablemanager/display'] = array(
129 'title' => 'Display',
130 'description' => 'Display settings for the administration screen.',
131 'file' => 'includes/settings.inc',
132 'page callback' => 'drupal_get_form',
133 'page arguments' => array('tablemanager_display_settings'),
134 'access arguments' => array('administer tablemanager settings'),
135 'type' => MENU_LOCAL_TASK,
136 'weight' => 1,
137 );
138 $items['admin/settings/tablemanager/bar_chart'] = array(
139 'title' => 'Bar Chart',
140 'description' => 'Display settings for the Bar Chart entry type.',
141 'file' => 'includes/settings.inc',
142 'page callback' => 'drupal_get_form',
143 'page arguments' => array('tablemanager_bar_chart_settings'),
144 'access arguments' => array('administer tablemanager settings'),
145 'type' => MENU_LOCAL_TASK,
146 'weight' => 2,
147 );
148 // Table delete + edit links
149 $items['admin/content/tablemanager/table_delete/%tablemanager_table'] = array(
150 'file' => 'includes/tables.inc',
151 'page callback' => 'drupal_get_form',
152 'page arguments' => array('tablemanager_table_delete', 4),
153 'access arguments' => array('administer tables'),
154 'type' => MENU_CALLBACK,
155 );
156 $items['admin/content/tablemanager/table_edit/%tablemanager_table'] = array(
157 'file' => 'includes/tables.inc',
158 'page callback' => 'drupal_get_form',
159 'page arguments' => array('tablemanager_table_edit', 4),
160 'access arguments' => array('administer tables'),
161 'type' => MENU_CALLBACK,
162 );
163 // Row delete + edit links
164 $items['tablemanager/edit/%tablemanager_row'] = array(
165 'file' => 'includes/rows.inc',
166 'page callback' => 'drupal_get_form',
167 'page arguments' => array('tablemanager_manage_row', 'edit', 2),
168 'access callback' => '_tablemanager_manage_row_access',
169 'access arguments' => array(2),
170 'type' => MENU_CALLBACK,
171 );
172 $items['tablemanager/%tablemanager_function'] = array(
173 'file' => 'includes/rows.inc',
174 'page callback' => 'drupal_get_form',
175 'page arguments' => array('tablemanager_row_delete', 1),
176 'load arguments' => array('%map', '%index'),
177 'access callback' => '_tablemanager_manage_row_access',
178 'access arguments' => array(1),
179 'type' => MENU_CALLBACK,
180 );
181 return $items;
182 } // tablemanager_menu
183
184 /**
185 * Implementation of hook_menu_alter().
186 */
187 function tablemanager_menu_alter(&$callbacks) {
188 // Overwrite the default callbacks for node/add page
189 $callbacks['node/add/table'] = array(
190 'title' => 'Table',
191 'description' => 'Create a new table.',
192 'title callback' => 'check_plain',
193 'page callback' => 'drupal_get_form',
194 'page arguments' => array('tablemanager_table_add'),
195 'access callback' => '_tablemanager_table_add_access',
196 'module' => 'tablemanager',
197 'file' => 'includes/tables.inc',
198 );
199 $callbacks['node/add/tablemanager']['page callback'] = 'drupal_get_form';
200 $callbacks['node/add/tablemanager']['page arguments'] = array('tablemanager_nodeadd_form');
201 $callbacks['node/add/tablemanager']['access callback'] = '_tablemanager_table_add_access';
202 $callbacks['node/add/tablemanager']['module'] = 'tablemanager';
203 unset($callbacks['node/add/tablemanager']['file']);
204 $callbacks['node/add/tablemanager/%tablemanager_table']['page callback'] = 'drupal_get_form';
205 $callbacks['node/add/tablemanager/%tablemanager_table']['page arguments'] = array('tablemanager_manage_row', 'add', 3);
206 $callbacks['node/add/tablemanager/%tablemanager_table']['access callback'] = '_tablemanager_table_add_access';
207 $callbacks['node/add/tablemanager/%tablemanager_table']['module'] = 'tablemanager';
208 $callbacks['node/add/tablemanager/%tablemanager_table']['file'] = 'includes/rows.inc';
209 } // tablemanager_menu_alter
210
211 /**
212 * Implementation of hook_node_info().
213 */
214 function tablemanager_node_info() {
215 $tables = tablemanager_nodeadd();
216 if ($tables) $description .= t('<dd>Choose from the following available tables: !tables', array('!tables' => tablemanager_nodeadd()));
217 return array('table' => array('name' => t('table'), 'module' => 'tablemanager', 'description' => t('Create a new table.')),
218 'tablemanager' => array('name' => t('table entry'), 'module' => 'tablemanager', 'description' => $description)
219 );
220 } // tablemanager_node_info
221
222 /**
223 * Implementation of hook_perm().
224 */
225 function tablemanager_perm() {
226 $perms = array('administer tables', 'list tables', 'administer/ create own tables');
227 foreach (tablemanager_tables() as $table) {
228 $perms[] = t('create @table content', array('@table' => $table['name']));
229 $perms[] = t('edit any @table content', array('@table' => $table['name']));
230 $perms[] = t('edit own @table content', array('@table' => $table['name']));
231 }
232 return $perms;
233 } // tablemanager_perm
234
235 /**
236 * Implementation of hook_theme().
237 */
238 function tablemanager_theme() {
239 return array(
240 'tablemanager_table_edit' => array(
241 'arguments' => array('form'),
242 'file' => 'includes/tables.inc',
243 ),
244 );
245 } // tablemanager_theme
246
247 /**
248 * Menu access callbacks:
249 */
250
251 /**
252 * Row add/ delete/ edit form access.
253 */
254 function _tablemanager_manage_row_access($info) {
255 // broken, but broken with a reason... I need to alter this to cope with multiple selections with both delete AND edit
256 global $user;
257 unset($flag);
258 $flag = $user->uid == $info->uid && user_access("edit own $info->name content") ? TRUE : $flag;
259 $flag = user_access('administer tables') || user_access("edit any $info->name content") ? TRUE : $flag;
260 $flag = $user->uid == $info->tableuid && user_access('administer/ create own tables') ? TRUE : $flag;
261 return $flag ? TRUE : FALSE;
262 } // _tablemanager_manage_row_access
263
264 /**
265 * Table add form access.
266 */
267 function _tablemanager_table_add_access() {
268 return user_access('administer tables') || user_access('administer/ create own tables') ? TRUE : FALSE;
269 } // _tablemanager_table_add_access
270
271 /**
272 * Table add form access.
273 */
274 function _tablemanager_table_manage_access() {
275 return user_access("create ". $table['name'] ." content") || user_access('administer tables') || user_access('administer/ create own tables') && $user->uid == $table['uid'] ? TRUE : FALSE;
276 } // _tablemanager_table_manage_access
277
278 /**
279 * Menu load arguments:
280 */
281
282 /**
283 * Row functions.
284 */
285 function tablemanager_function_load($op, $rows, $index) {
286 // remove 'tablemanager/(delete|edit)'
287 $rows = array_slice($rows, $index + 1);
288 unset($tid);
289 switch ($op) {
290 case 'delete':
291 case 'edit':
292 // 'rows' can't be empty
293 if (empty($rows)) return FALSE;
294 foreach ($rows as $row) {
295 // check all rows are numeric
296 if (!is_numeric($row)) {
297 return FALSE;
298 }
299 // check all rows exist and have same parent table
300 $fetch = db_result(db_query('SELECT tid FROM {tablemanager_data} WHERE id = %d', $row));
301 if (!$fetch) {
302 return FALSE;
303 }
304 if (!$tid) {
305 $tid = $fetch;
306 continue;
307 }
308 if ($tid != $fetch) {
309 return FALSE;
310 }
311 }
312 // if tests pass, return something useful
313 return array('tid' => $tid, 'rows' => $rows);
314 break;
315 }
316 return FALSE;
317 } // tablemanager_function_load
318
319 /**
320 * Ensures the table-xxx parameter passed through node/add/tablemanager + admin/content/tablemanager/table_delete/xxx is valid.
321 */
322 function tablemanager_table_load($string) {
323 $check = preg_match('/^table-(\d+)/i', $string, $match);
324 if (!$check) {
325 return FALSE;
326 }
327 if (tablemanager_table_exists($match[1])) {
328 return $match[1];
329 }
330 return FALSE;
331 } // tablemanager_table_load
332
333 /**
334 * Tablemanager functions:
335 */
336
337 /**
338 * Returns attributes if they're present.
339 */
340 function tablemanager_attributes(&$text, &$attributes) {
341 $pattern = "/\[(align|axis|bgcolor|class|style|valign) *= *(\w+)\]/i";
342 preg_match_all($pattern, $text, $matches);
343 foreach ($matches[0] as $no => $match) {
344 $attributes[] = array($matches[1][$no] => $matches[2][$no]);
345 $text = str_replace($matches[0][$no], '', $text);
346 }
347 } // tablemanager_attributes
348
349 /**
350 * Imports css file.
351 */
352 function tablemanager_css($tid = 0) {
353 // quick check that we've been passed a number
354 if (!is_numeric($tid)) {
355 return;
356 }
357 // check if a css file exists for given tid
358 $path = "/tablemanager_css/tablemanager_0.css";
359 print file_check_path($path);
360 if ($file = file_check_path($path)) {
361 drupal_add_css($file);
362 }
363 else {
364 // else import the default css file
365 drupal_add_css(drupal_get_path('module', 'tablemanager') .'/misc/tablemanager0.css');
366 }
367 } // tablemanager_css
368
369 /**
370 * Returns description field from specified table.
371 */
372 function tablemanager_desc($tid) {
373 $fetch = db_fetch_object(db_query('SELECT description FROM {tablemanager} WHERE tid = %d', $tid));
374 return $fetch->description;
375 } // tablemanager_desc
376
377 /**
378 * The main user hook to display a table.
379 */
380 function tablemanager_display_new($tid, $list_length = NULL, $params = array()) {
381 global $user, $pager_page_array, $pager_total_items, $pager_total;
382 // check parameters are valid
383 if (!$tid || !is_numeric($tid)) {
384 return 'Invalid Table ID';
385 }
386 if (isset($params['display'])) {
387 $params['display']['end'] = $params['display']['end'] ? $params['display']['end'] : $params['display']['start'];
388 }
389 elseif (!is_array($params['display'])) {
390 unset($params['display']);
391 }
392 if (!is_array($params['attributes'])) {
393 unset($params['attributes']);
394 }
395 $params['links'] = $params['links'] === "FALSE" ? FALSE : $params['links'];
396 $uid = tablemanager_fetch_uid($tid);
397 if ($list_length == NULL || $list_length == "NULL") {
398 $list_length = db_result(db_query('SELECT COUNT(id) FROM {tablemanager_data} WHERE tid = %d', $tid));
399 if (!$list_length) {
400 $list_length = 1;
401 }
402 }
403
404 // to-do...
405
406 } //tablemanager_display
407
408 /**
409 * The main user hook to display a table.
410 */
411 function tablemanager_display($tid, $list_length = NULL, $links = FALSE, $date = array(), $attrib = array()) {
412 global $user, $pager_page_array, $pager_total_items, $pager_total;
413 if (!$tid || !is_numeric($tid)) {
414 return 'Invalid Table ID';
415 }
416 if ($date) {
417 $date['end'] = $date['end'] ? $date['end'] : $date['start'];
418 }
419 elseif (!is_array($date)) {
420 $date = array();
421 }
422 if (!is_array($attrib)) {
423 $attrib = array();
424 }
425 $links = $links === "FALSE" ? FALSE : $links;
426 $uid = tablemanager_fetch_uid($tid);
427 if ($list_length == NULL || $list_length == "NULL") {
428 $list_length = db_result(db_query('SELECT COUNT(id) FROM {tablemanager_data} WHERE tid = %d', $tid));
429 if (!$list_length) {
430 $list_length = 1;
431 }
432 }
433 $fetch = db_query('SELECT tmd.id, tm.uid AS tableuid, tmd.uid, tm.name, tm.description, tm.header, tmd.data FROM {tablemanager} tm LEFT JOIN {tablemanager_data} tmd ON tm.tid = tmd.tid WHERE tm.tid = %d ORDER BY tmd.id', $tid);
434 $path = base_path() . drupal_get_path('module', 'tablemanager');
435 unset($flag);
436 $rows = array();
437 $types = array();
438 while ($result = db_fetch_object($fetch)) {
439 if (!$header) {
440 $header = unserialize($result->header);
441 foreach ($header as $a) {
442 if (is_array($a) && array_key_exists('type', $a)) {
443 array_push($types, $a['type']);
444 }
445 else {
446 array_push($types, '1');
447 }
448 }
449 if (variable_get('tablemanager_css', 0)) {
450 tablemanager_css();
451 }
452 $description = variable_get('tablemanager_descriptions', 0) ? $result->description : NULL;
453 switch (variable_get('tablemanager_names', NULL)) {
454 case "table number":
455 $name = "Table ". $tid;
456 break;
457 case "names":
458 $name = $result->name == $tid ? "Table ". $tid : $result->name;
459 break;
460 default:
461 $name = NULL;
462 break;
463 } // end switch
464 if ($links) {
465 if ($user->uid == $result->tableuid && user_access('administer/ create own tables') || user_access('edit own '. tablemanager_fetch_name($tid) .' content') || user_access('edit any '. tablemanager_fetch_name($tid) .' content') || user_access('administer tables')) {
466 if (in_array($user->uid, $uid) || user_access('edit any '. tablemanager_fetch_name($tid) .' content') || user_access('administer tables') || user_access('administer/ create own tables')) {
467 array_push($header, array('data' => t('Operations'), 'colspan' => '2'));
468 $flag = TRUE;
469 }
470 }
471 }
472 }
473 $data = unserialize($result->data);
474 unset($cancelrow);
475 if ($data) {
476 // Check for data types
477 unset($temp);
478 $col = 0;
479 foreach ($data as $a) {
480 $attributes = array();
481 if ($a == "||") { // Colspan
482 $last = array_pop($temp);
483 $up1 = $last['data'];
484 $up2 = $last['colspan'] ? $last['colspan'] : '1';
485 $up2++;
486 $temp[$col + 1] = array('data' => $up1, 'colspan' => $up2);
487 $col++;
488 continue;
489 }
490 if ($a || $a == "0") {
491 if ($date && $col == $date['column']-1) {
492 if (strcasecmp($a, $date['start']) < 0 || strcasecmp($a, $date['end']) > 0) {
493 $cancelrow = TRUE;
494 break;
495 }
496 }
497 $temp[$col + 1] = array('data' => tablemanager_process_cell($col, $a, $header[$col]));
498 }
499 else {
500 $temp[$col + 1] = array('data' => '');
501 }
502 $col++;
503 } // end foreach
504 } // end if
505 if ($cancelrow) {
506 unset($temp);
507 }
508 if ($temp && $links && $flag) {
509 if ($user->uid == $result->uid || user_access("edit any ". tablemanager_fetch_name($tid) ." content") || user_access('administer tables') || user_access('administer/ create own tables')) {
510 array_push($temp, l(t('edit'), "tablemanager/edit/$result->id"), l(t('delete'), "tablemanager/delete/$result->id"));
511 }
512 else {
513 array_push($temp, NULL, NULL);
514 }
515 }
516 if ($temp) {
517 $rows[] = array('data' => $temp);
518 }
519 } // end while
520 if (!$header) {
521 return 'Invalid Table ID';
522 }
523 if (!$rows) {
524 $output[] = array(array('data' => t('No table data available.'), 'colspan' => 2));
525 if ($flag) {
526 array_pop($header);
527 }
528 }
529 $temp = tablesort_get_order($header);
530 if ($temp['sql']) {
531 tablemanager_sort($rows, $temp['sql'], tablesort_get_sort($header));
532 }
533 // if results need to be paged we have to split $row (which contains ALL the results)
534 // into separate pages because we're having to avoid using 'pager' functionality
535 $count = 1;
536 $page = isset($_GET['page']) ? $_GET['page'] : '';
537 $pager_page_array = explode(',', $page);
538 $from = ($pager_page_array[$tid] * $list_length);
539 foreach ($rows as $temp) {
540 if ($count > $from && $count <= ($from + $list_length)) {
541 $output[] = $temp;
542 }
543 $count++;
544 } // end foreach
545 if (!array_key_exists('class', $attrib) && variable_get('tablemanager_css', 0)) {
546 $attrib['class'] = 'tablemanager';
547 $attrib['id'] = 'tablemanager-table-'. $tid;
548 }
549 if (variable_get('tablemanager_names', NULL)) $table .= "<h2>$name</h2>";
550 $table .= theme('table', $header, $output, $attrib, $description);
551 if ($links) {
552 $table .= ($user->uid == $result->tableuid && user_access('administer/ create own tables')) || user_access('create '. tablemanager_fetch_name($tid) .' content') || user_access('administer tables') ? '<p>'. l(t('Add new entry'), "node/add/tablemanager/table-$tid") : '';
553 }
554 // set global pager variables to correct values:
555 $pager_total_items[$tid] = count($rows);
556 $pager_total[$tid] = ceil($pager_total_items[$tid]/$list_length);
557 $pager_page_array[$tid] = max(0, min((int)$pager_page_array[$tid], ((int)$pager_total[$tid]) - 1));
558 $table .= theme('pager', array(), $list_length, $tid);
559 return $table;
560 } // tablemanager_display
561
562 /**
563 * Returns supplied rows in table form.
564 */
565 function tablemanager_display_rows($params = array()) {
566 if (!is_array($params)) {
567 return FALSE;
568 }
569 $full_header = unserialize(db_result(db_query('SELECT header FROM {tablemanager} WHERE tid = %d', $params['tid'])));
570 $fetch = db_query('SELECT id, uid, data FROM {tablemanager_data} WHERE tid = %d ORDER BY id', $params['tid']);
571 if (!isset($params['rows'])) {
572 // select a random row from table for an 'example' row
573 $temp = db_result(db_query('SELECT id FROM {tablemanager_data} WHERE tid = %d ORDER BY RAND()', $params['tid']));
574 // only append to rows array if a result is returned
575 if ($temp) {
576 $params['rows'][] = $temp;
577 }
578 }
579 if (!empty($params['rows'])) {
580 foreach ($params['rows'] as $row_id) {
581 $row = unserialize(db_result(db_query('SELECT data FROM {tablemanager_data} WHERE id = %d', $row_id)));
582 foreach ($row as $col => $cell) {
583 // Allow for colspan
584 if ($cell == "||") {
585 if ($col > 0) {
586 $last = array_pop($table[$row_id]);
587 $up1 = $last['data'];
588 $up2 = $last['colspan'] ? $last['colspan'] : '1';
589 $up2++;
590 $table[$row_id][] = array('data' => $up1, 'colspan' => $up2);
591 continue;
592 }
593 else {
594 // colspan is set for first cell so just blank it
595 $table[$row_id][$col] = array('data' => '');
596 continue;
597 }
598 }
599 $table[$row_id][$col] = array('data' => tablemanager_process_cell($col, $cell, $full_header[$col]));
600 }
601 }
602 }
603 // strip out everything but data field as display doesn't require any special viewing
604 foreach ($full_header as $info) {
605 $header[] = $info['data'];
606 }
607 return theme('table', $header, $table);
608 } // tablemanager_display_rows
609
610 /**
611 * Returns the user entered name for a table.
612 */
613 function tablemanager_fetch_name($tid) {
614 $result = db_fetch_object(db_query('SELECT name FROM {tablemanager} WHERE tid = %d', $tid));
615 return $result->name;
616 } // tablemanager_fetch_name
617
618 /**
619 * Returns an array of valid tablemanager entry types.
620 */
621 function tablemanager_fetch_types($descriptions = TRUE) {
622 if ($descriptions) {
623 return array(1 => NULL, 2 => t('Please enter a numeric value'), 3 => t('Please enter a valid date'), 4 => t("Please enter a full URL, such as 'http://www.yourlink.com'"), 5 => t('Please enter a valid email address'), 6 => t('Please enter a number between %d and %d'), 7 => t(''));
624 }
625 return array(1 => t('Text'), 2 => t('Numeric'), 3 => t('Date'), 4 => t('URL'), 5 => t('Email'), 6 => t('Bar Chart'), 7 => t('Selection'));
626 } // tablemanager_fetch_types
627
628 /**
629 * Returns an array of users who have entered data in a table.
630 */
631 function tablemanager_fetch_uid($tid) {
632 $uid = array();
633 $sql = db_query('SELECT uid FROM {tablemanager_data} WHERE tid = %d', $tid);
634 while ($result = db_fetch_object($sql)) {
635 $uid[] = $result->uid;
636 }
637 return $uid;
638 } // tablemanager_fetch_uid
639
640 /**
641 * Returns a list of table ids.
642 */
643 function tablemanager_load_tables() {
644 $tables = array();
645 $sql = db_query('SELECT tid, name FROM {tablemanager} ORDER BY tid');
646 while ($result = db_fetch_object($sql)) {
647 $tables[$result->tid] = $result->tid == $result->name ? "Table $result->tid" : "Table $result->tid: $result->name";
648 }
649 return $tables;
650 } // tablemanager_load_tables
651
652 /**
653 * Returns all table types for create content page.
654 */
655 function tablemanager_nodeadd() {
656 global $user;
657 unset($flag);
658 $items = '<dl>';
659 foreach (tablemanager_tables() as $no => $table) {
660 if (user_access('administer/ create own tables') && $user->uid == $table['uid'] || user_access('create '. $table['name'] .' content') || user_access('administer tables')) {
661 $table['name'] = $table['name'] == $table['tid'] ? 'Table '. $table['name'] : $table['name'];
662 $table['description'] = $table['description'] ? $table['description'] : '&nbsp;';
663 $items .= '<dt>'. l($table['name'], "node/add/tablemanager/table-$no") .'</dt><dd>'. $table['description'] .'</dd>';
664 $flag = TRUE;
665 }
666 }
667 $items .= '</dl>';
668 if (!$flag) {
669 return FALSE;
670 }
671 return $items;
672 } // tablemanager_nodeadd
673
674 /**
675 * Returns a form displaying table types for create content page.
676 */
677 function tablemanager_nodeadd_form() {
678 return array('display' => array('#type' => 'markup', '#value' => tablemanager_nodeadd()));
679 } // tablemanager_nodeadd_form
680
681 /**
682 * Function to apply entry types to cells.
683 */
684 function tablemanager_process_cell($col, $data, $args = array()) {
685 $attributes = array();
686 $path = drupal_get_path('module', 'tablemanager');
687 switch ($args['type']) {
688 default: // Text + Numeric
689 tablemanager_attributes($data, $attributes);
690 $temp = $data;
691 if ($attributes) {
692 foreach ($attributes as $attribute) {
693 foreach ($attribute as $key => $value) {
694 $temp[$key] = $value;
695 }
696 }
697 }
698 break;
699 case "3": // Date
700 $exp = explode('/', $data);
701 $temp = "<!-- $data -->". date(variable_get('tablemanager_date_format', 'jS F Y'), mktime(0, 0, 0, $exp['1'], $exp['2'], $exp['0']));
702 break;
703 case "4": // URL
704 // To do - split this nicely into display name and link rather than one entry for both
705 $temp = l($data, $data);
706 break;
707 case "5": // Email
708 // To do - split this nicely into display name and email rather than one entry for both
709 $temp = l($data, 'mailto:'. $data);
710 break;
711 case "6": // Bar Chart
712 $temp = theme_image($path .'/misc/'. variable_get('tablemanager_bc_colour', 'red') .'-bar.png', $data, '', array('style' => 'vertical-align: middle; margin: 5px 5px 5px 0;', 'width' => $data, 'height' => '16'), FALSE);
713 if (variable_get('tablemanager_bc_end', 1)) {
714 $temp = $temp . $data;
715 }
716 break;
717 case "7": // Selection
718 $choices = explode("\n", $args['choices']);
719 $temp = $choices[$data];
720 break;
721 case "8": // Node Reference
722 break;
723 case "9": // User Reference
724 break;
725 } // end switch
726 return $temp;
727 } // tablemanager_process_cell
728
729 /**
730 * Function to match and process the tablemanager filter.
731 */
732 function _tablemanager_process_text($text) {
733 $pattern = "/\[tablemanager:(\d+,?\w*,?\w*,?\w*=?\d*\|?\w*=?\w*\|?\w*=?\w*,?.*?)\]/i";
734 if (preg_match_all($pattern, $text, $matches)) {
735 $date = array();
736 $attrib = array();
737 foreach ($matches[1] as $no => $args) {
738 $arg = explode(',', $args);
739 if ($arg[3]) {
740 $params = explode('|', $arg[3]);
741 foreach ($params as $param) {
742 $temp = explode('=', $param);
743 $date[trim($temp[0])] = trim(trim($temp[1]), '"');
744 }
745 }
746 if ($arg[4]) {
747 $params = explode('|', $arg[4]);
748 foreach ($params as $param) {
749 $temp = explode('=', $param);
750 $attrib[trim($temp[0])] = trim($temp[1]);
751 }
752 }
753 $text = str_replace($matches[0][$no], tablemanager_display(trim($arg[0]), trim($arg[1]), trim($arg[2]), $date, $attrib), $text);
754 }
755 }
756 return $text;
757 } // _tablemanager_process_text
758
759 /**
760 * Ensures the row parameter passed through tablemanager/xxx is valid.
761 */
762 function tablemanager_row_load($id) {
763 if (!is_numeric($id)) {
764 return FALSE;
765 }
766 $fetch = db_fetch_object(db_query('SELECT tm.tid, tm.uid AS tableuid, tm.name, tmd.uid, tm.header, tmd.data, tmd.format
767 FROM {tablemanager} tm
768 INNER JOIN {tablemanager_data} tmd ON tm.tid = tmd.tid
769 WHERE tmd.id = %d', $id));
770 if ($fetch) {
771 return $fetch;
772 }
773 else {
774 return FALSE;
775 }
776 } // tablemanager_row_load
777
778 /**
779 * Sets to a valid table if current table is deleted.
780 */
781 function tablemanager_set_tid($dir = 'DESC') {
782 $fetch = db_fetch_object(db_query('SELECT tid FROM {tablemanager} ORDER BY tid %s', $dir));
783 $tid = $fetch->tid;
784 variable_set('tablemanager_table', $tid);
785 return;
786 } // tablemanager_set_tid
787
788 /**
789 * Sort function.
790 */
791 function tablemanager_sort(&$data, $field, $sort = "asc") {
792 $next = $field + 1;
793 $code = array_key_exists($next, $data) ? "if (0 == (\$cmp = strnatcasecmp(strip_tags_c(\$a['data']['$field']['data']), strip_tags_c(\$b['data']['$field']['data'])))) return strnatcasecmp(strip_tags_c(\$a['data']['$next']['data']), strip_tags_c(\$b['data']['$next']['data'])); else return \$cmp;" : "return strnatcasecmp(strip_tags_c(\$a['data']['$field']['data']), strip_tags_c(\$b['data']['$field']['data']));";
794 if ($sort == "asc") {
795 uasort($data, create_function('$a,$b', $code));
796 }
797 else {
798 uasort($data, create_function('$b,$a', $code));
799 }
800 } // tablemanager_sort
801
802 /**
803 * Checks to see if a table actually exists.
804 */
805 function tablemanager_table_exists($tid) {
806 $fetch = db_result(db_query('SELECT tid FROM {tablemanager} WHERE tid = %d', $tid));
807 if ($fetch) {
808 return TRUE;
809 }
810 return FALSE;
811 } // tablemanager_table_exists
812
813 /**
814 * Returns an array containing all tables.
815 */
816 function tablemanager_tables() {
817 $tables = array();
818 $sql = db_query('SELECT * FROM {tablemanager}');
819 while ($result = db_fetch_object($sql)) {
820 $tables[$result->tid] = array('tid' => $result->tid, 'uid' => $result->uid, 'name' => $result->name, 'description' => $result->description, 'header' => $result->header);
821 }
822 return $tables;
823 } // tablemanager_tables
824
825 /**
826 * Validates string.
827 */
828 function tablemanager_validatestring($word, $name) {
829 if (eregi('[^a-z 0-9_-]', $word)) {
830 form_set_error($name, t('The specified %name contains one or more illegal characters.<br />Special characters except space, dash (-) and underscore (_) are not allowed.', array('%name' => $name)));
831 }
832 return;
833 } // tablemanager_validatestring
834
835 /**
836 * Other functions:
837 */
838
839 /**
840 * Comments aren't sortable, so we temporarily alter them to sort on them.
841 */
842 function strip_tags_c($string, $allowed_tags = '<!-->') {
843 $allow_comments = (strpos($allowed_tags, '<!-->') !== false );
844 if ($allow_comments) {
845 $string = str_replace(array('<!--', '-->'), array('&lt;!--', '--&gt;'), $string);
846 $allowed_tags = str_replace('<!-->', '', $allowed_tags);
847 }
848 $string = strip_tags($string, $allowed_tags);
849 if ($allow_comments) {
850 $string = str_replace(array('&lt;!--', '--&gt;'), array('<!--', '-->'), $string);
851 }
852 return $string;
853 } // strip_tags_c

  ViewVC Help
Powered by ViewVC 1.1.2