3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to group
20 * contacts. This class provides functionality for the actual
21 * addition of contacts to groups.
23 class CRM_Event_Form_Task_AddToGroup
extends CRM_Event_Form_Task
{
26 * The context that we are working on.
33 * The groupId retrieved from the GET vars.
40 * The title of the group.
47 * Build all the data structures needed to build the form.
51 public function preProcess() {
52 // initialize the task and row fields
55 parent
::setContactIDs();
56 $this->_context
= $this->get('context');
57 $this->_id
= $this->get('amtgID');
61 * Build the form object.
66 public function buildQuickForm() {
68 //create radio buttons to select existing group or add a new group
69 $options = [ts('Add Contact To Existing Group'), ts('Create New Group')];
72 $this->addRadio('group_option', ts('Group Options'), $options, ['onclick' => "return showElements();"]);
74 $this->add('text', 'title', ts('Group Name:') . ' ',
75 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title')
77 $this->addRule('title', ts('Name already exists in Database.'),
78 'objectExists', ['CRM_Contact_DAO_Group', $this->_id
, 'title']
81 $this->add('textarea', 'description', ts('Description:') . ' ',
82 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
85 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
86 if (!CRM_Core_Permission
::access('CiviMail')) {
87 $isWorkFlowEnabled = CRM_Mailing_Info
::workflowEnabled();
88 if ($isWorkFlowEnabled &&
89 !CRM_Core_Permission
::check('create mailings') &&
90 !CRM_Core_Permission
::check('schedule mailings') &&
91 !CRM_Core_Permission
::check('approve mailings')
93 unset($groupTypes['Mailing List']);
97 if (!empty($groupTypes)) {
98 $this->addCheckBox('group_type',
101 NULL, NULL, NULL, NULL, ' '
106 // add select for groups
107 $group = ['' => ts('- select group -')] + CRM_Core_PseudoConstant
::group();
109 $groupElement = $this->add('select', 'group_id', ts('Select Group'), $group);
111 $this->_title
= $group[$this->_id
];
113 if ($this->_context
=== 'amtg') {
114 $groupElement->freeze();
116 // also set the group title
117 $groupValues = ['id' => $this->_id
, 'title' => $this->_title
];
118 $this->assign_by_ref('group', $groupValues);
121 // Set dynamic page title for 'Add Members Group (confirm)'
123 CRM_Utils_System
::setTitle(ts('Add Contacts: %1', [1 => $this->_title
]));
126 CRM_Utils_System
::setTitle(ts('Add Contacts to A Group'));
129 $this->addDefaultButtons(ts('Add to Group'));
133 * Set the default form values.
137 * the default array reference
139 public function setDefaultValues() {
142 if ($this->_context
=== 'amtg') {
143 $defaults['group_id'] = $this->_id
;
146 $defaults['group_option'] = 0;
151 * Add local and global form rules.
156 public function addRules() {
157 $this->addFormRule(['CRM_Event_Form_Task_AddToGroup', 'formRule']);
161 * Global validation rules for the form.
163 * @param array $params
164 * Posted values of the form.
167 * list of errors to be posted back to the form
169 public static function formRule($params) {
172 if (!empty($params['group_option']) && empty($params['title'])) {
173 $errors['title'] = "Group Name is a required field";
175 elseif (empty($params['group_option']) && empty($params['group_id'])) {
176 $errors['group_id'] = "Select Group is a required field.";
179 return empty($errors) ?
TRUE : $errors;
183 * Process the form after the input has been submitted and validated.
188 public function postProcess() {
189 $params = $this->controller
->exportValues();
190 $groupOption = $params['group_option'] ??
NULL;
193 $groupParams['title'] = $params['title'];
194 $groupParams['description'] = $params['description'];
195 $groupParams['visibility'] = "User and User Admin Only";
196 if (array_key_exists('group_type', $params) && is_array($params['group_type'])) {
197 $groupParams['group_type'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
198 array_keys($params['group_type'])
199 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
202 $groupParams['group_type'] = '';
204 $groupParams['is_active'] = 1;
206 $createdGroup = CRM_Contact_BAO_Group
::create($groupParams);
207 $groupID = $createdGroup->id
;
208 $groupName = $groupParams['title'];
211 $groupID = $params['group_id'];
212 $group = CRM_Core_PseudoConstant
::group();
213 $groupName = $group[$groupID];
216 list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($this->_contactIds
, $groupID);
219 ts('%count contact added to group', [
221 'plural' => '%count contacts added to group',
225 $status[] = ts('%count contact was already in group', [
226 'count' => $notAdded,
227 'plural' => '%count contacts were already in group',
230 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
231 CRM_Core_Session
::setStatus($status, ts('Added Contact to %1', [
234 'plural' => 'Added Contacts to %1',
235 ]), 'success', ['expires' => 0]);