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
21 * This class provides the functionality to group
22 * contacts. This class provides functionality for the actual
23 * addition of contacts to groups.
25 class CRM_Event_Form_Task_AddToGroup
extends CRM_Event_Form_Task
{
28 * The context that we are working on.
35 * The groupId retrieved from the GET vars.
42 * The title of the group.
49 * Build all the data structures needed to build the form.
53 public function preProcess() {
54 // initialize the task and row fields
57 parent
::setContactIDs();
58 $this->_context
= $this->get('context');
59 $this->_id
= $this->get('amtgID');
63 * Build the form object.
68 public function buildQuickForm() {
70 //create radio buttons to select existing group or add a new group
71 $options = [ts('Add Contact To Existing Group'), ts('Create New Group')];
74 $this->addRadio('group_option', ts('Group Options'), $options, ['onclick' => "return showElements();"]);
76 $this->add('text', 'title', ts('Group Name:') . ' ',
77 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title')
79 $this->addRule('title', ts('Name already exists in Database.'),
80 'objectExists', ['CRM_Contact_DAO_Group', $this->_id
, 'title']
83 $this->add('textarea', 'description', ts('Description:') . ' ',
84 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
87 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
88 if (!CRM_Core_Permission
::access('CiviMail')) {
89 $isWorkFlowEnabled = CRM_Mailing_Info
::workflowEnabled();
90 if ($isWorkFlowEnabled &&
91 !CRM_Core_Permission
::check('create mailings') &&
92 !CRM_Core_Permission
::check('schedule mailings') &&
93 !CRM_Core_Permission
::check('approve mailings')
95 unset($groupTypes['Mailing List']);
99 if (!empty($groupTypes)) {
100 $this->addCheckBox('group_type',
103 NULL, NULL, NULL, NULL, ' '
108 // add select for groups
109 $group = ['' => ts('- select group -')] + CRM_Core_PseudoConstant
::group();
111 $groupElement = $this->add('select', 'group_id', ts('Select Group'), $group);
113 $this->_title
= $group[$this->_id
];
115 if ($this->_context
=== 'amtg') {
116 $groupElement->freeze();
118 // also set the group title
119 $groupValues = ['id' => $this->_id
, 'title' => $this->_title
];
120 $this->assign_by_ref('group', $groupValues);
123 // Set dynamic page title for 'Add Members Group (confirm)'
125 CRM_Utils_System
::setTitle(ts('Add Contacts: %1', [1 => $this->_title
]));
128 CRM_Utils_System
::setTitle(ts('Add Contacts to A Group'));
131 $this->addDefaultButtons(ts('Add to Group'));
135 * Set the default form values.
139 * the default array reference
141 public function setDefaultValues() {
144 if ($this->_context
=== 'amtg') {
145 $defaults['group_id'] = $this->_id
;
148 $defaults['group_option'] = 0;
153 * Add local and global form rules.
158 public function addRules() {
159 $this->addFormRule(['CRM_Event_Form_Task_AddToGroup', 'formRule']);
163 * Global validation rules for the form.
165 * @param array $params
166 * Posted values of the form.
169 * list of errors to be posted back to the form
171 public static function formRule($params) {
174 if (!empty($params['group_option']) && empty($params['title'])) {
175 $errors['title'] = "Group Name is a required field";
177 elseif (empty($params['group_option']) && empty($params['group_id'])) {
178 $errors['group_id'] = "Select Group is a required field.";
181 return empty($errors) ?
TRUE : $errors;
185 * Process the form after the input has been submitted and validated.
190 public function postProcess() {
191 $params = $this->controller
->exportValues();
192 $groupOption = CRM_Utils_Array
::value('group_option', $params, NULL);
195 $groupParams['title'] = $params['title'];
196 $groupParams['description'] = $params['description'];
197 $groupParams['visibility'] = "User and User Admin Only";
198 if (array_key_exists('group_type', $params) && is_array($params['group_type'])) {
199 $groupParams['group_type'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
200 array_keys($params['group_type'])
201 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
204 $groupParams['group_type'] = '';
206 $groupParams['is_active'] = 1;
208 $createdGroup = CRM_Contact_BAO_Group
::create($groupParams);
209 $groupID = $createdGroup->id
;
210 $groupName = $groupParams['title'];
213 $groupID = $params['group_id'];
214 $group = CRM_Core_PseudoConstant
::group();
215 $groupName = $group[$groupID];
218 list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($this->_contactIds
, $groupID);
221 ts('%count contact added to group', [
223 'plural' => '%count contacts added to group',
227 $status[] = ts('%count contact was already in group', [
228 'count' => $notAdded,
229 'plural' => '%count contacts were already in group',
232 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
233 CRM_Core_Session
::setStatus($status, ts('Added Contact to %1', [
236 'plural' => 'Added Contacts to %1',
237 ]), 'success', ['expires' => 0]);