-- CRM-12682 applied patch and made some modifications
[civicrm-core.git] / CRM / Contact / Form / Task / Delete.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 * 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 /**
52 * cache shared address message so we don't query twice
53 */
54 protected $_sharedAddressMessage = NULL;
55
56 /**
57 * build all the data structures needed to build the form
58 *
59 * @return void
60 * @access public
61 */
62 function preProcess() {
63
64 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
65 $this, FALSE
66 );
67
68 $this->_searchKey = CRM_Utils_Request::retrieve('key', 'String', $this);
69
70 // sort out whether it’s a delete-to-trash, delete-into-oblivion or restore (and let the template know)
71 $values = $this->controller->exportValues();
72 $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));
73 $this->_restore = (CRM_Utils_Request::retrieve('restore', 'Boolean', $this) or CRM_Utils_Array::value('task', $values) == CRM_Contact_Task::RESTORE);
74
75 if ($this->_restore && !CRM_Core_Permission::check('access deleted contacts')) {
76 CRM_Core_Error::fatal(ts('You do not have permission to access this contact.'));
77 }
78 elseif (!CRM_Core_Permission::check('delete contacts')) {
79 CRM_Core_Error::fatal(ts('You do not have permission to delete this contact.'));
80 }
81
82 $this->assign('trash', CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete', NULL) and !$this->_skipUndelete);
83 $this->assign('restore', $this->_restore);
84
85 if ($this->_restore) {
86 CRM_Utils_System::setTitle(ts('Restore Contact'));
87 }
88
89 if ($cid) {
90 if (!CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT)) {
91 CRM_Core_Error::fatal(ts('You do not have permission to delete this contact. Note: you can delete contacts if you can edit them.'));
92 } elseif (CRM_Contact_BAO_Contact::checkDomainContact($cid)) {
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
129 $message = ts('One of the selected contacts has an address record that is shared with 1 other contact.', array('plural' => 'One or more selected contacts have address records which are shared with %count other contacts.', 'count' => $sharedAddressCount));
130 }
131 else {
132 // only one contact deleted
133 $message = ts('This contact has an address record which is shared with 1 other contact.', array('plural' => 'This contact has an address record which is shared with %count other contacts.', 'count' => $sharedAddressCount));
134 }
135 CRM_Core_Session::setStatus($message . ' ' . ts('Shared addresses will not be removed or altered but will no longer be shared.'), ts('Shared Addesses Owner'));
136 }
137
138 // set in form controller so that queries are not fired again
139 $this->set('sharedAddressMessage', $this->_sharedAddressMessage);
140 }
141 }
142
143 /**
144 * Build the form
145 *
146 * @access public
147 *
148 * @return void
149 */
150 function buildQuickForm() {
151 $label = $this->_restore ? ts('Restore Contact(s)') : ts('Delete Contact(s)');
152
153 if ($this->_single) {
154 // also fix the user context stack in case the user hits cancel
155 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'basic');
156 if ($context == 'search' && CRM_Utils_Rule::qfKey($this->_searchKey)) {
157 $urlParams = "&context=$context&key=$this->_searchKey";
158 }
159 else {
160 $urlParams = '';
161 }
162
163 $session = CRM_Core_Session::singleton();
164 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
165 'reset=1&cid=' . $this->_contactIds[0] . $urlParams
166 ));
167 $this->addDefaultButtons($label, 'done', 'cancel');
168 }
169 else {
170 $this->addDefaultButtons($label, 'done');
171 }
172 }
173
174 /**
175 * process the form after the input has been submitted and validated
176 *
177 * @access public
178 *
179 * @return None
180 */
181 public function postProcess() {
182 $session = CRM_Core_Session::singleton();
183 $currentUserId = $session->get('userID');
184
185 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'basic');
186 $urlParams = 'force=1';
187 $urlString = "civicrm/contact/search/$context";
188
189 if (CRM_Utils_Rule::qfKey($this->_searchKey)) {
190 $urlParams .= "&qfKey=$this->_searchKey";
191 }
192 elseif ($context == 'search') {
193 $urlParams .= "&qfKey={$this->controller->_key}";
194 $urlString = 'civicrm/contact/search';
195 }
196 elseif ($context == 'smog') {
197 $urlParams .= "&qfKey={$this->controller->_key}&context=smog";
198 $urlString = 'civicrm/group/search';
199 }
200 else {
201 $urlParams = "reset=1";
202 $urlString = 'civicrm/dashboard';
203 }
204
205 // Delete/Restore Contacts. Report errors.
206 $deleted = 0;
207 $not_deleted = array();
208 foreach ($this->_contactIds as $cid) {
209 $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
210 if (CRM_Contact_BAO_Contact::checkDomainContact($cid)) {
211 $session->setStatus(ts("'%1' cannot be deleted because the information is used for special system purposes.", array(1 => $name)), 'Cannot Delete Domain Contact', 'error');
212 continue;
213 }
214 if ($currentUserId == $cid && !$this->_restore) {
215 $session->setStatus(ts("You are currently logged in as '%1'. You cannot delete yourself.", array(1 => $name)), 'Unable To Delete', 'error');
216 continue;
217 }
218 if (CRM_Contact_BAO_Contact::deleteContact($cid, $this->_restore, $this->_skipUndelete)) {
219 $deleted++;
220 }
221 else {
222 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$cid");
223 $not_deleted[$cid] = "<a href='$url'>$name</a>";
224 }
225 }
226 if ($deleted) {
227 $title = ts('Deleted');
228 if ($this->_restore) {
229 $title = ts('Restored');
230 $status = ts('%1 has been restored from the trash.', array(1 => $name, 'plural' => '%count contacts restored from trash.', 'count' => $deleted));
231 }
232 elseif ($this->_skipUndelete) {
233 $status = ts('%1 has been permanently deleted.', array(1 => $name, 'plural' => '%count contacts permanently deleted.', 'count' => $deleted));
234 }
235 else {
236 $status = ts('%1 has been moved to the trash.', array(1 => $name, 'plural' => '%count contacts moved to trash.', 'count' => $deleted));
237 }
238 $session->setStatus($status, $title, 'success');
239 }
240 // Alert user of any failures
241 if ($not_deleted) {
242 $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.');
243 $title = ts('Unable to Delete');
244 $session->setStatus('<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>' . $status, $title, 'error');
245 }
246
247 if (isset($this->_sharedAddressMessage) && $this->_sharedAddressMessage['count'] > 0 && !$this->_restore) {
248 if (count($this->_sharedAddressMessage['contactList']) == 1) {
249 $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.');
250 }
251 else {
252 $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.');
253 }
254 $message .= '<ul><li>' . implode('</li><li>', $this->_sharedAddressMessage['contactList']) . '</li></ul>';
255
256 $session->setStatus($message, ts('Shared Addesses Owner Deleted'), 'info', array('expires' => 0));
257
258 $this->set('sharedAddressMessage', NULL);
259 }
260
261 if ($this->_single && empty($this->_skipUndelete)) {
262 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactIds[0]}"));
263 }
264 else {
265 $session->replaceUserContext(CRM_Utils_System::url($urlString, $urlParams));
266 }
267 }
268 //end of function
269 }
270