Skip to content

Commit 9225e19

Browse files
committed
Set connected fields code example
1 parent c8a2430 commit 9225e19

File tree

10 files changed

+554
-0
lines changed

10 files changed

+554
-0
lines changed

public/assets/search.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ let DS_SEARCH = (function () {
88
CONNECT: 'connect',
99
WEBFORMS: 'webforms',
1010
NOTARY: 'notary',
11+
MAESTRO: 'maestro',
12+
CONNECTEDFIELDS: 'connectedfields',
1113
};
1214

1315
let processJSONData = function () {
@@ -131,6 +133,10 @@ let DS_SEARCH = (function () {
131133
return "web";
132134
case API_TYPES.NOTARY:
133135
return "n";
136+
case API_TYPES.CONNECTEDFIELDS:
137+
return "cf";
138+
case API_TYPES.MAESTRO:
139+
return "mae";
134140
}
135141
}
136142

src/Controllers/Auth/DocuSign.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ public function getDefaultScopes(): array
130130
return [
131131
"signature"
132132
];
133+
} elseif ($_SESSION['api_type'] == ApiTypes::CONNECTEDFIELDS) {
134+
return [
135+
"signature adm_store_unified_repo_read"
136+
];
133137
} else {
134138
return [
135139
"signature"
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace DocuSign\Controllers;
4+
5+
use DocuSign\Services\SignatureClientService;
6+
use DocuSign\Services\RouterService;
7+
use DocuSign\Services\IRouterService;
8+
use DocuSign\Services\ApiTypes;
9+
use DocuSign\Services\ManifestService;
10+
11+
abstract class ConnectedFieldsApiBaseController extends BaseController
12+
{
13+
private const MINIMUM_BUFFER_MIN = 3;
14+
protected SignatureClientService $clientService;
15+
protected IRouterService $routerService;
16+
protected array $args;
17+
18+
public function __construct()
19+
{
20+
$this->args = $this->getTemplateArgs();
21+
$this->clientService = new SignatureClientService($this->args);
22+
$this->routerService = new RouterService();
23+
if (defined("static::EG")) {
24+
$this->checkDsToken();
25+
}
26+
}
27+
28+
abstract protected function getTemplateArgs(): array;
29+
30+
/**
31+
* Base controller
32+
*
33+
* @param null $eg
34+
* @return void
35+
*/
36+
public function controller($eg = null, $args = null): void
37+
{
38+
if (empty($eg)) {
39+
$eg = static::EG;
40+
$this->codeExampleText = $this->getPageText(static::EG);
41+
}
42+
43+
$this->codeExampleText = $this->getPageText(static::EG);
44+
45+
if ($this->isMethodGet()) {
46+
$this->getController(
47+
$eg,
48+
$args,
49+
basename(static::FILE)
50+
);
51+
}
52+
53+
if ($this->isMethodPost()) {
54+
$this->createController();
55+
}
56+
}
57+
58+
/**
59+
* Show the example's form page
60+
*
61+
* @param $eg
62+
* @param $basename string|null
63+
* @param $brand_languages array|null
64+
* @param $brands array|null
65+
* @param $permission_profiles array|null
66+
* @param $groups array|null
67+
* @return void
68+
*/
69+
protected function getController(
70+
$eg,
71+
$args = null,
72+
?string $basename
73+
): void {
74+
if ($this->isHomePage(static::EG)) {
75+
$GLOBALS['twig']->display(static::EG . '.html', [
76+
'title' => $this->homePageTitle(static::EG),
77+
'show_doc' => false,
78+
'common_texts' => $this->getCommonText()
79+
]);
80+
} else {
81+
$currentAPI = ManifestService::getAPIByLink(static::EG);
82+
if ($this->routerService->dsTokenOk() && $currentAPI === $_SESSION['api_type']) {
83+
$displayOptions = [
84+
'title' => $this->routerService->getTitle(static::EG),
85+
'source_file' => $basename,
86+
'args' => $args,
87+
'source_url' => $GLOBALS['DS_CONFIG']['github_example_url'] . "/ConnectedFields/". $basename,
88+
'documentation' => $GLOBALS['DS_CONFIG']['documentation'] . static::EG,
89+
'show_doc' => $GLOBALS['DS_CONFIG']['documentation'],
90+
'signer_name' => $GLOBALS['DS_CONFIG']['signer_name'],
91+
'signer_email' => $GLOBALS['DS_CONFIG']['signer_email'],
92+
'code_example_text' => $this->codeExampleText,
93+
'common_texts' => $this->getCommonText()
94+
];
95+
96+
$GLOBALS['twig']->display($this->routerService->getTemplate(static::EG), $displayOptions);
97+
} else {
98+
$_SESSION['prefered_api_type'] = ApiTypes::CONNECTEDFIELDS;
99+
$this->saveCurrentUrlToSession(static::EG);
100+
header('Location: ' . $GLOBALS['app_url'] . 'index.php?page=' . static::LOGIN_REDIRECT);
101+
exit;
102+
}
103+
}
104+
}
105+
106+
/**
107+
* Declaration for the base controller creator. Each creator should be described in specific Controller
108+
*/
109+
abstract protected function createController(): void;
110+
111+
/**
112+
* Check email input value using regular expression
113+
* @param $email
114+
* @return string
115+
*/
116+
protected function checkEmailInputValue($email): string
117+
{
118+
return preg_replace('/([^\w +\-\@\.\,])+/', '', $email);
119+
}
120+
121+
/**
122+
* Check input values using regular expressions
123+
* @param $value
124+
* @return string
125+
*/
126+
protected function checkInputValues($value): string
127+
{
128+
return preg_replace('/([^\w \-\@\.\,])+/', '', $value);
129+
}
130+
131+
/**
132+
* Check ds
133+
*/
134+
protected function checkDsToken(): void
135+
{
136+
$currentAPI = ManifestService::getAPIByLink(static::EG);
137+
138+
if (!$this->routerService->dsTokenOk(self::MINIMUM_BUFFER_MIN) || $currentAPI !== $_SESSION['api_type']) {
139+
$_SESSION['prefered_api_type'] = ApiTypes::CONNECTEDFIELDS;
140+
$this->clientService->needToReAuth(static::EG);
141+
}
142+
}
143+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace DocuSign\Controllers\Examples\ConnectedFields;
4+
5+
use DocuSign\eSign\Client\ApiException;
6+
use DocuSign\Controllers\ConnectedFieldsApiBaseController;
7+
use DocuSign\Services\Examples\ConnectedFields\SetConnectedFieldsService;
8+
use DocuSign\Services\ManifestService;
9+
10+
class Cf001SetConnectedFields extends ConnectedFieldsApiBaseController
11+
{
12+
const EG = 'cf001'; # reference (and url) for this example
13+
14+
const FILE = __FILE__;
15+
16+
private string $orgId;
17+
18+
/**
19+
* Create a new controller instance.
20+
* @return void
21+
* @throws ApiException
22+
*/
23+
public function __construct()
24+
{
25+
parent::__construct();
26+
27+
$this->codeExampleText = $this->getPageText(static::EG);
28+
$this->checkDsToken();
29+
30+
$accessToken = $_SESSION['ds_access_token'];
31+
$accountId = $_SESSION['ds_account_id'];
32+
33+
$filteredAppsJson = $this->getFilteredAppsJson($accountId, $accessToken);
34+
35+
if ($filteredAppsJson === null || empty($filteredAppsJson)) {
36+
$this->clientService->showDoneTemplate(
37+
$this->codeExampleText["ExampleName"],
38+
$this->codeExampleText["ExampleName"],
39+
$this->codeExampleText["AdditionalPage"][0]["ResultsPageText"]
40+
);
41+
} else {
42+
$_SESSION["apps"] = $filteredAppsJson;
43+
parent::controller(null, ['apps' => $filteredAppsJson]);
44+
}
45+
}
46+
47+
private function getFilteredAppsJson(string $accountId, string $accessToken): ?array
48+
{
49+
$apps = SetConnectedFieldsService::getConnectedFieldsTabGroups($accountId, $accessToken);
50+
$filteredApps = SetConnectedFieldsService::filterData($apps);
51+
return json_decode($filteredApps, true);
52+
}
53+
54+
/**
55+
* Check the access token and call the worker method
56+
* @return void
57+
* @throws ApiException for API problems.
58+
*/
59+
public function createController(): void
60+
{
61+
$this->checkDsToken();
62+
63+
$args = $this->getTemplateArgs();
64+
65+
$accessToken = $_SESSION['ds_access_token'];
66+
$accountId = $_SESSION['ds_account_id'];
67+
$pdfDoc = $GLOBALS['DS_CONFIG']['doc_pdf'];
68+
$selectedAppId = $args['selected_app_id'];
69+
$basePath = self::DEMO_DOCS_PATH;
70+
$app = $this->getAppById($selectedAppId);
71+
72+
$envelopeId = SetConnectedFieldsService::sendEnvelope(
73+
$_SESSION['ds_base_path'],
74+
$accessToken,
75+
$accountId,
76+
$pdfDoc,
77+
$basePath,
78+
$app,
79+
$args['signer_name'],
80+
$args['signer_email']
81+
);
82+
83+
if ($envelopeId) {
84+
$this->clientService->showDoneTemplateFromManifest(
85+
$this->codeExampleText,
86+
null,
87+
ManifestService::replacePlaceholders(
88+
"{0}",
89+
$envelopeId,
90+
$this->codeExampleText["ResultsPageText"]
91+
)
92+
);
93+
}
94+
}
95+
96+
private function getAppById(string $selectedAppId): array
97+
{
98+
return array_values(array_filter($_SESSION["apps"], function ($item) use ($selectedAppId) {
99+
return isset($item['appId']) && $item['appId'] === $selectedAppId;
100+
}))[0];
101+
}
102+
103+
/**
104+
* Get specific template arguments
105+
* @return array
106+
*/
107+
public function getTemplateArgs(): array
108+
{
109+
return [
110+
'signer_email' => $this->checkInputValues($_POST['signer_email']),
111+
'signer_name' => $this->checkInputValues($_POST['signer_name']),
112+
'selected_app_id' => $_POST['appId'],
113+
];
114+
}
115+
}

src/Services/ApiTypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ abstract class ApiTypes
1313
const MAESTRO = 'Maestro';
1414
const WEBFORMS = 'WebForms';
1515
const NOTARY = 'Notary';
16+
const CONNECTEDFIELDS = 'ConnectedFields';
1617
}

0 commit comments

Comments
 (0)