[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/data/data_ui/ -> data_ui.admin.inc (source)

   1  <?php
   2  /**
   3   * @file
   4   * Admin UI functions.
   5   */
   6  
   7  /**
   8   * List all tables for viewing content.
   9   */
  10  function data_ui_view() {
  11    $tables = data_get_all_tables();
  12    $rows = array();
  13    foreach ($tables as $table) {
  14      $row = array(
  15        $table->get('title'),
  16        $table->get('name'),
  17        db_result(db_query('SELECT COUNT(*) FROM {'. db_escape_table($table->get('name')) .'}')),
  18      );
  19      if (module_exists('views')) {
  20        $path = data_ui_get_default_path($table->get('name'));
  21        $row[] = $path ? l(t('View'), $path) : l(t('Edit schema'), 'admin/build/data/edit/' . $table->get('name'));
  22      }
  23      $rows[] = $row;
  24    }
  25    $header = array(t('Title'), t('Name'), t('Number of rows'));
  26    if (module_exists('views')) {
  27      $header[] = '&nbsp;';
  28    }
  29    return theme('table', $header, $rows);
  30  }
  31  
  32  /**
  33   * Main page for data table management.
  34   */
  35  function data_ui_manage() {
  36    $tables = data_get_all_tables();
  37    $rows = array();
  38    foreach ($tables as $table) {
  39      // Build operations depending on configuration status.
  40      $operations = array();
  41      if ($table->get('export_type') == EXPORT_IN_CODE) {
  42        $status = t('Default');
  43        $operations[] = l(t('Override'), 'admin/build/data/edit/'. $table->get('name'));
  44      }
  45      else if ($table->get('export_type') == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) {
  46        $status = t('Overridden');
  47        $operations[] = l(t('Edit'), 'admin/build/data/edit/'. $table->get('name'));
  48        $operations[] = l(t('Revert'), 'admin/build/data/revert/'. $table->get('name'));
  49      }
  50      else {
  51        $status = t('Normal');
  52        $operations[] = l(t('Edit'), 'admin/build/data/edit/'. $table->get('name'));
  53        $operations[] = l(t('Drop'), 'admin/build/data/drop/'. $table->get('name'));
  54      }
  55      if (module_exists('ctools')) {
  56        $operations[] = l(t('Export'), 'admin/build/data/export/'. $table->get('name'));
  57      }
  58  
  59      $row = array();
  60      $row[] = check_plain($table->get('title'));
  61      $row[] = check_plain($table->get('name'));
  62      $row[] = $status;
  63      $row[] = implode(' | ', $operations);
  64  
  65      $rows[] = $row;
  66    }
  67    $rows[] = array(
  68      l(t('Create new table'), 'admin/build/data/create'),
  69      '&nbsp;',
  70      '&nbsp;',
  71      '&nbsp;',
  72    );
  73    $header = array(t('Title'), t('Name'), t('Status'), t('Operations'));
  74    return theme('table', $header, $rows);
  75  }
  76  
  77  /**
  78   * Comparison page.
  79   */
  80  function data_ui_compare() {
  81    $rows = array();
  82    $tables = data_get_all_tables();
  83    foreach ($tables as $table) {
  84      $row = array();
  85      $comp = $table->compareSchema();
  86      $row[] = check_plain($table->get('name'));
  87      $status = $comp['status'];
  88      if ($status != 'same') {
  89        $status .= ' - '. l(t('adjust'), 'admin/build/data/compare/'. $table->get('name'));
  90      }
  91      $row[] = $status;
  92      $row[] = empty($comp['warning']) ? '-' : $comp['warning'];
  93      $rows[] = $row;
  94    }
  95    $header = array(t('Name'), t('Status'), t('Warnings'));
  96    return theme('table', $header, $rows);
  97  }
  98  
  99  /**
 100   * Adjust table schema form: Present the user with the difference between schema information and
 101   * the actual schema in the Database and offer three options:
 102   *
 103   * - Adjust schema info,
 104   * - adjust database or
 105   * - leave it.
 106   */
 107  function data_ui_adjust_form(&$form_state, $table) {
 108    drupal_set_title(t('Adjust @table', array('@table' => $table->get('name'))));
 109    $comparison = $table->compareSchema();
 110  
 111    $form = array();
 112    $form['#redirect'] = 'admin/build/data/compare';
 113    $form['#table'] = $table;
 114    $form['#comparison'] = $comparison;
 115    $form['comparison'] = array(
 116      '#type' => 'fieldset',
 117      '#title' => t('Comparison'),
 118    );
 119    $form['comparison']['comparison']['#value'] = theme('data_ui_schema_compare_table', $comparison);
 120  
 121    if ($comparison['status'] == 'different') {
 122      $form['update_schema'] = array(
 123        '#type' => 'fieldset',
 124        '#title' => t('Option 1: Update schema information'),
 125        );
 126      $form['update_schema']['description'] = array(
 127        '#value' => t('<p>This option will update the schema information about this table.</p>'),
 128      );
 129      $form['update_schema']['submit'] = array(
 130        '#type' => 'submit',
 131        '#submit' => array('data_ui_adjust_form_submit_update_schema'),
 132        '#value' => t('Update schema information'),
 133      );
 134      $form['alter_table'] = array(
 135        '#type' => 'fieldset',
 136        '#title' => t('Option 2: Alter table'),
 137        );
 138      $form['alter_table']['description'] = array(
 139        '#value' => t('<p>Review the changes above carefully!
 140          This option will alter the database table and can very
 141          easily cause data loss.</p>'),
 142      );
 143      $form['alter_table']['submit'] = array(
 144        '#type' => 'submit',
 145        '#submit' => array('data_ui_adjust_form_submit_alter_table'),
 146        '#value' => t('Alter table'),
 147      );
 148    }
 149    elseif ($comparison['status'] == 'missing') {
 150      $form['alter_table'] = array(
 151        '#type' => 'fieldset',
 152        '#title' => t('Create table'),
 153        );
 154      $form['alter_table']['description'] = array(
 155        '#value' => t('<p>Create a new table from schema information.</p>'),
 156      );
 157      $form['alter_table']['submit'] = array(
 158        '#type' => 'submit',
 159        '#submit' => array('data_ui_adjust_form_submit_create_table'),
 160        '#value' => t('Create table'),
 161      );
 162    }
 163    $form['cancel'] = array(
 164      '#type' => 'fieldset',
 165      '#title' => t('Don\'t change anything'),
 166      );
 167    $form['cancel']['cancel'] = array(
 168      '#type' => 'submit',
 169      '#value' => t('Cancel'),
 170    );
 171    return $form;
 172  }
 173  
 174  /**
 175   * Submit handler for data_ui_adjust_form().
 176   */
 177  function data_ui_adjust_form_submit_update_schema($form, &$form_state) {
 178    $table = $form['#table'];
 179    $schema = schema_invoke('inspect');
 180    if (isset($schema[$table->get('name')])) {
 181      $table->update(array('table_schema' => $schema[$table->get('name')]));
 182      drupal_set_message(t('Updated schema for @table', array('@table' => $table->get('name'))));
 183    }
 184    else {
 185      drupal_set_message(t('Error updating schema'), 'error');
 186    }
 187  }
 188  
 189  /**
 190   * Submit handler for data_ui_adjust_form().
 191   */
 192  function data_ui_adjust_form_submit_alter_table($form, &$form_state) {
 193    $resolved = $resolved = array();
 194    if (isset($form['#comparison']['reasons'])) {
 195      foreach ($form['#comparison']['reasons'] as $field_reason) {
 196        try {
 197          data_alter_table($form['#table'], $field_reason);
 198          $resolved[] = $field_reason;
 199        }
 200        catch (Exception $e) {
 201          $unresolved[] = $field_reason;
 202        }
 203      }
 204    }
 205    if (count($resolved)) {
 206      drupal_set_message(t('Resolved') . theme('item_list', $resolved));
 207    }
 208    if (count($unresolved)) {
 209      drupal_set_message(t('Could not resolve') . theme('item_list', $unresolved), 'error');
 210    }
 211  }
 212  
 213  /**
 214   * Submit handler for data_ui_adjust_form().
 215   */
 216  function data_ui_adjust_form_submit_create_table($form, &$form_state) {
 217    $table = $form['#table'];
 218    $ret = array();
 219    db_create_table($ret, $table->get('name'), $table->get('table_schema'));
 220    drupal_get_schema($table->get('name'), TRUE);
 221    if ($ret[0]['success']) {
 222      drupal_set_message(t('Created table @table', array('@table' => $table->get('name'))));
 223    }
 224    else {
 225      drupal_set_message(t('Error creating table'), 'error');
 226    }
 227  }
 228  
 229  /**
 230   * Form callback for adopt table form.
 231   *
 232   * 'Orphaned' tables are database tables that don't have an active schema
 233   * definition.
 234   */
 235  function data_ui_adopt_form($form_state) {
 236    $form = array();
 237  
 238    // Compile a list of orphaned tables.
 239    $drupal_schema = drupal_get_schema(NULL, TRUE);
 240    $db_schema = schema_invoke('inspect');
 241    $orphaned_tables = array();
 242    foreach ($db_schema as $name => $table) {
 243      if (!isset($drupal_schema[$name])) {
 244        $orphaned_tables[$name] = $name;
 245      }
 246    }
 247  
 248    $form['orphaned_tables'] = array(
 249      '#type' => 'checkboxes',
 250      '#title' => t('Orphaned Tables'),
 251      '#options' => $orphaned_tables,
 252    );
 253    if (count($orphaned_tables) < 1) {
 254      $form['no_orphaned_tables'] = array(
 255        '#type' => 'item',
 256        '#value' => t('There are no orphaned tables in your database.'),
 257      );
 258    }
 259    $form['submit'] = array(
 260      '#type' => 'submit',
 261      '#value' => 'Adopt',
 262    );
 263    return $form;
 264  }
 265  
 266  /**
 267   * Submit handler for adopt table form.
 268   */
 269  function data_ui_adopt_form_submit($form, &$form_state) {
 270    data_include('DataTable');
 271    $tables = array_keys(array_filter($form_state['values']['orphaned_tables']));
 272    foreach ($tables as $table_name) {
 273      $table = DataTable::instance($table_name);
 274      $table->adopt();
 275      unset($table);
 276    }
 277  
 278    DataTable::clearCaches();
 279  
 280    $form_state['redirect'] = 'admin/build/data';
 281  }
 282  
 283  /**
 284   * Form callback for create table form.
 285   */
 286  function data_ui_create_form(&$form_state) {
 287    $form = array();
 288    if (!$form_state['storage']['field_num']) {
 289      $form['name'] = array(
 290        '#type' => 'textfield',
 291        '#title' => t('Table name'),
 292        '#description' => t('Machine readable name of the table - e. g. "my_table". Must only contain lower case letters and _.'),
 293        '#required' => TRUE,
 294      );
 295      $form['title'] = array(
 296        '#type' => 'textfield',
 297        '#title' => t('Table title'),
 298        '#description' => t('Natural name of the table - e. g. "My Table".'),
 299        '#required' => TRUE,
 300      );
 301      $form['field_num'] = array(
 302        '#type' => 'textfield',
 303        '#title' => t('Number of fields'),
 304        '#description' => t('The number of fields this table should contain.'),
 305        '#default_value' => 1,
 306        '#required' => TRUE,
 307      );
 308      $form['submit'] = array(
 309        '#type' => 'submit',
 310        '#value' => t('Next'),
 311      );
 312    }
 313    else {
 314      $form['help']['#value'] = t('Define the fields of the new table.');
 315      $form['fields'] = array(
 316        '#tree' => TRUE,
 317      );
 318      for ($i = 0; $i < $form_state['storage']['field_num']; $i++) {
 319        $form['fields']['field_'. $i] = _data_ui_field_form(TRUE);
 320      }
 321      $form['submit'] = array(
 322        '#type' => 'submit',
 323        '#value' => t('Create'),
 324      );
 325    }
 326    return $form;
 327  }
 328  
 329  /**
 330   * Validate handler for create table form.
 331   */
 332  function data_ui_create_form_validate($form, &$form_state) {
 333    if (data_get_table(trim($form_state['storage']['name']))) {
 334      form_set_error('name', t('Name is already taken.'));
 335    }
 336    if (isset($form_state['values']['field_num'])) {
 337      if (is_numeric($form_state['values']['field_num'])) {
 338        if ($form_state['values']['field_num'] < 1) {
 339          form_set_error('field_num', t('At least one field must be created.'));
 340        }
 341      }
 342      else {
 343        form_set_error('field_num', t('Enter a number greater than 0.'));
 344      }
 345    }
 346    // Check for dupes.
 347    if (isset($form_state['values']['fields'])) {
 348      $names = array();
 349      foreach ($form_state['values']['fields'] as $field) {
 350        if (is_numeric($names[$field['name']])) {
 351          form_set_error('name', t('Names can\'t be numbers.'));
 352        }
 353        if (!isset($names[$field['name']])) {
 354          $names[$field['name']] = $field['name'];
 355        }
 356        else {
 357          form_set_error('name', t('Names must be unique.'));
 358        }
 359      }
 360    }
 361  }
 362  
 363  /**
 364   * Submit handler for create table form.
 365   */
 366  function data_ui_create_form_submit($form, &$form_state) {
 367  
 368    if (isset($form_state['values']['field_num'])) {
 369      $form_state['storage'] = $form_state['values'];
 370    }
 371    elseif (isset($form_state['values']['fields'])) {
 372  
 373      // Create a schema from user input.
 374      $schema = $index = $primary = $meta = array();
 375      foreach ($form_state['values']['fields'] as $field) {
 376        $field['name'] = db_escape_table($field['name']);
 377        
 378        $schema['fields'][$field['name']] = data_build_field_definition($field);
 379        $meta['fields'][$field['name']]['label'] = check_plain($field['label']);
 380  
 381        // Limit index if field type is text.
 382        if (!empty($field['index'])) {
 383          $index[$field['name']] = data_get_index_definition($field['name'], $field);
 384        }
 385        if (!empty($field['primary'])) {
 386          $primary[] = data_get_pk_definition($field['name'], $field);
 387        }
 388      }
 389      $schema['indexes'] = $index;
 390      $schema['primary key'] = $primary;
 391  
 392      // Create table.
 393      if ($table = data_create_table(trim($form_state['storage']['name']), $schema, trim($form_state['storage']['title']))) {
 394        $meta = $table->update(array('meta' => $meta));
 395        drupal_set_message(t('Created table @table', array('@table' => $table->get('name'))));
 396      }
 397      else {
 398        drupal_set_message(t('Error creating table'), 'error');
 399      }
 400  
 401      // Unset storage to enable redirect.
 402      unset($form_state['storage']);
 403      $form_state['redirect'] = 'admin/build/data';
 404    }
 405  }
 406  
 407  /**
 408   * Form callback for revert table form.
 409   */
 410  function data_ui_revert_form(&$form_state, $table) {
 411    $form = array();
 412    $form['#redirect'] = 'admin/build/data';
 413    $form['#table'] = $table;
 414  
 415    return confirm_form($form,
 416      t('Revert this table?'),
 417      'admin/build/data',
 418      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'))),
 419      t('Revert'), t('Cancel')
 420    );
 421  }
 422  
 423  /**
 424   * Submit handler for data_ui_revert_form().
 425   */
 426  function data_ui_revert_form_submit($form, &$form_state) {
 427    $table = $form['#table'];
 428    $table->revert();
 429  }
 430  
 431  /**
 432   * Form callback for drop table form.
 433   */
 434  function data_ui_drop_form(&$form_state, $table) {
 435    $form = array();
 436    $form['#redirect'] = 'admin/build/data';
 437    $form['#table'] = $table;
 438  
 439    return confirm_form($form,
 440      t('Drop this table?'),
 441      'admin/build/data',
 442      t('Are you sure you would like to drop table @table? This action cannot be undone.', array('@table' => $table->get('name'))),
 443      t('Drop'), t('Cancel')
 444    );
 445  }
 446  
 447  /**
 448   * Submit handler for data_ui_drop_form().
 449   */
 450  function data_ui_drop_form_submit($form, &$form_state) {
 451    $table = $form['#table'];
 452    data_drop_table(db_escape_table($table->get('name')));
 453  }
 454  
 455  /**
 456   * Form callback for editing a table.
 457   */
 458  function data_ui_edit_form(&$form_state, $table) {
 459    drupal_set_title(check_plain($table->get('title')));
 460  
 461    $schema = $table->get('table_schema');
 462    $meta = $table->get('meta');
 463  
 464    $form = array();
 465    // Keep table.
 466    $form['table'] = array(
 467      '#type' => 'value',
 468      '#value' => $table,
 469    );
 470  
 471    // Existing fields.
 472    $form['fields'] = array('#tree' => TRUE);
 473    if (isset($schema['fields'])) {
 474      foreach ($schema['fields'] as $field_name => $field) {
 475        $form['fields'][$field_name] = array();
 476        $form['fields'][$field_name]['selected'] = array(
 477          '#type' => 'checkbox',
 478        );
 479        $form['fields'][$field_name]['name'] = array('#value' => check_plain($field_name));
 480        $form['fields'][$field_name]['label'] = array(
 481          '#type' => 'textfield',
 482          '#size' => 20,
 483          '#default_value' => $meta['fields'][$field_name]['label'],
 484        );
 485        $form['fields'][$field_name]['type'] = array(
 486          '#type' => 'select',
 487          '#options' => data_get_field_types(),
 488          '#default_value' => $field['type'],
 489        );
 490        $form['fields'][$field_name]['size'] = array(
 491          '#type' => 'select',
 492          '#options' => data_get_field_sizes(),
 493          '#default_value' => isset($field['size']) ? $field['size'] : 'normal',
 494        );
 495        $form['fields'][$field_name]['unsigned'] = array(
 496          '#type' => 'checkbox',
 497          '#default_value' => isset($field['unsigned']) ? $field['unsigned'] : FALSE,
 498        );
 499        $form['fields'][$field_name]['index'] = array(
 500          '#type' => 'checkbox',
 501          '#default_value' => isset($schema['indexes'][$field_name]),
 502        );
 503        $form['fields'][$field_name]['primary'] = array(
 504          '#type' => 'checkbox',
 505          '#default_value' => isset($schema['primary key']) ? in_array($field_name, $schema['primary key']) : FALSE,
 506        );
 507        if (isset($meta['join']) && $join = _data_ui_get_join($meta['join'], $field_name)) {
 508          $join = $join['left_table'] .'.'. $join['left_field'];
 509        }
 510        else {
 511          $join = t('<none>');
 512        }
 513        $join = l($join, 'admin/build/data/edit/'. $table->get('name') .'/join/'.$field_name);
 514        $form['fields'][$field_name]['join']['#value'] = $join;
 515      }
 516    }
 517  
 518    // Add a new field.
 519    $form['new'] = _data_ui_field_form();
 520    $form['new']['primary'] = array(
 521      '#type' => 'markup',
 522      '#value' => '&nbsp;',
 523      );
 524    $form['new']['join'] = array(
 525      '#type' => 'markup',
 526      '#value' => '&nbsp;',
 527      );
 528    $form['new']['add'] = array(
 529      '#type' => 'submit',
 530      '#value' => t('Add new'),
 531    );
 532  
 533    // Bulk operations.
 534    $options = array(
 535      t('Bulk operations'),
 536      'delete' => t('Delete all selected'),
 537    );
 538    $form['bulk_operation'] = array(
 539      '#type' => 'select',
 540      '#options' => $options,
 541    );
 542    $form['submit'] = array(
 543      '#type' => 'submit',
 544      '#value' => t('Save'),
 545    );
 546  
 547    return $form;
 548  }
 549  
 550  /**
 551   * Submit form.
 552   */
 553  function data_ui_edit_form_submit($form, &$form_state) {
 554    $table = $form_state['values']['table'];
 555    $schema = $table->get('table_schema');
 556  
 557    if (!db_table_exists($table->get('name'))) {
 558      drupal_set_message(t('Table does not exist in database.'), 'error');
 559      if (module_exists('schema')) {
 560        drupal_set_message(t('Go to !compare to resolve conflicts.', array('!compare' => l(t('Compare schemas'), 'admin/build/data/compare'))), 'error');
 561      }
 562      return;
 563    }
 564  
 565    try {
 566      if ($form_state['clicked_button']['#value'] == t('Save')) {
 567        $fields = $schema['fields'];
 568        
 569        $new_fields = $form_state['values']['fields'];
 570  
 571        $new_index = array();
 572        $new_primary_key = array();
 573  
 574        if (empty($form_state['values']['bulk_operation']) && isset($fields)) {
 575  
 576          // Convert schema.
 577          foreach ($fields as $field_name => $field) {
 578            $safe_field_name = db_escape_table($field_name);
 579            
 580            if ($new_spec = _data_ui_changed($new_fields[$safe_field_name], $field)) {
 581              $table->changeField($safe_field_name, $new_spec);
 582              drupal_set_message(t('Changed field !field_name', array('!field_name' => $safe_field_name)));
 583            }
 584            if ($new_fields[$safe_field_name]['index']) {
 585              $new_index[] = $safe_field_name;
 586            }
 587            if ($new_fields[$safe_field_name]['primary']) {
 588              $new_primary_key[] = $safe_field_name;
 589            }
 590          }
 591          $table->changeIndex($new_index);
 592          $table->changePrimaryKey($new_primary_key);
 593  
 594          // Update meta data.
 595          $meta = $table->get('meta');
 596          foreach ($new_fields as $safe_field_name => $field) {
 597            $meta['fields'][$safe_field_name]['label'] = check_plain($field['label']);
 598          }
 599          $table->update(array('meta' => $meta));
 600          drupal_set_message(t('Saved changes'));
 601        }
 602        else {
 603          // Bulk updates.
 604          switch ($form_state['values']['bulk_operation']) {
 605            case 'delete':
 606              foreach ($new_fields as $field_name => $field) {
 607                
 608                if (!empty($field['selected'])) {
 609                  // One field must stay.
 610                  $schema = $table->get('table_schema');
 611                  if (count($schema['fields']) > 1) {
 612                    $table->dropField(db_escape_table($field_name));
 613                    drupal_set_message(t('Deleted field !field_name', array('!field_name' => check_plain($field_name))));
 614                  }
 615                  else {
 616                    drupal_set_message('You cannot delete all fields from a table, drop the table instead.', 'error');
 617                  }
 618                }
 619              }
 620              break;
 621          }
 622        }
 623      }
 624      elseif ($form_state['clicked_button']['#value'] == t('Add new')) {
 625        $new = $form_state['values']['new'];
 626        $spec = data_build_field_definition($new);
 627        $table->addField(db_escape_table($new['name']), $spec);
 628        drupal_set_message(t('Added field !field', array('!field' => check_plain($new['name']))));
 629        if (!empty($new['index'])) {
 630          $table->addIndex(db_escape_table($new['name']));
 631          drupal_set_message(t('Added index for field !field', array('!field' => check_plain($new['name']))));
 632        }
 633        $meta = $table->get('meta');
 634        $meta['fields'][db_escape_table($new['name'])]['label'] = check_plain($new['label']);
 635        $table->update(array('meta' => $meta));
 636      }
 637    }
 638    catch (DataException $e) {
 639      drupal_set_message($e->getMessage(), 'error');
 640    }
 641  }
 642  
 643  /**
 644   * Edit title form.
 645   */
 646  function data_ui_edit_title_form(&$form_state, $table) {
 647    drupal_set_title(check_plain($table->get('title')));
 648  
 649    $form = array();
 650    $form['#table'] = $table;
 651    // Table title.
 652    $form['title'] = array(
 653      '#type' => 'textfield',
 654      '#title' => t('Title'),
 655      '#description' => t('Natural name of the table.'),
 656      '#default_value' => db_escape_table($table->get('title')),
 657    );
 658    $form['submit'] = array(
 659      '#type' => 'submit',
 660      '#value' => t('Save'),
 661    );
 662    return $form;
 663  }
 664  
 665  /**
 666   * Submit handler for data_ui_edit_title_form().
 667   */
 668  function data_ui_edit_title_form_submit($form, &$form_state) {
 669    $form['#table']->update(array('title' => db_escape_table($form_state['values']['title'])));
 670  }
 671  
 672  /**
 673   * Views handler configuration form.
 674   */
 675  function data_ui_views_form(&$form_state, $table) {
 676    drupal_set_title(check_plain($table->get('title')));
 677  
 678    module_load_include('inc', 'data', 'data.views');
 679  
 680    $schema = $table->get('table_schema');
 681    $meta = $table->get('meta');
 682  
 683    $form = array();
 684    // Keep table.
 685    $form['#table'] = $table;
 686  
 687    // Existing fields.
 688    $form['fields'] = array('#tree' => TRUE);
 689    if (isset($schema['fields'])) {
 690      foreach ($schema['fields'] as $field_name => $field) {
 691        $form['fields'][$field_name] = array();
 692        $form['fields'][$field_name]['name'] = array('#value' => check_plain($field_name));
 693        $form['fields'][$field_name]['views_field_handler'] = array(
 694          '#type' => 'select',
 695          '#options' => data_get_views_handler_options('field'),
 696          '#default_value' => data_get_views_handler('field', $table, $field_name),
 697        );
 698        $form['fields'][$field_name]['views_filter_handler'] = array(
 699          '#type' => 'select',
 700          '#options' => data_get_views_handler_options('filter'),
 701          '#default_value' => data_get_views_handler('filter', $table, $field_name),
 702        );
 703        $form['fields'][$field_name]['views_argument_handler'] = array(
 704          '#type' => 'select',
 705          '#options' => data_get_views_handler_options('argument'),
 706          '#default_value' => data_get_views_handler('argument', $table, $field_name),
 707        );
 708        $form['fields'][$field_name]['views_sort_handler'] = array(
 709          '#type' => 'select',
 710          '#options' => data_get_views_handler_options('sort'),
 711          '#default_value' => data_get_views_handler('sort', $table, $field_name),
 712        );
 713      }
 714    }
 715    $form['notice'] = array(
 716      '#type' => 'markup',
 717      '#value' => '<p>' . t('Note that not every handler will make sense or function correctly with every type of field.') . '</p>',
 718    );
 719    $form['submit'] = array(
 720      '#type' => 'submit',
 721      '#value' => t('Save'),
 722    );
 723  
 724    return $form;
 725  }
 726  
 727  /**
 728   * Submit handler for data_ui_views_form().
 729   */
 730  function data_ui_views_form_submit($form, &$form_state) {
 731    $table = $form['#table'];
 732    $meta = $table->get('meta');
 733    if (isset($form_state['values']['fields'])) {
 734      foreach ($form_state['values']['fields'] as $field_name => $handlers) {
 735        foreach ($handlers as $handler_type => $handler) {
 736          $meta['fields'][$field_name][$handler_type] = $handler;
 737        }
 738      }
 739    }
 740    $table->update(array('meta' => $meta));
 741    views_invalidate_cache();
 742  }
 743  
 744  /**
 745   * Join form.
 746   */
 747  function data_ui_join_form(&$form_state, $table, $field_name) {
 748    // drupal_set_title(t('Join field'));
 749  
 750    $schema = $table->get('table_schema');
 751    $meta = $table->get('meta');
 752  
 753    // Validate input.
 754    if (!isset($field_name) || !isset($schema['fields'][$field_name])) {
 755      drupal_set_message(t('Invalid field.'), 'error');
 756      drupal_goto('admin/build/data/edit/'. check_url($table->get('name')));
 757    }
 758  
 759    // List all tables that schema API knows about as optoins.
 760    // @todo: This is a looong list - needs some AHAH to scale better.
 761    $table_schemas = drupal_get_schema();
 762    ksort($table_schemas);
 763    $options = array();
 764    foreach ($table_schemas as $table_name => $schema) {
 765      if ($table->get('name') != $table_name) {
 766        foreach ($schema['fields'] as $name => $info) {
 767          $options[$table_name][$table_name .'.'. $name] = $table_name .'.'. $name;
 768        }
 769      }
 770    }
 771  
 772    // Build form.
 773    $form = array();
 774    $form['#table'] = $table;
 775    $form['#field_name'] = $field_name;
 776    $form['#redirect'] = 'admin/build/data/edit/'. check_url($table->get('name'));
 777    $join = _data_ui_get_join($meta['join'], $field_name);
 778    $form['#original_join'] = $join;
 779    $form['left'] = array(
 780      '#type' => 'select',
 781      '#title' => t('Join @table_field to ', array('@table_field' => $table->get('name') .'.'. $field_name)),
 782      '#options' => $options,
 783      '#default_value' => $join['left_table'] .'.'. $join['left_field'],
 784    );
 785    $form['inner_join'] = array(
 786      '#type' => 'radios',
 787      '#title' => t('Join type'),
 788      '#options' => array(t('Left join'), t('Inner join')),
 789      '#default_value' => $join['inner_join'] ? $join['inner_join'] : 0,
 790    );
 791  
 792    // Use confirm form for its formatting.
 793    $form = confirm_form($form,
 794      t('Join field'),
 795      'admin/build/data/edit/'. check_url($table->get('name')),
 796      '',
 797      t('Save'), t('Cancel')
 798    );
 799    $form['actions']['delete'] = array(
 800      '#type' => 'submit',
 801      '#value' => t('Delete'),
 802      '#submit' => array('data_ui_join_form_submit_delete'),
 803    );
 804    krsort($form['actions']);
 805  
 806    return $form;
 807  }
 808  
 809  /**
 810   * Submit handler for data_ui_join_form().
 811   */
 812  function data_ui_join_form_submit($form, &$form_state) {
 813    list($left_table, $left_field) = explode('.', $form_state['values']['left']);
 814    $form['#table']->link($left_table, $left_field, $form['#field_name'], $form_state['values']['inner_join']);
 815    drupal_set_message(t('Updated join information.'));
 816  }
 817  
 818  /**
 819   * Submit handler for data_ui_join_form() - handles deletion.
 820   */
 821  function data_ui_join_form_submit_delete($form, &$form_state) {
 822    // Use the original join information.
 823    $form['#table']->unlink($form['#original_join']['left_table']);
 824    drupal_set_message(t('Removed join information.'));
 825  }
 826  
 827  /**
 828   * Export form.
 829   */
 830  function data_ui_export_form(&$form_state, $table) {
 831    $code = data_export(db_escape_table($table->get('name')));
 832  
 833    $form['export'] = array(
 834      '#title' => t('Export table definition'),
 835      '#type' => 'textarea',
 836      '#value' => $code,
 837      '#rows' => substr_count($code, "\n"),
 838    );
 839    return $form;
 840  }
 841  
 842  /**
 843   * View table with krumo().
 844   */
 845  function data_ui_view_schema($table) {
 846    drupal_set_title(check_plain($table->get('title')));
 847    $output = '<H3>'. t('Schema') .'</H3>';
 848    $output .= kprint_r( $table->get('table_schema'), TRUE);
 849    
 850    
 851    if ($meta = $table->get('meta')) {
 852      $output .= '<H3>'. t('Meta info') .'</H3>';
 853      $output .= kprint_r($table->get('meta'), TRUE);
 854    }
 855    return $output;
 856  }
 857  
 858  /**
 859   * Theme data_ui_create_form.
 860   */
 861  function theme_data_ui_create_form($form) {
 862  
 863    // Render field definition form elements in a table.
 864    if (isset($form['fields'])) {
 865      $output = drupal_render($form['help']);
 866  
 867      $rows = array();
 868      foreach (element_children($form['fields']) as $e) {
 869        $row = array();
 870        foreach (element_children($form['fields'][$e]) as $f) {
 871          $row[] = drupal_render($form['fields'][$e][$f]);
 872        }
 873        $rows[] = $row;
 874      }
 875      $header = array(t('Name *'), t('Label'), t('Type'), t('Size'), t('Unsigned'), t('Index'), t('Primary key'));
 876      $output .= theme('table', $header, $rows);
 877  
 878      $output .= drupal_render($form);
 879      return $output;
 880    }
 881    return drupal_render($form);
 882  }
 883  
 884  /**
 885   * Theme data_ui_edit_form.
 886   */
 887  function theme_data_ui_edit_form($form) {
 888  
 889    // Format existing fields.
 890    $rows = array();
 891    foreach (element_children($form['fields']) as $e) {
 892      $row = array();
 893      foreach (element_children($form['fields'][$e]) as $f) {
 894        $row[] = drupal_render($form['fields'][$e][$f]);
 895      }
 896      $row[] = '&nbsp;';
 897      $rows[] = $row;
 898    }
 899  
 900    // New fields form.
 901    $row = array('&nbsp;');
 902    foreach (element_children($form['new']) as $e) {
 903      $row[] = drupal_render($form['new'][$e]);
 904    }
 905    $rows[] = $row;
 906  
 907    $header = array(t('Select'), t('Name'), t('Label'), t('Type'), t('Size'), t('Unsigned'), t('Index'), t('Primary key'), t('Joins'));
 908    $output = theme('table', $header, $rows);
 909    $output .= drupal_render($form);
 910    return $output;
 911  }
 912  
 913  /**
 914   * Theme data_ui_views_form.
 915   */
 916  function theme_data_ui_views_form($form) {
 917  
 918    // Format existing fields.
 919    $rows = array();
 920    foreach (element_children($form['fields']) as $e) {
 921      $row = array();
 922      foreach (element_children($form['fields'][$e]) as $f) {
 923        $row[] = drupal_render($form['fields'][$e][$f]);
 924      }
 925      $row[] = '&nbsp;';
 926      $rows[] = $row;
 927    }
 928  
 929    $header = array(t('Name'), t('Field handler'), t('Filter handler'), t('Argument handler'), t('Sort handler'));
 930    $output = theme('table', $header, $rows);
 931    $output .= drupal_render($form);
 932    return $output;
 933  }
 934  
 935  /**
 936   * Theme a schema module comparison result. Ie. the result of schema_compare_table().
 937   *
 938   * @todo: move to schema module - write a patch.
 939   */
 940  function theme_data_ui_schema_compare_table($comparison) {
 941    $output = '';
 942    foreach ($comparison as $k => $v) {
 943      if (!empty($v)) {
 944        if (is_string($k)) {
 945          $output .= '<dl>';
 946          $output .= '<dt>'. ucfirst($k) .':</dt>';
 947          $output .= '<dd>';
 948          if (is_string($v)) {
 949            $output .= $v;
 950          }
 951          elseif (is_array($v)) {
 952            $output .= theme('item_list', $v);
 953          }
 954          $output .= '</dd>';
 955          $output .= '</dl>';
 956        }
 957      }
 958    }
 959    return $output;
 960  }
 961  
 962  /**
 963   * Magic helper function. Detect changed between keys in $new and $field
 964   * and return a new field spec based on $field IF there are differences.
 965   *
 966   * Otherwise return FALSE.
 967   *
 968   * Currently checked: type, unsigned, default.
 969   *
 970   * @todo This is insane, we need to use clean specs instead of converting
 971   * existing specs. This will require us also to not allow changes to indexes,
 972   * PKs and field specs on the same form.
 973   */
 974  function _data_ui_changed($new, $field) {
 975    $changed = FALSE;
 976    if ($field['type'] != $new['type']) {
 977      $field['type'] = $new['type'];
 978      $changed = TRUE;
 979    }
 980    if ($field['unsigned'] != $new['unsigned']) {
 981      $field['unsigned'] = $new['unsigned'];
 982      $changed = TRUE;
 983    }
 984    if ($changed) {
 985      if ($field['type'] == 'text') {
 986        unset($field['default']);
 987      }
 988      return $field;
 989    }
 990    return FALSE;
 991  }
 992  
 993  /**
 994   * Helper function that generates a form snippet for defining a field.
 995   */
 996  function _data_ui_field_form($required = FALSE) {
 997    $form = array();
 998    $form['#tree'] = TRUE;
 999    $form['name'] = array(
1000      '#type' => 'textfield',
1001      '#size' => 20,
1002      '#required' => $required,
1003    );
1004    $form['label'] = array(
1005      '#type' => 'textfield',
1006      '#size' => 20,
1007    );
1008    $form['type'] = array(
1009      '#type' => 'select',
1010      '#options' => data_get_field_types(),
1011    );
1012    $form['size'] = array(
1013      '#type' => 'select',
1014      '#options' => data_get_field_sizes(),
1015    );
1016    $form['unsigned'] = array(
1017      '#type' => 'checkbox',
1018    );
1019    $form['index'] = array(
1020      '#type' => 'checkbox',
1021    );
1022    $form['primary'] = array(
1023      '#type' => 'checkbox',
1024    );
1025    return $form;
1026  }
1027  
1028  /**
1029   * Helper function to get link information for a specific field.
1030   */
1031  function _data_ui_get_join($join, $field) {
1032    if (is_array($join)) {
1033      foreach ($join as $left_table => $info) {
1034        if ($info['field'] == $field) {
1035          $info['left_table'] = $left_table;
1036          return $info;
1037        }
1038      }
1039    }
1040    return FALSE;
1041  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7