-
Notifications
You must be signed in to change notification settings - Fork 62
/
CivicrmContactAddToGroup.php
207 lines (189 loc) · 5.51 KB
/
CivicrmContactAddToGroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace Drupal\civicrm_entity\Plugin\Action;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsPreconfigurationInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\civicrm_entity\CiviCrmApi;
if (!class_exists('Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase')) {
return;
}
/**
* Action to add CiviCRM Contact to a CiviCRM group.
*
* @Action(
* id = "civicrm_contact_add_to_group",
* label = @Translation("Add Contact to Group"),
* type = "civicrm_contact",
* confirm = TRUE,
* )
*/
class CivicrmContactAddToGroup extends ViewsBulkOperationsActionBase implements ViewsBulkOperationsPreconfigurationInterface, PluginFormInterface, ContainerFactoryPluginInterface {
/**
* The CiviCRM API service.
*
* @var \Drupal\civicrm_entity\CiviCrmApi
*/
protected $civicrmApi;
/**
* CivicrmContactAddToGroup constructor.
*
* @param array $configuration
* The configuration.
* @param string $plugin_id
* The plugin id.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\civicrm_entity\CiviCrmApi $civicrm_entity_api
* The CiviCRM API service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CiviCrmApi $civicrm_entity_api) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->civicrmApi = $civicrm_entity_api;
}
/**
* Create method.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container.
* @param array $configuration
* The configuration.
* @param string $plugin_id
* The plugin id.
* @param mixed $plugin_definition
* The plugin definition.
*
* @return static
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('civicrm_entity.api')
);
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if (!empty($this->configuration['selected_group']) && !empty($entity)) {
// So do we need to check if a contact is in the group already?
try {
$this->civicrmApi->save('GroupContact', [
'group_id' => $this->configuration['selected_group'],
'contact_id' => $entity->id(),
]);
$this->messenger()->addMessage('Added: ' . $entity->label() . ' to group: ' . $this->fetchGroupTitle($this->configuration['selected_group']));
}
catch (\Exception $e) {
}
}
}
/**
* {@inheritdoc}
*/
public function buildPreConfigurationForm(array $form, array $values, FormStateInterface $form_state): array {
$groups = $this->fetchGroups();
$form['allowed_groups'] = [
'#title' => $this->t('Allowed Groups'),
'#type' => 'select',
'#multiple' => TRUE,
'#options' => $groups,
'#default_value' => $values['allowed_groups'] ?? [],
];
return $form;
}
/**
* Configuration form builder.
*
* If this method has implementation, the action is
* considered to be configurable.
*
* @param array $form
* Form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*
* @return array
* The configuration form.
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$groups = [];
if (!empty($this->context['preconfiguration']['allowed_groups'])) {
$groups = $this->fetchGroups($this->context['preconfiguration']['allowed_groups']);
}
$form['selected_group'] = [
'#title' => t('Group'),
'#type' => 'select',
'#options' => $groups,
'#default_value' => $form_state->getValue('selected_group'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $object->access('update', $account, $return_as_object);
}
/**
* Fetch array of group titles, keyed by id.
*
* @param array $ids
* Array of ids.
*
* @return array
* The array of group titles.
*/
private function fetchGroups(array $ids = []) {
$groups = [];
try {
$params = [
'sequential' => FALSE,
'return' => ["title"],
'options' => ['limit' => 0],
];
if (!empty($ids)) {
$params['id'] = ['IN' => $ids];
}
$api_groups = $this->civicrmApi->get('group', $params);
if (!empty($api_groups)) {
foreach ($api_groups as $gid => $group) {
$groups[$gid] = $group['title'];
}
}
}
catch (\Exception $e) {
}
return $groups;
}
/**
* Return group title given group id.
*
* @param int $group_id
* The group id.
*
* @return string
* The title.
*/
private function fetchGroupTitle($group_id) {
try {
if (!empty($group_id) && is_numeric($group_id)) {
$value = $this->civicrmApi->getSingle('Group', [
'return' => ["title"],
'id' => $group_id,
]);
if (!empty($value['title'])) {
return $value['title'];
}
}
}
catch (\Exception $e) {
}
return '';
}
}