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