Commit 0ca30daf authored by bgm's avatar bgm Committed by Aegir user
Browse files

Implement hook_civicrm_post() to automatically save templates to disk.

parent 23fbd3ef
......@@ -13,4 +13,39 @@ https://docs.civicrm.org/sysadmin/en/latest/customize/extensions/
## Usage
(* FIXME: Where would a new user navigate to get started? What changes would they see? *)
By default, this extension will save message template contents under
'msgtplhelper' in the 'civicrm/upload' directory.
This can be changed by updating the setting:
```
drush cvapi Setting.create msgtplhelper_dest_directory=/path/to/my/msg_templates
```
(at the moment, there isn't an admin interface for these settings)
All Message Templates (also known as Workflow Messages) will be saved, both
the system ones (ex: online contribution receipt) and the user-defined ones.
The template title, subject, html and text part are saved in separate files.
This is to make it easier to re-import the contents of a file into CiviCRM.
It also ensures that the files are easy to edit with any text editor.
# Support
Please post bug reports in the issue tracker of this project on github:
https://github.com/coopsymbiotic/msgtplhelper/issues
Commercial support is available through Coop SymbioTIC:
https://www.symbiotic.coop/en
# Copyright
License: AGPL 3
Copyright (C) 2018 Mathieu Lutfy (mathieu@symbiotic.coop)
Copyright (C) 2018 Coop SymbioTIC (info@symbiotic.coop)
https://www.symbiotic.coop/en
Distributed under the terms of the GNU Affero General public license (AGPL).
See LICENSE.txt for details.
......@@ -74,3 +74,64 @@ function msgtplhelper_civicrm_disable() {
function msgtplhelper_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_msgtplhelper_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implements hook_civicrm_postProcess().
* This hook is called after a db write on some core objects.
*
* @param string $formName
* @param CRM_Core_Form $form
*
* NB: It would be better to use hook_civicrm_postProcess, but the
* MessageTemplate form does not call it (as of CiviCRM 5.0.0).
*/
function msgtplhelper_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if (!($objectName == 'MessageTemplate' && in_array($op, ['create', 'edit']))) {
return;
}
// Given a workflow_id (objectId), fetch the machine name of the template.
$tpl = civicrm_api3('MessageTemplate', 'getsingle', [
'id' => $objectId,
]);
// System Workflow Messages are saved using their system name, because
// it looks prettier that way.
//
// User-defined messages are saved using 'tpl-1234-[...]'. Not as pretty,
// but we don't want to venture in sanitizing a proper filename, which
// could lead to security issues.
$basename = 'msg-' . $objectId;
if (!empty($tpl['workflow_id'])) {
$option_value = civicrm_api3('OptionValue', 'getsingle', [
'id' => $tpl['workflow_id'],
]);
$basename = $option_value['name'];
}
// This defaults to $config->uploadDir/msgtplhelper
// c.f. settings
$destination_directory = Civi::settings()->get('msgtplhelper_dest_directory');
if (!is_dir($destination_directory)) {
CRM_Utils_File::createDir($destination_directory);
}
foreach (array('title', 'subject', 'text', 'html') as $part) {
$filename = $destination_directory . '/' . $basename . '-' . $part . '.txt';
$fp = fopen($filename, 'w');
if (! $fp) {
Civi::log()->error('FAILED to create file: ' . $filename);
CRM_Core_Session::setStatus(E::ts('Failed to save to disk: %1', [1=>$filename]), '', 'warning');
// Unlikely we can write other files, avoid flooding the logs.
return;
}
fwrite($fp, $tpl['msg_' . $part]);
fclose($fp);
}
}
<?php
use CRM_Msgtplhelper_ExtensionUtil as E;
return [
'msgtplhelper_dest_directory' => [
'group_name' => E::ts('Message Template Helper Settings'),
'group' => 'msgtplhelper',
'name' => 'msgtplhelper_dest_directory',
'type' => 'String',
'add' => '4.7',
'default' => $config->uploadDir . '/msgtplhelper',
'title' => E::ts('Destination Directory'),
'is_domain' => 1,
'is_contact' => 0,
'description' => E::ts('Directory where to save message templates'),
'help_text' => E::ts('Directory where to save message templates'),
'html_type' => 'Text',
'html_attributes' => [
'size' => 50,
],
'quick_form_type' => 'Element',
],
];
Supports Markdown
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