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