Merge pull request #22884 from civicrm/5.47
[civicrm-core.git] / CRM / Admin / Page / MessageTemplates.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Page for displaying list of message templates.
20 */
21 class CRM_Admin_Page_MessageTemplates extends CRM_Core_Page_Basic {
22
23 /**
24 * The action links that we need to display for the browse screen.
25 *
26 * @var array
27 */
28 public static $_links = NULL;
29
30 /**
31 * ids of templates which diverted from the default ones and can be reverted
32 * @var array
33 */
34 protected $_revertible = [];
35
36 /**
37 * set to the id that we’re reverting at the given moment (if we are)
38 * @var int
39 */
40 protected $_revertedId;
41
42 /**
43 * @param string $title
44 * @param int $mode
45 */
46 public function __construct($title = NULL, $mode = NULL) {
47 parent::__construct($title, $mode);
48
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
51 $sql = '
52 SELECT diverted.id, orig.id orig_id
53 FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
54 diverted.workflow_name = orig.workflow_name 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
59 )
60 )
61 ';
62 $dao = CRM_Core_DAO::executeQuery($sql);
63 while ($dao->fetch()) {
64 $this->_revertible[$dao->id] = $dao->orig_id;
65 }
66 }
67
68 /**
69 * Get BAO Name.
70 *
71 * @return string
72 * Classname of BAO.
73 */
74 public function getBAOName() {
75 return 'CRM_Core_BAO_MessageTemplate';
76 }
77
78 /**
79 * Get action Links.
80 *
81 * @return array
82 * (reference) of action links
83 */
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']);
87 self::$_links = [
88 CRM_Core_Action::UPDATE => [
89 'name' => ts('Edit'),
90 'url' => 'civicrm/admin/messageTemplates/add',
91 'qs' => 'action=update&id=%%id%%&reset=1',
92 'title' => ts('Edit this message template'),
93 ],
94 CRM_Core_Action::DISABLE => [
95 'name' => ts('Disable'),
96 'ref' => 'crm-enable-disable',
97 'title' => ts('Disable this message template'),
98 ],
99 CRM_Core_Action::ENABLE => [
100 'name' => ts('Enable'),
101 'ref' => 'crm-enable-disable',
102 'title' => ts('Enable this message template'),
103 ],
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'),
109 ],
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'),
116 ],
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'),
122 ],
123 ];
124 }
125 return self::$_links;
126 }
127
128 /**
129 * @param CRM_Core_DAO $object
130 * @param int $action
131 * @param array $values
132 * @param array $links
133 * @param string $permission
134 * @param bool $forceAction
135 */
136 public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) {
137 if ($object->workflow_name) {
138 // do not expose action link for reverting to default if the template did not diverge or we just reverted it now
139 if (!array_key_exists($object->id, $this->_revertible) or
140 ($this->_action & CRM_Core_Action::REVERT and $object->id == $this->_revertedId)
141 ) {
142 $action &= ~CRM_Core_Action::REVERT;
143 $action &= ~CRM_Core_Action::VIEW;
144 }
145
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_name) {
149 $action &= ~CRM_Core_Action::DISABLE;
150 $action &= ~CRM_Core_Action::DELETE;
151 }
152
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,
155 [
156 'id' => $object->id,
157 'orig_id' => $this->_revertible[$object->id] ?? NULL,
158 ],
159 ts('more'),
160 FALSE,
161 'messageTemplate.manage.action',
162 'MessageTemplate',
163 $object->id
164 );
165 }
166 else {
167 $action &= ~CRM_Core_Action::REVERT;
168 $action &= ~CRM_Core_Action::VIEW;
169 parent::action($object, $action, $values, $links, $permission);
170 }
171 }
172
173 /**
174 * @param null $args
175 * @param null $pageArgs
176 * @param null $sort
177 *
178 * @throws Exception
179 */
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);
186 }
187
188 return parent::run($args, $pageArgs, $sort);
189 }
190
191 /**
192 * Get name of edit form.
193 *
194 * @return string
195 * Classname of edit form.
196 */
197 public function editForm() {
198 return 'CRM_Admin_Form_MessageTemplates';
199 }
200
201 /**
202 * Get edit form name.
203 *
204 * @return string
205 * name of this page.
206 */
207 public function editName() {
208 return ts('Message Template');
209 }
210
211 /**
212 * Get user context.
213 *
214 * @param null $mode
215 *
216 * @return string
217 * user context.
218 */
219 public function userContext($mode = NULL) {
220 return 'civicrm/admin/messageTemplates';
221 }
222
223 /**
224 * Browse all entities.
225 */
226 public function browse() {
227 $action = func_num_args() ? func_get_arg(0) : NULL;
228 if ($this->_action & CRM_Core_Action::ADD) {
229 return;
230 }
231 $links = $this->links();
232 if ($action == NULL) {
233 if (!empty($links)) {
234 $action = array_sum(array_keys($links));
235 }
236 }
237
238 if ($action & CRM_Core_Action::DISABLE) {
239 $action -= CRM_Core_Action::DISABLE;
240 }
241
242 if ($action & CRM_Core_Action::ENABLE) {
243 $action -= CRM_Core_Action::ENABLE;
244 }
245
246 $messageTemplate = new CRM_Core_BAO_MessageTemplate();
247 $messageTemplate->orderBy('msg_title' . ' asc');
248
249 $userTemplates = [];
250 $workflowTemplates = [];
251
252 // find all objects
253 $messageTemplate->find();
254 while ($messageTemplate->fetch()) {
255 $values[$messageTemplate->id] = ['class' => ''];
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);
259
260 if (!$messageTemplate->workflow_name) {
261 $userTemplates[$messageTemplate->id] = $values[$messageTemplate->id];
262 }
263 elseif (!$messageTemplate->is_reserved) {
264 $workflowTemplates[$messageTemplate->id] = $values[$messageTemplate->id];
265 }
266 }
267
268 $rows = [
269 'userTemplates' => $userTemplates,
270 'workflowTemplates' => $workflowTemplates,
271 ];
272
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'));
277 Civi::resources()
278 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
279 ->addSetting([
280 'tabSettings' => ['active' => $_GET['selectedChild'] ?? NULL],
281 ]);
282 }
283
284 }