Commit | Line | Data |
---|---|---|
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 | */ | |
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 | ||
55 | function __construct($title = NULL, $mode = NULL) { | |
6a488035 TO |
56 | parent::__construct($title, $mode); |
57 | ||
58 | // fetch the ids of templates which diverted from defaults and can be reverted – | |
59 | // these templates have the same workflow_id as the defaults; defaults are reserved | |
60 | $sql = ' | |
61 | SELECT diverted.id, orig.id orig_id | |
62 | FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON ( | |
63 | diverted.workflow_id = orig.workflow_id AND | |
64 | orig.is_reserved = 1 AND ( | |
65 | diverted.msg_subject != orig.msg_subject OR | |
66 | diverted.msg_text != orig.msg_text OR | |
67 | diverted.msg_html != orig.msg_html | |
68 | ) | |
69 | ) | |
70 | '; | |
71 | $dao = CRM_Core_DAO::executeQuery($sql); | |
72 | while ($dao->fetch()) { | |
73 | $this->_revertible[$dao->id] = $dao->orig_id; | |
74 | } | |
75 | } | |
76 | ||
77 | /** | |
78 | * Get BAO Name | |
79 | * | |
80 | * @return string Classname of BAO. | |
81 | */ | |
82 | function getBAOName() { | |
c6327d7d | 83 | return 'CRM_Core_BAO_MessageTemplate'; |
6a488035 TO |
84 | } |
85 | ||
86 | /** | |
87 | * Get action Links | |
88 | * | |
89 | * @return array (reference) of action links | |
90 | */ | |
91 | function &links() { | |
92 | if (!(self::$_links)) { | |
93 | $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')); | |
94 | self::$_links = array( | |
95 | CRM_Core_Action::UPDATE => array( | |
96 | 'name' => ts('Edit'), | |
97 | 'url' => 'civicrm/admin/messageTemplates/add', | |
98 | 'qs' => 'action=update&id=%%id%%&reset=1', | |
99 | 'title' => ts('Edit this message template'), | |
100 | ), | |
101 | CRM_Core_Action::DISABLE => array( | |
102 | 'name' => ts('Disable'), | |
4d17a233 | 103 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
104 | 'title' => ts('Disable this message template'), |
105 | ), | |
106 | CRM_Core_Action::ENABLE => array( | |
107 | 'name' => ts('Enable'), | |
4d17a233 | 108 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
109 | 'title' => ts('Enable this message template'), |
110 | ), | |
111 | CRM_Core_Action::DELETE => array( | |
112 | 'name' => ts('Delete'), | |
113 | 'url' => 'civicrm/admin/messageTemplates', | |
114 | 'qs' => 'action=delete&id=%%id%%', | |
115 | 'title' => ts('Delete this message template'), | |
116 | ), | |
117 | CRM_Core_Action::REVERT => array( | |
118 | 'name' => ts('Revert to Default'), | |
119 | 'extra' => "onclick = 'return confirm(\"$confirm\");'", | |
120 | 'url' => 'civicrm/admin/messageTemplates', | |
121 | 'qs' => 'action=revert&id=%%id%%&selectedChild=workflow', | |
122 | 'title' => ts('Revert this workflow message template to the system default'), | |
123 | ), | |
124 | CRM_Core_Action::VIEW => array( | |
125 | 'name' => ts('View Default'), | |
126 | 'url' => 'civicrm/admin/messageTemplates', | |
127 | 'qs' => 'action=view&id=%%orig_id%%&reset=1', | |
128 | 'title' => ts('View the system default for this workflow message template'), | |
129 | ), | |
130 | ); | |
131 | } | |
132 | return self::$_links; | |
133 | } | |
134 | ||
135 | function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) { | |
136 | if ($object->workflow_id) { | |
137 | // do not expose action link for reverting to default if the template did not diverge or we just reverted it now | |
138 | if (!in_array($object->id, array_keys($this->_revertible)) or | |
139 | ($this->_action & CRM_Core_Action::REVERT and $object->id == $this->_revertedId) | |
140 | ) { | |
141 | $action &= ~CRM_Core_Action::REVERT; | |
142 | $action &= ~CRM_Core_Action::VIEW; | |
143 | } | |
144 | ||
145 | // default workflow templates shouldn’t be deletable | |
146 | // workflow templates shouldn’t have disable/enable actions (at least for CiviCRM 3.1) | |
147 | if ($object->workflow_id) { | |
148 | $action &= ~CRM_Core_Action::DISABLE; | |
149 | $action &= ~CRM_Core_Action::DELETE; | |
150 | } | |
151 | ||
152 | // rebuild the action links HTML, as we need to handle %%orig_id%% for revertible templates | |
153 | $values['action'] = CRM_Core_Action::formLink($links, $action, array( | |
154 | 'id' => $object->id, | |
155 | 'orig_id' => CRM_Utils_Array::value($object->id, $this->_revertible), | |
87dab4a4 AH |
156 | ), |
157 | ts('more'), | |
158 | FALSE, | |
159 | 'messageTemplate.manage.action', | |
160 | 'MessageTemplate', | |
161 | $object->id | |
162 | ); | |
6a488035 TO |
163 | } |
164 | else { | |
165 | $action &= ~CRM_Core_Action::REVERT; | |
166 | $action &= ~CRM_Core_Action::VIEW; | |
167 | parent::action($object, $action, $values, $links, $permission); | |
168 | } | |
169 | } | |
170 | ||
171 | function run($args = NULL, $pageArgs = NULL, $sort = NULL) { | |
172 | // handle the revert action and offload the rest to parent | |
173 | if (CRM_Utils_Request::retrieve('action', 'String', $this) & CRM_Core_Action::REVERT) { | |
174 | ||
175 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
176 | if (!$this->checkPermission($id, NULL)) { | |
177 | CRM_Core_Error::fatal(ts('You do not have permission to revert this template.')); | |
178 | } | |
179 | ||
180 | $this->_revertedId = $id; | |
181 | ||
c6327d7d | 182 | CRM_Core_BAO_MessageTemplate::revert($id); |
6a488035 TO |
183 | } |
184 | ||
185 | $this->assign('selectedChild', CRM_Utils_Request::retrieve('selectedChild', 'String', $this)); | |
186 | ||
187 | return parent::run($args, $pageArgs, $sort); | |
188 | } | |
189 | ||
190 | /** | |
191 | * Get name of edit form | |
192 | * | |
193 | * @return string Classname of edit form. | |
194 | */ | |
195 | function editForm() { | |
196 | return 'CRM_Admin_Form_MessageTemplates'; | |
197 | } | |
198 | ||
199 | /** | |
200 | * Get edit form name | |
201 | * | |
202 | * @return string name of this page. | |
203 | */ | |
204 | function editName() { | |
205 | return ts('Message Template'); | |
206 | } | |
207 | ||
208 | /** | |
209 | * Get user context. | |
210 | * | |
211 | * @return string user context. | |
212 | */ | |
213 | function userContext($mode = NULL) { | |
214 | return 'civicrm/admin/messageTemplates'; | |
215 | } | |
216 | ||
217 | /** | |
218 | * browse all entities. | |
219 | * | |
220 | * @param int $action | |
221 | * | |
222 | * @return void | |
223 | * @access public | |
224 | */ | |
225 | function browse() { | |
4d17a233 | 226 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); |
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 | ||
249 | $userTemplates = array(); | |
250 | $workflowTemplates = array(); | |
251 | ||
252 | // find all objects | |
253 | $messageTemplate->find(); | |
254 | while ($messageTemplate->fetch()) { | |
255 | $values[$messageTemplate->id] = array(); | |
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 | ||
260 | if (!$messageTemplate->workflow_id) { | |
261 | $userTemplates[$messageTemplate->id] = $values[$messageTemplate->id]; | |
262 | } | |
263 | elseif (!$messageTemplate->is_reserved) { | |
264 | $workflowTemplates[$messageTemplate->id] = $values[$messageTemplate->id]; | |
265 | } | |
266 | } | |
267 | ||
268 | $rows = array( | |
269 | 'userTemplates' => $userTemplates, | |
270 | 'workflowTemplates' => $workflowTemplates, | |
271 | ); | |
272 | ||
273 | $this->assign('rows', $rows); | |
274 | } | |
275 | } | |
276 |