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