Merge pull request #14868 from civicrm/5.16
[civicrm-core.git] / CRM / Admin / Page / MessageTemplates.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Page for displaying list of message templates.
36 */
37 class CRM_Admin_Page_MessageTemplates extends CRM_Core_Page_Basic {
38
39 /**
40 * The action links that we need to display for the browse screen.
41 *
42 * @var array
43 */
44 public static $_links = NULL;
45
46 /**
47 * ids of templates which diverted from the default ones and can be reverted
48 * @var array
49 */
50 protected $_revertible = [];
51
52 /**
53 * set to the id that we’re reverting at the given moment (if we are)
54 * @var int
55 */
56 protected $_revertedId;
57
58 /**
59 * @param null $title
60 * @param null $mode
61 */
62 public function __construct($title = NULL, $mode = NULL) {
63 parent::__construct($title, $mode);
64
65 // fetch the ids of templates which diverted from defaults and can be reverted –
66 // these templates have the same workflow_id as the defaults; defaults are reserved
67 $sql = '
68 SELECT diverted.id, orig.id orig_id
69 FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
70 diverted.workflow_id = orig.workflow_id AND
71 orig.is_reserved = 1 AND (
72 diverted.msg_subject != orig.msg_subject OR
73 diverted.msg_text != orig.msg_text OR
74 diverted.msg_html != orig.msg_html
75 )
76 )
77 ';
78 $dao = CRM_Core_DAO::executeQuery($sql);
79 while ($dao->fetch()) {
80 $this->_revertible[$dao->id] = $dao->orig_id;
81 }
82 }
83
84 /**
85 * Get BAO Name.
86 *
87 * @return string
88 * Classname of BAO.
89 */
90 public function getBAOName() {
91 return 'CRM_Core_BAO_MessageTemplate';
92 }
93
94 /**
95 * Get action Links.
96 *
97 * @return array
98 * (reference) of action links
99 */
100 public function &links() {
101 if (!(self::$_links)) {
102 $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']);
103 self::$_links = [
104 CRM_Core_Action::UPDATE => [
105 'name' => ts('Edit'),
106 'url' => 'civicrm/admin/messageTemplates/add',
107 'qs' => 'action=update&id=%%id%%&reset=1',
108 'title' => ts('Edit this message template'),
109 ],
110 CRM_Core_Action::DISABLE => [
111 'name' => ts('Disable'),
112 'ref' => 'crm-enable-disable',
113 'title' => ts('Disable this message template'),
114 ],
115 CRM_Core_Action::ENABLE => [
116 'name' => ts('Enable'),
117 'ref' => 'crm-enable-disable',
118 'title' => ts('Enable this message template'),
119 ],
120 CRM_Core_Action::DELETE => [
121 'name' => ts('Delete'),
122 'url' => 'civicrm/admin/messageTemplates',
123 'qs' => 'action=delete&id=%%id%%',
124 'title' => ts('Delete this message template'),
125 ],
126 CRM_Core_Action::REVERT => [
127 'name' => ts('Revert to Default'),
128 'extra' => "onclick = 'return confirm(\"$confirm\");'",
129 'url' => 'civicrm/admin/messageTemplates',
130 'qs' => 'action=revert&id=%%id%%&selectedChild=workflow',
131 'title' => ts('Revert this workflow message template to the system default'),
132 ],
133 CRM_Core_Action::VIEW => [
134 'name' => ts('View Default'),
135 'url' => 'civicrm/admin/messageTemplates',
136 'qs' => 'action=view&id=%%orig_id%%&reset=1',
137 'title' => ts('View the system default for this workflow message template'),
138 ],
139 ];
140 }
141 return self::$_links;
142 }
143
144 /**
145 * @param CRM_Core_DAO $object
146 * @param int $action
147 * @param array $values
148 * @param array $links
149 * @param string $permission
150 * @param bool $forceAction
151 */
152 public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) {
153 if ($object->workflow_id) {
154 // do not expose action link for reverting to default if the template did not diverge or we just reverted it now
155 if (!in_array($object->id, array_keys($this->_revertible)) or
156 ($this->_action & CRM_Core_Action::REVERT and $object->id == $this->_revertedId)
157 ) {
158 $action &= ~CRM_Core_Action::REVERT;
159 $action &= ~CRM_Core_Action::VIEW;
160 }
161
162 // default workflow templates shouldn’t be deletable
163 // workflow templates shouldn’t have disable/enable actions (at least for CiviCRM 3.1)
164 if ($object->workflow_id) {
165 $action &= ~CRM_Core_Action::DISABLE;
166 $action &= ~CRM_Core_Action::DELETE;
167 }
168
169 // rebuild the action links HTML, as we need to handle %%orig_id%% for revertible templates
170 $values['action'] = CRM_Core_Action::formLink($links, $action,
171 [
172 'id' => $object->id,
173 'orig_id' => CRM_Utils_Array::value($object->id, $this->_revertible),
174 ],
175 ts('more'),
176 FALSE,
177 'messageTemplate.manage.action',
178 'MessageTemplate',
179 $object->id
180 );
181 }
182 else {
183 $action &= ~CRM_Core_Action::REVERT;
184 $action &= ~CRM_Core_Action::VIEW;
185 parent::action($object, $action, $values, $links, $permission);
186 }
187 }
188
189 /**
190 * @param null $args
191 * @param null $pageArgs
192 * @param null $sort
193 *
194 * @throws Exception
195 */
196 public function run($args = NULL, $pageArgs = NULL, $sort = NULL) {
197 $id = $this->getIdAndAction();
198 // handle the revert action and offload the rest to parent
199 if ($this->_action & CRM_Core_Action::REVERT) {
200 $this->_revertedId = $id;
201 CRM_Core_BAO_MessageTemplate::revert($id);
202 }
203
204 return parent::run($args, $pageArgs, $sort);
205 }
206
207 /**
208 * Get name of edit form.
209 *
210 * @return string
211 * Classname of edit form.
212 */
213 public function editForm() {
214 return 'CRM_Admin_Form_MessageTemplates';
215 }
216
217 /**
218 * Get edit form name.
219 *
220 * @return string
221 * name of this page.
222 */
223 public function editName() {
224 return ts('Message Template');
225 }
226
227 /**
228 * Get user context.
229 *
230 * @param null $mode
231 *
232 * @return string
233 * user context.
234 */
235 public function userContext($mode = NULL) {
236 return 'civicrm/admin/messageTemplates';
237 }
238
239 /**
240 * Browse all entities.
241 */
242 public function browse() {
243 $action = func_num_args() ? func_get_arg(0) : NULL;
244 if ($this->_action & CRM_Core_Action::ADD) {
245 return;
246 }
247 $links = $this->links();
248 if ($action == NULL) {
249 if (!empty($links)) {
250 $action = array_sum(array_keys($links));
251 }
252 }
253
254 if ($action & CRM_Core_Action::DISABLE) {
255 $action -= CRM_Core_Action::DISABLE;
256 }
257
258 if ($action & CRM_Core_Action::ENABLE) {
259 $action -= CRM_Core_Action::ENABLE;
260 }
261
262 $messageTemplate = new CRM_Core_BAO_MessageTemplate();
263 $messageTemplate->orderBy('msg_title' . ' asc');
264
265 $userTemplates = [];
266 $workflowTemplates = [];
267
268 // find all objects
269 $messageTemplate->find();
270 while ($messageTemplate->fetch()) {
271 $values[$messageTemplate->id] = [];
272 CRM_Core_DAO::storeValues($messageTemplate, $values[$messageTemplate->id]);
273 // populate action links
274 $this->action($messageTemplate, $action, $values[$messageTemplate->id], $links, CRM_Core_Permission::EDIT);
275
276 if (!$messageTemplate->workflow_id) {
277 $userTemplates[$messageTemplate->id] = $values[$messageTemplate->id];
278 }
279 elseif (!$messageTemplate->is_reserved) {
280 $workflowTemplates[$messageTemplate->id] = $values[$messageTemplate->id];
281 }
282 }
283
284 $rows = [
285 'userTemplates' => $userTemplates,
286 'workflowTemplates' => $workflowTemplates,
287 ];
288
289 $this->assign('rows', $rows);
290 $this->assign('canEditSystemTemplates', CRM_Core_Permission::check('edit system workflow message templates'));
291 $this->assign('canEditMessageTemplates', CRM_Core_Permission::check('edit message templates'));
292 $this->assign('canEditUserDrivenMessageTemplates', CRM_Core_Permission::check('edit user-driven message templates'));
293 }
294
295 }