Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / CRM / Contact / Form / GroupContact.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 */
33
34/**
5a409b50 35 * This class generates form components for groupContact.
6a488035
TO
36 */
37class CRM_Contact_Form_GroupContact extends CRM_Core_Form {
38
39 /**
40 * The groupContact id, used when editing the groupContact
41 *
42 * @var int
43 */
44 protected $_groupContactId;
45
46 /**
47 * The contact id, used when add/edit groupContact
48 *
49 * @var int
50 */
51 protected $_contactId;
52
f30a8dfb
TM
53 /**
54 * Explicitly declare the entity api name.
55 */
56 public function getDefaultEntity() {
57 return 'GroupContact';
58 }
59
60 /**
61 * Explicitly declare the form context.
62 */
63 public function getDefaultContext() {
64 return 'create';
65 }
66
5a409b50 67 /**
68 * Pre process form.
69 */
00be9182 70 public function preProcess() {
6a488035
TO
71 $this->_contactId = $this->get('contactId');
72 $this->_groupContactId = $this->get('groupContactId');
edc80cda 73 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035
TO
74 }
75
6a488035 76 /**
fe482240 77 * Build the form object.
6a488035
TO
78 */
79 public function buildQuickForm() {
80 // get the list of all the groups
81 if ($this->_context == 'user') {
82 $onlyPublicGroups = CRM_Utils_Request::retrieve('onlyPublicGroups', 'Boolean', $this, FALSE);
ec6d2df4
AS
83 $ids = CRM_Core_PseudoConstant::allGroup();
84 $heirGroups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids);
85
be2fb01f 86 $allGroups = [];
ec6d2df4
AS
87 foreach ($heirGroups as $id => $group) {
88 // make sure that this group has public visibility
89 if ($onlyPublicGroups && $group['visibility'] == 'User and User Admin Only') {
90 continue;
91 }
92 $allGroups[$id] = $group;
93 }
6a488035
TO
94 }
95 else {
96 $allGroups = CRM_Core_PseudoConstant::group();
97 }
98
99 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
f828fa2c 100 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($allGroups, NULL, '&nbsp;&nbsp;', TRUE);
6a488035
TO
101
102 // get the list of groups contact is currently in ("Added") or unsubscribed ("Removed").
103 $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($this->_contactId);
104
105 // Remove current groups from drowdown options ($groupSelect)
106 if (is_array($currentGroups)) {
107 // Compare array keys, since the array values (group title) in $groupList may have extra spaces for indenting child groups
108 $groupSelect = array_diff_key($groupHierarchy, $currentGroups);
109 }
110 else {
111 $groupSelect = $groupHierarchy;
112 }
113
be2fb01f 114 $groupSelect = ['' => ts('- select group -')] + $groupSelect;
6a488035
TO
115
116 if (count($groupSelect) > 1) {
117 $session = CRM_Core_Session::singleton();
118 // user dashboard
119 if (strstr($session->readUserContext(), 'user')) {
120 $msg = ts('Join a Group');
121 }
122 else {
123 $msg = ts('Add to a group');
124 }
125
be2fb01f 126 $this->addField('group_id', ['class' => 'crm-action-menu fa-plus', 'placeholder' => $msg, 'options' => $groupSelect]);
6a488035 127
be2fb01f 128 $this->addButtons([
69078420
SL
129 [
130 'type' => 'next',
131 'name' => ts('Add'),
132 'isDefault' => TRUE,
133 ],
134 ]);
6a488035
TO
135 }
136 }
137
138 /**
5a409b50 139 * Post process form.
6a488035
TO
140 */
141 public function postProcess() {
be2fb01f 142 $contactID = [$this->_contactId];
353ffa53
TO
143 $groupId = $this->controller->exportValue('GroupContact', 'group_id');
144 $method = ($this->_context == 'user') ? 'Web' : 'Admin';
6a488035
TO
145
146 $session = CRM_Core_Session::singleton();
147 $userID = $session->get('userID');
148
149 if ($userID == $this->_contactId) {
150 $method = 'Web';
151 }
152 $groupContact = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactID, $groupId, $method);
153
154 if ($groupContact && $this->_context != 'user') {
155 $groups = CRM_Core_PseudoConstant::group();
be2fb01f 156 CRM_Core_Session::setStatus(ts("Contact has been added to '%1'.", [1 => $groups[$groupId]]), ts('Added to Group'), 'success');
6a488035
TO
157 }
158 }
96025800 159
6a488035 160}