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