Skip to content

Commit 9a854ef

Browse files
authored
Merge pull request #175 from docusign/webforms-example
Added second web forms example
2 parents 3a65f57 + fe45bbe commit 9a854ef

9 files changed

Lines changed: 1322 additions & 732 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"docusign/esign-client": "^8.2.0",
2323
"docusign/rooms-client": "^2.1.1",
2424
"docusign/monitor-client": "^1.2.1",
25-
"docusign/webforms-client": "^2.0.0",
25+
"docusign/webforms-client": "^2.1.0",
2626
"twig/twig": "^3.21.1",
2727
"league/oauth2-client": "^2.7.0",
2828
"ext-json": "*",

composer.lock

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

public/demo_documents/web-form-config.json

Lines changed: 671 additions & 633 deletions
Large diffs are not rendered by default.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
namespace DocuSign\Controllers\Examples\WebForms;
4+
5+
use DocuSign\Controllers\WebFormsApiBaseController;
6+
use DocuSign\Services\Examples\WebForms\CreateRemoteInstanceService;
7+
use DocuSign\WebForms\Client\ApiException;
8+
use DocuSign\Services\ManifestService;
9+
10+
class EG002CreateRemoteInstance extends WebFormsApiBaseController
11+
{
12+
const EG = 'web002'; # reference (and URL) for this example
13+
const FILE = __FILE__;
14+
const EMBED = 'webforms/embed';
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'] ?? null,
37+
'base_path' => $_SESSION['ds_base_path'] ?? null,
38+
'ds_access_token' => $_SESSION['ds_access_token'] ?? null
39+
];
40+
}
41+
42+
/**
43+
* Main controller logic
44+
*
45+
* @return void
46+
* @throws ApiException
47+
* @throws \DocuSign\eSign\Client\ApiException
48+
*/
49+
protected function createController(): void
50+
{
51+
$this->checkDsToken();
52+
$accountId = $this->args['account_id'] ?? null;
53+
54+
if (empty($_SESSION['is_initial_entry'])) {
55+
$templatesApi = $this->eSignatureClientService->getTemplatesApi();
56+
57+
$templatesByName = CreateRemoteInstanceService::getTemplatesByName(
58+
$templatesApi,
59+
self::WEB_FORM_EXAMPLE_TEMPLATE,
60+
$accountId
61+
);
62+
if (empty($templatesByName) || count($templatesByName) == 0) {
63+
$createTemplate = CreateRemoteInstanceService::createTemplate(
64+
$this->args,
65+
self::WEB_FORM_EXAMPLE_TEMPLATE,
66+
$this::DEMO_DOCS_PATH,
67+
$this->eSignatureClientService
68+
);
69+
$_SESSION['web_forms_template_id'] = $createTemplate['template_id'];
70+
} else {
71+
$_SESSION['web_forms_template_id'] = $templatesByName[0]['template_id'];
72+
}
73+
74+
CreateRemoteInstanceService::addTemplateIdToForm(
75+
$this::DEMO_DOCS_PATH . 'web-form-config.json',
76+
$_SESSION['web_forms_template_id']
77+
);
78+
79+
$this->askToCreateWebForm();
80+
return;
81+
}
82+
83+
$_SESSION['is_initial_entry'] = null;
84+
85+
$formList = CreateRemoteInstanceService::getFormsByName(
86+
$this->clientService->formManagementApi(),
87+
$accountId,
88+
self::WEB_FORM_EXAMPLE_TEMPLATE
89+
);
90+
91+
if (empty($formList->getItems()) || count($formList->getItems()) == 0) {
92+
$GLOBALS['twig']->display(
93+
'error.html',
94+
[
95+
'error_code' => '400',
96+
'error_message' => $this->codeExampleText['CustomErrorTexts'][0]['ErrorMessage'],
97+
'common_texts' => ManifestService::getCommonTexts()
98+
]
99+
);
100+
return;
101+
}
102+
103+
$formId = $formList->getItems()[0]->getId();
104+
105+
$webFormInstance = CreateRemoteInstanceService::createRemoteInstance(
106+
$this->clientService->formInstanceManagementApi(),
107+
$accountId,
108+
$formId,
109+
$GLOBALS['DS_CONFIG']['signer_name'],
110+
$GLOBALS['DS_CONFIG']['signer_email']
111+
);
112+
113+
if ($webFormInstance) {
114+
$this->clientService->showDoneTemplateFromManifest(
115+
$this->codeExampleText,
116+
null,
117+
ManifestService::replacePlaceholders(
118+
'{0}',
119+
$webFormInstance['envelopes'][0]['id'],
120+
ManifestService::replacePlaceholders('{1}', $webFormInstance['id'], $this->codeExampleText['ResultsPageText'])
121+
)
122+
);
123+
}
124+
}
125+
126+
/**
127+
* @return void
128+
*/
129+
public function askToCreateWebForm(): void
130+
{
131+
$_SESSION['is_initial_entry'] = true;
132+
133+
$GLOBALS['twig']->display(
134+
self::EMBED . '.html',
135+
[
136+
'common_texts' => $this->getCommonText(),
137+
'code_example_text' => $this->codeExampleText,
138+
'description' => ManifestService::replacePlaceholders(
139+
'{0}',
140+
'public/demo_documents',
141+
$this->codeExampleText['AdditionalPage'][0]['ResultsPageText']
142+
),
143+
]
144+
);
145+
}
146+
}

0 commit comments

Comments
 (0)