1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CloudControl\Cms\components { |
4
|
|
|
|
5
|
|
|
use CloudControl\Cms\storage\Document; |
6
|
|
|
use CloudControl\Cms\storage\Storage; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DocumentComponent |
10
|
|
|
* |
11
|
|
|
* Has optional parameter `folder` to prefix the relative url with a folder |
12
|
|
|
* Has optional parameter `document` to select a given document |
13
|
|
|
* Has optional parameter `documentParameterName` to select the parametername to be used |
14
|
|
|
* to set the found document to. |
15
|
|
|
* |
16
|
|
|
* @package CloudControl\Cms\components |
17
|
|
|
*/ |
18
|
|
|
class DocumentComponent extends NotFoundComponent |
19
|
|
|
{ |
20
|
|
|
protected $documentParameterName = 'document'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Storage $storage |
24
|
|
|
* |
25
|
|
|
* @return mixed|void |
26
|
|
|
* @throws \Exception |
27
|
|
|
*/ |
28
|
|
|
public function run(Storage $storage) |
29
|
|
|
{ |
30
|
|
|
parent::run($storage); |
31
|
|
|
|
32
|
|
|
$this->checkParameters(); |
33
|
|
|
|
34
|
|
|
if ($this->matchedSitemapItem === null) { // If no sitemapitem, its an application component |
35
|
|
|
$this->runLikeApplicationComponent(); |
36
|
|
|
} else { |
37
|
|
|
$this->runLikeRegularComponent(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Checks to see if any parameters were defined in the cms and acts according |
43
|
|
|
*/ |
44
|
|
|
private function checkParameters() |
45
|
|
|
{ |
46
|
|
|
if (isset($this->parameters['documentParameterName'])) { |
47
|
|
|
$this->documentParameterName = $this->parameters['documentParameterName']; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Run as application component |
53
|
|
|
* |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
private function runLikeApplicationComponent() |
57
|
|
|
{ |
58
|
|
|
if (isset($this->parameters['document'])) { |
59
|
|
|
$this->parameters[$this->documentParameterName] = $this->storage->getDocuments()->getDocumentBySlug($this->parameters['document']); |
60
|
|
|
unset($this->parameters['document']); |
61
|
|
|
} else { |
62
|
|
|
throw new \Exception('When used as application component, you need to specify a document.'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Run as regular component |
68
|
|
|
* |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
private function runLikeRegularComponent() |
72
|
|
|
{ |
73
|
|
|
if ($this->matchedSitemapItem->regex == false || isset($this->parameters['document'])) { |
74
|
|
|
$this->runWithoutRegex(); |
75
|
|
|
} else { |
76
|
|
|
$this->runWithRegex(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Run without regex |
82
|
|
|
* |
83
|
|
|
* @throws \Exception |
84
|
|
|
*/ |
85
|
|
|
private function runWithoutRegex() |
86
|
|
|
{ |
87
|
|
|
if (isset($this->parameters['document'])) { |
88
|
|
|
$this->runByDocumentParameter(); |
89
|
|
|
} else { |
90
|
|
|
throw new \Exception('When not using a regex, you need to set the parameter `document` with the path to the document in this sitemap item: ' . $this->matchedSitemapItem->title); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Run with regex |
96
|
|
|
* |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
private function runWithRegex() |
100
|
|
|
{ |
101
|
|
|
$relativeDocumentUri = $this->checkForSpecificFolder(); |
102
|
|
|
|
103
|
|
|
$document = $this->storage->getDocuments()->getDocumentBySlug($relativeDocumentUri); |
104
|
|
|
|
105
|
|
|
if ($document instanceof Document && $document->state == 'published' && $document->type != 'folder') { |
106
|
|
|
$this->parameters[$this->documentParameterName] = $document; |
107
|
|
|
} else { |
108
|
|
|
$this->set404Header(); |
109
|
|
|
$this->set404Template(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Run using the given `document` parameter |
115
|
|
|
*/ |
116
|
|
|
private function runByDocumentParameter() |
117
|
|
|
{ |
118
|
|
|
$document = $this->storage->getDocuments()->getDocumentBySlug($this->parameters['document']); |
119
|
|
|
if ($document instanceof Document) { |
120
|
|
|
$this->parameters[$this->documentParameterName] = $document; |
121
|
|
|
} else { |
122
|
|
|
$this->set404Header(); |
123
|
|
|
$this->set404Template(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return mixed|string |
129
|
|
|
*/ |
130
|
|
|
private function checkForSpecificFolder() |
131
|
|
|
{ |
132
|
|
|
$relativeDocumentUri = current($this->matchedSitemapItem->matches[1]); |
133
|
|
|
if (isset($this->parameters['folder'])) { |
134
|
|
|
if (substr($this->parameters['folder'], -1) !== '/') { |
135
|
|
|
$this->parameters['folder'] = $this->parameters['folder'] . '/'; |
136
|
|
|
} |
137
|
|
|
$relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri; |
138
|
|
|
} |
139
|
|
|
return $relativeDocumentUri; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |