Skip to content

Commit 490035e

Browse files
authored
Merge pull request #171 from docusign/set-connected-fields-example
Set connected fields code example
2 parents c8a2430 + 7a9a23e commit 490035e

File tree

10 files changed

+569
-0
lines changed

10 files changed

+569
-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: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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($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+
$args,
48+
basename(static::FILE)
49+
);
50+
}
51+
52+
if ($this->isMethodPost()) {
53+
$this->createController();
54+
}
55+
}
56+
57+
/**
58+
* Show the example's form page
59+
*
60+
* @param $eg
61+
* @param $basename string|null
62+
* @param $brand_languages array|null
63+
* @param $brands array|null
64+
* @param $permission_profiles array|null
65+
* @param $groups array|null
66+
* @return void
67+
*/
68+
protected function getController(
69+
$args,
70+
?string $basename
71+
): void {
72+
if ($this->isHomePage(static::EG)) {
73+
$GLOBALS['twig']->display(static::EG . '.html', [
74+
'title' => $this->homePageTitle(static::EG),
75+
'show_doc' => false,
76+
'common_texts' => $this->getCommonText()
77+
]);
78+
} else {
79+
$currentAPI = ManifestService::getAPIByLink(static::EG);
80+
if ($this->routerService->dsTokenOk() && $currentAPI === $_SESSION['api_type']) {
81+
$displayOptions = [
82+
'title' => $this->routerService->getTitle(static::EG),
83+
'source_file' => $basename,
84+
'args' => $args,
85+
'source_url' => $GLOBALS['DS_CONFIG']['github_example_url'] . "/ConnectedFields/". $basename,
86+
'documentation' => $GLOBALS['DS_CONFIG']['documentation'] . static::EG,
87+
'show_doc' => $GLOBALS['DS_CONFIG']['documentation'],
88+
'signer_name' => $GLOBALS['DS_CONFIG']['signer_name'],
89+
'signer_email' => $GLOBALS['DS_CONFIG']['signer_email'],
90+
'code_example_text' => $this->codeExampleText,
91+
'common_texts' => $this->getCommonText()
92+
];
93+
94+
$GLOBALS['twig']->display($this->routerService->getTemplate(static::EG), $displayOptions);
95+
} else {
96+
$_SESSION['prefered_api_type'] = ApiTypes::CONNECTEDFIELDS;
97+
$this->saveCurrentUrlToSession(static::EG);
98+
header('Location: ' . $GLOBALS['app_url'] . 'index.php?page=' . static::LOGIN_REDIRECT);
99+
exit;
100+
}
101+
}
102+
}
103+
104+
/**
105+
* Declaration for the base controller creator. Each creator should be described in specific Controller
106+
*/
107+
abstract protected function createController(): void;
108+
109+
/**
110+
* Check email input value using regular expression
111+
* @param $email
112+
* @return string
113+
*/
114+
protected function checkEmailInputValue($email): string
115+
{
116+
return preg_replace('/([^\w +\-\@\.\,])+/', '', $email);
117+
}
118+
119+
/**
120+
* Check input values using regular expressions
121+
* @param $value
122+
* @return string
123+
*/
124+
protected function checkInputValues($value): string
125+
{
126+
return preg_replace('/([^\w \-\@\.\,])+/', '', $value);
127+
}
128+
129+
/**
130+
* Check ds
131+
*/
132+
protected function checkDsToken(): void
133+
{
134+
$currentAPI = ManifestService::getAPIByLink(static::EG);
135+
136+
if (!$this->routerService->dsTokenOk(self::MINIMUM_BUFFER_MIN) || $currentAPI !== $_SESSION['api_type']) {
137+
$_SESSION['prefered_api_type'] = ApiTypes::CONNECTEDFIELDS;
138+
$this->clientService->needToReAuth(static::EG);
139+
}
140+
}
141+
}
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(['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)