Unverified Commit 75e3f689 authored by MikeyMJCO's avatar MikeyMJCO Committed by GitHub
Browse files

Merge pull request #1 from artfulrobot/master

Few fixes, improvements
parents ccd8a516 4110953d
......@@ -41,11 +41,22 @@ it with the command-line tool [cv](https://github.com/civicrm/cv).
git clone https://github.com/civicrm/org.civicrm.mosaicomsgtpl.git
cv en mosaicomsgtpl
```
## Usage
## Normal Usage
Simply enable the module. All ordinary tasks should be handled automatically.
CiviCRM Message Templates have both a *name* and an email *subject*, but Mosaico templates do not have a subject. By default the name of the Mosaico template will be used for both the message template name and also its subject. This might or might not be appropriate to your use case.
As a work-around, you can use the format `title | subject` to specify your Mosaico template name. e.g.
| Mosaico Template Title | Message Template Title | Email subject |
| ----------------------------------------- |--------------------------| ------------------|
| Welcome supporter | Welcome supporter | Welcome supporter |
| Thanks - existing donors \| Well hi there | Thanks - existing donors | Well hi there |
## API Usage
System administrators and developers may wish to use the synchronization API:
* Synchronize all templates: `cv api Job.mosaico_msg_sync`
......@@ -56,3 +67,4 @@ System administrators and developers may wish to use the synchronization API:
* Synchronization is one-way. To make changes, you should only use the Mosaico template editor.
* When you load a Mosaico template into a richtext editor (such as CKEditor or TinyMCE), you should tred
carefully: only make small changes to the text. Changing the layout in a substantive way would be difficult.
......@@ -10,7 +10,13 @@ use CRM_Mosaicomsgtpl_ExtensionUtil as E;
* @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
*/
function _civicrm_api3_job_mosaico_msg_sync_spec(&$spec) {
$spec['id']['api.required'] = 0;
$spec['id'] = [
'description' => 'If given, only this template is sync-ed, otherwise all Mosaico templates are processed.',
'api.required' => 0,
];
$spec['is_new'] = [
'description' => 'If true, the msg_tpl_id will be set to zero so that using Copy to create a new template does not duplicate the msg_tpl_id.',
];
}
/**
......@@ -34,24 +40,46 @@ function civicrm_api3_job_mosaico_msg_sync($params) {
foreach ($existingMosTpls['values'] as $existingMosTpl) {
if (isset($existingMosTpl['msg_tpl_id'])) {
civicrm_api3('MessageTemplate', 'create', array(
'id' => $existingMosTpl['msg_tpl_id'],
'msg_html' => _civicrm_api3_job_mosaico_msg_filter($existingMosTpl['html']),
));
// Handle common parameters for things that can be updated...
// Split the Mosaico message title into title and subject.
//
// This is a big ugly, but Mosaico templates do not store a subject.
// Being able to edit the subject of a message template is essential, but
// being able to administer templates by an internal name is also a very
// cool feature ("Initial welcome email").
//
// We allow the Mosaico title to include a subject following the | symbol.
preg_match('/^(.+?)\s*[|]\s*(.+)$/', $existingMosTpl['title'], $_);
$createParams = [
'msg_html' => _civicrm_api3_job_mosaico_msg_filter($existingMosTpl['html']),
'msg_title' => empty($_[1]) ? $existingMosTpl['title'] : $_[1],
'msg_subject' => empty($_[2]) ? $existingMosTpl['title'] : $_[2], // default to title, as before.
];
// When a template is created from a Mosaico Base Template, it will not have a msg_tpl_id.
// However when a template is created from a Copy of a MosaicoTemplate, it will come in
// with the original MosaicoTemplate's msg_tpl_id, which is not what we want. Consult
// `is_new` to determine this case (which is set in the post hook if the op was 'create').
$isNewTpl = !isset($existingMosTpl['msg_tpl_id']) || !empty($params['is_new']);
if ($isNewTpl) {
// Need to create a new MessageTemplate.
$createParams['is_reserved'] = 1;
$createParams['msg_tpl_id'] = 0; // We set this later.
}
else {
$newTpl = array();
$newTpl['msg_title'] = $existingMosTpl['title'];
$newTpl['msg_subject'] = $existingMosTpl['title'];
$newTpl['msg_html'] = _civicrm_api3_job_mosaico_msg_filter($existingMosTpl['html']);
$newTpl['is_reserved'] = 1;
// Editing an existing MosaicoTemplate.
$createParams['id'] = $existingMosTpl['msg_tpl_id'];
}
$newTplResult = civicrm_api3('MessageTemplate', 'create', $newTpl);
$result = civicrm_api3('MessageTemplate', 'create', $createParams);
if ($isNewTpl) {
// We're likely called after updating a MosaicoTemplate... don't recurse...
CRM_Core_DAO::executeQuery('UPDATE civicrm_mosaico_template SET msg_tpl_id = %1 WHERE id = %2', array(
1 => array($newTplResult['id'], 'Positive'),
1 => array($result['id'], 'Positive'),
2 => array($existingMosTpl['id'], 'Positive'),
));
}
......@@ -62,7 +90,6 @@ function civicrm_api3_job_mosaico_msg_sync($params) {
return civicrm_api3_create_success(array('processed' => $count), $params, 'Job', 'mosaico_msg_sync');
}
/**
* Filter the HTML content.
*
......
......@@ -4,13 +4,14 @@ require_once 'mosaicomsgtpl.civix.php';
use CRM_Mosaicomsgtpl_ExtensionUtil as E;
function mosaico_civicrm_post($op, $objectName, $objectId, &$objectRef = NULL) {
function mosaicomsgtpl_civicrm_post($op, $objectName, $objectId, &$objectRef = NULL) {
if (($op === 'create' || $op === 'edit') && $objectName === 'MosaicoTemplate') {
if (Civi::settings()->get('mosaicomsgtpl_suspend')) {
return;
}
civicrm_api3('Job', 'mosaico_msg_sync', array(
'id' => $objectId,
'id' => $objectId,
'is_new' => ($op === 'create'),
));
}
}
......
......@@ -88,6 +88,57 @@ class api_v3_Job_MosaicoMsgSyncTest extends \PHPUnit_Framework_TestCase implemen
$this->assertEquals(1 + $oldCount, $newCount);
}
/**
* Test clones work.
*/
public function testClone() {
$this->assertEquals('MosaicoTemplate', CRM_Core_DAO_AllCoreTables::getBriefName('CRM_Mosaico_DAO_MosaicoTemplate'));
// Create the first template and run the sync.
$first = $this->createMosaicoTemplate(array('title' => 'First example'));
$result = civicrm_api3('Job', 'mosaico_msg_sync', ['id' => $first['id']]);
$oldCount = CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_msg_template');
// Create the clone and run the sync on it passing is_new (which is what the post hook does).
$second = civicrm_api3('MosaicoTemplate', 'clone', ['id'=>$first['id'], 'title' => 'clone']);
$result = civicrm_api3('Job', 'mosaico_msg_sync', ['id' => $second['id'], 'is_new' => TRUE]);
// Count templates - should be one more.
$newCount = CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_msg_template');
$this->assertEquals(1 + $oldCount, $newCount);
}
/**
* Test title/subject parsing.
*
* @dataProvider titleSubjectParsingProvider
*/
public function testTitleSubjectParsing($input, $title, $subject) {
$this->assertEquals('MosaicoTemplate', CRM_Core_DAO_AllCoreTables::getBriefName('CRM_Mosaico_DAO_MosaicoTemplate'));
$first = $this->createMosaicoTemplate(array('title' => $input));
civicrm_api3('Job', 'mosaico_msg_sync', ['id' => $first['id']]);
// Reload to get the msg_tpl_id.
$first = civicrm_api3('MosaicoTemplate', 'getsingle', ['id' => $first['id']]);
$this->assertGreaterThan(0, (int) $first['msg_tpl_id']);
$tpl = civicrm_api3('MessageTemplate', 'getsingle', ['id' => $first['msg_tpl_id']]);
$this->assertEquals($title, $tpl['msg_title']);
$this->assertEquals($subject, $tpl['msg_subject']);
}
/**
* Data provider
*/
public function titleSubjectParsingProvider() {
return [
['input' => 'A simple subject', 'title' => 'A simple subject', 'subject' => 'A simple subject'],
['input' => 'internal title|public subject', 'title' => 'internal title', 'subject' => 'public subject'],
['input' => 'internal title | public subject', 'title' => 'internal title', 'subject' => 'public subject'],
['input' => 'internal title | public subject | with pipe', 'title' => 'internal title', 'subject' => 'public subject | with pipe'],
];
}
protected function createMosaicoTemplate($params = array()) {
$defaults = array(
'title' => 'The Title',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment