|
| 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