$mg = new CRM_Mailing_DAO_MailingGroup();
foreach (array('groups', 'mailings') as $entity) {
foreach (array('include', 'exclude', 'base') as $type) {
- if (isset($params[$entity]) && !empty($params[$entity][$type]) &&
- is_array($params[$entity][$type])) {
- foreach ($params[$entity][$type] as $entityId) {
- $mg->reset();
- $mg->mailing_id = $mailing->id;
- $mg->entity_table = ($entity == 'groups') ? $groupTableName : $mailingTableName;
- $mg->entity_id = $entityId;
- $mg->group_type = $type;
- $mg->save();
- }
+ if (isset($params[$entity][$type])) {
+ self::replaceGroups($mailing->id, $type, $entity, $params[$entity][$type]);
}
}
}
return $mailing;
}
+ /**
+ * Replace the list of recipients on a given mailing
+ *
+ * @param int $mailingId
+ * @param string $type 'include' or 'exclude'
+ * @param string $entity 'groups' or 'mailings'
+ * @param array<int> $entityIds
+ * @throws CiviCRM_API3_Exception
+ */
+ public static function replaceGroups($mailingId, $type, $entity, $entityIds) {
+ $values = array();
+ foreach ($entityIds as $entityId) {
+ $values[] = array('entity_id' => $entityId);
+ }
+ civicrm_api3('mailing_group', 'replace', array(
+ 'mailing_id' => $mailingId,
+ 'group_type' => $type,
+ 'entity_table' => ($entity == 'groups') ? CRM_Contact_BAO_Group::getTableName() : CRM_Mailing_BAO_Mailing::getTableName(),
+ 'values' => $values,
+ ));
+ }
+
/**
* get hash value of the mailing
*
$this->getAndCheck($this->_params, $result['id'], 'mailing');
}
+ /**
+ * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]".
+ * Make sure these work
+ */
+ public function testMagicGroups_create_update() {
+ // BEGIN SAMPLE DATA
+ $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
+ $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
+ $contactIDs['a'] = $this->individualCreate(array('email' => 'include.me@example.org', 'first_name' => 'Includer', 'last_name' => 'Person'));
+ $contactIDs['b'] = $this->individualCreate(array('email' => 'exclude.me@example.org', 'last_name' => 'Excluder', 'last_name' => 'Excluder'));
+ $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['a'], 'contact_id' => $contactIDs['a']));
+ $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['b'], 'contact_id' => $contactIDs['b']));
+ // END SAMPLE DATA
+
+ // ** Pass 1: Create
+ $createParams = $this->_params;
+ $createParams['groups']['include'] = array($groupIDs['a']);
+ $createParams['groups']['exclude'] = array();
+ $createParams['mailings']['include'] = array();
+ $createParams['mailings']['exclude'] = array();
+ $createResult = $this->callAPISuccess('Mailing', 'create', $createParams);
+ $getGroup1 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
+ $getGroup1_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup1['values']));
+ $this->assertEquals(array($groupIDs['a']), $getGroup1_ids);
+
+ // ** Pass 2: Update without any changes to groups[include]
+ $nullopParams = $createParams;
+ $nullopParams['id'] = $createResult['id'];
+ unset($nullopParams['groups']['include']);
+ $this->callAPISuccess('Mailing', 'create', $nullopParams);
+ $getGroup2 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
+ $getGroup2_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup2['values']));
+ $this->assertEquals(array($groupIDs['a']), $getGroup2_ids);
+
+ // ** Pass 3: Update with different groups[include]
+ $updateParams = $createParams;
+ $updateParams['id'] = $createResult['id'];
+ $updateParams['groups']['include'] = array($groupIDs['b']);
+ $this->callAPISuccess('Mailing', 'create', $updateParams);
+ $getGroup3 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
+ $getGroup3_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup3['values']));
+ $this->assertEquals(array($groupIDs['b']), $getGroup3_ids);
+ }
+
public function testMailerPreview() {
// BEGIN SAMPLE DATA
$contactID = $this->individualCreate();