Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Activenet registration #1297

Open
wants to merge 17 commits into
base: 8.x-1.x
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions modules/custom/activenet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ print_r($centers); //returns json response of all centers
All available methods:
```
getSites()
getCenters()
getActivities()
getActivityTypes()
getActivityCategories()
getActivityOtherCategories()
getFlexRegPrograms()
getFlexRegProgramTypes()
getMembershipPackages()
getMembershipCategories()
getActivityDetail($id)
```
68 changes: 54 additions & 14 deletions modules/custom/activenet/src/ActivenetClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,37 @@
*
* @package Drupal\activenet
*
* @method mixed getBranches(array $args)
* @method mixed getSessions(array $args)
* @method mixed getPrograms(array $args)
* @method mixed getChildCarePrograms(array $args)
* @method mixed getMembershipTypes(array $args)
* @method mixed getCenters(array $args)
* @method mixed getSites(array $args)
* @method mixed getActivities(array $args)
* @method mixed getActivityTypes(array $args)
* @method mixed getActivityOtherCategories(array $args)
* @method mixed getFlexRegPrograms(array $args)
* @method mixed getFlexRegProgramTypes(array $args)
* @method mixed getMembershipPackages(array $args)
* @method mixed getMembershipCategories(array $args)
* @method mixed getActivityDetail(integer $id)
*/
class ActivenetClient extends Client implements ActivenetClientInterface {

/**
* Settings
*
* @var array of settings from config
*/
protected $api_settings;

/**
* ActivenetClient constructor
* @param array $api_settings
* The api config settings.
*/

public function setApi(array $api_settings) {
$this->api_settings = $api_settings;
}



/**
* Wrapper for 'request' method.
Expand All @@ -33,7 +57,6 @@ class ActivenetClient extends Client implements ActivenetClientInterface {
* @throws \Drupal\activenet\ActivenetClientException
*/
private function makeRequest($method, $uri, array $parameters = []) {

try {
$response = $this->request($method, $uri, $parameters);

Expand Down Expand Up @@ -78,23 +101,20 @@ private function makeRequest($method, $uri, array $parameters = []) {
* @throws ActivenetClientException.
*/
public function __call($method, $args) {
$settings = \Drupal::config('activenet.settings');
$api_key = $settings->get('api_key');
$base_uri = $settings->get('base_uri');
if(!$this->api_settings) throw new ActivenetClientException(sprintf('Please inject api settings using "$this->setAPI($api_settings)".'));

$api_key = $this->api_settings['api_key'];
$base_uri = $this->api_settings['base_uri'];
// Prepare suffix for the endpoint.
$suffix = '';

if (empty($args)) {
$args[] = [
'api_key' => $api_key
];
$args[]['api_key'] = $api_key;
}
else {
$args = ['api_key', $api_key];
$args[0]['api_key'] = $api_key;
}
$suffix = '?' . http_build_query($args[0], '', '&');

switch ($method) {
case 'makeRequest':
throw new ActivenetClientException(sprintf('Please, extend Activenet client!', $method));
Expand All @@ -108,8 +128,20 @@ public function __call($method, $args) {
case 'getActivities':
return $this->makeRequest('get', $base_uri . 'activities' . $suffix);

case 'getActivityTypes':
return $this->makeRequest('get', $base_uri . 'activitytypes' . $suffix);

case 'getActivityCategories':
return $this->makeRequest('get', $base_uri . 'activitycategories' . $suffix);

case 'getActivityOtherCategories':
return $this->makeRequest('get', $base_uri . 'activityothercategories' . $suffix);

case 'getFlexRegPrograms':
return $this->makeRequest('get', $base_uri . 'flexregprograms' . $suffix);

case 'getFlexRegProgramTypes':
return $this->makeRequest('get', $base_uri . 'flexregprogramtypes' . $suffix);

case 'getMembershipPackages':
return $this->makeRequest('get', $base_uri . 'membershippackages' . $suffix);
Expand All @@ -121,4 +153,12 @@ public function __call($method, $args) {
throw new ActivenetClientException(sprintf('Method %s not implemented yet.', $method));
}

public function getActivityDetail(integer $id){
if(!$this->api_settings) throw new ActivenetClientException(sprintf('Please inject api settings using "$this->setAPI($api_settings)".'));

$base_uri = $this->api_settings['base_uri'];
$suffix = '?api_key=' . $this->api_settings['api_key'];
return $this->makeRequest('get', $base_uri . 'activities/' . $id . $suffix);
}

}
13 changes: 11 additions & 2 deletions modules/custom/activenet/src/ActivenetClientFactory.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ public function get() {
$settings = $this->configFactory->get('activenet.settings');
$config = [
'base_uri' => $settings->get('base_uri'),
'headers' => ['Accept' => 'application/json'],
'headers' => [
'Accept' => 'application/json',
'page_info' => '{"total_records_per_page":200}',
],
];
return new ActivenetClient($config);
$api_config = [
'base_uri' => $settings->get('base_uri'),
'api_key' => $settings->get('api_key'),
];
$client = new ActivenetClient($config);
$client->setApi($api_config);
return $client;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: ActiveNet Registration
type: module
descripton: Provide a entity for creating Active Community registrations
core: 8.x
package: OpenY
dependencies:
- activenet:activenet
- openy_socrates:openy_socrates
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @file
* Contains activenet_registration.module.
*/

use Drupal\Core\Routing\RouteMatchInterface;

/**
* Implements hook_help().
*/
function activenet_registration_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.activenet_registration':
$output = '<h3>' . t('Active Net Registration for communities') . '</h3>';
$output .= '<p>' . t('Instructions about selections go here.') . '</p>';
}
}


/**
* Implements hook_theme().
*/
function activenet_registration_theme($existing, $type, $theme, $path) {
return [
'activenet_activity_registration' => [
'variables' => [
'name' => NULL,
'registration_url' => NULL,
'gender' => NULL,
'site_name' => NULL,
'date_start' => NULL,
'date_end' => NULL,
'date_pattern' => NULL,
],
],
'activenet_flex_registration' =>[
'variables' => [
'name' => NULL,
'registration_url' => NULL,
'gender' => NULL,
'site_name' => NULL,
],
],
'activenet_site' =>[
'variables' => [
'site_id' => NULL,
'site_name' => NULL,
],
],
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
administer activenet registrations:
title: 'Administer ActiveNet registrations'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
activenet_registration.settings:
path: '/admin/config/services/activenet/registration'
defaults:
_form: '\Drupal\activenet_registration\Form\SettingsForm'
_title: 'ActiveNet Communities Registration settings'
requirements:
_permission: 'administer activenet registration'
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
activenet_registration.cache:
class: Drupal\Core\Cache\CacheBackendInterface
factory: cache_factory:get
arguments: [activenet_registration]
activenet_registration.datastorage:
class: Drupal\activenet_registration\DataStorage
arguments: ['@activenet.client', '@activenet_registration.cache', '@config.factory']
tags:
-
name: openy_cron_service
periodicity: 60
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
base_uri: ''
143 changes: 143 additions & 0 deletions modules/custom/activenet_registration/src/DataStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace Drupal\activenet_registration;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Url;
use Drupal\activenet\ActivenetClient;
use Drupal\openy_socrates\OpenyCronServiceInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\SetCookie;

/**
* Class DataStorage.
*/
class DataStorage implements DataStorageInterface, OpenyCronServiceInterface {

/**
* Activenet Client
*
* @var \Drupal\activenet\ActivenetClient
*/
protected $client;

/**
* Cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* {@inheritdoc}
*/
public function runCronServices() {
$this->resetCache();
$this->warmCache();
\Drupal::logger('ActiveNet Registration')->info("Cron service run succeeded. New API call made.");
}

/**
* DataStorage constructor.
*
* @param \Drupal\activenet\ActivenetClient $client
* Daxko client.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* Cache backend.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
*/
public function __construct(ActivenetClient $client, CacheBackendInterface $cache, ConfigFactoryInterface $config_factory) {
$this->client = $client;
$this->cache = $cache;
$this->configFactory = $config_factory;
}

/**
* Delete all caches.
*/
public function resetCache() {
$this->cache->deleteAll();
}

/**
* Warm up all cache.
*
* @ingroup cache
*/
public function warmCache() {
$this->getSites();
$this->getProgramTypes();
$this->getActivityTypes();
$this->getCategories();
$this->getOtherCategories();
}

public function getSites() {
$cid = __METHOD__;
if ($cache = $this->cache->get($cid)) {
return $cache->data;
}

$sites = $this->client->getSites();
$this->cache->set($cid, $sites);

return $sites;
}

public function getProgramTypes() {
$cid = __METHOD__;
if ($cache = $this->cache->get($cid)) {
return $cache->data;
}

$programs = $this->client->getFlexRegProgramTypes();
$this->cache->set($cid, $programs);

return $programs;
}

public function getActivityTypes() {
$cid = __METHOD__;
if ($cache = $this->cache->get($cid)) {
return $cache->data;
}

$sites = $this->client->getActivityTypes();
$this->cache->set($cid, $sites);

return $sites;
}

public function getCategories() {
$cid = __METHOD__;
if ($cache = $this->cache->get($cid)) {
return $cache->data;
}

$sites = $this->client->getActivityCategories();
$this->cache->set($cid, $sites);

return $sites;
}

public function getOtherCategories() {
$cid = __METHOD__;
if ($cache = $this->cache->get($cid)) {
return $cache->data;
}

$sites = $this->client->getActivityOtherCategories();
$this->cache->set($cid, $sites);

return $sites;
}

}
10 changes: 10 additions & 0 deletions modules/custom/activenet_registration/src/DataStorageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Drupal\activenet_registration;

/**
* Interface DataStorageInterface.
*/
interface DataStorageInterface {

}
Loading