1 | <?php |
||
15 | * @var null|string |
||
16 | */ |
||
17 | protected $responseFolder = null; |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $subTemplate = 'cms/documents/document-form-form'; |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $formParameterName = 'form'; |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $thankYouMessage = 'Thank you for sending us your response.'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $formId; |
||
35 | /** |
||
36 | * @var null|string |
||
37 | */ |
||
38 | private $getPathBackup = null; |
||
39 | |||
40 | /** |
||
41 | * @var null|\stdClass |
||
42 | */ |
||
43 | private $userSessionBackup = null; |
||
44 | |||
45 | /** |
||
46 | * @param Storage $storage |
||
47 | * @return void |
||
48 | * @throws \Exception |
||
49 | */ |
||
50 | public function run(Storage $storage) |
||
51 | { |
||
52 | parent::run($storage); |
||
53 | |||
54 | $this->checkParameters(); |
||
55 | |||
56 | if ($this->documentType === null || $this->responseFolder === null) { |
||
57 | throw new \Exception('Parameters `documentType` and `responseFolder` are required for usage with this form'); |
||
58 | } |
||
59 | |||
60 | $this->setFormId(); |
||
61 | $this->initialize($storage); |
||
62 | $this->checkSubmit($storage); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param null|Application $application |
||
67 | * @throws \Exception |
||
68 | */ |
||
69 | public function render($application = null) |
||
70 | { |
||
71 | $request = $this->request; |
||
72 | if (isset($request::$get['path'])) { |
||
73 | $this->getPathBackup = $request::$get['path']; |
||
74 | } |
||
75 | $request::$get['path'] = $this->responseFolder; |
||
76 | $form = $this->renderTemplate($this->subTemplate); |
||
77 | if ($this->getPathBackup !== null) { |
||
78 | $request::$get['path'] = $this->getPathBackup; |
||
79 | } else { |
||
80 | unset($request::$get['path']); |
||
81 | } |
||
82 | if ($this->isFormSubmitted($this->request)) { |
||
83 | $this->parameters[$this->formParameterName] = '<a name="' . $this->formId . '"></a>' . $this->thankYouMessage; |
||
84 | } else { |
||
85 | $this->parameters[$this->formParameterName] = $form; |
||
86 | } |
||
87 | |||
88 | parent::render($application); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Checks if parameters were given in the CMS configuration and |
||
93 | * sets them to their respective fields |
||
94 | */ |
||
95 | private function checkParameters() |
||
96 | { |
||
97 | if (isset($this->parameters['documentType'])) { |
||
98 | $this->documentType = $this->parameters['documentType']; |
||
99 | unset($this->parameters['documentType']); |
||
100 | } |
||
101 | |||
102 | if (isset($this->parameters['responseFolder'])) { |
||
103 | $this->responseFolder = $this->parameters['responseFolder']; |
||
104 | unset($this->parameters['responseFolder']); |
||
105 | } |
||
106 | |||
107 | if (isset($this->parameters['subTemplate'])) { |
||
108 | $this->subTemplate = $this->parameters['subTemplate']; |
||
109 | unset($this->parameters['subTemplate']); |
||
110 | } |
||
111 | |||
112 | if (isset($this->parameters['formParameterName'])) { |
||
113 | $this->formParameterName = $this->parameters['formParameterName']; |
||
114 | unset($this->parameters['formParameterName']); |
||
115 | } |
||
116 | |||
117 | if (isset($this->parameters['thankYouMessage'])) { |
||
118 | $this->thankYouMessage = $this->parameters['thankYouMessage']; |
||
119 | unset($this->parameters['thankYouMessage']); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Sets variables needed for rendering the form template |
||
125 | * @param $storage |
||
126 | */ |
||
127 | private function initialize($storage) |
||
128 | { |
||
129 | $this->parameters['smallestImage'] = $storage->getSmallestImageSet()->slug; |
||
130 | $this->parameters['cmsPrefix'] = ''; |
||
131 | |||
132 | $this->parameters['documentType'] = $this->storage->getDocumentTypeBySlug($this->documentType, true); |
||
133 | $this->parameters['documentTypes'] = $this->storage->getDocumentTypes(); |
||
134 | $this->parameters['hideTitleAndState'] = true; |
||
135 | $this->parameters['formId'] = $this->formId; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * If the form has been submitted, save the document |
||
227 | } |