|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DocuSign\Controllers\Examples\Maestro; |
| 4 | + |
| 5 | +use DocuSign\Controllers\MaestroApiBaseController; |
| 6 | +use DocuSign\Maestro\Client\ApiException; |
| 7 | +use DocuSign\Services\Examples\Maestro\TriggerMaestroWorkflowService; |
| 8 | +use DocuSign\Services\ManifestService; |
| 9 | +use DocuSign\Maestro\Model\TriggerWorkflowViaPostResponse; |
| 10 | + |
| 11 | +class Eg001TriggerWorkflow extends MaestroApiBaseController |
| 12 | +{ |
| 13 | + const EG = 'mae001'; # reference (and url) for this example |
| 14 | + |
| 15 | + const FILE = __FILE__; |
| 16 | + |
| 17 | + /** |
| 18 | + * Create a new controller instance |
| 19 | + * |
| 20 | + * @return void |
| 21 | + * @throws ApiException |
| 22 | + */ |
| 23 | + public function __construct() |
| 24 | + { |
| 25 | + parent::__construct(); |
| 26 | + $this->checkDsToken(); |
| 27 | + $this->codeExampleText = $this->getPageText(static::EG); |
| 28 | + $workflowName = 'Example workflow - send invite to signer'; |
| 29 | + $accountId = $this->args['account_id']; |
| 30 | + $workflowManagementApi = $this->clientService->workflowManagementApi(); |
| 31 | + |
| 32 | + try { |
| 33 | + $workflowDefinitions = TriggerMaestroWorkflowService::getWorkflowDefinitions( |
| 34 | + $workflowManagementApi, |
| 35 | + $accountId |
| 36 | + ); |
| 37 | + |
| 38 | + $this->selectNewestWorkflowByName($workflowDefinitions, $workflowName); |
| 39 | + } catch (ApiException $e) { |
| 40 | + if ($e->getCode() == 403) { |
| 41 | + $this->contactSupportToEnableFeature($e); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if ($_COOKIE["template_id"] != null && $_SESSION["workflow_id"] === null) { |
| 46 | + try { |
| 47 | + $createdWorkflowDefinition = TriggerMaestroWorkflowService::createWorkflow( |
| 48 | + $workflowManagementApi, |
| 49 | + $accountId, |
| 50 | + $_COOKIE["template_id"] |
| 51 | + ); |
| 52 | + |
| 53 | + $_SESSION["workflow_id"] = $createdWorkflowDefinition->getWorkflowDefinitionId(); |
| 54 | + |
| 55 | + $publishWorkflowUrl = TriggerMaestroWorkflowService::publishWorkflow( |
| 56 | + $workflowManagementApi, |
| 57 | + $accountId, |
| 58 | + $_SESSION["workflow_id"] |
| 59 | + ); |
| 60 | + |
| 61 | + $_SESSION["workflow_published"] = true; |
| 62 | + $this->openPublishWorkflowPage($publishWorkflowUrl); |
| 63 | + } catch (ApiException $e) { |
| 64 | + if ($e->getCode() == 403) { |
| 65 | + $this->contactSupportToEnableFeature($e); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if ($_SESSION['workflow_published']) { |
| 71 | + $publishWorkflowUrl = TriggerMaestroWorkflowService::publishWorkflow( |
| 72 | + $workflowManagementApi, |
| 73 | + $accountId, |
| 74 | + $_SESSION["workflow_id"] |
| 75 | + ); |
| 76 | + |
| 77 | + if ($publishWorkflowUrl == null) { |
| 78 | + $_SESSION["workflow_published"] = false; |
| 79 | + } else { |
| 80 | + $this->openPublishWorkflowPage($publishWorkflowUrl); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + parent::controller(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Check the access token and call the worker method |
| 89 | + * @return void |
| 90 | + * @throws ApiException |
| 91 | + */ |
| 92 | + public function createController(): void |
| 93 | + { |
| 94 | + $this->getTemplateArgs(); |
| 95 | + |
| 96 | + $workflowId = $_SESSION["workflow_id"]; |
| 97 | + $workflowManagementApi = $this->clientService->workflowManagementApi(); |
| 98 | + |
| 99 | + $trigger = new TriggerWorkflowViaPostResponse(); |
| 100 | + try { |
| 101 | + $workflowDefinition = TriggerMaestroWorkflowService::getWorkflowDefinition( |
| 102 | + $workflowManagementApi, |
| 103 | + $this->args['account_id'], |
| 104 | + $workflowId |
| 105 | + ); |
| 106 | + |
| 107 | + $triggerUrl = $workflowDefinition->getTriggerUrl(); |
| 108 | + |
| 109 | + $queryParams = parse_url($triggerUrl, PHP_URL_QUERY); |
| 110 | + parse_str($queryParams, $params); |
| 111 | + |
| 112 | + $mtid = $params['mtid']; |
| 113 | + $mtsec = $params['mtsec']; |
| 114 | + |
| 115 | + $triggerApi = $this->clientService->workflowTriggerApi(); |
| 116 | + |
| 117 | + $trigger = TriggerMaestroWorkflowService::triggerWorkflow( |
| 118 | + $triggerApi, |
| 119 | + $this->args['account_id'], |
| 120 | + $this->args['envelope_args']['instance_name'], |
| 121 | + $this->args['envelope_args']['signer_name'], |
| 122 | + $this->args['envelope_args']['signer_email'], |
| 123 | + $this->args['envelope_args']['cc_name'], |
| 124 | + $this->args['envelope_args']['cc_email'], |
| 125 | + $mtid, |
| 126 | + $mtsec |
| 127 | + ); |
| 128 | + $_SESSION['instance_id'] = $trigger->getInstanceId(); |
| 129 | + } catch (ApiException $e) { |
| 130 | + if ($e->getCode() == 403) { |
| 131 | + $this->contactSupportToEnableFeature($e); |
| 132 | + } |
| 133 | + } |
| 134 | + $this->clientService->showDoneTemplateFromManifest( |
| 135 | + $this->codeExampleText, |
| 136 | + json_encode($trigger->__toString()) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get specific template arguments |
| 142 | + * @return array |
| 143 | + */ |
| 144 | + public function getTemplateArgs(): array |
| 145 | + { |
| 146 | + $envelope_args = [ |
| 147 | + 'instance_name' => $_POST['instance_name'], |
| 148 | + 'signer_email' => $_POST['signer_email'], |
| 149 | + 'signer_name' => $_POST['signer_name'], |
| 150 | + 'cc_email' => $_POST['cc_email'], |
| 151 | + 'cc_name' => $_POST['cc_name'], |
| 152 | + ]; |
| 153 | + return [ |
| 154 | + 'account_id' => $_SESSION['ds_account_id'], |
| 155 | + 'base_path' => $_SESSION['ds_base_path'], |
| 156 | + 'ds_access_token' => $_SESSION['ds_access_token'], |
| 157 | + 'envelope_args' => $envelope_args |
| 158 | + ]; |
| 159 | + } |
| 160 | + |
| 161 | + private function contactSupportToEnableFeature($e) |
| 162 | + { |
| 163 | + $GLOBALS['twig']->display('error.html', [ |
| 164 | + 'error_code' => $e->getCode(), |
| 165 | + 'error_message' => ManifestService::replacePlaceholders( |
| 166 | + '{0}', |
| 167 | + 'Maestro', |
| 168 | + ManifestService::getCommonTexts()['ContactSupportToEnableFeature'] |
| 169 | + ), |
| 170 | + 'common_texts' => ManifestService::getCommonTexts() |
| 171 | + ]); |
| 172 | + exit; |
| 173 | + } |
| 174 | + |
| 175 | + private function selectNewestWorkflowByName($workflowDefinitions, $workflowName) |
| 176 | + { |
| 177 | + if ($workflowDefinitions['count'] > 0) { |
| 178 | + $filteredWorkflows = array_filter( |
| 179 | + $workflowDefinitions['value'], |
| 180 | + function ($workflow) use ($workflowName) { |
| 181 | + return $workflow['name'] === $workflowName; |
| 182 | + } |
| 183 | + ); |
| 184 | + |
| 185 | + usort($filteredWorkflows, function ($wf1, $wf2) { |
| 186 | + return strtotime($wf2['lastUpdatedDate']) - strtotime($wf1['lastUpdatedDate']); |
| 187 | + }); |
| 188 | + |
| 189 | + $workflow = reset($filteredWorkflows); |
| 190 | + |
| 191 | + if ($workflow) { |
| 192 | + $_SESSION["workflow_id"] = $workflow['id']; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private function openPublishWorkflowPage($publishWorkflowUrl) |
| 198 | + { |
| 199 | + $GLOBALS['twig']->display("maestro/eg001_publish_workflow.html", [ |
| 200 | + 'title' => $this->routerService->getTitle(static::EG), |
| 201 | + 'consent_url' => ManifestService::replacePlaceholders( |
| 202 | + '{0}', |
| 203 | + $publishWorkflowUrl, |
| 204 | + $this->codeExampleText['AdditionalPage'][0]['ResultsPageText'] |
| 205 | + ), |
| 206 | + 'code_example_text' => $this->codeExampleText, |
| 207 | + 'common_texts' => $this->getCommonText() |
| 208 | + ]); |
| 209 | + exit(); |
| 210 | + } |
| 211 | +} |
0 commit comments