function postProcess()
 {
     $values = $this->controller->exportValues($this->_name);
     // Combine all fields into on active_fields array for easier processing.
     $active_fields = array();
     if (array_key_exists('active_fundraising_fields', $values)) {
         $active_fields = $active_fields + $values['active_fundraising_fields'];
     }
     if (array_key_exists('active_membership_fields', $values)) {
         $active_fields = $active_fields + $values['active_membership_fields'];
     }
     if (array_key_exists('active_event_standard_fields', $values)) {
         $active_fields = $active_fields + $values['active_event_standard_fields'];
     }
     if (array_key_exists('active_event_turnout_fields', $values)) {
         $active_fields = $active_fields + $values['active_event_turnout_fields'];
     }
     if (count($active_fields) > 0) {
         $current_active_fields = sumfields_get_setting('active_fields', array());
         $new_active_fields = $this->options_to_array($active_fields);
         if ($current_active_fields != $new_active_fields) {
             // Setting 'new_active_fields' will alert the system that we have
             // field changes to be applied.
             sumfields_save_setting('new_active_fields', $new_active_fields);
         }
     }
     if (array_key_exists('financial_type_ids', $values)) {
         sumfields_save_setting('financial_type_ids', $this->options_to_array($values['financial_type_ids']));
     }
     if (array_key_exists('membership_financial_type_ids', $values)) {
         sumfields_save_setting('membership_financial_type_ids', $this->options_to_array($values['membership_financial_type_ids']));
     }
     if (array_key_exists('event_type_ids', $values)) {
         sumfields_save_setting('event_type_ids', $this->options_to_array($values['event_type_ids']));
     }
     if (array_key_exists('participant_status_ids', $values)) {
         sumfields_save_setting('participant_status_ids', $this->options_to_array($values['participant_status_ids']));
     }
     if (array_key_exists('participant_noshow_status_ids', $values)) {
         sumfields_save_setting('participant_noshow_status_ids', $this->options_to_array($values['participant_noshow_status_ids']));
     }
     $session = CRM_Core_Session::singleton();
     sumfields_save_setting('generate_schema_and_data', 'scheduled:' . date('Y-m-d H:i:s'));
     if ($values['when_to_apply_change'] == 'on_submit') {
         $returnValues = array();
         if (!sumfields_gen_data($returnValues)) {
             $msg = ts("There was an error applying your changes.", array('domain' => 'net.ourpowerbase.sumfields'));
         } else {
             $msg = ts("Changes were applied successfully.", array('domain' => 'net.ourpowerbase.sumfields'));
         }
     } else {
         $session->setStatus(ts("Your summary fields will begin being generated on the next scheduled job. It may take up to an hour to complete.", array('domain' => 'net.ourpowerbase.sumfields')));
     }
     $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/setting/sumfields'));
 }
/**
 * Used for generating the schema and data 
 *
 * Should be called whenever the extension has the chosen
 * fields saved or via Cron job.
 *
 * Called by the API gendata.
 */
function sumfields_gen_data(&$returnValues)
{
    // generate_schema_and_data variable can be set to any of the following:
    //
    // NULL It has never been run, no need to run
    // scheduled:YYYY-MM-DD HH:MM:SS -> it should be run
    // running:YYYY-MM-DD HH:MM:SS -> it is currently running, and started at the given date/time
    // success:YYYY-MM-DD HH:MM:SS -> It completed successfully on the last run at the given date/time
    // failed:YYYY-MM-DD HH:MM:SS -> It failed on the last run at the given date/time
    //
    // We will run if we are scheduled to run OR if the fiscal year has turned
    // and we haven't yet run this fiscal year
    $status = $new_status = sumfields_get_setting('generate_schema_and_data', FALSE);
    $date = date('Y-m-d H:i:s');
    $exception = FALSE;
    $status_name = NULL;
    $status_date = NULL;
    if (preg_match('/^([a-z]+):([0-9 -:]+)$/', $status, $matches)) {
        $status_name = $matches[1];
        $status_date = $matches[2];
        // Check if the fiscal year has turned over since we last ran.
        // (also, only attempt to do a new fiscal year run if the last run
        // was successful to avoid loops of failed runs).
        if ($status_name == 'success') {
            $fiscal_dates = sumfields_get_fiscal_dates();
            $ts_fiscal_year_begin = strtotime($fiscal_dates['%current_fiscal_year_begin']);
            $ts_last_run = strtotime($status_date);
            if ($ts_fiscal_year_begin > $ts_last_run) {
                // We need to re-generate totals because the fiscal year has changed.
                $status_name = 'scheduled';
            }
        }
    }
    if ($status_name == 'scheduled') {
        $new_status = 'running:' . $date;
        sumfields_save_setting('generate_schema_and_data', $new_status);
        // Check to see if the new_active_fields setting is set. This means we have to alter the fields
        // from the current setting.
        $new_active_fields = sumfields_get_setting('new_active_fields', NULL);
        if (!is_null($new_active_fields)) {
            if (!sumfields_alter_table()) {
                // If we fail to properly alter the table, bail and record that we had an error.
                $date = date('Y-m-d H:i:s');
                $new_status = 'failed:' . $date;
                $exception = TRUE;
            } else {
                // Set new active fields to NULL to indicate that they no longer
                // need to be updated
                sumfields_save_setting('new_active_fields', NULL);
            }
        }
        if (!$exception) {
            if (sumfields_generate_data_based_on_current_data()) {
                CRM_Core_DAO::triggerRebuild();
                $date = date('Y-m-d H:i:s');
                $new_status = 'success:' . $date;
            } else {
                $date = date('Y-m-d H:i:s');
                $new_status = 'fail:' . $date;
                $exception = TRUE;
            }
        }
    }
    $returnValues = array("Original Status: {$status}, New Status: {$new_status}");
    sumfields_save_setting('generate_schema_and_data', $new_status);
    if (!$exception) {
        return FALSE;
    }
    return TRUE;
}