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