function buildQuickForm()
 {
     $action = CRM_Utils_Array::value('action', $_REQUEST, '');
     if (empty($action)) {
         $action = CRM_Utils_Array::value('action', $_POST, '');
     }
     $id = CRM_Utils_Request::retrieve('id', 'Integer', $this);
     if (empty($id)) {
         $id = CRM_Utils_Array::value('id', $_POST, '');
     }
     if ($action == 'update') {
         CRM_Utils_System::setTitle('Edit Dotmailer Mapping');
         $dmMappingDetails = CRM_Dotmailer_Utils::getDotmailerMappingDetails($id);
         $defaults = $dmMappingDetails[$id];
         $this->setDefaults($defaults);
     } elseif ($action == 'add') {
         CRM_Utils_System::setTitle('Add Dotmailer Mapping');
     } elseif ($action == 'delete') {
         $this->assign('id', $id);
         $dmMappingDetails = CRM_Dotmailer_Utils::getDotmailerMappingDetails($id);
         $this->assign('activity_type_label', $dmMappingDetails[$id]['activity_type_label']);
         $this->assign('campaign_label', $dmMappingDetails[$id]['campaign_label']);
         CRM_Utils_System::setTitle('Delete Dotmailer mapping');
     } elseif ($action == 'force_delete' & !empty($id)) {
         $sql = "DELETE FROM " . DOTMAILER_SETTINGS_TABLE_NAME . " WHERE id = {$id}";
         CRM_Core_DAO::executeQuery($sql);
         $session = CRM_Core_Session::singleton();
         $message = ts('Dotmailer mapping deleted');
         CRM_Core_Session::setStatus($message, 'Dotmailer mapping', 'success');
         CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/dotmailer/settings', 'reset=1'));
         CRM_Utils_System::civiExit();
     }
     // Activity types
     $activityTypes = CRM_Dotmailer_Utils::getActivityTypes();
     $this->add('select', 'activity_type_id', ts('CiviCRM Activity Type'), array('' => '- select -') + $activityTypes, TRUE);
     // Active campaigns
     $allActiveCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, TRUE, FALSE);
     $this->add('select', 'campaign_id', ts('CiviCRM Campaign'), array('' => '- select -') + $allActiveCampaigns, FALSE);
     // Get list of Dotmailer Address Books
     $dmAddressBooks = civicrm_api('Dotmailer', 'getaddressbooks', array('version' => 3));
     $this->add('select', 'dotmailer_address_book_id', ts('Dotmailer Address Book'), array('' => '- select -') + $dmAddressBooks['values'], TRUE);
     // Get list of Campaigns
     $dmCampaigns = civicrm_api('Dotmailer', 'getcampaigns', array('version' => 3));
     $this->add('select', 'dotmailer_campaign_id', ts('Dotmailer Campaign'), array('' => '- select -') + $dmCampaigns['values'], FALSE);
     $this->addElement('hidden', 'action', $action);
     $this->addElement('hidden', 'id', $id);
     $this->addFormRule(array('CRM_Dotmailer_Form_DmMapping', 'formRule'));
     $this->addButtons(array(array('type' => 'submit', 'name' => ts('Save'), 'isDefault' => TRUE)));
     // export form elements
     $this->assign('elementNames', $this->getRenderableElementNames());
     parent::buildQuickForm();
 }
 static function getDotmailerMappingDetails($id = NULL)
 {
     $whereClauses = $params = $result = array();
     $whereClauses[] = ' (1) ';
     $whereClause = '';
     if (!empty($id)) {
         $whereClauses[] = "id = %1";
         $params[1] = array($id, 'String');
     }
     if (!empty($whereClauses)) {
         $whereClause = @implode(' AND ', $whereClauses);
     }
     $activityTypes = CRM_Dotmailer_Utils::getActivityTypes();
     $allActiveCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, TRUE, FALSE);
     $dmAddressBooks = civicrm_api('Dotmailer', 'getaddressbooks', array('version' => 3));
     $dmCampaigns = civicrm_api('Dotmailer', 'getcampaigns', array('version' => 3));
     $query = "SELECT * FROM " . DOTMAILER_SETTINGS_TABLE_NAME . " WHERE {$whereClause}";
     $dao = CRM_Core_DAO::executeQuery($query, $params);
     while ($dao->fetch()) {
         $result[$dao->id] = $dao->toArray();
         $result[$dao->id]['activity_type_label'] = $activityTypes[$dao->activity_type_id];
         if (!empty($dao->campaign_id) && $dao->campaign_id != 'NULL') {
             $result[$dao->id]['campaign_label'] = $allActiveCampaigns[$dao->campaign_id];
         }
         $result[$dao->id]['dotmailer_address_book_label'] = $dmAddressBooks['values'][$dao->dotmailer_address_book_id];
         if (!empty($dao->dotmailer_campaign_id) && $dao->dotmailer_campaign_id != 'NULL') {
             $result[$dao->id]['dotmailer_campaign_label'] = $dmCampaigns['values'][$dao->dotmailer_campaign_id];
         }
     }
     return $result;
 }