Skip to content

Commit 469daee

Browse files
authored
Merge pull request #137 from docusign/feature-webforms-example
Added Web Forms code example
2 parents a7ef160 + 6592fcb commit 469daee

File tree

18 files changed

+1674
-110
lines changed

18 files changed

+1674
-110
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"docusign/esign-client": "^6.15.0-rc",
2121
"docusign/rooms-client": "^2.1.0",
2222
"docusign/monitor-client": "^1.1.0",
23+
"docusign/webforms-client": "^v1.0.1-rc10",
2324
"twig/twig": "^3.5.1",
2425
"league/oauth2-client": "^2.6.1",
2526
"ext-json": "*",

composer.lock

Lines changed: 184 additions & 109 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/assets/search.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ let DS_SEARCH = (function () {
55
CLICK: 'click',
66
ROOMS: 'rooms',
77
ADMIN: 'admin',
8-
CONNECT: 'connect'
8+
CONNECT: 'connect',
9+
WEBFORMS: 'webforms'
910
};
1011

1112
let processJSONData = function () {
@@ -125,6 +126,8 @@ let DS_SEARCH = (function () {
125126
return "eg";
126127
case API_TYPES.CONNECT:
127128
return "con";
129+
case API_TYPES.WEBFORMS:
130+
return "web";
128131
}
129132
}
130133

44.7 KB
Binary file not shown.

public/demo_documents/web-form-config.json

Lines changed: 622 additions & 0 deletions
Large diffs are not rendered by default.

src/Controllers/Auth/DocuSign.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public function getDefaultScopes(): array
121121
. " account_read domain_read identity_provider_read user_data_redact asset_group_account_read"
122122
. " asset_group_account_clone_write asset_group_account_clone_read"
123123
];
124+
} elseif ($_SESSION['api_type'] == ApiTypes::WEBFORMS) {
125+
return [
126+
"signature webforms_read webforms_instance_read webforms_instance_write"
127+
];
124128
} else {
125129
return [
126130
"signature"
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace DocuSign\Controllers\Examples\WebForms;
4+
5+
use DocuSign\Controllers\WebFormsApiBaseController;
6+
use DocuSign\Services\Examples\WebForms\CreateAndEmbedFormService;
7+
use DocuSign\WebForms\Client\ApiException;
8+
9+
class EG001CreateAndEmbedForm extends WebFormsApiBaseController
10+
{
11+
const EG = 'web001'; # reference (and URL) for this example
12+
const FILE = __FILE__;
13+
const EMBED = 'webforms/embed';
14+
const WEB_FORM = 'webforms/webForm';
15+
const WEB_FORM_EXAMPLE_TEMPLATE = "Web Form Example Template";
16+
17+
/**
18+
* Create a new controller instance.
19+
*
20+
* @return void
21+
*/
22+
public function __construct()
23+
{
24+
parent::__construct();
25+
parent::controller();
26+
}
27+
28+
/**
29+
* Get specific template arguments
30+
*
31+
* @return array
32+
*/
33+
public function getTemplateArgs(): array
34+
{
35+
return [
36+
'account_id' => $_SESSION['ds_account_id'],
37+
'base_path' => $_SESSION['ds_base_path'],
38+
'ds_access_token' => $_SESSION['ds_access_token']
39+
];
40+
}
41+
42+
/**
43+
* 1. Check the token
44+
* 2. Call the worker method
45+
* 3. Return create room data
46+
*
47+
* @return void
48+
* @throws ApiException
49+
* @throws \DocuSign\eSign\Client\ApiException
50+
*/
51+
protected function createController(): void
52+
{
53+
$this->checkDsToken();
54+
$accountId = $this->args["account_id"];
55+
56+
if ($_SESSION['can_embed_form'] == null) {
57+
$templatesApi = $this->eSignatureClientService->getTemplatesApi();
58+
59+
$templatesByName = CreateAndEmbedFormService::getTemplatesByName(
60+
$templatesApi,
61+
self::WEB_FORM_EXAMPLE_TEMPLATE,
62+
$accountId
63+
);
64+
if ($templatesByName == null || count($templatesByName) == 0) {
65+
$createTemplate = CreateAndEmbedFormService::createTemplate(
66+
$this->args,
67+
self::WEB_FORM_EXAMPLE_TEMPLATE,
68+
$this::DEMO_DOCS_PATH,
69+
$this->eSignatureClientService
70+
);
71+
$_SESSION['web_forms_template_id'] = $createTemplate["template_id"];
72+
} else {
73+
$_SESSION['web_forms_template_id'] = $templatesByName[0]["template_id"];
74+
}
75+
76+
CreateAndEmbedFormService::addTemplateIdToForm(
77+
$this::DEMO_DOCS_PATH . "web-form-config.json",
78+
$_SESSION['web_forms_template_id']
79+
);
80+
81+
$this->askToCreateWebForm();
82+
}
83+
84+
$_SESSION['can_embed_form'] = null;
85+
86+
$formList = CreateAndEmbedFormService::getForms(
87+
$this->clientService->FormManagementApi(),
88+
$accountId
89+
);
90+
91+
if ($formList->getItems() == null || count($formList->getItems()) == 0) {
92+
$this->askToCreateWebForm();
93+
}
94+
95+
$formId = $formList->getItems()[0]->getId();
96+
97+
$webFormInstance = CreateAndEmbedFormService::createInstance(
98+
$this->clientService->FormInstanceManagementApi(),
99+
$accountId,
100+
$formId
101+
);
102+
103+
$GLOBALS['twig']->display(
104+
self::WEB_FORM . '.html',
105+
[
106+
'instance_token ' => $webFormInstance["instance_token"],
107+
'url' => $webFormInstance["form_url"],
108+
'integration_key' => $GLOBALS['DS_CONFIG']['ds_client_id'],
109+
'common_texts' => $this->getCommonText(),
110+
]
111+
);
112+
exit();
113+
}
114+
115+
/**
116+
* @return void
117+
*/
118+
public function askToCreateWebForm(): void
119+
{
120+
$_SESSION['can_embed_form'] = true;
121+
122+
$GLOBALS['twig']->display(
123+
self::EMBED . '.html',
124+
[
125+
'common_texts' => $this->getCommonText(),
126+
'code_example_text' => $this->codeExampleText,
127+
'description' => $this->codeExampleText["AdditionalPage"][1]["ResultsPageText"],
128+
]
129+
);
130+
131+
exit();
132+
}
133+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace DocuSign\Controllers;
4+
5+
use DocuSign\Services\ApiTypes;
6+
use DocuSign\Services\ManifestService;
7+
use DocuSign\Services\RouterService;
8+
use DocuSign\Services\SignatureClientService;
9+
use DocuSign\Services\WebFormsApiClientService;
10+
11+
abstract class WebFormsApiBaseController extends BaseController
12+
{
13+
private const MINIMUM_BUFFER_MIN = 3;
14+
protected WebFormsApiClientService $clientService;
15+
protected SignatureClientService $eSignatureClientService;
16+
protected RouterService $routerService;
17+
protected array $args;
18+
19+
public function __construct()
20+
{
21+
$this->args = $this->getTemplateArgs();
22+
$this->clientService = new WebFormsApiClientService($this->args);
23+
$this->eSignatureClientService = new SignatureClientService($this->args);
24+
25+
$this->routerService = new RouterService();
26+
$this->codeExampleText = $this->getPageText(static::EG);
27+
28+
if (defined("static::EG")) {
29+
$this->checkDsToken();
30+
}
31+
}
32+
33+
abstract protected function getTemplateArgs(): array;
34+
35+
/**
36+
* Check ds
37+
*/
38+
protected function checkDsToken(): void
39+
{
40+
$currentAPI = ManifestService::getAPIByLink(static::EG);
41+
42+
if (!$this->routerService->dsTokenOk(self::MINIMUM_BUFFER_MIN)
43+
|| $currentAPI !== $_SESSION['api_type']
44+
) {
45+
$_SESSION['prefered_api_type'] = ApiTypes::WEBFORMS;
46+
$this->clientService->needToReAuth(static::EG);
47+
}
48+
}
49+
50+
/**
51+
* Base controller
52+
*
53+
* @param $args array|null
54+
* @return void
55+
*/
56+
public function controller(array $args = null): void
57+
{
58+
$this->codeExampleText = $this->getPageText(static::EG);
59+
60+
if ($this->isMethodGet()) {
61+
$this->getController($this->routerService, basename(static::FILE), $args);
62+
}
63+
if ($this->isMethodPost()) {
64+
$this->routerService->checkCsrf();
65+
$this->createController();
66+
}
67+
}
68+
69+
/**
70+
* Show the example's form page
71+
*
72+
* @param $routerService RouterService
73+
* @param $basename string|null
74+
* @param $args array|null
75+
* @return void
76+
*/
77+
private function getController(
78+
RouterService $routerService,
79+
?string $basename,
80+
?array $args
81+
): void {
82+
if ($this->isHomePage(static::EG)) {
83+
$GLOBALS['twig']->display(
84+
static::EG . '.html',
85+
[
86+
'title' => $this->homePageTitle(static::EG),
87+
'show_doc' => false,
88+
'common_texts' => $this->getCommonText()
89+
]
90+
);
91+
} else {
92+
$currentAPI = ManifestService::getAPIByLink(static::EG);
93+
94+
if ($routerService->dsTokenOk() && $currentAPI === $_SESSION['api_type']) {
95+
$GLOBALS['twig']->display($routerService->getTemplate(static::EG), [
96+
'title' => $routerService->getTitle(static::EG),
97+
'source_file' => $basename,
98+
'source_url' => $GLOBALS['DS_CONFIG']['github_example_url']
99+
. "/WebForm/" . $basename,
100+
'documentation' => $GLOBALS['DS_CONFIG']['documentation']
101+
. static::EG,
102+
'show_doc' => $GLOBALS['DS_CONFIG']['documentation'],
103+
'args' => $args,
104+
'code_example_text' => $this->codeExampleText,
105+
'common_texts' => $this->getCommonText()
106+
]);
107+
} else {
108+
# Save the current operation so it will be resumed after authentication
109+
$_SESSION['prefered_api_type'] = ApiTypes::WEBFORMS;
110+
$_SESSION['eg'] = $GLOBALS['app_url'] . 'index.php?page=' . static::EG;
111+
header(
112+
'Location: ' . $GLOBALS['app_url']
113+
. 'index.php?page=' . static::LOGIN_REDIRECT
114+
);
115+
exit;
116+
}
117+
}
118+
}
119+
120+
/**
121+
* Declaration for the base controller creator.
122+
* Each creator should be described in specific Controller
123+
*/
124+
abstract protected function createController(): void;
125+
126+
/**
127+
* @return array
128+
*/
129+
protected function getDefaultTemplateArgs(): array
130+
{
131+
return [
132+
'account_id' => $_SESSION['ds_account_id'],
133+
'base_path' => $_SESSION['ds_base_path'],
134+
'ds_access_token' => $_SESSION['ds_access_token']
135+
];
136+
}
137+
}

src/Services/ApiTypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ abstract class ApiTypes
1010
const ROOMS = 'Rooms';
1111
const ESIGNATURE = 'eSignature';
1212
const CONNECT = 'Connect';
13+
const WEBFORMS = 'WebForms';
1314
}

0 commit comments

Comments
 (0)