Convert remaining Authorize.net test to use guzzle
[civicrm-core.git] / CRM / Contact / Form / GroupContact.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 generates form components for groupContact.
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
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
51 /**
52 * Pre process form.
53 */
54 public function preProcess() {
55 $this->_contactId = $this->get('contactId');
56 $this->_groupContactId = $this->get('groupContactId');
57 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
58 }
59
60 /**
61 * Build the form object.
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);
67 $ids = CRM_Core_PseudoConstant::allGroup();
68 $heirGroups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids);
69
70 $allGroups = [];
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 }
76 $allGroups[$id] = $group;
77 }
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)
84 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($allGroups, NULL, '&nbsp;&nbsp;', TRUE);
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
98 $groupSelect = ['' => ts('- select group -')] + $groupSelect;
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
110 $this->addField('group_id', ['class' => 'crm-action-menu fa-plus', 'placeholder' => $msg, 'options' => $groupSelect]);
111
112 $this->addButtons([
113 [
114 'type' => 'next',
115 'name' => ts('Add'),
116 'isDefault' => TRUE,
117 ],
118 ]);
119 }
120 }
121
122 /**
123 * Post process form.
124 */
125 public function postProcess() {
126 $contactID = [$this->_contactId];
127 $groupId = $this->controller->exportValue('GroupContact', 'group_id');
128 $method = ($this->_context == 'user') ? 'Web' : 'Admin';
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();
140 CRM_Core_Session::setStatus(ts("Contact has been added to '%1'.", [1 => $groups[$groupId]]), ts('Added to Group'), 'success');
141 }
142 }
143
144 }