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'))));
 }
/**
 * Initialize all user settings.
 *
 * The user has the option to choose which fields they want, which contribution
 * types to include, which event types, etc. 
 * 
 * When initializing (which happens with the extension is enabled), we
 * don't choose any fields. By not choosing any fields, we don't add any
 * SQL triggers, and the extension is enabled relatively quickly.  
 *
 * When the user selects the fields they want, they can choose whether to
 * have the change go through immediately (risks timing out) or via the next
 * cron job.
 *
 * To make it more user-friendly, we choose standard options for the other user
 * selected preferences (e.g. which event types should be included, etc.)
 *
 **/
function sumfields_initialize_user_settings()
{
    $fields = array();
    sumfields_save_setting('active_fields', $fields);
    // Which financial_type_ids are used to calculate the general contribution
    // summary fields?
    $values = sumfields_get_all_financial_types();
    sumfields_save_setting('financial_type_ids', array_keys($values));
    // Which financial_type_ids are used to calculate the last membership
    // payment fields?
    sumfields_save_setting('membership_financial_type_ids', array_keys($values));
    // Which event type ids are used when calculating event fields?
    $values = sumfields_get_all_event_types();
    sumfields_save_setting('event_type_ids', array_keys($values));
    // Which participant status ids are used to calculate attendended events
    $values = sumfields_get_all_participant_status_types();
    // When initializing, only use the attended.
    $initial_status_types = preg_grep('/Attended/', $values);
    sumfields_save_setting('participant_status_ids', array_keys($initial_status_types));
    // Which participant status ids are used to calculate no shows
    $values = sumfields_get_all_participant_status_types();
    // When initializing, only use 'No-show' if it exists, otherwise nothing
    // (note: no-show was added in 4.4)
    $initial_noshow_status_types = preg_grep('/No-show/', $values);
    sumfields_save_setting('participant_noshow_status_ids', array_keys($initial_noshow_status_types));
}