You are here

function _migrate_dashboard_form in Migrate 6

Form definition for dashboard page

1 string reference to '_migrate_dashboard_form'
migrate_dashboard in ./migrate_pages.inc
Menu callback for dashboard page

File

./migrate_pages.inc, line 17

Code

function _migrate_dashboard_form($form_state) {
  migrate_check_advanced_help();
  $header = array(
    array(
      'data' => t('Clear'),
    ),
    array(
      'data' => t('Import'),
    ),
    array(
      'data' => t('Content Set'),
    ),
    array(
      'data' => t('Total rows'),
    ),
    array(
      'data' => t('Imported'),
    ),
    array(
      'data' => t('Unimported'),
    ),
    array(
      'data' => t('Last imported'),
    ),
  );
  $form['header'] = array(
    '#type' => 'value',
    '#value' => $header,
  );
  $sql = "SELECT *\n          FROM {migrate_content_sets}\n          ORDER BY weight, contenttype, view_name, view_args";
  $result = db_query($sql);
  $clearing = array();
  $importing = array();
  $rownum = 0;
  while ($row = db_fetch_object($result)) {
    $status = '';
    if ($row->mcsid) {
      $view = views_get_view($row->view_name);
      if (!$view) {
        drupal_set_message(t('View !view does not exist - either (re)create this view, or
            remove the content set !content_set', array(
          '!view' => $row->view_name,
          '!content_set' => l($row->description, "admin/content/migrate/content_set/{$row->mcsid}"),
        )));
        continue;
      }
      $clearing[$row->mcsid] = '';
      $importing[$row->mcsid] = '';
      $status = t('N/A');
      $maptable = migrate_map_table_name($row->mcsid);
      $sourcekey = $row->sourcekey;
      if (db_table_exists($maptable)) {
        $imported = db_result(db_query('SELECT COUNT(*) FROM {' . $maptable . '}'));
      }
      else {
        $imported = 0;
      }

      // If not caching counts, override the saved count with a fresh count
      if (!variable_get('migrate_cache_counts', 0)) {
        $row->rowcount = _migrate_get_view_count($view, $row->view_args);
      }
      $unimported = $row->rowcount - $imported;
      $msgtablename = migrate_message_table_name($row->mcsid);
      if (db_table_exists($msgtablename)) {
        if ($unimported > 0) {
          $messages = '';
          $numerrors = db_result(db_query("SELECT COUNT(DISTINCT sourceid)\n                                           FROM {" . $msgtablename . "}\n                                           WHERE level=%d", MIGRATE_MESSAGE_ERROR));
          if ($numerrors > 0) {
            if (module_exists('tw')) {
              $messages .= l(format_plural($numerrors, '1 error', '@count errors'), "admin/content/tw/view/{$msgtablename}/" . MIGRATE_MESSAGE_ERROR, array(
                'html' => TRUE,
              ));
            }
            else {
              $messages .= format_plural($numerrors, '1 error', '@count errors');
            }
            $messages .= '<br />';
          }
          $numwarnings = db_result(db_query("SELECT COUNT(DISTINCT sourceid)\n                                             FROM {" . $msgtablename . "}\n                                             WHERE level=%d", MIGRATE_MESSAGE_WARNING));
          if ($numwarnings > 0) {
            if (module_exists('tw')) {
              $messages .= l(format_plural($numwarnings, '1&nbsp;warning', '@count&nbsp;warnings'), "admin/content/tw/view/{$msgtablename}/" . MIGRATE_MESSAGE_WARNING, array(
                'html' => TRUE,
              ));
            }
            else {
              $messages .= format_plural($numwarnings, '1&nbsp;warning', '@count&nbsp;warnings');
            }
            $messages .= '<br />';
          }
          $numnotices = db_result(db_query("SELECT COUNT(DISTINCT sourceid)\n                                             FROM {" . $msgtablename . "}\n                                             WHERE level=%d", MIGRATE_MESSAGE_NOTICE));
          if ($numnotices > 0) {
            if (module_exists('tw')) {
              $messages .= l(format_plural($numnotices, '1&nbsp;notice', '@count&nbsp;notices'), "admin/content/tw/view/{$msgtablename}/" . MIGRATE_MESSAGE_NOTICE, array(
                'html' => TRUE,
              ));
            }
            else {
              $messages .= format_plural($numnotices, '1&nbsp;notice', '@count&nbsp;notices');
            }
            $messages .= '<br />';
          }
          if ($messages) {
            $unimported = $messages . " {$unimported}&nbsp;total";
          }
        }
        if ($imported > 0) {
          $numinformational = db_result(db_query("SELECT COUNT(DISTINCT sourceid)\n                                             FROM {" . $msgtablename . "}\n                                             WHERE level=%d", MIGRATE_MESSAGE_INFORMATIONAL));
          if ($numinformational > 0) {
            $imported .= '<br />';
            if (module_exists('tw')) {
              $imported .= l(format_plural($numinformational, '1 informational message', '@count informational messages'), "admin/content/tw/view/{$msgtablename}/" . MIGRATE_MESSAGE_INFORMATIONAL, array(
                'html' => TRUE,
              ));
            }
            else {
              $imported .= format_plural($numinformational, '1 informational message', '@count informational messages');
            }
            $imported .= '<br />';
          }
        }
      }
      else {
        $imported = '';
        $unimported = '';
      }
    }
    else {
      $imported = '';
      $unimported = '';
    }
    $form['data'][$rownum]['clearing'] = array(
      '#value' => 0,
    );
    $form['data'][$rownum]['importing'] = array(
      '#value' => 0,
    );
    $form['data'][$rownum]['description'] = array(
      '#value' => l($row->description, 'admin/content/migrate/content_set/' . $row->mcsid),
    );
    $form['data'][$rownum]['importrows'] = array(
      '#value' => check_plain($row->rowcount),
    );
    $form['data'][$rownum]['imported'] = array(
      '#value' => $imported,
    );
    $form['data'][$rownum]['unimported'] = array(
      '#value' => $unimported,
    );
    $form['data'][$rownum]['lastimported'] = array(
      '#value' => $row->lastimported,
    );
    $form['data'][$rownum]['mcsid'] = array(
      '#value' => check_plain($row->mcsid),
    );
    $form['data'][$rownum]['status'] = array(
      '#value' => $row->status,
    );
    $rownum++;
  }
  $form['clearing'] = array(
    '#type' => 'checkboxes',
    '#options' => $clearing,
  );
  $form['importing'] = array(
    '#type' => 'checkboxes',
    '#options' => $importing,
  );
  if (user_access(MIGRATE_ACCESS_ADVANCED)) {

    // Open the fieldset if we're coming back from a run
    // TODO: Or from a Stop
    $collapsed = strpos(referer_uri(), 'batch') === FALSE;
    $form['interactive'] = array(
      '#type' => 'fieldset',
      '#title' => t('Execute'),
      '#collapsible' => TRUE,
      '#collapsed' => $collapsed,
      '#description' => t('<p>While large migration tasks are best run via
        <a href="http://drupal.org/project/drush">drush</a>,
        you may choose here to execute tasks directly. Check any tasks you want to run,
        and click the Run button. Note that the settings below will be applied to each
        task you run.</p>
        <p>Running tasks may be cleanly stopped by clicking <b>Stop all running tasks</b>.'),
    );
    $form['interactive']['update'] = array(
      '#type' => 'checkbox',
      '#title' => t('Update previously-imported content'),
      '#description' => t('If unchecked, import operations will only process source
        data which has not already been imported. If checked, they will also replace
        previously-imported destination objects with the current source information.'),
      '#default_value' => variable_get('migrate_update', 0),
    );
    $form['interactive']['limit'] = array(
      '#type' => 'textfield',
      '#title' => t('Sample size'),
      '#size' => 4,
      '#description' => t('Number of records to process. Leave
        blank to process all records in the content set.'),
      '#default_value' => variable_get('migrate_limit', ''),
    );
    $form['interactive']['idlist'] = array(
      '#type' => 'textfield',
      '#title' => t('Source IDs'),
      '#size' => 30,
      '#description' => t('Enter a comma-separated list of IDs from the incoming content set, to
        process only those records.'),
    );
    $form['interactive']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Run'),
      '#submit' => array(
        '_migrate_dashboard_form_run',
      ),
    );
    $form['interactive']['stop'] = array(
      '#type' => 'submit',
      '#value' => t('Stop all running tasks'),
      '#submit' => array(
        '_migrate_dashboard_form_stop',
      ),
    );
  }

  // Fetch information on available destinations
  $desttypes = migrate_invoke_all('types');
  $form['addset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add a content set'),
    '#collapsible' => TRUE,
    '#collapsed' => $rownum == 0 ? FALSE : TRUE,
  );
  $form['addset']['machine_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Content set name'),
    '#size' => 24,
    '#maxlength' => 50,
    '#description' => t('This is the unique name of the content set. It must contain
      only alphanumeric characters and underscores; it is used to identify the
      content set internally and to generate map and message tables related to
      the content set.'),
  );
  $form['addset']['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description of the content set'),
    '#description' => t('User-friendly description of the content set'),
  );
  $form['addset']['contenttype'] = array(
    '#type' => 'select',
    '#title' => t('Destination'),
    '#options' => $desttypes,
    '#description' => t('The type of Drupal content which will store the incoming records.'),
  );
  $views = views_get_all_views();
  foreach ($views as $name => $view) {
    if ($view->tag) {
      $options[$name] = $view->tag . ': ' . $name;
    }
    else {
      $options[$name] = $name;
    }
    if ($view->description) {
      if (drupal_strlen($view->description) > 60) {
        $view->description = drupal_substr($view->description, 0, 57) . '...';
      }
      $options[$name] .= " ({$view->description})";
    }
  }
  asort($options);
  $form['addset']['view_name'] = array(
    '#type' => 'select',
    '#title' => t('Source view from which to import content'),
    '#options' => $options,
    '#description' => t('This View defines the records which are to be migrated.'),
  );
  $form['addset']['view_args'] = array(
    '#type' => 'textfield',
    '#title' => t('View arguments'),
    '#description' => t('Arguments to apply to the view when processing,
      separated with a / as though they were a URL path.'),
    '#maxlength' => 255,
    '#size' => 30,
  );
  $form['addset']['weight'] = array(
    '#type' => 'textfield',
    '#title' => t('Weight'),
    '#required' => TRUE,
    '#default_value' => 0,
    '#description' => t('The order in which content sets will be processed and displayed.'),
  );
  $form['addset']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
    '#submit' => array(
      '_migrate_content_set_form_submit',
    ),
    '#validate' => array(
      '_migrate_content_set_form_validate',
    ),
  );
  return $form;
}