You are here

function cron_debug_form in Cron Debug 7

Create the main form.

This form serves both as the launch form for the debug run and as the result display when it has run.

1 string reference to 'cron_debug_form'
cron_debug_menu in ./cron_debug.module
Implements hook_menu().

File

./cron_debug.module, line 100
Cron debugging for administrators.

Code

function cron_debug_form($form, &$form_state) {

  // Start the form
  $form = array();
  $num = 0;
  $reset = '';

  // Fetch note base on flag
  $module = cron_debug_check_flag();

  // Set a message to the user
  if ($module) {

    // An error of some kind occured.
    if ($module == CRON_DEBUG_RUNNING) {

      // Failed in cron_debug_run(), somehow.
      drupal_set_message(t('Cron debug may be running or halted before it was finished.'));
    }
    else {

      // Failed in $module_cron().
      drupal_set_message(t('Cron presumably failed while running the cron hook <em>@module_cron()</em> in the module @module.', array(
        '@module' => $module,
      )), 'error');
    }
    drupal_set_message(t('You can see details about the progress of cron functions <a href="@url">in the log</a>. Filter for <em>cron debug</em> messages and look at the most recent entries at the top of the list.', array(
      '@url' => url('admin/reports/dblog'),
    )));
  }

  // Get info about modules
  $modules_data = system_rebuild_module_data();

  // Data for cron hooks table
  $header = array(
    'module' => t('Module'),
    'description' => t('Description'),
    'status' => t('Status'),
    'operations' => t('Operations'),
  );
  $rows = array();
  foreach (module_implements('cron') as $module) {
    $rows[$module] = array(
      'module' => '<strong>' . check_plain($modules_data[$module]->info['name']) . '</strong>',
      'description' => filter_xss($modules_data[$module]->info['description']),
      'status' => empty($form_state['storage']) ? ' ' : (!empty($form_state['storage']['results'][$module]) ? $form_state['storage']['results'][$module] : t('Not run')),
      'operations' => l(t('Run individually.'), 'admin/config/system/cron/debug/run/' . $module, array(
        'attributes' => array(
          'title' => t('Run the function @function', array(
            '@function' => $module . '_cron()',
          )),
        ),
      )),
    );
    $values[$module] = !empty($form_state['storage']['results'][$module]) || empty($form_state['storage']) ? $module : FALSE;
  }

  // Buttons
  $form['actions'] = array(
    '#type' => 'actions',
    '#weight' => 30,
  );

  // Set form stage storage
  if (!isset($form_state['storage'])) {
    $form_state['storage'] = array(
      'results' => array(
        'done' => '',
      ),
    );
  }
  else {

    // Add reset link
    $form['actions']['reset'] = array(
      '#type' => 'link',
      '#title' => t('Reset'),
      '#href' => 'admin/config/system/cron/debug',
      '#attributes' => array(
        'title' => t('Clear results and reset the form'),
      ),
      '#weight' => 20,
    );
  }

  // Create checkboxes and display results if any
  $form['cron'] = array(
    '#type' => 'tableselect',
    '#title' => t('Cron hooks'),
    '#header' => $header,
    '#options' => $rows,
    '#default_value' => $values,
    '#suffix' => '<p>' . $form_state['storage']['results']['done'] . t('The cron hooks wil be run in the sequence listed above. Uncheck the cron hooks you do not want to run. Please notice that your regular cron jobs will run if you have set up cron on the web server or <a href="@url">enabled cron in the setup</a>. You might want to disable cron <a href="@url">here</a> or on the server while debugging with Cron Debug.', array(
      '@url' => url('admin/config/system/cron'),
    )) . '</p>',
    '#weight' => 20,
  );
  $form['actions']['run'] = array(
    '#type' => 'submit',
    '#value' => t('Run'),
    '#weight' => 10,
  );
  return $form;
}