Stripping CVS keywords
[project/data.git] / data_ui / data_ui.admin.inc
CommitLineData
2ace3f91 1<?php
3a16de16
AB
2/**
3 * @file
4 * Admin UI functions.
5 */
2ace3f91
AB
6
7/**
8 * Overview over all tables.
9 */
10function data_ui_overview() {
2ace3f91
AB
11 $tables = data_get_all_tables();
12 $rows = array();
13 foreach ($tables as $table) {
72ba49d9
AB
14 // Build operations depending on configuration status.
15 $operations = array();
16 if ($table->get('export_type') == EXPORT_IN_CODE) {
17 $status = t('Default');
18 $operations[] = l(t('Override'), 'admin/content/data/edit/'. $table->get('name'));
19 }
20 else if ($table->get('export_type') == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) {
21 $status = t('Overridden');
22 $operations[] = l(t('Edit'), 'admin/content/data/edit/'. $table->get('name'));
23 $operations[] = l(t('Revert'), 'admin/content/data/revert/'. $table->get('name'));
24 }
25 else {
26 $status = t('Normal');
27 $operations[] = l(t('Edit'), 'admin/content/data/edit/'. $table->get('name'));
70a595f9 28 $operations[] = l(t('Drop'), 'admin/content/data/drop/'. $table->get('name'));
72ba49d9
AB
29 }
30 if (module_exists('ctools')) {
31 $operations[] = l(t('Export'), 'admin/content/data/export/'. $table->get('name'));
32 }
33
34 $row = array();
35 $row[] = $table->get('name');
36 $row[] = $status;
37 $row[] = implode(' | ', $operations);
38
39 $rows[] = $row;
2ace3f91 40 }
03608987 41 $rows[] = array(
f48c6947 42 l(t('Create new table'), 'admin/content/data/create'),
03608987 43 '&nbsp;',
12aae5b6 44 '&nbsp;',
03608987 45 );
72ba49d9 46 $header = array(t('Name'), t('Status'), t('Operations'));
2ace3f91
AB
47 return theme('table', $header, $rows);
48}
49
50/**
d4c26f4b
AB
51 * Comparison page.
52 */
53function data_ui_compare() {
54 $rows = array();
55 $tables = data_get_all_tables();
56 foreach ($tables as $table) {
57 $row = array();
58 $comp = $table->compareSchema();
59 $row[] = $table->get('name');
2c44bed5
AB
60 $status = $comp['status'];
61 if ($status != 'same') {
62 $status .= ' - '. l(t('adjust'), 'admin/content/data/compare/'. $table->get('name'));
d4c26f4b 63 }
2c44bed5
AB
64 $row[] = $status;
65 $row[] = empty($comp['warning']) ? '-' : $comp['warning'];
d4c26f4b
AB
66 $rows[] = $row;
67 }
2c44bed5 68 $header = array(t('Name'), t('Status'), t('Warnings'));
d4c26f4b
AB
69 return theme('table', $header, $rows);
70}
71
72/**
2c44bed5
AB
73 * Adjust table schema form: Present the user with the difference between schema information and
74 * the actual schema in the Database and offer three options:
75 *
76 * - Adjust schema info,
77 * - adjust database or
78 * - leave it.
79 */
80function data_ui_adjust_form(&$form_state, $table) {
81 drupal_set_title(t('Adjust !table', array('!table' => $table->get('name'))));
82 $comparison = $table->compareSchema();
83
84 $form = array();
85 $form['#redirect'] = 'admin/content/data/compare';
86 $form['#table'] = $table;
12aae5b6 87 $form['#comparison'] = $comparison;
2c44bed5
AB
88 $form['comparison'] = array(
89 '#type' => 'fieldset',
90 '#title' => t('Comparison'),
91 );
92 $form['comparison']['comparison']['#value'] = theme('data_ui_schema_compare_table', $comparison);
93
94 if ($comparison['status'] == 'different') {
95 $form['update_schema'] = array(
96 '#type' => 'fieldset',
97 '#title' => t('Option 1: Update schema information'),
98 );
99 $form['update_schema']['description'] = array(
100 '#value' => t('<p>This option will update the schema information about this table.</p>'),
101 );
102 $form['update_schema']['submit'] = array(
103 '#type' => 'submit',
104 '#submit' => array('data_ui_adjust_form_submit_update_schema'),
105 '#value' => t('Update schema information'),
106 );
107 $form['alter_table'] = array(
108 '#type' => 'fieldset',
109 '#title' => t('Option 2: Alter table'),
110 );
111 $form['alter_table']['description'] = array(
112 '#value' => t('<p>Review the changes above carefully!
113 This option will alter the database table and can very
114 easily cause data loss.</p>'),
115 );
116 $form['alter_table']['submit'] = array(
117 '#type' => 'submit',
118 '#submit' => array('data_ui_adjust_form_submit_alter_table'),
119 '#value' => t('Alter table'),
120 );
121 }
122 elseif ($comparison['status'] == 'missing') {
123 $form['alter_table'] = array(
124 '#type' => 'fieldset',
125 '#title' => t('Create table'),
126 );
127 $form['alter_table']['description'] = array(
128 '#value' => t('<p>Create a new table from schema information.</p>'),
129 );
130 $form['alter_table']['submit'] = array(
131 '#type' => 'submit',
132 '#submit' => array('data_ui_adjust_form_submit_create_table'),
133 '#value' => t('Create table'),
134 );
135 }
136 $form['cancel'] = array(
137 '#type' => 'fieldset',
138 '#title' => t('Don\'t change anything'),
139 );
140 $form['cancel']['cancel'] = array(
141 '#type' => 'submit',
142 '#value' => t('Cancel'),
143 );
144 return $form;
145}
146
147/**
148 * Submit handler for data_ui_adjust_form().
149 */
150function data_ui_adjust_form_submit_update_schema($form, &$form_state) {
151 $table = $form['#table'];
152 $schema = schema_invoke('inspect');
153 if (isset($schema[$table->get('name')])) {
154 $table->update(array('table_schema' => $schema[$table->get('name')]));
155 drupal_set_message(t('Updated schema for !table', array('!table' => $table->get('name'))));
156 }
157 else {
158 drupal_set_message(t('Error updating schema'), 'error');
159 }
160}
161
162/**
163 * Submit handler for data_ui_adjust_form().
164 */
165function data_ui_adjust_form_submit_alter_table($form, &$form_state) {
12aae5b6
AB
166 $resolved = $resolved = array();
167 if (isset($form['#comparison']['reasons'])) {
168 foreach ($form['#comparison']['reasons'] as $field_reason) {
7fa8a6aa 169 if (_data_ui_alter_table($form['#table'], $field_reason)) {
12aae5b6
AB
170 $resolved[] = $field_reason;
171 }
172 else {
173 $unresolved[] = $field_reason;
174 }
175 }
176 }
177 if (count($resolved)) {
178 drupal_set_message(t('Resolved') . theme_item_list($resolved));
179 }
180 if (count($unresolved)) {
181 drupal_set_message(t('Could not resolve') . theme_item_list($unresolved), 'error');
182 }
2c44bed5
AB
183}
184
185/**
186 * Submit handler for data_ui_adjust_form().
187 */
188function data_ui_adjust_form_submit_create_table($form, &$form_state) {
189 $table = $form['#table'];
190 $ret = array();
191 db_create_table($ret, $table->get('name'), $table->get('table_schema'));
b7f7b1e8 192 drupal_get_schema($table->get('name'), TRUE);
2c44bed5
AB
193 if ($ret[0]['success']) {
194 drupal_set_message(t('Created table !table', array('!table' => $table->get('name'))));
195 }
196 else {
197 drupal_set_message(t('Error creating table'), 'error');
198 }
199}
200
201/**
e6f88837 202 * Form callback for create table form.
03608987 203 */
dcdaddd1 204function data_ui_create_form(&$form_state) {
03608987 205 $form = array();
120dbbcb 206 if (!$form_state['storage']['field_num']) {
f48c6947
AB
207 $form['name'] = array(
208 '#type' => 'textfield',
209 '#title' => t('Table name'),
210 '#description' => t('Machine readable name of the table - e. g. "my_table". Must only contain lower case letters and _.'),
211 '#required' => TRUE,
212 );
213 $form['title'] = array(
214 '#type' => 'textfield',
215 '#title' => t('Table title'),
216 '#description' => t('Natural name of the table - e. g. "My Table".'),
217 );
218 $form['field_num'] = array(
219 '#type' => 'textfield',
220 '#title' => t('Number of fields'),
221 '#description' => t('The number of fields this table should contain.'),
222 '#default_value' => 1,
223 '#required' => TRUE,
224 );
225 $form['submit'] = array(
226 '#type' => 'submit',
227 '#value' => t('Next'),
228 );
229 }
120dbbcb
AB
230 else {
231 $form['help']['#value'] = t('Define the fields of the new table.');
232 $form['fields'] = array(
233 '#tree' => TRUE,
234 );
235 for ($i = 0; $i < $form_state['storage']['field_num']; $i++) {
236 $form['fields']['field_'. $i] = _data_ui_field_form(TRUE);
237 }
238 $form['submit'] = array(
239 '#type' => 'submit',
240 '#value' => t('Create'),
241 );
242 }
03608987
AB
243 return $form;
244}
245
246/**
e6f88837 247 * Validate handler for create table form.
03608987 248 */
dcdaddd1 249function data_ui_create_form_validate($form, &$form_state) {
03608987
AB
250 if (data_get_table(data_name($form_state['values']['name']))) {
251 form_set_error('name', t('Name is already taken.'));
252 }
f48c6947
AB
253 if (isset($form_state['values']['field_num'])) {
254 if (is_numeric($form_state['values']['field_num'])) {
255 if ($form_state['values']['field_num'] < 1) {
256 form_set_error('field_num', t('At least one field must be created.'));
257 }
258 }
259 else {
260 form_set_error('field_num', t('Enter a number greater than 0.'));
261 }
262 }
263 // Check for dupes.
264 if (isset($form_state['values']['fields'])) {
265 $names = array();
266 foreach ($form_state['values']['fields'] as $field) {
267 if (is_numeric($names[$field['name']])) {
268 form_set_error('name', t('Names can\'t be numbers.'));
269 }
270 if (!isset($names[$field['name']])) {
271 $names[$field['name']] = $field['name'];
272 }
273 else {
274 form_set_error('name', t('Names must be unique.'));
275 }
276 }
277 }
03608987
AB
278}
279
280/**
e6f88837 281 * Submit handler for create table form.
2ace3f91 282 */
dcdaddd1 283function data_ui_create_form_submit($form, &$form_state) {
19aba4f3 284
f48c6947
AB
285 if (isset($form_state['values']['field_num'])) {
286 $form_state['storage'] = $form_state['values'];
287 }
288 elseif (isset($form_state['values']['fields'])) {
120dbbcb
AB
289
290 // Create a schema from user input.
19aba4f3 291 $schema = $index = $primary = $meta = array();
f48c6947 292 foreach ($form_state['values']['fields'] as $field) {
19aba4f3
AB
293 $schema['fields'][$field['name']] = data_build_field_definition($field);
294 $meta['fields'][$field['name']]['label'] = $field['label'];
295
296 // Limit index if field type is text.
120dbbcb 297 if (!empty($field['index'])) {
19aba4f3 298 $index[$field['name']] = data_get_index_definition($field['name'], $field['type']);
120dbbcb
AB
299 }
300 if (!empty($field['primary'])) {
19aba4f3 301 $primary[] = data_get_pk_definition($field['name'], $field['type']);
120dbbcb 302 }
f48c6947 303 }
120dbbcb 304 $schema['indexes'] = $index;
ad756c3e 305 $schema['primary key'] = $primary;
19aba4f3
AB
306
307 // Create table.
f48c6947 308 if ($table = data_create_table(data_name(trim($form_state['storage']['name'])), $schema, trim($form_state['storage']['title']))) {
19aba4f3 309 $meta = $table->update(array('meta' => $meta));
f48c6947
AB
310 drupal_set_message(t('Created table !table', array('!table' => $table->get('name'))));
311 }
312 else {
313 drupal_set_message(t('Error creating table'), 'error');
314 }
19aba4f3 315
120dbbcb
AB
316 // Unset storage to enable redirect.
317 unset($form_state['storage']);
318 $form_state['redirect'] = 'admin/content/data';
f48c6947 319 }
03608987
AB
320}
321
72ba49d9
AB
322/**
323 * Form callback for revert table form.
324 */
325function data_ui_revert_form(&$form_state, $table) {
326 $form = array();
327 $form['#redirect'] = 'admin/content/data';
328 $form['#table'] = $table;
329
330 return confirm_form($form,
331 t('Revert this table?'),
332 'admin/content/data',
333 t('Are you sure you would like to revert table !table? This will reset all information about this table its definition in code. This action cannot be undone.', array('!table' => $table->get('name'))),
334 t('Revert'), t('Cancel')
335 );
336}
337
338/**
fed65212 339 * Submit handler for data_ui_revert_form().
72ba49d9
AB
340 */
341function data_ui_revert_form_submit($form, &$form_state) {
342 $table = $form['#table'];
343 $table->revert();
344}
03608987
AB
345
346/**
fed65212 347 * Form callback for drop table form.
03608987
AB
348 */
349function data_ui_drop_form(&$form_state, $table) {
350 $form = array();
351 $form['#redirect'] = 'admin/content/data';
352 $form['#table'] = $table;
353
354 return confirm_form($form,
355 t('Drop this table?'),
0ef5d565 356 'admin/content/data',
03608987
AB
357 t('Are you sure you would like to drop table !table? This action cannot be undone.', array('!table' => $table->get('name'))),
358 t('Drop'), t('Cancel')
359 );
360}
361
362/**
363 * Submit handler for data_ui_drop_form().
364 */
365function data_ui_drop_form_submit($form, &$form_state) {
366 $table = $form['#table'];
f48c6947 367 data_drop_table($table->get('name'));
03608987
AB
368}
369
370/**
371 * Form callback for editing a table.
372 */
373function data_ui_edit_form(&$form_state, $table) {
2ace3f91
AB
374 drupal_set_title(t('Data table !table', array('!table' => $table->get('name'))));
375 $schema = $table->get('table_schema');
376 $meta = $table->get('meta');
377
378 $form = array();
379 // Keep table.
380 $form['table'] = array(
381 '#type' => 'value',
382 '#value' => $table,
383 );
384
385 // Existing fields.
386 $form['fields'] = array('#tree' => TRUE);
cff26223
AB
387 if (isset($schema['fields'])) {
388 foreach ($schema['fields'] as $field_name => $field) {
389 $form['fields'][$field_name] = array();
390 $form['fields'][$field_name]['selected'] = array(
391 '#type' => 'checkbox',
392 );
393 $form['fields'][$field_name]['name'] = array('#value' => $field_name);
394 $form['fields'][$field_name]['label'] = array(
395 '#type' => 'textfield',
396 '#size' => 20,
397 '#default_value' => $meta['fields'][$field_name]['label'],
398 );
399 $form['fields'][$field_name]['type'] = array(
400 '#type' => 'select',
19aba4f3 401 '#options' => data_get_field_types(),
cff26223
AB
402 '#default_value' => $field['type'],
403 );
404 $form['fields'][$field_name]['unsigned'] = array(
405 '#type' => 'checkbox',
406 '#default_value' => $field['unsigned'],
407 );
120dbbcb
AB
408 $form['fields'][$field_name]['index'] = array(
409 '#type' => 'checkbox',
ad756c3e 410 '#default_value' => isset($schema['indexes'][$field_name]),
120dbbcb
AB
411 );
412 $form['fields'][$field_name]['primary'] = array(
413 '#type' => 'checkbox',
ad756c3e 414 '#default_value' => isset($schema['primary key']) ? in_array($field_name, $schema['primary key']) : FALSE,
120dbbcb 415 );
0cd5c3d6
AB
416 if ($join = _data_ui_get_join($meta['join'], $field_name)) {
417 $join = $join['left_table'] .'.'. $join['left_field'];
3c77dfc3
AB
418 }
419 else {
0cd5c3d6 420 $join = t('<none>');
3c77dfc3 421 }
0cd5c3d6
AB
422 $join = l($join, 'admin/content/data/edit/'. $table->get('name') .'/join/'.$field_name);
423 $form['fields'][$field_name]['join']['#value'] = $join;
cff26223 424 }
2ace3f91
AB
425 }
426
427 // Add a new field.
f48c6947 428 $form['new'] = _data_ui_field_form();
0ef5d565
AB
429 $form['new']['primary'] = array(
430 '#type' => 'markup',
431 '#value' => '&nbsp;',
432 );
0cd5c3d6
AB
433 $form['new']['join'] = array(
434 '#type' => 'markup',
435 '#value' => '&nbsp;',
436 );
2ace3f91
AB
437 $form['new']['add'] = array(
438 '#type' => 'submit',
439 '#value' => t('Add new'),
440 );
441
442 // Bulk operations.
443 $options = array(
444 t('Bulk operations'),
445 'delete' => t('Delete all selected'),
446 );
447 $form['bulk_operation'] = array(
448 '#type' => 'select',
449 '#options' => $options,
450 );
451 $form['submit'] = array(
452 '#type' => 'submit',
453 '#value' => t('Save'),
454 );
455
456 return $form;
457}
458
459/**
460 * Submit form.
461 */
03608987 462function data_ui_edit_form_submit($form, &$form_state) {
2ace3f91
AB
463 $table = $form_state['values']['table'];
464 $schema = $table->get('table_schema');
465
3ab8d2b4
AB
466 if (!$table->exists()) {
467 drupal_set_message(t('Table does not exist in database.'), 'error');
468 if (module_exists('schema')) {
469 drupal_set_message(t('Go to !compare to resolve conflicts.', array('!compare' => l(t('Compare schemas'), 'admin/content/data/compare'))), 'error');
470 }
471 return;
472 }
473
2ace3f91
AB
474 if ($form_state['clicked_button']['#value'] == t('Save')) {
475 $fields = $schema['fields'];
476 $new_fields = $form_state['values']['fields'];
44da723a 477
ad756c3e
AB
478 $new_index = array();
479 $new_primary_key = array();
480
03608987 481 if (empty($form_state['values']['bulk_operation']) && isset($fields)) {
44da723a
AB
482
483 // Convert schema.
484 foreach ($fields as $field_name => $field) {
485 if ($new_spec = _data_ui_changed($new_fields[$field_name], $field)) {
486 $table->changeField($field_name, $new_spec);
487 drupal_set_message(t('Changed field !field_name', array('!field_name' => $field_name)));
488 }
ad756c3e
AB
489 if ($new_fields[$field_name]['index']) {
490 $new_index[] = $field_name;
491 }
492 if ($new_fields[$field_name]['primary']) {
493 $new_primary_key[] = $field_name;
494 }
2ace3f91 495 }
d57cb606
AB
496 if (!$table->changeIndex($new_index)) {
497 drupal_set_message(t('Error changing indexes'), 'error');
498 }
499 if (!$table->changePrimaryKey($new_primary_key)) {
500 drupal_set_message(t('Error changing primary key'), 'error');
501
502 }
ad756c3e 503
44da723a
AB
504 // Update meta data.
505 $meta = $table->get('meta');
506 foreach ($new_fields as $field_name => $field) {
507 $meta['fields'][$field_name]['label'] = $field['label'];
508 }
509 $table->update(array('meta' => $meta));
d57cb606 510 drupal_set_message(t('Saved changes'));
2ace3f91 511 }
44da723a
AB
512 else {
513 // Bulk updates.
514 switch ($form_state['values']['bulk_operation']) {
515 case 'delete':
516 foreach ($new_fields as $field_name => $field) {
f48c6947
AB
517 if (!empty($field['selected'])) {
518 // One field must stay.
519 $schema = $table->get('table_schema');
520 if (count($schema['fields']) > 1) {
03608987
AB
521 $table->dropField($field_name);
522 drupal_set_message(t('Deleted field !field_name', array('!field_name' => $field_name)));
523 }
f48c6947
AB
524 else {
525 drupal_set_message('You cannot delete all fields from a table, drop the table instead.', 'error');
526 }
44da723a 527 }
2ace3f91 528 }
44da723a
AB
529 break;
530 }
2ace3f91
AB
531 }
532 }
533 elseif ($form_state['clicked_button']['#value'] == t('Add new')) {
534 $new = $form_state['values']['new'];
19aba4f3 535 $spec = data_build_field_definition($new);
3ab8d2b4
AB
536 if ($table->addField($new['name'], $spec)) {
537 drupal_set_message(t('Added field !field', array('!field' => $new['name'])));
538 }
539 else {
540 drupal_set_message(t('Error creating field'), 'error');
541 return;
542 }
0ef5d565 543 if (!empty($new['index'])) {
3ab8d2b4
AB
544 if ($table->addIndex($new['name'])) {
545 drupal_set_message(t('Added index for field !field', array('!field' => $new['name'])));
546 }
547 else {
548 drupal_set_message(t('Error adding index'), 'error');
549 return;
550 }
03608987 551 }
2ace3f91
AB
552 $meta = $table->get('meta');
553 $meta['fields'][$new['name']]['label'] = $new['label'];
554 $table->update(array('meta' => $meta));
555 }
556}
557
f48c6947 558/**
0cd5c3d6
AB
559 * Join form.
560 */
561function data_ui_join_form(&$form_state, $table, $field_name) {
562 // drupal_set_title(t('Join field'));
563
564 $schema = $table->get('table_schema');
565 $meta = $table->get('meta');
566
567 // Validate input.
568 if (!isset($field_name) || !isset($schema['fields'][$field_name])) {
569 drupal_set_message(t('Invalid field.'), 'error');
570 drupal_goto('admin/content/data/edit/'. $table->get('name'));
571 }
572
573 // List all tables that schema API knows about as optoins.
574 // @todo: This is a looong list - needs some AHAH to scale better.
575 $table_schemas = drupal_get_schema();
576 ksort($table_schemas);
577 $options = array();
578 foreach ($table_schemas as $table_name => $schema) {
579 if ($table->get('name') != $table_name) {
580 foreach ($schema['fields'] as $name => $info) {
581 $options[$table_name][$table_name .'.'. $name] = $table_name .'.'. $name;
582 }
583 }
584 }
585
586 // Build form.
587 $form = array();
588 $form['#table'] = $table;
589 $form['#field_name'] = $field_name;
590 $form['#redirect'] = 'admin/content/data/edit/'. $table->get('name');
591 $join = _data_ui_get_join($meta['join'], $field_name);
592 $form['#original_join'] = $join;
593 $form['left'] = array(
594 '#type' => 'select',
595 '#title' => t('Join !table_field to', array('!table_field' => $table->get('name') .'.'. $field_name)),
596 '#options' => $options,
597 '#default_value' => $join['left_table'] .'.'. $join['left_field'],
598 );
599 $form['inner_join'] = array(
600 '#type' => 'radios',
601 '#title' => t('Join type'),
602 '#options' => array(t('Left join'), t('Inner join')),
603 '#default_value' => $join['inner_join'] ? $join['inner_join'] : 0,
604 );
605
606 // Use confirm form for its formatting.
607 $form = confirm_form($form,
608 t('Join field'),
609 'admin/content/data/edit/'. $table->get('name'),
610 '',
611 t('Save'), t('Cancel')
612 );
613 $form['actions']['delete'] = array(
614 '#type' => 'submit',
615 '#value' => t('Delete'),
616 '#submit' => array('data_ui_join_form_submit_delete'),
617 );
618 krsort($form['actions']);
619
620 return $form;
621}
622
623/**
624 * Submit handler for data_ui_join_form().
625 */
626function data_ui_join_form_submit($form, &$form_state) {
627 list($left_table, $left_field) = explode('.', $form_state['values']['left']);
628 $form['#table']->link($left_table, $left_field, $form['#field_name'], $form_state['values']['inner_join']);
629 drupal_set_message(t('Updated join information.'));
630}
631
632/**
633 * Submit handler for data_ui_join_form() - handles deletion.
634 */
635function data_ui_join_form_submit_delete($form, &$form_state) {
636 // Use the original join information.
637 $form['#table']->unlink($form['#original_join']['left_table']);
638 drupal_set_message(t('Removed join information.'));
639}
640
641/**
72ba49d9
AB
642 * Export form.
643 */
644function data_ui_export_form(&$form_state, $table) {
645 $code = data_export($table->get('name'));
646
647 $form['export'] = array(
648 '#title' => t('Export table definition'),
649 '#type' => 'textarea',
650 '#value' => $code,
651 '#rows' => substr_count($code, "\n"),
652 );
653 return $form;
654}
655
656/**
dcdaddd1 657 * Theme data_ui_create_form.
f48c6947 658 */
dcdaddd1 659function theme_data_ui_create_form($form) {
f48c6947
AB
660
661 // Render field definition form elements in a table.
662 if (isset($form['fields'])) {
663 $output = drupal_render($form['help']);
664
665 $rows = array();
666 foreach (element_children($form['fields']) as $e) {
667 $row = array();
668 foreach (element_children($form['fields'][$e]) as $f) {
669 $row[] = drupal_render($form['fields'][$e][$f]);
670 }
671 $rows[] = $row;
672 }
120dbbcb 673 $header = array(t('Name *'), t('Label'), t('Type'), t('Unsigned'), t('Index'), t('Primary key'));
f48c6947
AB
674 $output .= theme('table', $header, $rows);
675
676 $output .= drupal_render($form);
677 return $output;
678 }
679 return drupal_render($form);
680}
2ace3f91
AB
681
682/**
683 * Theme data_ui_admin_form.
684 */
03608987 685function theme_data_ui_edit_form($form) {
2ace3f91
AB
686
687 // Format existing fields.
688 $rows = array();
689 foreach (element_children($form['fields']) as $e) {
690 $row = array();
0ef5d565 691 foreach (element_children($form['fields'][$e]) as $f) {
2ace3f91
AB
692 $row[] = drupal_render($form['fields'][$e][$f]);
693 }
694 $row[] = '&nbsp;';
695 $rows[] = $row;
696 }
697
698 // New fields form.
699 $row = array('&nbsp;');
700 foreach (element_children($form['new']) as $e) {
701 $row[] = drupal_render($form['new'][$e]);
702 }
703 $rows[] = $row;
704
0cd5c3d6 705 $header = array(t('Select'), t('Name'), t('Label'), t('Type'), t('Unsigned'), t('Index'), t('Primary key'), t('Joins'));
2ace3f91
AB
706 $output .= theme('table', $header, $rows);
707 $output .= drupal_render($form);
708 return $output;
709}
710
711/**
2c44bed5
AB
712 * Theme a schema module comparison result. Ie. the result of schema_compare_table().
713 *
714 * @todo: move to schema module - write a patch.
715 */
716function theme_data_ui_schema_compare_table($comparison) {
717 $output = '';
718 foreach ($comparison as $k => $v) {
719 if (!empty($v)) {
720 if (is_string($k)) {
721 $output .= '<dl>';
722 $output .= '<dt>'. ucfirst($k) .':</dt>';
723 $output .= '<dd>';
724 if (is_string($v)) {
725 $output .= $v;
726 }
727 elseif (is_array($v)) {
728 $output .= theme('item_list', $v);
729 }
730 $output .= '</dd>';
731 $output .= '</dl>';
732 }
733 }
734 }
735 return $output;
736}
737
738/**
2ace3f91
AB
739 * Magic helper function. Detect changed between keys in $new and $field
740 * and return a new field spec based on $field IF there are differences.
741 *
742 * Otherwise return FALSE.
743 *
744 * Currently checked: type, unsigned
745 */
746function _data_ui_changed($new, $field) {
747 $changed = FALSE;
748 if ($field['type'] != $new['type']) {
749 $field['type'] = $new['type'];
750 $changed = TRUE;
751 }
752 if ($field['unsigned'] != $new['unsigned']) {
753 $field['unsigned'] = $new['unsigned'];
754 $changed = TRUE;
755 }
756 if ($changed) {
757 return $field;
758 }
759 return FALSE;
f48c6947
AB
760}
761
762/**
763 * Helper function that generates a form snippet for defining a field.
764 */
765function _data_ui_field_form($required = FALSE) {
766 $form = array();
767 $form['#tree'] = TRUE;
768 $form['name'] = array(
769 '#type' => 'textfield',
770 '#size' => 20,
771 '#required' => $required,
772 );
773 $form['label'] = array(
774 '#type' => 'textfield',
775 '#size' => 20,
776 );
777 $form['type'] = array(
778 '#type' => 'select',
19aba4f3 779 '#options' => data_get_field_types(),
f48c6947
AB
780 );
781 $form['unsigned'] = array(
782 '#type' => 'checkbox',
783 );
120dbbcb
AB
784 $form['index'] = array(
785 '#type' => 'checkbox',
786 );
787 $form['primary'] = array(
788 '#type' => 'checkbox',
789 );
f48c6947 790 return $form;
ad756c3e 791}
3c77dfc3
AB
792
793/**
794 * Helper function to get link information for a specific field.
795 */
0cd5c3d6 796function _data_ui_get_join($join, $field) {
3c77dfc3
AB
797 if (is_array($join)) {
798 foreach ($join as $left_table => $info) {
799 if ($info['field'] == $field) {
0cd5c3d6
AB
800 $info['left_table'] = $left_table;
801 return $info;
3c77dfc3
AB
802 }
803 }
804 }
805 return FALSE;
806}
7fa8a6aa
AB
807
808/**
809 * Helper function for adjusting a table's real schema.
810 * @todo: this should live in schema module and should use better defined $reason keys.
811 */
812function _data_ui_alter_table($table, $field_reason) {
813 list($field, $reason) = explode(': ', $field_reason);
814 $schema = $table->get('table_schema');
815
816 switch ($reason) {
817 case 'not in database':
818 if (isset($schema['fields'][$field])) {
819 return $table->addField($field, $schema['fields'][$field]);
820 }
821 return FALSE;
822
823 case 'missing in database':
824 list($type, $field) = explode(' ', $field);
825 // @todo: support multiple keys.
826 if ($type == 'indexes') {
827 return $table->addIndex($field);
828 }
829 elseif ($type == 'unique keys') {
830 return $table->addUniqueKey($field);
831 }
832 elseif ($type == 'primary key') {
833 return $table->addPrimaryKey($schema['primary keys']);
834 }
835 return FALSE;
836
837 case 'primary key:<br />declared': // @todo: yikes!
838 $table->dropPrimaryKey();
839 return $table->changePrimaryKey($schema['primary keys']);
840 case 'missing in schema':
841 if ($field == 'primary key') {
842 return $table->dropPrimaryKey();
843 }
844 return FALSE;
845
846 case 'unexpected column in database':
d57cb606 847 return $table->dropField($field);
7fa8a6aa
AB
848
849 }
850 return FALSE;
851}