[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/migrate/ -> migrate_pages.inc (source)

   1  <?php
   2  // $Id: migrate_pages.inc,v 1.1.2.43 2009/09/18 19:13:05 mikeryan Exp $
   3  define('MIGRATE_COLS_ALL', 1);
   4  define('MIGRATE_COLS_EMPTY', 2);
   5  define('MIGRATE_COLS_NONEMPTY', 3);
   6  
   7  
   8  /**
   9   * @file
  10   */
  11  
  12  function migrate_front() {
  13    migrate_check_advanced_help();
  14  
  15    module_load_include('inc', 'system', 'system.admin');
  16    return system_admin_menu_block_page();
  17  }
  18  
  19  function migrate_dashboard() {
  20    return drupal_get_form('_migrate_dashboard_form');
  21  }
  22  
  23  function _migrate_dashboard_form($form_state) {
  24    $header = array(
  25      array('data' => t('Clear')),
  26      array('data' => t('Import')),
  27      array('data' => t('Scan')),
  28      array('data' => t('Content Set')),
  29      array('data' => t('Source')),
  30      array('data' => t('Rows in Set')),
  31      array('data' => t('Imported')),
  32      array('data' => t('Unimported')),
  33      array('data' => t('Last imported')),
  34      );
  35    $form['header'] = array('#type' => 'value', '#value' => $header);
  36    $sql = "SELECT *
  37            FROM {migrate_content_sets}
  38            ORDER BY weight, contenttype, view_name";
  39    $result = db_query($sql);
  40  
  41    $clearing = array();
  42    $clearingenable = array();
  43    $importing = array();
  44    $importingenable = array();
  45    $scanning = array();
  46    $scanningenable = array();
  47    $rownum = 0;
  48    while ($row = db_fetch_object($result)) {
  49      $status = '';
  50      if ($row->mcsid) {
  51        $view = views_get_view($row->view_name);
  52        if (!$view) {
  53          drupal_set_message(t('View !view does not exist - either (re)create this view, or
  54            remove the content set using it.', array('!view' => $row->view_name)));
  55          continue;
  56        }
  57        $clearing[$row->mcsid] = '';
  58        $importing[$row->mcsid] = '';
  59        $scanning[$row->mcsid] = '';
  60        $status = t('N/A');
  61        $maptable = migrate_map_table_name($row->mcsid);
  62        $sourcekey = $row->sourcekey;
  63        if ($row->clearing) {
  64          $clearingenable[] = $row->mcsid;
  65        }
  66        if ($row->importing) {
  67          $importingenable[] = $row->mcsid;
  68        }
  69        if ($row->scanning) {
  70          $scanningenable[] = $row->mcsid;
  71        }
  72        $imported = db_result(db_query('SELECT COUNT(*) FROM {' . $maptable . '}'));
  73  
  74        // If not caching counts, override the saved count with a fresh count
  75        if (!variable_get('migrate_cache_counts', 0)) {
  76          $row->rowcount = _migrate_get_view_count($view);
  77        }
  78  
  79        $unimported = $row->rowcount - $imported;
  80  
  81        $msgtablename = migrate_message_table_name($row->mcsid);
  82        if ($unimported > 0) {
  83          $messages = '';
  84          /* Restore when views integration is fully implemented
  85          $unimported = l($unimported, 'admin/content/migrate/audit/'.$row->view_name.'_unimported');*/
  86          $numerrors = db_result(db_query("SELECT COUNT(DISTINCT sourceid)
  87                                           FROM {" . $msgtablename . "}
  88                                           WHERE level=%d",
  89                                          MIGRATE_MESSAGE_ERROR));
  90          if ($numerrors > 0) {
  91            $messages .= l(format_plural($numerrors, '1&nbsp;error', '@count&nbsp;errors'),
  92              "admin/content/tw/view/$msgtablename/" . MIGRATE_MESSAGE_ERROR,
  93              array('html' => TRUE))
  94              . '<br />';
  95          }
  96          $numwarnings = db_result(db_query("SELECT COUNT(DISTINCT sourceid)
  97                                             FROM {" . $msgtablename . "}
  98                                             WHERE level=%d",
  99                                            MIGRATE_MESSAGE_WARNING));
 100          if ($numwarnings > 0) {
 101            $messages .= l(format_plural($numwarnings, '1&nbsp;warning', '@count&nbsp;warnings'),
 102              "admin/content/tw/view/$msgtablename/" . MIGRATE_MESSAGE_WARNING,
 103              array('html' => TRUE))
 104              . '<br />';
 105          }
 106          $numnotices = db_result(db_query("SELECT COUNT(DISTINCT sourceid)
 107                                             FROM {" . $msgtablename . "}
 108                                             WHERE level=%d",
 109                                            MIGRATE_MESSAGE_NOTICE));
 110          if ($numnotices > 0) {
 111            $messages .= l(format_plural($numnotices, '1&nbsp;notice', '@count&nbsp;notices'),
 112              "admin/content/tw/view/$msgtablename/" . MIGRATE_MESSAGE_NOTICE,
 113              array('html' => TRUE))
 114              . '<br />';
 115          }
 116          if ($messages) {
 117            $unimported = $messages . " $unimported&nbsp;total";
 118          }
 119        }
 120        if ($imported > 0) {
 121          $numinformational = db_result(db_query("SELECT COUNT(DISTINCT sourceid)
 122                                             FROM {" . $msgtablename . "}
 123                                             WHERE level=%d",
 124                                            MIGRATE_MESSAGE_INFORMATIONAL));
 125          if ($numinformational > 0) {
 126            $imported .= '<br />' .
 127              l(format_plural($numinformational, '1 informational message', '@count informational messages'),
 128              "admin/content/tw/view/$msgtablename/" . MIGRATE_MESSAGE_INFORMATIONAL,
 129              array('html' => TRUE))
 130              . '<br />';
 131          }
 132        }
 133      }
 134      else {
 135        $imported = '';
 136        $unimported = '';
 137      }
 138  
 139      $form['data'][$rownum]['clearing'] = array('#value' => check_plain($row->clearing));
 140      $form['data'][$rownum]['importing'] = array('#value' => check_plain($row->importing));
 141      $form['data'][$rownum]['scanning'] = array('#value' => check_plain($row->scanning));
 142      $form['data'][$rownum]['description'] = array('#value' =>
 143        l($row->description, 'admin/content/migrate/content_sets/' . $row->mcsid));
 144      $form['data'][$rownum]['view_name'] = array('#value' =>
 145        l($row->view_name, 'admin/build/views/edit/'. $row->view_name));
 146      $form['data'][$rownum]['importrows'] = array('#value' => check_plain($row->rowcount));
 147      $form['data'][$rownum]['imported'] = array('#value' => $imported);
 148      $form['data'][$rownum]['unimported'] = array('#value' => $unimported);
 149      $form['data'][$rownum]['lastimported'] = array('#value' => $row->lastimported);
 150      $form['data'][$rownum]['mcsid'] = array('#value' => check_plain($row->mcsid));
 151      $form['data'][$rownum]['semaphore'] = array('#value' => $row->semaphore);
 152      $rownum++;
 153    }
 154  
 155    $form['clearing'] = array(
 156      '#type' => 'checkboxes',
 157      '#options' => $clearing,
 158      '#default_value' => $clearingenable,
 159    );
 160    $form['importing'] = array(
 161      '#type' => 'checkboxes',
 162      '#options' => $importing,
 163      '#default_value' => $importingenable,
 164    );
 165    $form['scanning'] = array(
 166      '#type' => 'checkboxes',
 167      '#options' => $scanning,
 168      '#default_value' => $scanningenable,
 169    );
 170    if (user_access(MIGRATE_ACCESS_ADVANCED)) {
 171      $form['interactive'] = array(
 172        '#type' => 'fieldset',
 173        '#title' => t('Interactive mode'),
 174        '#collapsible' => TRUE,
 175        '#collapsed' => FALSE,
 176        '#description' => t('While large migration tasks are best run in the non interactively (via drush or cron),
 177          you may choose here to begin tasks interactively; either because they are relatively small,
 178          or to test subsets of the migration.'),
 179      );
 180      $form['interactive']['donow'] = array(
 181        '#type' => 'checkbox',
 182        '#default_value' => 1,
 183        '#title' => t('Enable'),
 184        '#description' => t('If enabled, processing of selected processes begins immediately
 185          upon clicking Submit, with any unfinished processing continued in the background.
 186          If disabled, processing will be done entirely in the background beginning with the next cron.'),
 187      );
 188      $form['interactive']['limit'] = array(
 189        '#type' => 'textfield',
 190        '#title' => t('Sample size'),
 191        '#size' => 4,
 192        '#description' => t('Number of records to process in the current interactive run. Leave
 193          blank to process all records that can be completed within the PHP max_execution_time.'),
 194        '#default_value' => variable_get('migrate_limit', ''),
 195      );
 196      $form['interactive']['idlist'] = array(
 197        '#type' => 'textfield',
 198        '#title' => t('Source IDs'),
 199        '#size' => 30,
 200        '#description' => t('Enter a comma-separated list of IDs from the incoming content set, to
 201          process only those records in the current interactive run.'),
 202      );
 203      $form['clearsemaphores'] = array(
 204        '#type' => 'checkbox',
 205        '#title' => t('Unstick migration process'),
 206        '#description' => t('An error during processing can leave a migration semaphore enabled,
 207          preventing further processing. When this happens, you will see the message that a content
 208          set "has an operation already in progress" when you are sure there is no process currently running
 209          (even in cron or drush). Check this box to clear the semaphores when this happens.'),
 210      );
 211      $form['submit'] = array(
 212        '#type' => 'submit',
 213        '#value' => t("Submit"),
 214      );
 215    }
 216    return $form;
 217  }
 218  
 219  function theme_migrate_dashboard($form) {
 220    $output = drupal_render($form['description']);
 221    if (isset($form['data']) && is_array($form['data'])) {
 222      foreach (element_children($form['data']) as $rownum) {
 223        $row = array();
 224        foreach (element_children($form['data'][$rownum]) as $colname) {
 225          if ($colname == 'clearing' || $colname == 'importing' || $colname == 'scanning') {
 226            $row[] = drupal_render($form[$colname][$form['data'][$rownum]['mcsid']['#value']]);
 227            // Throw out the column contents
 228            drupal_render($form['data'][$rownum][$colname]);
 229            drupal_render($form['data'][$rownum]['mcsid']);
 230          }
 231          else {
 232            $row[] = drupal_render($form['data'][$rownum][$colname]);
 233          }
 234        }
 235        // Highlight any process currently running
 236        if ($form['data'][$rownum]['semaphore']['#value']) {
 237          $rows[] = array('data' => $row, 'class' => 'migrate-running');
 238        }
 239        else {
 240          $rows[] = $row;
 241        }
 242      }
 243    }
 244  
 245    $header = $form['header']['#value'];
 246    if (!$rows) {
 247      $rows[] = array(array('data' => t('No data in the table.'), 'colspan' => count($header)));
 248    }
 249  
 250    $output .= theme('table', $header, $rows, array('class' => 'migrate-dashboard'));
 251    $output .= drupal_render($form);
 252  
 253    return $output;
 254  }
 255  
 256  /*
 257   * Implementation of hook_submit().
 258   */
 259  function _migrate_dashboard_form_submit($form, &$form_state) {
 260    $started = time();
 261    foreach ($form_state['values']['importing'] as $mcsid => $value) {
 262      if ($value) {
 263        $importing = TRUE;
 264      }
 265      else {
 266        $importing = FALSE;
 267      }
 268      if ($form_state['values']['clearing'][$mcsid]) {
 269        $clearing = TRUE;
 270      }
 271      else {
 272        $clearing = FALSE;
 273      }
 274      if ($form_state['values']['scanning'][$mcsid]) {
 275        $scanning = TRUE;
 276      }
 277      else {
 278        $scanning = FALSE;
 279      }
 280      db_query("UPDATE {migrate_content_sets}
 281                SET clearing=%d, importing=%d, scanning=%d
 282                WHERE mcsid=%d",
 283               $clearing, $importing, $scanning, $mcsid);
 284    }
 285    if ($form_state['values']['clearsemaphores']) {
 286      db_query("UPDATE {migrate_content_sets} SET semaphore=0");
 287    }
 288    if ($form_state['values']['donow']) {
 289      variable_set('migrate_limit', $form_state['values']['limit']);
 290      $batch = array(
 291        'operations' => array(
 292          array('migrate_content_process_all_batch', array($started, $form_state['values']['limit'],
 293            trim($form_state['values']['idlist'])))),
 294        'title' => t('Migration processing'),
 295        'file' => drupal_get_path('module', 'migrate') . '/migrate_pages.inc',
 296        'init_message' => t('Starting migration process'),
 297        'progress_message' => t(''),
 298        'error_message' => t('An error occurred. Some or all of the migrate processing has failed.'),
 299        'finished' => '_migrate_content_process_finish',
 300      );
 301  
 302      batch_set($batch);
 303    }
 304  }
 305  
 306  /**
 307   * Batch API finished callback - report results
 308   *
 309   * @param $success
 310   * @param $results
 311   * @param $operations
 312   */
 313  function _migrate_content_process_finish($success, $results, $operations) {
 314    foreach ($results as $result) {
 315      drupal_set_message($result);
 316    }
 317  }
 318  
 319  /**
 320   * Enter description here...
 321   *
 322   * @return unknown
 323   */
 324  function migrate_content_sets() {
 325    return (drupal_get_form('_migrate_content_set_form'));
 326  }
 327  
 328  function _migrate_content_set_form($form_state) {
 329    // Fetch information on available destinations
 330    $desttypes = migrate_invoke_all('types');
 331  
 332    $header = array(
 333      array('data' => t('Description'), 'field' => 'description'),
 334      array('data' => t('Destination'), 'field' => 'contenttype'),
 335      array('data' => t('Source view'), 'field' => 'view_name'),
 336      array('data' => t('Weight'), 'field' => 'weight', 'sort' => 'asc'),
 337    );
 338  
 339    $sql = "SELECT *
 340            FROM {migrate_content_sets} ";
 341    $sql .= tablesort_sql($header);
 342    $result = db_query($sql);
 343    $used_views = array();
 344    while ($row = db_fetch_object($result)) {
 345      if ($row->desttype) {
 346        $destination = $desttypes[$row->contenttype . '/' . $row->desttype];
 347      }
 348      else {
 349        $destination = $desttypes[$row->contenttype];
 350      }
 351      $rows[] = array('data' =>
 352        array(
 353          l($row->description, 'admin/content/migrate/content_sets/' . $row->mcsid,
 354            array('html' => TRUE)),
 355          $destination,
 356          l($row->view_name, 'admin/build/views/edit/' . $row->view_name),
 357          $row->weight,
 358        ),
 359        'class' => "migrate-content-sets-tr",
 360      );
 361      $used_views[$row->view_name] = $row->view_name;
 362    }
 363  
 364    if (!isset($rows)) {
 365      $rows[] = array(array('data' => t('No content sets have been defined'), 'colspan' => count($header)));
 366    }
 367  
 368    $form['setlist'] = array(
 369      '#value' => theme('table', $header, $rows, array('id' => 'migrate-content-sets')),
 370    );
 371    $form['addset'] = array(
 372      '#type' => 'fieldset',
 373      '#title' => t('Add a content set'),
 374      '#collapsible' => TRUE,
 375    );
 376  
 377    $form['addset']['description'] = array(
 378      '#type' => 'textfield',
 379      '#title' => t('Description of the content set'),
 380      '#description' => t('A friendly phrase only used for display.'),
 381    );
 382  
 383    $form['addset']['contenttype'] = array(
 384      '#type' => 'select',
 385      '#title' => t('Destination'),
 386      '#options' => $desttypes,
 387      '#description' => t('The type of Drupal content which will store the incoming records.'),
 388    );
 389  
 390    $views = views_get_all_views();
 391    foreach ($views as $name => $view) {
 392      if (!isset($used_views[$name])) {
 393        if ($view->tag) {
 394          $options[$name] = $view->tag . ': ' . $name;
 395        }
 396        else {
 397          $options[$name] = $name;
 398        }
 399        if ($view->description) {
 400          if (drupal_strlen($view->description) > 60) {
 401            $view->description = drupal_substr($view->description, 0, 57) . '...';
 402          }
 403          $options[$name] .= " ($view->description)";
 404        }
 405      }
 406    }
 407    asort($options);
 408    $form['addset']['sourceview'] = array(
 409      '#type' => 'select',
 410      '#title' => t('Source view from which to import content'),
 411      '#options' => $options,
 412      '#description' => t('This View defines the records which are to be migrated.'),
 413    );
 414  
 415    $form['addset']['weight'] = array(
 416      '#type' => 'textfield',
 417      '#title' => t('Weight'),
 418      '#description' => t('The order in which content sets will be processed and displayed.'),
 419    );
 420    $form['addset']['add'] = array(
 421      '#type' => 'submit',
 422      '#value' => t('Add'),
 423    );
 424    return $form;
 425  }
 426  
 427  function _migrate_content_set_validate($form_state) {
 428    $description = trim($form_state['values']['description']);
 429    if (!$description) {
 430      form_set_error('description', t('A description is required.'));
 431    }
 432    $weight = trim($form_state['values']['weight']);
 433    if (!is_numeric($weight) || (((int)$weight) != $weight)) {
 434      form_set_error('weight', t('Weight must be an integer value (positive or negative)'));
 435    }
 436  }
 437  
 438  function _migrate_content_set_form_validate($form, $form_state) {
 439    _migrate_content_set_validate($form_state);
 440  }
 441  
 442  function _migrate_content_set_form_submit($form, &$form_state) {
 443    $content_set['view_name'] = $form_state['values']['sourceview'];
 444    $content_set['description'] = $form_state['values']['description'];
 445    $destination = explode('/', $form_state['values']['contenttype']);
 446    $content_set['contenttype'] = $destination[0];
 447    if (isset($destination[1])) {
 448      $content_set['desttype'] = $destination[1];
 449    }
 450    $content_set['weight'] = $form_state['values']['weight'];
 451  
 452    $mcsid = migrate_save_content_set($content_set);
 453    // Go straight to the mapping form
 454    $form_state['redirect'] = "admin/content/migrate/content_sets/$mcsid";
 455  }
 456  
 457  /**
 458   * Menu callback function.
 459   */
 460  function migrate_content_set_mappings($form_state, $mcsid) {
 461    $row = db_fetch_object(db_query("SELECT * FROM {migrate_content_sets}
 462                                     WHERE mcsid=%d",
 463                                    $mcsid));
 464    $desttype = $row->desttype;
 465    $view_name = $row->view_name;
 466    $sourcekey = $row->sourcekey;
 467    $description = $row->description;
 468    $contenttype = $row->contenttype;
 469    $desttype = $row->desttype;
 470    $weight = $row->weight;
 471  
 472    // Fetch information on available destinations
 473    $desttypes = migrate_invoke_all('types');
 474    $destfields = migrate_invoke_all("fields_$contenttype", $desttype);
 475  
 476    $form['mcsid'] = array(
 477      '#type' => 'value',
 478      '#value' => $mcsid,
 479    );
 480    $form['description'] = array(
 481      '#type' => 'textfield',
 482      '#title' => t('Description of the content set'),
 483      '#default_value' => $description,
 484    );
 485    $form['show_view_name'] = array(
 486      '#prefix' => '<div>',
 487      '#value' => t('<strong>Source view:</strong> ') . l($view_name, 'admin/build/views/edit/' . $view_name),
 488      '#suffix' => '</div>',
 489    );
 490    $form['view_name'] = array(
 491      '#type' => 'value',
 492      '#value' => $view_name,
 493    );
 494    if ($desttype) {
 495      $destination = $desttypes["$contenttype/$desttype"];
 496    }
 497    else {
 498      $destination = $desttypes[$contenttype];
 499    }
 500    $form['show_contenttype'] = array(
 501      '#prefix' => '<div>',
 502      '#value' => t('<strong>Destination:</strong> ') . $destination,
 503      '#suffix' => '</div>',
 504    );
 505    $form['contenttype'] = array(
 506      '#type' => 'value',
 507      '#value' => $contenttype,
 508    );
 509    $form['desttype'] = array(
 510      '#type' => 'value',
 511      '#value' => $desttype,
 512    );
 513  
 514    $form['weight'] = array(
 515      '#type' => 'textfield',
 516      '#title' => t('Weight'),
 517      '#description' => t('The order in which content sets will be processed and displayed.'),
 518      '#default_value' => $weight,
 519    );
 520  
 521    $form['header'] = array(
 522      '#type' => 'value',
 523      '#value' => array(
 524        array('data' => t('Source field')),
 525        array('data' => t('Default value')),
 526        array('data' => t('Destination field')),
 527      ),
 528    );
 529  
 530    $view = views_get_view($view_name);
 531    if (!$view) {
 532      drupal_set_message(t('View !view does not exist - either (re)create a view with
 533        this name, or delete this content set.', array('!view' => $view_name)));
 534    }
 535    else {
 536      // Need to fill in the query, to find out the aliases that will be returned by the
 537      // query
 538      $view->build();
 539      $fields = $view->get_items('field');
 540      $srcoptions = array();
 541      foreach ($view->query->fields as $fieldalias => $field) {
 542        $fieldname = $field['field'];
 543        $fieldtable = $field['table'];
 544        // The field name can be ambiguous (e.g., two map tables in the view), so
 545        // we can't just do $fields[$fieldname] - we need to iterate and match the
 546        // table as weel
 547        foreach ($fields as $viewfieldname => $viewfield) {
 548          if ($viewfield['field'] == $fieldname && $viewfield['table'] == $fieldtable) {
 549            $srcoptions[$fieldalias] = $viewfield['label'];
 550            break;
 551          }
 552        }
 553        if (!isset($srcoptions[$fieldalias])) {
 554          $srcoptions[$fieldalias] = $fieldname;
 555        }
 556      }
 557  
 558      $form['sourcekey'] = array(
 559        '#type' => 'select',
 560        '#options' => $srcoptions,
 561        '#default_value' => $sourcekey,
 562        '#title' => t('Primary key of source view'),
 563      );
 564  
 565      $mappings = array();
 566      $defaults = array();
 567  
 568      $srcoptions = array_merge(array('' => t('<none>')), $srcoptions);
 569      foreach ($destfields as $destfield => $destname) {
 570        $sql = "SELECT *
 571                FROM {migrate_content_mappings}
 572                WHERE mcsid=%d AND destfield='%s'";
 573        $result = db_query($sql, $mcsid, $destfield);
 574        $row = db_fetch_object($result);
 575        $cols[] = $destfield;
 576        $form['srcfield'][$destfield] = array(
 577          '#type' => 'select',
 578          '#options' => $srcoptions,
 579          '#default_value' => isset($row->srcfield) ? $row->srcfield : '',
 580        );
 581        $form['default_value'][$destfield] = array(
 582          '#type' => 'textfield',
 583          '#default_value' => isset($row->default_value) ? $row->default_value : '',
 584          '#size' => 25,
 585          '#maxlength' => 255,
 586        );
 587        $form['destfield'][$destfield] = array('#value' => $destname);
 588      }
 589  
 590      $form['cols'] = array(
 591        '#type' => 'value',
 592        '#value' => $cols,
 593      );
 594  
 595      $form['submit'] = array(
 596        '#type' => 'submit',
 597        '#value' => t('Submit changes'),
 598      );
 599    }
 600    $form['delete'] = array(
 601      '#type' => 'submit',
 602      '#value' => t('Delete'),
 603    );
 604    $form['#tree'] = TRUE;
 605    return $form;
 606  }
 607  
 608  function theme_migrate_content_set_mappings($form) {
 609    $output = drupal_render($form['description']);
 610    $output .= drupal_render($form['show_view_name']);
 611    $output .= drupal_render($form['show_contenttype']);
 612    $output .= drupal_render($form['desttype']);
 613    $output .= drupal_render($form['sourcekey']);
 614    $output .= drupal_render($form['weight']);
 615  
 616    if (isset($form['destfield']) && is_array($form['destfield'])) {
 617      foreach (element_children($form['destfield']) as $destfield) {
 618        $row = array();
 619        $row[] = drupal_render($form['srcfield'][$destfield]);
 620        $row[] = drupal_render($form['default_value'][$destfield]);
 621        $row[] = drupal_render($form['destfield'][$destfield]);
 622        $rows[] = $row;
 623      }
 624    }
 625  
 626    $header = $form['header']['#value'];
 627    if (!$rows) {
 628      $rows[] = array(array('data' => t('No data in the table.'), 'colspan' => count($header)));
 629    }
 630    $output .=     t('Map fields in the Source recordset to proprties in a Drupal object. More complex fields such as multiple value elements need to be handled in code using the prepare hook.');
 631    $output .= theme('table', $header, $rows);
 632    $output .= drupal_render($form['submit']);
 633    $output .= drupal_render($form);
 634  
 635    return $output;
 636  }
 637  
 638  function migrate_content_set_mappings_validate($form, $form_state) {
 639    _migrate_content_set_validate($form_state);
 640  }
 641  
 642  /**
 643   * Implementation of hook_submit().
 644   */
 645  function migrate_content_set_mappings_submit($form, &$form_state) {
 646    $mcsid = $form_state['values']['mcsid'];
 647    if ($form_state['clicked_button']['#parents'][0] == 'delete') {
 648      migrate_delete_content_set($mcsid);
 649      drupal_set_message(t('Content set deleted'));
 650      $form_state['redirect'] = 'admin/content/migrate/content_sets';
 651    }
 652    else {
 653      // Get list of current mappings, so we know if any need deletion
 654      $sql = "SELECT mcmid,destfield FROM {migrate_content_mappings} WHERE mcsid=%d";
 655      $result = db_query($sql, $mcsid);
 656      $prevdests = array();
 657      while ($row = db_fetch_object($result)) {
 658        $prevdests[$row->destfield] = $row->mcmid;
 659      }
 660      $sourcekey = $form_state['values']['sourcekey'];
 661      foreach ($form_state['values']['cols'] as $key => $destfield) {
 662        $mapping = new stdClass;
 663        $mapping->mcsid = $mcsid;
 664        $mapping->srcfield = $form_state['values']['srcfield'][$destfield];
 665        $mapping->destfield = $destfield;
 666        $mapping->default_value = $form_state['values']['default_value'][$destfield];
 667        $mapping->mcmid = db_result(db_query(
 668          "SELECT mcmid
 669           FROM {migrate_content_mappings}
 670           WHERE mcsid=%d AND destfield='%s'",
 671          $mcsid, $destfield));
 672        migrate_save_content_mapping($mapping);
 673        // This mapping is currently valid, so remove from the deletion list
 674        unset($prevdests[$mapping->destfield]);
 675      }
 676      // Delete any mappings we didn't save
 677      foreach ($prevdests as $mcmid) {
 678        migrate_delete_content_mapping($mcmid);
 679      }
 680      migrate_save_content_set($form_state['values']);
 681      drupal_set_message('Changes saved');
 682    }
 683  }
 684  
 685  function migrate_tools() {
 686    return drupal_get_form('_migrate_tools_form');
 687  }
 688  
 689  function _migrate_tools_form($form_state) {
 690    $header = array(
 691      array('data' => t('Clearing')),
 692      array('data' => t('Type')),
 693      array('data' => t('# Nodes')),
 694      );
 695    $form['header'] = array('#type' => 'value', '#value' => $header);
 696  
 697    $sql = "SELECT type, count(type) AS numnodes
 698            FROM {node}
 699            GROUP BY type
 700            ORDER BY type";
 701    $result = db_query($sql);
 702  
 703    $clearing = array();
 704    $rownum = 0;
 705    while ($row = db_fetch_object($result)) {
 706      $clearing[$row->type] = '';
 707  
 708      $form['data'][$rownum]['clearing'] = array('#value' => check_plain(0));
 709      $form['data'][$rownum]['type'] = array('#value' => check_plain($row->type));
 710      $form['data'][$rownum]['numnodes'] = array('#value' => check_plain($row->numnodes));
 711      $rownum++;
 712    }
 713  
 714    $form['clearing'] = array(
 715      '#type' => 'checkboxes',
 716      '#options' => $clearing,
 717      '#default_value' => array(),
 718    );
 719    $form['submit'] = array(
 720      '#type' => 'submit',
 721      '#value' => t("Delete nodes"),
 722    );
 723    return $form;
 724  }
 725  
 726  function theme_migrate_tools($form) {
 727    $output = drupal_render($form['description']);
 728    if (isset($form['data']) && is_array($form['data'])) {
 729      foreach (element_children($form['data']) as $rownum) {
 730        $row = array();
 731        foreach (element_children($form['data'][$rownum]) as $colname) {
 732          if ($colname == 'clearing') {
 733            $row[] = drupal_render($form[$colname][$form['data'][$rownum]['type']['#value']]);
 734            // Throw out the column contents
 735            drupal_render($form['data'][$rownum][$colname]);
 736          }
 737          else {
 738            $row[] = drupal_render($form['data'][$rownum][$colname]);
 739          }
 740        }
 741        $rows[] = $row;
 742      }
 743    }
 744  
 745    $header = $form['header']['#value'];
 746    if (!$rows) {
 747      $rows[] = array(array('data' => t('No data in the table.'), 'colspan' => count($header)));
 748    }
 749  
 750    $output .= theme('table', $header, $rows);
 751    $output .= drupal_render($form);
 752  
 753    return $output;
 754  }
 755  
 756  /*
 757   * Implementation of hook_submit().
 758   */
 759  function _migrate_tools_form_submit($form, &$form_state) {
 760    $started = time();
 761    foreach ($form_state['values']['clearing'] as $type => $value) {
 762      if ($value) {
 763        drupal_set_message("Deleting all $type nodes");
 764        $sql = "SELECT nid FROM {node} WHERE type='%s'";
 765        $result = db_query($sql, $type);
 766        while ($row = db_fetch_object($result)) {
 767          node_delete($row->nid);
 768        }
 769      }
 770    }
 771  }
 772  
 773  function migrate_settings() {
 774    return drupal_get_form('_migrate_settings_form');
 775  }
 776  
 777  function _migrate_settings_form($form_state) {
 778    $form['enable_cron'] = array(
 779      '#type' => 'checkbox',
 780      '#title' => t('Enable cron processing'),
 781      '#description' => t('If this is enabled, any operations checked off on the
 782        dashboard page will be processed when the cron operation runs. Disable this
 783        if you only perform migration operations interactively or via drush.'),
 784      '#default_value' => variable_get('migrate_enable_cron', 0),
 785    );
 786  
 787    $form['display_timers'] = array(
 788      '#type' => 'checkbox',
 789      '#title' => t('Display timers when processing'),
 790      '#description' => t('To diagnose performance bottlenecks, turn this toggle
 791        on - at the completion of a processing round, cumulative times of
 792        tasks will be displayed.'),
 793      '#default_value' => variable_get('migrate_display_timers', 0),
 794    );
 795  
 796    $form['hide_help_message'] = array(
 797      '#type' => 'checkbox',
 798      '#title' => t('Ignore missing advanced help module'),
 799      '#description' => t('Migrate uses the Advanced Help module to provide help text; if this module is not present Migrate will complain, unless this setting is checked.'),
 800      '#default_value' => variable_get('migrate_hide_help_message', FALSE),
 801    );
 802  
 803    $form['cache_counts'] = array(
 804      '#type' => 'checkbox',
 805      '#title' => t('Cache content set counts'),
 806      '#description' => t('With large and/or complex content sets, getting the <strong>Rows
 807        in Set</strong> value on the Process page can be time-consuming. Enable caching to
 808        store the last known count in the database.'),
 809      '#default_value' => variable_get('migrate_cache_counts', 0),
 810    );
 811  
 812    if (variable_get('migrate_cache_counts', 0)) {
 813      $form['refresh_counts'] = array(
 814        '#type' => 'checkbox',
 815        '#title' => t('Refresh cached content set counts'),
 816        '#description' => t('Update the cached content set counts to reflect their current values.'),
 817        '#default_value' => 0,
 818      );
 819    }
 820  
 821    $form['submit'] = array(
 822      '#type' => 'submit',
 823      '#value' => t("Submit"),
 824    );
 825    return $form;
 826  }
 827  
 828  function theme_migrate_settings($form) {
 829    $output = '';
 830    $output .= drupal_render($form['enable_cron']);
 831    $output .= drupal_render($form['display_timers']);
 832    $output .= drupal_render($form['cache_counts']);
 833    $output .= drupal_render($form['refresh_counts']);
 834    $output .= drupal_render($form);
 835  
 836    return $output;
 837  }
 838  
 839  /*
 840   * Implementation of hook_submit().
 841   */
 842  function _migrate_settings_form_submit($form, &$form_state) {
 843    $values = $form_state['values'];
 844    variable_set('migrate_enable_cron', $values['enable_cron']);
 845    variable_set('migrate_display_timers', $values['display_timers']);
 846    variable_set('migrate_hide_help_message', $values['hide_help_message']);
 847    $original_cache = variable_get('migrate_cache_counts', 0);
 848    variable_set('migrate_cache_counts', $values['cache_counts']);
 849    if (isset($values['refresh_counts']) || ($values['cache_counts'] && !$original_cache)) {
 850      $sql = "SELECT mcsid,view_name FROM {migrate_content_sets}";
 851      $result = db_query($sql);
 852      while ($row = db_fetch_object($result)) {
 853        $rowcount = _migrate_get_view_count($row->view_name);
 854        $sql = "UPDATE {migrate_content_sets}
 855                SET rowcount=%d WHERE mcsid=%d";
 856        db_query($sql, $rowcount, $row->mcsid);
 857      }
 858      if ($values['refresh_counts']) {
 859        drupal_set_message(t('Content set counts refreshed'));
 860      }
 861      else {
 862        drupal_set_message(t('Content set counts saved'));
 863      }
 864    }
 865    drupal_set_message(t('Settings saved'));
 866  }
 867  
 868  function _migrate_get_view_count($view) {
 869    if (is_string($view)) {
 870      $view = views_get_view($view);
 871    }
 872    // Force execution of count query, with minimal unnecessary results returned
 873    // @TODO: Find way to execute ONLY count query
 874    $view->pager['items_per_page'] = 1;
 875    $view->get_total_rows = TRUE;
 876    $view->execute();
 877    return $view->total_rows;
 878  }


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