function buildQuickForm()
 {
     // add form elements
     // Add the API Email element
     $this->addElement('text', 'api_email_address', ts('API Email Address'), array('size' => 48, TRUE));
     $this->addElement('text', 'api_password', ts('API Password'), array('size' => 48, TRUE));
     $this->add('select', 'api_audience_type', ts('Audience Type'), array('' => '- select -') + $GLOBALS["DotMailerAudienceType"], FALSE);
     $this->add('select', 'api_opt_in_type', ts('Opt In Type'), array('' => '- select -') + $GLOBALS["DotMailerOptInType"], FALSE);
     $this->add('select', 'api_email_type', ts('Email Type'), array('' => '- select -') + $GLOBALS["DotMailerEmailType"], FALSE);
     $this->add('textarea', 'api_notes', ts('Notes'), array("rows" => 4, "cols" => 60));
     /*$apiActivityTypes = &$this->addElement('advmultiselect', 'activity_types',
         ts('Activity Types') . ' ', CRM_Dotmailer_Utils::getActivityTypes(),
         array(
           'size' => 5,
           'style' => 'width:200px',
           'class' => 'advmultiselect',
         )
       );*/
     $this->addButtons(array(array('type' => 'submit', 'name' => ts('Save'), 'isDefault' => TRUE)));
     if (!empty($GLOBALS["DotMailerCiviCRMDataFieldsMapping"]) && DOTMAILER_PROCESS_CUSTOM_DATA_FIELDS == 1) {
         $this->assign('dmCiviCRMFieldMapping', $GLOBALS["DotMailerCiviCRMDataFieldsMapping"]);
     }
     // Get activity type/campaign and DM mapping
     $dmMappings = CRM_Dotmailer_Utils::getDotmailerMappingDetails();
     $this->assign('dmMappings', $dmMappings);
     // export form elements
     $this->assign('elementNames', $this->getRenderableElementNames());
     parent::buildQuickForm();
 }
 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();
 }
/**
 * Implementation of hook_civicrm_post
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
 */
function dotmailer_civicrm_post($op, $objectName, $objectId, &$objectRef)
{
    // Backoffice - Create activity
    if ($op == 'create' && $objectName == 'Activity') {
        // Proceed with Dotmailer subscription
        CRM_Dotmailer_Utils::processDotmailerSubscription($objectId);
    }
    // Create bespoke activity when a contribution is created
    // so that we can process dotmailer subscription from activity
    //if ($op == 'create' && $objectName == 'Contribution' && !empty($objectRef->campaign_id) && $objectRef->campaign_id != 'null') {
    if ($op == 'create' && $objectName == 'Contribution') {
        CRM_Dotmailer_Utils::createActivityForContribution($objectRef);
    }
}
 static function processSync($params)
 {
     $limit = CRM_Utils_Array::value('limit', $params, 50);
     $dmMappings = CRM_Dotmailer_Utils::getDotmailerMappingDetails();
     if (empty($dmMappings)) {
         return;
     }
     $activityTypes = array();
     foreach ($dmMappings as $key => $value) {
         $activityTypes[] = $value['activity_type_id'];
     }
     $activityTypeStr = implode(', ', $activityTypes);
     $query = "SELECT id from civicrm_activity \n        WHERE id NOT IN (SELECT entity_id FROM civicrm_value_dotmailer_subscription)\n        AND activity_type_id IN ({$activityTypeStr})\n        LIMIT {$limit}";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         self::processDotmailerSubscription($dao->id);
     }
 }
/**
 * CiviCRM to Mailchimp Sync
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_dotmailer_sync($params)
{
    $result = array();
    $result = CRM_Dotmailer_Utils::processSync($params);
    return civicrm_api3_create_success();
}