From: Tim Otten Date: Tue, 7 Apr 2015 01:37:24 +0000 (-0700) Subject: CRM-16238 - Implement "Re-use Mailing" X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=00dc999a4d049adabccefdb0d0504d09c9bc7219;p=civicrm-core.git CRM-16238 - Implement "Re-use Mailing" --- diff --git a/CRM/Mailing/Controller/Send.php b/CRM/Mailing/Controller/Send.php index 3f0f1c787b..bd277a31fc 100644 --- a/CRM/Mailing/Controller/Send.php +++ b/CRM/Mailing/Controller/Send.php @@ -64,11 +64,11 @@ class CRM_Mailing_Controller_Send extends CRM_Core_Controller { 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'])); } } diff --git a/api/v3/Mailing.php b/api/v3/Mailing.php index 0739b9aea8..46c97b86d8 100755 --- a/api/v3/Mailing.php +++ b/api/v3/Mailing.php @@ -161,6 +161,54 @@ function _civicrm_api3_mailing_create_spec(&$params) { } } +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. * diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index fff3c7b597..ca57d4a732 100755 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -511,6 +511,50 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp"; $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------------