diff --git a/modules/access_affinitygroup/access_affinitygroup.install b/modules/access_affinitygroup/access_affinitygroup.install
index e857f2d7..da6eec35 100644
--- a/modules/access_affinitygroup/access_affinitygroup.install
+++ b/modules/access_affinitygroup/access_affinitygroup.install
@@ -25,7 +25,7 @@ function access_affinitygroup_update_10002() {
// Get 'state' taxonomy term IDs for recruiting states.
$recruiting_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'recruiting']);
$in_progress_recruiting_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'In Progress and Recruiting']);
-
+
$term_ids = [];
if ($recruiting_term) {
$term_ids[] = array_keys($recruiting_term)[0];
@@ -33,7 +33,7 @@ function access_affinitygroup_update_10002() {
if ($in_progress_recruiting_term) {
$term_ids[] = array_keys($in_progress_recruiting_term)[0];
}
-
+
if (!empty($term_ids)) {
// Get all mentorship nodes currently in recruiting states that are approved and published
$query = \Drupal::entityQuery('node')
@@ -43,15 +43,89 @@ function access_affinitygroup_update_10002() {
->condition('status', 1)
->accessCheck(FALSE);
$existing_recruiting_nids = $query->execute();
-
+
// Set these as already notified to prevent sending emails for existing mentorships
\Drupal::state()->set('access_affinitygroup.notified_mentorships', array_values($existing_recruiting_nids));
-
+
$count = count($existing_recruiting_nids);
\Drupal::logger('access_affinitygroup')->info('Update 10002: Marked @count existing recruiting mentorships (approved and published) as already notified to prevent duplicate emails.', ['@count' => $count]);
-
+
return "Marked $count existing recruiting mentorships (approved and published) as already notified.";
}
-
+
return 'No recruiting mentorships found to mark as notified.';
}
+
+/**
+ * Update user CC ids.
+ */
+function access_affinitygroup_update_10003() {
+ $connection = \Drupal::database();
+ $query = $connection->select('user__field_constant_contact_id', 'cci');
+ $query->fields('cci', ['entity_id', 'field_constant_contact_id_value']);
+ $result = $query->execute()->fetchAll();
+ foreach ($result as $row) {
+ $user_id = $row->entity_id;
+ $cc_id = $row->field_constant_contact_id_value;
+
+ $user_cc_id = [
+ 'test' => '',
+ 'openondemand' => '',
+ 'support' => $cc_id,
+ ];
+
+ $user_cc_id = json_encode($user_cc_id);
+
+ $query = \Drupal::database()->update('user__field_constant_contact_id')
+ ->fields([
+ 'field_constant_contact_id_value' => $user_cc_id,
+ ])
+ ->condition('entity_id', $user_id, '=')
+ ->execute();
+ }
+}
+
+/**
+ * Set forced token state.
+ */
+function access_affinitygroup_update_10004() {
+ \Drupal::state()->set('access_affinitygroup.forcedTokenSettings', NULL);
+}
+
+/**
+ * Set Refresh token state.
+ */
+function access_affinitygroup_update_10005() {
+ $current_token = \Drupal::state()->get('access_affinitygroup.refresh_token');
+ $new_token = [
+ "support" => $current_token,
+ "openondemand" => '',
+ "test" => '',
+ ];
+ $new_token = json_encode($new_token);
+ \Drupal::state()->set('access_affinitygroup.refresh_token', $new_token);
+}
+
+/**
+ * Set forced token state.
+ */
+function access_affinitygroup_update_10006() {
+ // Get config setting 'access_affinitygroup.settings'.
+ $ag_config = \Drupal::configFactory()->getEditable('access_affinitygroup.settings');
+
+ // Get access_token
+ $access_token = $ag_config->get('access_token');
+
+ $new_token = [
+ "support" => $access_token,
+ "openondemand" => '',
+ "test" => '',
+ ];
+ $new_token = json_encode($new_token);
+
+ \Drupal::state()->set('access_affinitygroup.access_token', $new_token);
+
+ // Remove config item access_token from above.
+ $ag_config->set('access_token', NULL)->save();
+}
+
diff --git a/modules/access_affinitygroup/access_affinitygroup.module b/modules/access_affinitygroup/access_affinitygroup.module
index b5806f79..bee8f1cd 100644
--- a/modules/access_affinitygroup/access_affinitygroup.module
+++ b/modules/access_affinitygroup/access_affinitygroup.module
@@ -240,7 +240,7 @@ function access_affinitygroup_entity_presave(EntityInterface $entity) {
// Get all existing CC lists.
$cca = new ConstantContactApi();
- $lists = $cca->apiCall('/contact_lists ');
+ $lists = $cca->apiCall('/contact_lists');
if (empty($lists)) {
return;
}
@@ -270,7 +270,7 @@ function access_affinitygroup_entity_presave(EntityInterface $entity) {
$post_data = json_encode($post_data);
// Create Constant contact list named with AG title.
- $created_list = $cca->apiCall('/contact_lists ', $post_data, 'POST');
+ $created_list = $cca->apiCall('/contact_lists', $post_data, 'POST');
if (!empty($created_list)) {
$list_id = $created_list->list_id ? Xss::filter($created_list->list_id) : '';
@@ -857,6 +857,8 @@ function makeListMembershipJSON($flagEntityId, $userDetails) {
$ccIdField = $userDetails->get('field_constant_contact_id')->getValue();
if (!empty($ccIdField)) {
$user_cc_id = $ccIdField[0]['value'];
+ $constantcontactapi = new ConstantContactApi();
+ $user_cc_id = $constantcontactapi->getUserCcId($user_cc_id);
}
if (empty($user_cc_id)) {
@@ -916,20 +918,50 @@ function access_affinitygroup_user_login(UserInterface $account) {
if (!empty($field_val)) {
$cca_user_id = $field_val[0]['value'];
}
+
// If user did not already have the CC id, try to add to CC.
- if (empty($cca_user_id)) {
+ if (empty($cca_user_id) || !json_validate($cca_user_id)) {
+ $user_cc_id = [
+ 'test' => '',
+ 'openondemand' => '',
+ 'support' => '',
+ ];
+
+ $constantcontactapi = new ConstantContactApi();
+ $env = $constantcontactapi->getEnvironment();
$firstName = $user_detail->get('field_user_first_name')->getString();
$lastName = $user_detail->get('field_user_last_name')->getString();
- $cca_user_id = addUserToConstantContact($current_user->getEmail(), $firstName, $lastName);
- if (empty($cca_user_id)) {
+
+ $user_cc_id[$env] = addUserToConstantContact($current_user->getEmail(), $firstName, $lastName);
+
+ if (empty($user_cc_id[$env])) {
showStatus("Could not add user to Constant Contact.");
} else {
- $user_detail->set('field_constant_contact_id', $cca_user_id);
+ $user_cc_id = json_encode($user_cc_id);
+ $user_detail->set('field_constant_contact_id', $user_cc_id);
$user_detail->save();
}
} else {
+ $constantcontactapi = new ConstantContactApi();
+ $env = $constantcontactapi->getEnvironment();
+ $user_cc_id = $user_detail->get('field_constant_contact_id')->getString();
+ $user_cc_id = json_decode($user_cc_id, TRUE);
+
+ if (empty($user_cc_id[$env])) {
+ $user_cc_id[$env] = addUserToConstantContact($current_user->getEmail(), $firstName, $lastName);
+ // Flatten if Array for $user_cc_id[$env]
+ $user_cc_id[$env] = is_array($user_cc_id[$env]) ? implode(',', $user_cc_id[$env]) : $user_cc_id[$env];
+ if (empty($user_cc_id[$env])) {
+ showStatus("Could not add user to Constant Contact.");
+ } else {
+ $user_cc_id = json_encode($user_cc_id);
+ $user_detail->set('field_constant_contact_id', $user_cc_id);
+ $user_detail->save();
+ }
+ }
+
// This else just for debugging in early stages
// showStatus("Login and NOT attempting add of new constant contact id.");.
}
@@ -1107,23 +1139,19 @@ function access_affinitygroup_cron() {
try {
- // Make sure we are on the support domain, This is crucial for
- // the constant contact connection.
- $host = \Drupal::request()->getHttpHost();
- if ($host !== 'support.access-ci.org') {
- \Drupal::logger('cron_affinitygroup')->debug("On $host: blocked Constant Contact cron");
- return;
- }
// 1. Refresh the Constant Contact Token
if (shouldRun('token')) {
$currentTime = \Drupal::time()->getCurrentTime();
\Drupal::state()->set('access_affinitygroup.crontime-t', $currentTime);
- \Drupal::logger('access_affinitygroup')->notice('Cron: token refresh ' . date("Y-m-d H:i:s", $currentTime) . " on $host");
+ \Drupal::logger('access_affinitygroup')->notice('Cron: token refresh ' . date("Y-m-d H:i:s", $currentTime));
- $cca = new ConstantContactApi();
- $cca->newToken();
+ $support = new ConstantContactApi('support');
+ $support->newToken();
+
+ $ood = new ConstantContactApi('openondemand');
+ $ood->newToken();
}
// 2. call xdusage api to get users and create/update their
diff --git a/modules/access_affinitygroup/src/Commands/AffinityGroupCommands.php b/modules/access_affinitygroup/src/Commands/AffinityGroupCommands.php
index c1ddf58e..3c8b0520 100644
--- a/modules/access_affinitygroup/src/Commands/AffinityGroupCommands.php
+++ b/modules/access_affinitygroup/src/Commands/AffinityGroupCommands.php
@@ -72,9 +72,13 @@ public function initConstantContact() {
$this->output()->writeln($last_name);
// Get the Constant Contact id for the User.
+ $env = $cca->getEnvironment();
+
$field_val = $user->get('field_constant_contact_id')->getValue();
if (!empty($field_val) && $field_val != 0) {
$cc_id = $field_val[0]['value'];
+ $cc_id = json_decode($cc_id, TRUE);
+ $cc_id = $cc_id[$env];
$this->output()->writeln($first_name . ' ' . $last_name . ' already has cc id: ' . $cc_id);
}
else {
@@ -92,7 +96,16 @@ public function initConstantContact() {
usleep(500);
}
$this->output()->writeln($cc_id);
- $user->set('field_constant_contact_id', $cc_id);
+
+ $user_cc_id = [
+ 'test' => '',
+ 'openondemand' => '',
+ 'support' => '',
+ ];
+ $user_cc_id[$env] = $cc_id;
+ $user_cc_id = json_encode($user_cc_id);
+
+ $user->set('field_constant_contact_id', $user_cc_id);
$user->save();
$this->output()->writeln('Added ' . $first_name . ' ' . $last_name);
}
@@ -126,6 +139,8 @@ public function showAffinityGroups(string $agName = '', $options = ['uidonly' =>
$uidOnly = $options['uidonly'];
$headOnly = $options['headonly'];
+ $cca = new ConstantContactApi();
+
// Get all the Affinity Groups.
$agCount = 0;
$nids = \Drupal::entityQuery('node')
@@ -203,7 +218,18 @@ public function showAffinityGroups(string $agName = '', $options = ['uidonly' =>
// Get the Constant Contact id for the User.
$field_val = $user->get('field_constant_contact_id')->getValue();
if (!empty($field_val) && $field_val != 0) {
+ // Get the Constant Contact id for the User.
+ $env = $cca->getEnvironment();
+
$cc_id = $field_val[0]['value'];
+ $cc_id = json_decode($cc_id, TRUE);
+ if (isset($cc_id[$env])) {
+ $cc_id = $cc_id[$env];
+ }
+ else {
+ $cc_id = 'NO cc id for ' . $env;
+ }
+
}
$this->output()->writeln('cc id: ' . $cc_id);
}
diff --git a/modules/access_affinitygroup/src/Form/ConstantContact.php b/modules/access_affinitygroup/src/Form/ConstantContact.php
index a9856818..6602d827 100644
--- a/modules/access_affinitygroup/src/Form/ConstantContact.php
+++ b/modules/access_affinitygroup/src/Form/ConstantContact.php
@@ -32,17 +32,21 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$noConstantContactCalls = \Drupal::configFactory()->getEditable('access_affinitygroup.settings')->get('noConstantContactCalls');
+ $forcedTokenSettings = \Drupal::state()->get('access_affinitygroup.forcedTokenSettings');
+ $supportToken = $forcedTokenSettings == 'support' ? 1 : 0;
+ $openondemandToken = $forcedTokenSettings == 'openondemand' ? 1 : 0;
+ $testToken = $forcedTokenSettings == 'test' ? 1 : 0;
+
$request = \Drupal::request();
$code = $request->get('code');
$refresh_token = $request->get('refresh_token');
+ $cca = new ConstantContactApi();
if ($refresh_token) {
- $cca = new ConstantContactApi();
$cca->newToken();
}
if ($code) {
- $cca = new ConstantContactApi();
$cca->initializeToken($code);
}
@@ -73,9 +77,17 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#description' => $this->t('Select Constant Contact permissions.'),
];
+ $environment = $cca->getEnvironment();
+
+ $form['clear'] = [
+ '#type' => 'submit',
+ '#value' => $this->t('Clear tokens for') . ' ' . $environment,
+ '#submit' => [[$this, 'clearTokens']],
+ ];
+
$form['submit'] = [
'#type' => 'submit',
- '#value' => $this->t('Authorize App'),
+ '#value' => $this->t('Authorize App on') . ' ' . $environment,
'#submit' => [[$this, 'submitForm']],
];
@@ -98,6 +110,40 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#value' => $this->t('Save Disable Setting'),
'#submit' => [[$this, 'doSaveDisableCC']],
];
+
+ $form['force_token_title'] = [
+ '#markup' => '