3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Page for displaying list of message templates.
21 class CRM_Admin_Page_MessageTemplates
extends CRM_Core_Page_Basic
{
24 * The action links that we need to display for the browse screen.
28 public static $_links = NULL;
31 * ids of templates which diverted from the default ones and can be reverted
34 protected $_revertible = [];
37 * set to the id that we’re reverting at the given moment (if we are)
40 protected $_revertedId;
46 public function __construct($title = NULL, $mode = NULL) {
47 parent
::__construct($title, $mode);
49 // fetch the ids of templates which diverted from defaults and can be reverted –
50 // these templates have the same workflow_id as the defaults; defaults are reserved
52 SELECT diverted.id, orig.id orig_id
53 FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
54 diverted.workflow_id = orig.workflow_id AND
55 orig.is_reserved = 1 AND (
56 diverted.msg_subject != orig.msg_subject OR
57 diverted.msg_text != orig.msg_text OR
58 diverted.msg_html != orig.msg_html
62 $dao = CRM_Core_DAO
::executeQuery($sql);
63 while ($dao->fetch()) {
64 $this->_revertible
[$dao->id
] = $dao->orig_id
;
74 public function getBAOName() {
75 return 'CRM_Core_BAO_MessageTemplate';
82 * (reference) of action links
84 public function &links() {
85 if (!(self
::$_links)) {
86 $confirm = ts('Are you sure you want to revert this template to the default for this workflow? You will lose any customizations you have made.', ['escape' => 'js']) . '\n\n' . ts('We recommend that you save a copy of the your customized Text and HTML message content to a text file before reverting so you can combine your changes with the system default messages as needed.', ['escape' => 'js']);
88 CRM_Core_Action
::UPDATE
=> [
90 'url' => 'civicrm/admin/messageTemplates/add',
91 'qs' => 'action=update&id=%%id%%&reset=1',
92 'title' => ts('Edit this message template'),
94 CRM_Core_Action
::DISABLE
=> [
95 'name' => ts('Disable'),
96 'ref' => 'crm-enable-disable',
97 'title' => ts('Disable this message template'),
99 CRM_Core_Action
::ENABLE
=> [
100 'name' => ts('Enable'),
101 'ref' => 'crm-enable-disable',
102 'title' => ts('Enable this message template'),
104 CRM_Core_Action
::DELETE
=> [
105 'name' => ts('Delete'),
106 'url' => 'civicrm/admin/messageTemplates',
107 'qs' => 'action=delete&id=%%id%%',
108 'title' => ts('Delete this message template'),
110 CRM_Core_Action
::REVERT
=> [
111 'name' => ts('Revert to Default'),
112 'extra' => "onclick = 'return confirm(\"$confirm\");'",
113 'url' => 'civicrm/admin/messageTemplates',
114 'qs' => 'action=revert&id=%%id%%&selectedChild=workflow',
115 'title' => ts('Revert this workflow message template to the system default'),
117 CRM_Core_Action
::VIEW
=> [
118 'name' => ts('View Default'),
119 'url' => 'civicrm/admin/messageTemplates',
120 'qs' => 'action=view&id=%%orig_id%%&reset=1',
121 'title' => ts('View the system default for this workflow message template'),
125 return self
::$_links;
129 * @param CRM_Core_DAO $object
131 * @param array $values
132 * @param array $links
133 * @param string $permission
134 * @param bool $forceAction
136 public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) {
137 if ($object->workflow_id
) {
138 // do not expose action link for reverting to default if the template did not diverge or we just reverted it now
139 if (!in_array($object->id
, array_keys($this->_revertible
)) or
140 ($this->_action
& CRM_Core_Action
::REVERT
and $object->id
== $this->_revertedId
)
142 $action &= ~CRM_Core_Action
::REVERT
;
143 $action &= ~CRM_Core_Action
::VIEW
;
146 // default workflow templates shouldn’t be deletable
147 // workflow templates shouldn’t have disable/enable actions (at least for CiviCRM 3.1)
148 if ($object->workflow_id
) {
149 $action &= ~CRM_Core_Action
::DISABLE
;
150 $action &= ~CRM_Core_Action
::DELETE
;
153 // rebuild the action links HTML, as we need to handle %%orig_id%% for revertible templates
154 $values['action'] = CRM_Core_Action
::formLink($links, $action,
157 'orig_id' => $this->_revertible
[$object->id
] ??
NULL,
161 'messageTemplate.manage.action',
167 $action &= ~CRM_Core_Action
::REVERT
;
168 $action &= ~CRM_Core_Action
::VIEW
;
169 parent
::action($object, $action, $values, $links, $permission);
175 * @param null $pageArgs
180 public function run($args = NULL, $pageArgs = NULL, $sort = NULL) {
181 $id = $this->getIdAndAction();
182 // handle the revert action and offload the rest to parent
183 if ($this->_action
& CRM_Core_Action
::REVERT
) {
184 $this->_revertedId
= $id;
185 CRM_Core_BAO_MessageTemplate
::revert($id);
188 return parent
::run($args, $pageArgs, $sort);
192 * Get name of edit form.
195 * Classname of edit form.
197 public function editForm() {
198 return 'CRM_Admin_Form_MessageTemplates';
202 * Get edit form name.
207 public function editName() {
208 return ts('Message Template');
219 public function userContext($mode = NULL) {
220 return 'civicrm/admin/messageTemplates';
224 * Browse all entities.
226 public function browse() {
227 $action = func_num_args() ?
func_get_arg(0) : NULL;
228 if ($this->_action
& CRM_Core_Action
::ADD
) {
231 $links = $this->links();
232 if ($action == NULL) {
233 if (!empty($links)) {
234 $action = array_sum(array_keys($links));
238 if ($action & CRM_Core_Action
::DISABLE
) {
239 $action -= CRM_Core_Action
::DISABLE
;
242 if ($action & CRM_Core_Action
::ENABLE
) {
243 $action -= CRM_Core_Action
::ENABLE
;
246 $messageTemplate = new CRM_Core_BAO_MessageTemplate();
247 $messageTemplate->orderBy('msg_title' . ' asc');
250 $workflowTemplates = [];
253 $messageTemplate->find();
254 while ($messageTemplate->fetch()) {
255 $values[$messageTemplate->id
] = [];
256 CRM_Core_DAO
::storeValues($messageTemplate, $values[$messageTemplate->id
]);
257 // populate action links
258 $this->action($messageTemplate, $action, $values[$messageTemplate->id
], $links, CRM_Core_Permission
::EDIT
);
260 if (!$messageTemplate->workflow_name
) {
261 $userTemplates[$messageTemplate->id
] = $values[$messageTemplate->id
];
263 elseif (!$messageTemplate->is_reserved
) {
264 $workflowTemplates[$messageTemplate->id
] = $values[$messageTemplate->id
];
269 'userTemplates' => $userTemplates,
270 'workflowTemplates' => $workflowTemplates,
273 $this->assign('rows', $rows);
274 $this->assign('canEditSystemTemplates', CRM_Core_Permission
::check('edit system workflow message templates'));
275 $this->assign('canEditMessageTemplates', CRM_Core_Permission
::check('edit message templates'));
276 $this->assign('canEditUserDrivenMessageTemplates', CRM_Core_Permission
::check('edit user-driven message templates'));