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