Merge pull request #18757 from civicrm/5.31
[civicrm-core.git] / CRM / Event / 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
20 * participations. This class provides functionality for the actual
21 * deletion.
22 */
23 class CRM_Event_Form_Task_Delete extends CRM_Event_Form_Task {
24
25 /**
26 * Are we operating in "single mode", i.e. deleting one
27 * specific participation?
28 *
29 * @var bool
30 */
31 protected $_single = FALSE;
32
33 /**
34 * Build all the data structures needed to build the form.
35 *
36 * @return void
37 */
38 public function preProcess() {
39
40 //check for delete
41 if (!CRM_Core_Permission::checkActionPermission('CiviEvent', CRM_Core_Action::DELETE)) {
42 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
43 }
44 parent::preProcess();
45 foreach ($this->_participantIds as $participantId) {
46 if (CRM_Event_BAO_Participant::isPrimaryParticipant($participantId)) {
47 $this->assign('additionalParticipants', TRUE);
48 }
49 }
50 }
51
52 /**
53 * Build the form object.
54 *
55 *
56 * @return void
57 */
58 public function buildQuickForm() {
59 $deleteParticipants = [
60 1 => ts('Delete this participant record along with associated participant record(s).'),
61 2 => ts('Delete only this participant record.'),
62 ];
63
64 $this->addRadio('delete_participant', NULL, $deleteParticipants, NULL, '<br />');
65 $this->setDefaults(['delete_participant' => 1]);
66
67 $this->addDefaultButtons(ts('Delete Participations'), 'done');
68 }
69
70 /**
71 * Process the form after the input has been submitted and validated.
72 *
73 *
74 * @return void
75 */
76 public function postProcess() {
77 $params = $this->controller->exportValues($this->_name);
78
79 $participantLinks = NULL;
80 if (CRM_Utils_Array::value('delete_participant', $params) == 2) {
81 $links = [];
82 foreach ($this->_participantIds as $participantId) {
83 $additionalId = (CRM_Event_BAO_Participant::getAdditionalParticipantIds($participantId));
84 $participantLinks = (CRM_Event_BAO_Participant::getAdditionalParticipantUrl($additionalId));
85 }
86 }
87 $deletedParticipants = $additionalCount = 0;
88 foreach ($this->_participantIds as $participantId) {
89 if (CRM_Utils_Array::value('delete_participant', $params) == 1) {
90 $primaryParticipantId = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'registered_by_id', 'id');
91 if (CRM_Event_BAO_Participant::isPrimaryParticipant($participantId)) {
92 $additionalIds = (CRM_Event_BAO_Participant::getAdditionalParticipantIds($participantId));
93 $additionalCount += count($additionalIds);
94 foreach ($additionalIds as $value) {
95 CRM_Event_BAO_Participant::deleteParticipant($value);
96 }
97 CRM_Event_BAO_Participant::deleteParticipant($participantId);
98 $deletedParticipants++;
99 }
100 // delete participant only if it is not an additional participant
101 // or if it is additional and its primary participant is not selected in $this->_participantIds.
102 elseif (empty($primaryParticipantId) || (!in_array($primaryParticipantId, $this->_participantIds))) {
103 CRM_Event_BAO_Participant::deleteParticipant($participantId);
104 $deletedParticipants++;
105 }
106 }
107 else {
108 CRM_Event_BAO_Participant::deleteParticipant($participantId);
109 $deletedParticipants++;
110 }
111 }
112 if ($additionalCount) {
113 $deletedParticipants += $additionalCount;
114 }
115
116 $status = ts('%count participant deleted.', ['plural' => '%count participants deleted.', 'count' => $deletedParticipants]);
117
118 if ($participantLinks) {
119 $status .= '<p>' . ts('The following participants no longer have an event fee recorded. You can edit their registration and record a replacement contribution by clicking the links below:')
120 . '</p>' . $participantLinks;
121 }
122 CRM_Core_Session::setStatus($status, ts('Removed'), 'info');
123 }
124
125 }