Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
ce064e4f | 19 | * Page for displaying list of message templates. |
6a488035 TO |
20 | */ |
21 | class CRM_Admin_Page_MessageTemplates extends CRM_Core_Page_Basic { | |
22 | ||
23 | /** | |
eceb18cc | 24 | * The action links that we need to display for the browse screen. |
6a488035 TO |
25 | * |
26 | * @var array | |
6a488035 | 27 | */ |
62d3ee27 | 28 | public static $_links = NULL; |
6a488035 | 29 | |
62d3ee27 SL |
30 | /** |
31 | * ids of templates which diverted from the default ones and can be reverted | |
32 | * @var array | |
33 | */ | |
be2fb01f | 34 | protected $_revertible = []; |
6a488035 | 35 | |
62d3ee27 SL |
36 | /** |
37 | * set to the id that we’re reverting at the given moment (if we are) | |
38 | * @var int | |
39 | */ | |
430ae6dd TO |
40 | protected $_revertedId; |
41 | ||
e0ef6999 EM |
42 | /** |
43 | * @param null $title | |
44 | * @param null $mode | |
45 | */ | |
00be9182 | 46 | public function __construct($title = NULL, $mode = NULL) { |
6a488035 TO |
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_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 | |
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 | /** | |
eceb18cc | 69 | * Get BAO Name. |
6a488035 | 70 | * |
a6c01b45 CW |
71 | * @return string |
72 | * Classname of BAO. | |
6a488035 | 73 | */ |
00be9182 | 74 | public function getBAOName() { |
c6327d7d | 75 | return 'CRM_Core_BAO_MessageTemplate'; |
6a488035 TO |
76 | } |
77 | ||
78 | /** | |
eceb18cc | 79 | * Get action Links. |
6a488035 | 80 | * |
a6c01b45 CW |
81 | * @return array |
82 | * (reference) of action links | |
6a488035 | 83 | */ |
00be9182 | 84 | public function &links() { |
6a488035 | 85 | if (!(self::$_links)) { |
be2fb01f CW |
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 => [ | |
6a488035 TO |
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'), | |
be2fb01f CW |
93 | ], |
94 | CRM_Core_Action::DISABLE => [ | |
6a488035 | 95 | 'name' => ts('Disable'), |
4d17a233 | 96 | 'ref' => 'crm-enable-disable', |
6a488035 | 97 | 'title' => ts('Disable this message template'), |
be2fb01f CW |
98 | ], |
99 | CRM_Core_Action::ENABLE => [ | |
6a488035 | 100 | 'name' => ts('Enable'), |
4d17a233 | 101 | 'ref' => 'crm-enable-disable', |
6a488035 | 102 | 'title' => ts('Enable this message template'), |
be2fb01f CW |
103 | ], |
104 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
105 | 'name' => ts('Delete'), |
106 | 'url' => 'civicrm/admin/messageTemplates', | |
107 | 'qs' => 'action=delete&id=%%id%%', | |
108 | 'title' => ts('Delete this message template'), | |
be2fb01f CW |
109 | ], |
110 | CRM_Core_Action::REVERT => [ | |
6a488035 TO |
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'), | |
be2fb01f CW |
116 | ], |
117 | CRM_Core_Action::VIEW => [ | |
6a488035 TO |
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'), | |
be2fb01f CW |
122 | ], |
123 | ]; | |
6a488035 TO |
124 | } |
125 | return self::$_links; | |
126 | } | |
127 | ||
e0ef6999 EM |
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 | */ | |
00be9182 | 136 | public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) { |
6a488035 TO |
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) | |
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_id) { | |
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 | |
5d4fcf54 TO |
154 | $values['action'] = CRM_Core_Action::formLink($links, $action, |
155 | [ | |
353ffa53 | 156 | 'id' => $object->id, |
6b409353 | 157 | 'orig_id' => $this->_revertible[$object->id] ?? NULL, |
be2fb01f | 158 | ], |
87dab4a4 AH |
159 | ts('more'), |
160 | FALSE, | |
161 | 'messageTemplate.manage.action', | |
162 | 'MessageTemplate', | |
163 | $object->id | |
164 | ); | |
6a488035 TO |
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 | ||
e0ef6999 EM |
173 | /** |
174 | * @param null $args | |
175 | * @param null $pageArgs | |
176 | * @param null $sort | |
177 | * | |
178 | * @throws Exception | |
179 | */ | |
00be9182 | 180 | public function run($args = NULL, $pageArgs = NULL, $sort = NULL) { |
c1bd8375 | 181 | $id = $this->getIdAndAction(); |
6a488035 | 182 | // handle the revert action and offload the rest to parent |
c1bd8375 | 183 | if ($this->_action & CRM_Core_Action::REVERT) { |
6a488035 | 184 | $this->_revertedId = $id; |
c6327d7d | 185 | CRM_Core_BAO_MessageTemplate::revert($id); |
6a488035 | 186 | } |
c1bd8375 | 187 | |
6a488035 TO |
188 | return parent::run($args, $pageArgs, $sort); |
189 | } | |
190 | ||
191 | /** | |
eceb18cc | 192 | * Get name of edit form. |
6a488035 | 193 | * |
a6c01b45 CW |
194 | * @return string |
195 | * Classname of edit form. | |
6a488035 | 196 | */ |
00be9182 | 197 | public function editForm() { |
6a488035 TO |
198 | return 'CRM_Admin_Form_MessageTemplates'; |
199 | } | |
200 | ||
201 | /** | |
eceb18cc | 202 | * Get edit form name. |
6a488035 | 203 | * |
a6c01b45 CW |
204 | * @return string |
205 | * name of this page. | |
6a488035 | 206 | */ |
00be9182 | 207 | public function editName() { |
6a488035 TO |
208 | return ts('Message Template'); |
209 | } | |
210 | ||
211 | /** | |
212 | * Get user context. | |
213 | * | |
77b97be7 EM |
214 | * @param null $mode |
215 | * | |
a6c01b45 CW |
216 | * @return string |
217 | * user context. | |
6a488035 | 218 | */ |
00be9182 | 219 | public function userContext($mode = NULL) { |
6a488035 TO |
220 | return 'civicrm/admin/messageTemplates'; |
221 | } | |
222 | ||
223 | /** | |
100fef9d | 224 | * Browse all entities. |
6a488035 | 225 | */ |
00be9182 | 226 | public function browse() { |
6a488035 TO |
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 | ||
c6327d7d | 246 | $messageTemplate = new CRM_Core_BAO_MessageTemplate(); |
6a488035 TO |
247 | $messageTemplate->orderBy('msg_title' . ' asc'); |
248 | ||
be2fb01f CW |
249 | $userTemplates = []; |
250 | $workflowTemplates = []; | |
6a488035 TO |
251 | |
252 | // find all objects | |
253 | $messageTemplate->find(); | |
254 | while ($messageTemplate->fetch()) { | |
be2fb01f | 255 | $values[$messageTemplate->id] = []; |
6a488035 TO |
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 | ||
c4d7103b | 260 | if (!$messageTemplate->workflow_name) { |
6a488035 TO |
261 | $userTemplates[$messageTemplate->id] = $values[$messageTemplate->id]; |
262 | } | |
263 | elseif (!$messageTemplate->is_reserved) { | |
264 | $workflowTemplates[$messageTemplate->id] = $values[$messageTemplate->id]; | |
265 | } | |
266 | } | |
267 | ||
be2fb01f | 268 | $rows = [ |
6a488035 TO |
269 | 'userTemplates' => $userTemplates, |
270 | 'workflowTemplates' => $workflowTemplates, | |
be2fb01f | 271 | ]; |
6a488035 TO |
272 | |
273 | $this->assign('rows', $rows); | |
40a732a9 SA |
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')); | |
6a488035 | 277 | } |
96025800 | 278 | |
6a488035 | 279 | } |