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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the functionality to delete a group of | |
38 | * contacts. This class provides functionality for the actual | |
39 | * deletion. | |
40 | */ | |
41 | class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { | |
42 | ||
43 | /** | |
44 | * Are we operating in "single mode", i.e. sending email to one | |
45 | * specific contact? | |
46 | * | |
47 | * @var boolean | |
48 | */ | |
49 | protected $_single = FALSE; | |
50 | ||
51 | /** | |
100fef9d | 52 | * Cache shared address message so we don't query twice |
6a488035 TO |
53 | */ |
54 | protected $_sharedAddressMessage = NULL; | |
55 | ||
56 | /** | |
100fef9d | 57 | * Build all the data structures needed to build the form |
6a488035 TO |
58 | * |
59 | * @return void | |
6a488035 | 60 | */ |
00be9182 | 61 | public function preProcess() { |
6a488035 TO |
62 | |
63 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', | |
64 | $this, FALSE | |
65 | ); | |
66 | ||
67 | $this->_searchKey = CRM_Utils_Request::retrieve('key', 'String', $this); | |
68 | ||
69 | // sort out whether it’s a delete-to-trash, delete-into-oblivion or restore (and let the template know) | |
353ffa53 | 70 | $values = $this->controller->exportValues(); |
6a488035 | 71 | $this->_skipUndelete = (CRM_Core_Permission::check('access deleted contacts') and (CRM_Utils_Request::retrieve('skip_undelete', 'Boolean', $this) or CRM_Utils_Array::value('task', $values) == CRM_Contact_Task::DELETE_PERMANENTLY)); |
353ffa53 | 72 | $this->_restore = (CRM_Utils_Request::retrieve('restore', 'Boolean', $this) or CRM_Utils_Array::value('task', $values) == CRM_Contact_Task::RESTORE); |
6a488035 TO |
73 | |
74 | if ($this->_restore && !CRM_Core_Permission::check('access deleted contacts')) { | |
75 | CRM_Core_Error::fatal(ts('You do not have permission to access this contact.')); | |
76 | } | |
77 | elseif (!CRM_Core_Permission::check('delete contacts')) { | |
78 | CRM_Core_Error::fatal(ts('You do not have permission to delete this contact.')); | |
79 | } | |
80 | ||
81 | $this->assign('trash', CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete', NULL) and !$this->_skipUndelete); | |
82 | $this->assign('restore', $this->_restore); | |
83 | ||
84 | if ($this->_restore) { | |
85 | CRM_Utils_System::setTitle(ts('Restore Contact')); | |
86 | } | |
87 | ||
88 | if ($cid) { | |
89 | if (!CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT)) { | |
90 | CRM_Core_Error::fatal(ts('You do not have permission to delete this contact. Note: you can delete contacts if you can edit them.')); | |
0db6c3e1 TO |
91 | } |
92 | elseif (CRM_Contact_BAO_Contact::checkDomainContact($cid)) { | |
6a488035 TO |
93 | CRM_Core_Error::fatal(ts('This contact is a special one for the contact information associated with the CiviCRM installation for this domain. No one is allowed to delete it because the information is used for special system purposes.')); |
94 | } | |
95 | ||
96 | $this->_contactIds = array($cid); | |
97 | $this->_single = TRUE; | |
98 | $this->assign('totalSelectedContacts', 1); | |
99 | } | |
100 | else { | |
101 | parent::preProcess(); | |
102 | } | |
103 | ||
104 | $this->_sharedAddressMessage = $this->get('sharedAddressMessage'); | |
105 | if (!$this->_restore && !$this->_sharedAddressMessage) { | |
106 | // we check for each contact for shared contact address | |
107 | $sharedContactList = array(); | |
108 | $sharedAddressCount = 0; | |
109 | foreach ($this->_contactIds as $contactId) { | |
110 | // check if a contact that is being deleted has any shared addresses | |
111 | $sharedAddressMessage = CRM_Core_BAO_Address::setSharedAddressDeleteStatus(NULL, $contactId, TRUE); | |
112 | ||
113 | if ($sharedAddressMessage['count'] > 0) { | |
114 | $sharedAddressCount += $sharedAddressMessage['count']; | |
115 | $sharedContactList = array_merge($sharedContactList, | |
116 | $sharedAddressMessage['contactList'] | |
117 | ); | |
118 | } | |
119 | } | |
120 | ||
121 | $this->_sharedAddressMessage = array( | |
122 | 'count' => $sharedAddressCount, | |
123 | 'contactList' => $sharedContactList, | |
124 | ); | |
125 | ||
126 | if ($sharedAddressCount > 0) { | |
127 | if (count($this->_contactIds) > 1) { | |
128 | // more than one contact deleted | |
353ffa53 TO |
129 | $message = ts('One of the selected contacts has an address record that is shared with 1 other contact.', array( |
130 | 'plural' => 'One or more selected contacts have address records which are shared with %count other contacts.', | |
ae5ffbb7 | 131 | 'count' => $sharedAddressCount, |
353ffa53 | 132 | )); |
6a488035 TO |
133 | } |
134 | else { | |
135 | // only one contact deleted | |
353ffa53 TO |
136 | $message = ts('This contact has an address record which is shared with 1 other contact.', array( |
137 | 'plural' => 'This contact has an address record which is shared with %count other contacts.', | |
ae5ffbb7 | 138 | 'count' => $sharedAddressCount, |
353ffa53 | 139 | )); |
6a488035 TO |
140 | } |
141 | CRM_Core_Session::setStatus($message . ' ' . ts('Shared addresses will not be removed or altered but will no longer be shared.'), ts('Shared Addesses Owner')); | |
142 | } | |
143 | ||
144 | // set in form controller so that queries are not fired again | |
145 | $this->set('sharedAddressMessage', $this->_sharedAddressMessage); | |
146 | } | |
147 | } | |
148 | ||
149 | /** | |
c490a46a | 150 | * Build the form object |
6a488035 | 151 | * |
6a488035 TO |
152 | * |
153 | * @return void | |
154 | */ | |
00be9182 | 155 | public function buildQuickForm() { |
6a488035 TO |
156 | $label = $this->_restore ? ts('Restore Contact(s)') : ts('Delete Contact(s)'); |
157 | ||
158 | if ($this->_single) { | |
159 | // also fix the user context stack in case the user hits cancel | |
160 | $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'basic'); | |
161 | if ($context == 'search' && CRM_Utils_Rule::qfKey($this->_searchKey)) { | |
162 | $urlParams = "&context=$context&key=$this->_searchKey"; | |
163 | } | |
164 | else { | |
165 | $urlParams = ''; | |
166 | } | |
167 | ||
168 | $session = CRM_Core_Session::singleton(); | |
169 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
353ffa53 TO |
170 | 'reset=1&cid=' . $this->_contactIds[0] . $urlParams |
171 | )); | |
6a488035 TO |
172 | $this->addDefaultButtons($label, 'done', 'cancel'); |
173 | } | |
174 | else { | |
175 | $this->addDefaultButtons($label, 'done'); | |
176 | } | |
2efcf0c2 | 177 | |
f182074e PN |
178 | $this->addFormRule(array('CRM_Contact_Form_Task_Delete', 'formRule'), $this); |
179 | } | |
180 | ||
181 | /** | |
100fef9d | 182 | * Global form rule |
f182074e | 183 | * |
77c5b619 TO |
184 | * @param array $fields |
185 | * The input form values. | |
186 | * @param array $files | |
187 | * The uploaded files if any. | |
188 | * @param object $self | |
189 | * Form object. | |
f182074e | 190 | * |
72b3a70c CW |
191 | * @return bool|array |
192 | * true if no errors, else array of errors | |
f182074e | 193 | */ |
00be9182 | 194 | public static function formRule($fields, $files, $self) { |
f182074e PN |
195 | // CRM-12929 |
196 | $error = array(); | |
197 | if ($self->_skipUndelete) { | |
198 | CRM_Financial_BAO_FinancialItem::checkContactPresent($self->_contactIds, $error); | |
199 | } | |
200 | return $error; | |
6a488035 TO |
201 | } |
202 | ||
203 | /** | |
100fef9d | 204 | * Process the form after the input has been submitted and validated |
6a488035 | 205 | * |
6a488035 | 206 | * |
355ba699 | 207 | * @return void |
6a488035 TO |
208 | */ |
209 | public function postProcess() { | |
210 | $session = CRM_Core_Session::singleton(); | |
211 | $currentUserId = $session->get('userID'); | |
212 | ||
353ffa53 | 213 | $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'basic'); |
6a488035 TO |
214 | $urlParams = 'force=1'; |
215 | $urlString = "civicrm/contact/search/$context"; | |
216 | ||
217 | if (CRM_Utils_Rule::qfKey($this->_searchKey)) { | |
218 | $urlParams .= "&qfKey=$this->_searchKey"; | |
219 | } | |
220 | elseif ($context == 'search') { | |
221 | $urlParams .= "&qfKey={$this->controller->_key}"; | |
222 | $urlString = 'civicrm/contact/search'; | |
223 | } | |
224 | elseif ($context == 'smog') { | |
225 | $urlParams .= "&qfKey={$this->controller->_key}&context=smog"; | |
226 | $urlString = 'civicrm/group/search'; | |
227 | } | |
228 | else { | |
229 | $urlParams = "reset=1"; | |
230 | $urlString = 'civicrm/dashboard'; | |
231 | } | |
232 | ||
233 | // Delete/Restore Contacts. Report errors. | |
234 | $deleted = 0; | |
235 | $not_deleted = array(); | |
236 | foreach ($this->_contactIds as $cid) { | |
237 | $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name'); | |
238 | if (CRM_Contact_BAO_Contact::checkDomainContact($cid)) { | |
239 | $session->setStatus(ts("'%1' cannot be deleted because the information is used for special system purposes.", array(1 => $name)), 'Cannot Delete Domain Contact', 'error'); | |
240 | continue; | |
241 | } | |
242 | if ($currentUserId == $cid && !$this->_restore) { | |
243 | $session->setStatus(ts("You are currently logged in as '%1'. You cannot delete yourself.", array(1 => $name)), 'Unable To Delete', 'error'); | |
244 | continue; | |
245 | } | |
246 | if (CRM_Contact_BAO_Contact::deleteContact($cid, $this->_restore, $this->_skipUndelete)) { | |
247 | $deleted++; | |
248 | } | |
249 | else { | |
250 | $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$cid"); | |
251 | $not_deleted[$cid] = "<a href='$url'>$name</a>"; | |
252 | } | |
253 | } | |
254 | if ($deleted) { | |
255 | $title = ts('Deleted'); | |
256 | if ($this->_restore) { | |
257 | $title = ts('Restored'); | |
353ffa53 TO |
258 | $status = ts('%1 has been restored from the trash.', array( |
259 | 1 => $name, | |
260 | 'plural' => '%count contacts restored from trash.', | |
ae5ffbb7 | 261 | 'count' => $deleted, |
353ffa53 | 262 | )); |
6a488035 TO |
263 | } |
264 | elseif ($this->_skipUndelete) { | |
353ffa53 TO |
265 | $status = ts('%1 has been permanently deleted.', array( |
266 | 1 => $name, | |
267 | 'plural' => '%count contacts permanently deleted.', | |
ae5ffbb7 | 268 | 'count' => $deleted, |
353ffa53 | 269 | )); |
6a488035 TO |
270 | } |
271 | else { | |
353ffa53 TO |
272 | $status = ts('%1 has been moved to the trash.', array( |
273 | 1 => $name, | |
274 | 'plural' => '%count contacts moved to trash.', | |
ae5ffbb7 | 275 | 'count' => $deleted, |
353ffa53 | 276 | )); |
6a488035 TO |
277 | } |
278 | $session->setStatus($status, $title, 'success'); | |
279 | } | |
280 | // Alert user of any failures | |
281 | if ($not_deleted) { | |
282 | $status = ts('The contact might be the Membership Organization of a Membership Type. You will need to edit the Membership Type and change the Membership Organization before you can delete this contact.'); | |
283 | $title = ts('Unable to Delete'); | |
284 | $session->setStatus('<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>' . $status, $title, 'error'); | |
285 | } | |
286 | ||
287 | if (isset($this->_sharedAddressMessage) && $this->_sharedAddressMessage['count'] > 0 && !$this->_restore) { | |
288 | if (count($this->_sharedAddressMessage['contactList']) == 1) { | |
289 | $message = ts('The following contact had been sharing an address with a contact you just deleted. Their address will no longer be shared, but has not been removed or altered.'); | |
290 | } | |
291 | else { | |
292 | $message = ts('The following contacts had been sharing addresses with a contact you just deleted. Their addressses will no longer be shared, but have not been removed or altered.'); | |
293 | } | |
294 | $message .= '<ul><li>' . implode('</li><li>', $this->_sharedAddressMessage['contactList']) . '</li></ul>'; | |
295 | ||
296 | $session->setStatus($message, ts('Shared Addesses Owner Deleted'), 'info', array('expires' => 0)); | |
297 | ||
298 | $this->set('sharedAddressMessage', NULL); | |
299 | } | |
300 | ||
301 | if ($this->_single && empty($this->_skipUndelete)) { | |
302 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactIds[0]}")); | |
303 | } | |
304 | else { | |
305 | $session->replaceUserContext(CRM_Utils_System::url($urlString, $urlParams)); | |
306 | } | |
307 | } | |
96025800 | 308 | |
6a488035 | 309 | } |