Merge pull request #18076 from MegaphoneJon/better-on-behalf-of-2
[civicrm-core.git] / CRM / Case / Form / Task / Delete.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to delete a group of case records.
20 */
21 class CRM_Case_Form_Task_Delete extends CRM_Case_Form_Task {
22
23 /**
24 * Are we operating in "single mode", i.e. deleting one
25 * specific case?
26 *
27 * @var bool
28 */
29 protected $_single = FALSE;
30
31 /**
32 * Are we moving case to Trash.
33 *
34 * @var bool
35 */
36 public $_moveToTrash = TRUE;
37
38 /**
39 * Build all the data structures needed to build the form.
40 *
41 * @throws \CRM_Core_Exception
42 */
43 public function preProcess() {
44 if (!CRM_Core_Permission::checkActionPermission('CiviCase', CRM_Core_Action::DELETE)) {
45 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
46 }
47 parent::preProcess();
48 }
49
50 /**
51 * Build the form object.
52 */
53 public function buildQuickForm() {
54 $this->addDefaultButtons(ts('Delete cases'), 'done');
55 }
56
57 /**
58 * Process the form after the input has been submitted and validated.
59 */
60 public function postProcess() {
61 $deleted = $failed = 0;
62 foreach ($this->_entityIds as $caseId) {
63 if (CRM_Case_BAO_Case::deleteCase($caseId, $this->_moveToTrash)) {
64 $deleted++;
65 }
66 else {
67 $failed++;
68 }
69 }
70
71 if ($deleted) {
72 if ($this->_moveToTrash) {
73 $msg = ts('%count case moved to trash.', ['plural' => '%count cases moved to trash.', 'count' => $deleted]);
74 }
75 else {
76 $msg = ts('%count case permanently deleted.', ['plural' => '%count cases permanently deleted.', 'count' => $deleted]);
77 }
78 CRM_Core_Session::setStatus($msg, ts('Removed'), 'success');
79 }
80
81 if ($failed) {
82 CRM_Core_Session::setStatus(ts('1 could not be deleted.', ['plural' => '%count could not be deleted.', 'count' => $failed]), ts('Error'), 'error');
83 }
84 }
85
86 }