function buildQuickForm()
 {
     $custom = sumfields_get_custom_field_definitions();
     $options = array();
     $field_options = array();
     while (list($k, $v) = each($custom['fields'])) {
         $display = $v['display'];
         $field_options[$display][$k] = $v['label'];
     }
     if (count($field_options) == 0) {
         // This means neither CiviEvent or CiviContribute are enabled.
         $session = CRM_Core_Session::singleton();
         $session->setStatus(ts("Summary Fields is not particularly useful if CiviContribute and CiviEvent are both disabled. Try enabling at least one.", array('domain' => 'net.ourpowerbase.sumfields')));
         return;
     }
     // Evaluate the status of form changes and report to the user
     $apply_settings_status = sumfields_get_setting('generate_schema_and_data', FALSE);
     if (empty($apply_settings_status)) {
         $display_status = ts('The settings have never been saved (newly enabled)', array('domain' => 'net.ourpowerbase.sumfields'));
     } elseif (!preg_match('/^(scheduled|running|success|failed):([0-9 :\\-]+)$/', $apply_settings_status, $matches)) {
         $display_status = ts("Unable to determine status (%1).", array(1 => $apply_settings_status, 'domain' => 'net.ourpowerbase.sumfields'));
     } else {
         $display_status = NULL;
         $status = $matches[1];
         $date = $matches[2];
         switch ($status) {
             case 'scheduled':
                 $display_status = ts("Setting changes were saved on %1, but not yet applied; they should be applied shortly.", array(1 => $date, 'domain' => 'net.ourpowerbase.sumfields'));
                 break;
             case 'running':
                 $display_status = ts("Setting changes are in the process of being applied; the process started on %1.", array(1 => $date, 'domain' => 'net.ourpowerbase.sumfields'));
                 break;
             case 'success':
                 $display_status = ts("Setting changes were successfully applied on %1.", array(1 => $date, 'domain' => 'net.ourpowerbase.sumfields'));
                 break;
             case 'failed':
                 $display_status = ts("Setting changes failed to apply; the failed attempt happend on %1.", array(1 => $date, 'domain' => 'net.ourpowerbase.sumfields'));
                 break;
         }
     }
     $this->Assign('display_status', $display_status);
     // Evaluate status of the triggers and report to the user.
     if (sumfields_get_update_trigger('civicrm_contribution')) {
         $contribution_table_trigger_status = 'Enabled';
     } else {
         $contribution_table_trigger_status = 'Not Enabled';
     }
     $this->Assign('contribution_table_trigger_status', $contribution_table_trigger_status);
     if (sumfields_get_update_trigger('civicrm_participant')) {
         $participant_table_trigger_status = 'Enabled';
     } else {
         $participant_table_trigger_status = 'Not Enabled';
     }
     $this->Assign('participant_table_trigger_status', $participant_table_trigger_status);
     // Add active fields
     if (array_key_exists('fundraising', $field_options)) {
         $this->Assign('sumfields_active_fundraising', TRUE);
         $name = 'active_fundraising_fields';
         $label = ts('Fundraising Fields', array('domain' => 'net.ourpowerbase.sumfields'));
         $this->addCheckBox($name, $label, array_flip($field_options['fundraising']));
     }
     if (sumfields_component_enabled('CiviMember') && array_key_exists('membership', $field_options)) {
         $this->Assign('sumfields_active_membership', TRUE);
         $name = 'active_membership_fields';
         $label = ts('Membership Fields', array('domain' => 'net.ourpowerbase.sumfields'));
         $this->addCheckBox($name, $label, array_flip($field_options['membership']));
     }
     if (array_key_exists('event_standard', $field_options)) {
         $this->Assign('sumfields_active_event_standard', TRUE);
         $name = 'active_event_standard_fields';
         $label = ts('Standard Event Fields', array('domain' => 'net.ourpowerbase.sumfields'));
         $this->addCheckBox($name, $label, array_flip($field_options['event_standard']));
     }
     if (array_key_exists('event_turnout', $field_options)) {
         $this->Assign('sumfields_active_event_turnout', TRUE);
         $name = 'active_event_turnout_fields';
         $label = ts('Turnout Event Fields', array('domain' => 'net.ourpowerbase.sumfields'));
         $this->addCheckBox($name, $label, array_flip($field_options['event_turnout']));
     }
     if (sumfields_component_enabled('CiviMember')) {
         $this->assign('sumfields_member', TRUE);
         $name = 'membership_financial_type_ids';
         $label = ts('Membership Financial Types', array('domain' => 'net.ourpowerbase.sumfields'));
         $this->addCheckBox($name, $label, array_flip(sumfields_get_all_financial_types()));
         $this->addRule($name, ts('%1 is a required field.', array(1 => $label, 'domain' => 'net.ourpowerbase.sumfields')), 'required');
     }
     if (sumfields_component_enabled('CiviContribute')) {
         $this->assign('sumfields_contribute', TRUE);
         $name = 'financial_type_ids';
         $label = ts('Financial Types', array('domain' => 'net.ourpowerbase.sumfields'));
         $this->addCheckBox($name, $label, array_flip(sumfields_get_all_financial_types()));
         $this->addRule($name, ts('%1 is a required field.', array(1 => $label, 'domain' => 'net.ourpowerbase.sumfields')), 'required');
     }
     if (sumfields_component_enabled('CiviEvent')) {
         $this->assign('sumfields_event', TRUE);
         $name = 'event_type_ids';
         $label = ts('Event Types', array('domain' => 'net.ourpowerbase.sumfields'));
         $this->addCheckBox($name, $label, array_flip(sumfields_get_all_event_types()));
         $this->addRule($name, ts('%1 is a required field.', array(1 => $label, 'domain' => 'net.ourpowerbase.sumfields')), 'required');
         $label = ts('Participant Status (attended)', array('domain' => 'net.ourpowerbase.sumfields'));
         $name = 'participant_status_ids';
         $this->addCheckBox($name, $label, array_flip(sumfields_get_all_participant_status_types()));
         $this->addRule($name, ts('%1 is a required field.', array(1 => $label, 'domain' => 'net.ourpowerbase.sumfields')), 'required');
         $label = ts('Participant Status (did not attend)', array('domain' => 'net.ourpowerbase.sumfields'));
         $name = 'participant_noshow_status_ids';
         $this->addCheckBox($name, $label, array_flip(sumfields_get_all_participant_status_types()));
         $this->addRule($name, ts('%1 is a required field.', array(1 => $label, 'domain' => 'net.ourpowerbase.sumfields')), 'required');
     }
     $name = 'when_to_apply_change';
     $label = ts('When should these changes be applied?', array('domain' => 'net.ourpowerbase.sumfields'));
     $options = array('via_cron' => ts("On the next scheduled job (cron)", array('domain' => 'net.ourpowerbase.sumfields')), 'on_submit' => ts("When I submit this form", array('domain' => 'net.ourpowerbase.sumfields')));
     $this->addRadio($name, $label, $options);
     $this->addButtons(array(array('type' => 'next', 'name' => ts('Save'), 'spacing' => '         ', 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel'))));
 }
/**
 * Fix incorrect contributions summary fields.
 *
 * Find all summary fields with incorrect amounts and fix them. You may need to
 * add this as a cron job if you find that you are regularly getting
 * inconsistent summaries.
 *
 */
function sumfields_fix_inconsistent_summaries()
{
    // This means there is no update triggered defined.
    if (!($db_trigger_sql = sumfields_get_update_trigger('civicrm_contribution'))) {
        $msg = dt("Update trigger is not defined. This might explain inconsistent responses. " . "Rebuilding triggers, this may take a while.");
        drush_log($msg, 'error');
        CRM_Core_DAO::triggerRebuild();
        if (!sumfields_get_update_trigger('civicrm_contribution')) {
            $msg = dt("Still can't find trigger after rebuilding. Bailing...");
            drush_log($msg, 'error');
            return FALSE;
        }
    }
    $ids = sumfields_find_incorrect_total_lifetime_contribution_records();
    if ($ids && count($ids) > 0) {
        // Just re-initiate everything - who knows what might have gone wrong.
        drush_log(dt("Data is wrong. Re-running trigger creation."), 'error');
        CRM_Core_DAO::triggerRebuild();
        drush_log(dt("Repopulating all data."), 'error');
        sumfields_generate_data_based_on_current_data($session = NULL);
        return TRUE;
    }
}