else {
$redirect = CRM_Utils_System::url('civicrm/a/', NULL, TRUE, '/mailing/' . $mid);
}
+ CRM_Utils_System::redirect($redirect);
}
- CRM_Utils_System::redirect($redirect);
if ($mid && !$continue) {
- CRM_Core_Error::fatal('Not implemented: Re-use mailing');
- // CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/a/', NULL, TRUE, '/mailing/' . $mid));
+ $clone = civicrm_api3('Mailing', 'clone', array('id' => $mid));
+ CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/a/', NULL, TRUE, '/mailing/' . $clone['id']));
}
}
}
}
+function _civicrm_api3_mailing_clone_spec(&$spec) {
+ $mailingFields = CRM_Mailing_DAO_Mailing::fields();
+ $spec['id'] = $mailingFields['id'];
+ $spec['id']['api.required'] = 1;
+}
+
+function civicrm_api3_mailing_clone($params) {
+ $BLACKLIST = array(
+ 'id',
+ 'is_completed',
+ 'created_id',
+ 'created_date',
+ 'scheduled_id',
+ 'scheduled_date',
+ 'approver_id',
+ 'approval_date',
+ 'approval_status_id',
+ 'approval_note',
+ 'is_archived',
+ 'hash',
+ );
+
+ $get = civicrm_api3('Mailing', 'getsingle', array('id' => $params['id']));
+
+ $newParams = array();
+ $newParams['debug'] = CRM_Utils_Array::value('debug', $params);
+ $newParams['groups']['include'] = array();
+ $newParams['groups']['exclude'] = array();
+ $newParams['mailings']['include'] = array();
+ $newParams['mailings']['exclude'] = array();
+ foreach ($get as $field => $value) {
+ if (!in_array($field, $BLACKLIST)) {
+ $newParams[$field] = $value;
+ }
+ }
+
+ $dao = new CRM_Mailing_DAO_MailingGroup();
+ $dao->mailing_id = $params['id'];
+ $dao->find();
+ while ($dao->fetch()) {
+ // CRM-11431; account for multi-lingual
+ $entity = (substr($dao->entity_table, 0, 15) == 'civicrm_mailing') ? 'mailings' : 'groups';
+ $newParams[$entity][strtolower($dao->group_type)][] = $dao->entity_id;
+ }
+
+ return civicrm_api3('Mailing', 'create', $newParams);
+}
+
/**
* Handle a delete event.
*
$this->assertArrayHasKey('id', $result['values'][0]['children'][0]);
}
+ public function testClone() {
+ // BEGIN SAMPLE DATA
+ $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
+ $contactIDs['include_me'] = $this->individualCreate(array(
+ 'email' => 'include.me@example.org',
+ 'first_name' => 'Includer',
+ 'last_name' => 'Person',
+ ));
+ $this->callAPISuccess('GroupContact', 'create', array(
+ 'group_id' => $groupIDs['inc'],
+ 'contact_id' => $contactIDs['include_me'],
+ ));
+
+ $params = $this->_params;
+ $params['groups']['include'] = array($groupIDs['inc']);
+ $params['groups']['exclude'] = array();
+ $params['mailings']['include'] = array();
+ $params['mailings']['exclude'] = array();
+ // END SAMPLE DATA
+
+ $create = $this->callAPISuccess('Mailing', 'create', $params);
+ $createId = $create['id'];
+ $this->createLoggedInUser();
+ $clone = $this->callAPIAndDocument('Mailing', 'clone', array('id' => $create['id']), __FUNCTION__, __FILE__);
+ $cloneId = $clone['id'];
+
+ $this->assertNotEquals($createId, $cloneId, 'Create and clone should return different records');
+ $this->assertTrue(is_numeric($cloneId));
+
+ $this->assertNotEmpty($clone['values'][$cloneId]['subject']);
+ $this->assertEquals($params['subject'], $clone['values'][$cloneId]['subject'], "Cloned subject should match");
+
+ // created_id is special - populated based on current user (ie the cloner).
+ $this->assertNotEmpty($clone['values'][$cloneId]['created_id']);
+ $this->assertNotEquals($create['values'][$createId]['created_id'], $clone['values'][$cloneId]['created_id'], 'Clone should be created by a different person');
+
+ // Target groups+mailings are special.
+ $cloneGroups = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $cloneId, 'sequential' => 1));
+ $this->assertEquals(1, $cloneGroups['count']);
+ $this->assertEquals($cloneGroups['values'][0]['group_type'], 'Include');
+ $this->assertEquals($cloneGroups['values'][0]['entity_table'], 'civicrm_group');
+ $this->assertEquals($cloneGroups['values'][0]['entity_id'], $groupIDs['inc']);
+ }
+
//@ todo tests below here are all failure tests which are not hugely useful - need success tests
//------------ civicrm_mailing_event_bounce methods------------