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 contacts.
21 * This class provides functionality for the actual
22 * addition of contacts to groups.
24 class CRM_Contact_Form_Task_AddToGroup
extends CRM_Contact_Form_Task
{
27 * The context that we are working on
34 * The groupId retrieved from the GET vars
41 * The title of the group
48 * Build all the data structures needed to build the form.
50 public function preProcess() {
51 // initialize the task and row fields
54 $this->_context
= $this->get('context');
55 $this->_id
= $this->get('amtgID');
59 * Build the form object.
61 public function buildQuickForm() {
63 //create radio buttons to select existing group or add a new group
64 $options = [ts('Add Contact To Existing Group'), ts('Create New Group')];
67 $this->addRadio('group_option', ts('Group Options'), $options, ['onclick' => "return showElements();"]);
69 $this->add('text', 'title', ts('Group Name:') . ' ',
70 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title')
72 $this->addRule('title', ts('Name already exists in Database.'),
73 'objectExists', ['CRM_Contact_DAO_Group', $this->_id
, 'title']
76 $this->add('textarea', 'description', ts('Description:') . ' ',
77 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
80 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
81 if (!CRM_Core_Permission
::access('CiviMail')) {
82 $isWorkFlowEnabled = CRM_Mailing_Info
::workflowEnabled();
83 if ($isWorkFlowEnabled &&
84 !CRM_Core_Permission
::check('create mailings') &&
85 !CRM_Core_Permission
::check('schedule mailings') &&
86 !CRM_Core_Permission
::check('approve mailings')
88 unset($groupTypes['Mailing List']);
92 if (!empty($groupTypes)) {
93 $this->addCheckBox('group_type',
96 NULL, NULL, NULL, NULL, ' '
101 // add select for groups
102 $group = ['' => ts('- select group -')] + CRM_Core_PseudoConstant
::nestedGroup();
104 $groupElement = $this->add('select', 'group_id', ts('Select Group'), $group, FALSE, ['class' => 'crm-select2 huge']);
106 $this->_title
= $group[$this->_id
];
108 if ($this->_context
=== 'amtg') {
109 $groupElement->freeze();
111 // also set the group title
112 $groupValues = ['id' => $this->_id
, 'title' => $this->_title
];
113 $this->assign_by_ref('group', $groupValues);
116 // Set dynamic page title for 'Add Members Group (confirm)'
118 CRM_Utils_System
::setTitle(ts('Add Contacts: %1', [1 => $this->_title
]));
121 CRM_Utils_System
::setTitle(ts('Add Contacts to A Group'));
124 $this->addDefaultButtons(ts('Add to Group'));
128 * Set the default form values.
132 * the default array reference
134 public function setDefaultValues() {
137 if ($this->_context
=== 'amtg') {
138 $defaults['group_id'] = $this->_id
;
141 $defaults['group_option'] = 0;
146 * Add local and global form rules.
148 public function addRules() {
149 $this->addFormRule(['CRM_Contact_Form_Task_AddToGroup', 'formRule']);
153 * Global validation rules for the form.
155 * @param array $params
158 * list of errors to be posted back to the form
160 public static function formRule($params) {
163 if (!empty($params['group_option']) && empty($params['title'])) {
164 $errors['title'] = "Group Name is a required field";
166 elseif (empty($params['group_option']) && empty($params['group_id'])) {
167 $errors['group_id'] = "Select Group is a required field.";
170 return empty($errors) ?
TRUE : $errors;
174 * Process the form after the input has been submitted and validated.
176 public function postProcess() {
177 $params = $this->controller
->exportValues();
178 $groupOption = $params['group_option'] ??
NULL;
181 $groupParams['title'] = $params['title'];
182 $groupParams['description'] = $params['description'];
183 $groupParams['visibility'] = "User and User Admin Only";
184 if (array_key_exists('group_type', $params) && is_array($params['group_type'])) {
185 $groupParams['group_type'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
186 array_keys($params['group_type'])
187 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
190 $groupParams['group_type'] = '';
192 $groupParams['is_active'] = 1;
194 $createdGroup = CRM_Contact_BAO_Group
::create($groupParams);
195 $groupID = $createdGroup->id
;
196 $groupName = $groupParams['title'];
199 $groupID = $params['group_id'];
200 $group = CRM_Core_PseudoConstant
::group();
201 $groupName = $group[$groupID];
204 list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($this->_contactIds
, $groupID);
207 ts('%count contact added to group', [
209 'plural' => '%count contacts added to group',
213 $status[] = ts('%count contact was already in group', [
214 'count' => $notAdded,
215 'plural' => '%count contacts were already in group',
218 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
219 CRM_Core_Session
::setStatus($status, ts('Added Contact to %1', [
222 'plural' => 'Added Contacts to %1',
223 ]), 'success', ['expires' => 0]);
225 if ($this->_context
=== 'amtg') {
226 CRM_Core_Session
::singleton()
227 ->pushUserContext(CRM_Utils_System
::url('civicrm/group/search', "reset=1&force=1&context=smog&gid=$groupID"));