|
| 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 | +} |
0 commit comments