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