Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 80 additions & 6 deletions modules/access_affinitygroup/access_affinitygroup.install
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ 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];
}
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')
Expand All @@ -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();
}

60 changes: 44 additions & 16 deletions modules/access_affinitygroup/access_affinitygroup.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) : '';
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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.");.
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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);
}
Expand Down
Loading