CRM-16238 - Implement "Re-use Mailing"
authorTim Otten <totten@civicrm.org>
Tue, 7 Apr 2015 01:37:24 +0000 (18:37 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 7 Apr 2015 01:37:24 +0000 (18:37 -0700)
CRM/Mailing/Controller/Send.php
api/v3/Mailing.php
tests/phpunit/api/v3/MailingTest.php

index 3f0f1c787b093c6a5a18bd81b98e3664c9f12381..bd277a31fcac982d8f747472f8047de9eddb8d8d 100644 (file)
@@ -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']));
       }
     }
 
index 0739b9aea87ecc3d65b2c52e356adaa9f6df5058..46c97b86d82c2313b3f8f8dcdf2f86988f5767b4 100755 (executable)
@@ -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.
  *
index fff3c7b597425d9e0c969066337ecc8aae63652d..ca57d4a7323afb7c8942fcbebcdaed0dce57d955 100755 (executable)
@@ -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------------