1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Content; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File Based Content, code for loading additional content into view through |
7
|
|
|
* data["meta"]. |
8
|
|
|
*/ |
9
|
|
|
trait TFBCLoadAdditionalContent |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Load extra info into views based of meta information provided in each |
13
|
|
|
* view. |
14
|
|
|
* |
15
|
|
|
* @param array &$views with all views. |
16
|
|
|
* @param string $route current route |
17
|
|
|
* @param string $routeIndex route with appended /index |
18
|
|
|
* |
19
|
|
|
* @throws NotFoundException when mapping can not be done. |
20
|
|
|
* |
21
|
|
|
* @return void. |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
private function loadAdditionalContent(&$views, $route, $routeIndex) |
24
|
|
|
{ |
25
|
|
|
foreach ($views as $id => $view) { |
26
|
|
|
$meta = isset($view["data"]["meta"]) |
27
|
|
|
? $view["data"]["meta"] |
28
|
|
|
: null; |
29
|
|
|
|
30
|
|
|
if (is_array($meta)) { |
31
|
|
|
switch ($meta["type"]) { |
32
|
|
|
case "article-toc": |
33
|
|
|
$content = $views["main"]["data"]["content"]; |
34
|
|
|
$views[$id]["data"]["articleToc"] = $this->di->textFilter->createToc($content); |
|
|
|
|
35
|
|
|
break; |
36
|
|
|
|
37
|
|
|
case "breadcrumb": |
38
|
|
|
$views[$id]["data"]["breadcrumb"] = $this->createBreadcrumb($route); |
|
|
|
|
39
|
|
|
break; |
40
|
|
|
|
41
|
|
View Code Duplication |
case "next-previous": |
|
|
|
|
42
|
|
|
$baseRoute = dirname($routeIndex); |
43
|
|
|
list($next, $previous) = $this->findNextAndPrevious($routeIndex); |
44
|
|
|
$views[$id]["data"]["next"] = $next; |
45
|
|
|
$views[$id]["data"]["previous"] = $previous; |
46
|
|
|
break; |
47
|
|
|
|
48
|
|
|
case "single": // OBSOLETE |
49
|
|
|
case "content": |
50
|
|
|
$route = $this->getActiveRoute($meta["route"], $routeIndex); |
|
|
|
|
51
|
|
|
|
52
|
|
|
// Get the content |
53
|
|
|
$data = $this->getDataForAdditionalRoute($route); |
|
|
|
|
54
|
|
|
$views[$id]["data"] = array_merge_recursive_distinct($views[$id]["data"], $data); |
55
|
|
|
break; |
56
|
|
|
|
57
|
|
|
case "columns": |
58
|
|
|
$columns = $meta["columns"]; |
59
|
|
|
foreach ($columns as $key => $value) { |
60
|
|
|
$route = $this->getActiveRoute($value["route"], $routeIndex); |
|
|
|
|
61
|
|
|
$data = $this->getDataForAdditionalRoute($route); |
|
|
|
|
62
|
|
|
$columns[$key] = $data; |
63
|
|
|
} |
64
|
|
|
$views[$id]["data"]["columns"] = $columns; |
65
|
|
|
break; |
66
|
|
|
|
67
|
|
|
case "toc-sort": |
68
|
|
|
$baseRoute = dirname($routeIndex); |
69
|
|
|
$this->orderToc($baseRoute, $meta); |
|
|
|
|
70
|
|
|
break; |
71
|
|
|
|
72
|
|
View Code Duplication |
case "toc": |
|
|
|
|
73
|
|
|
$baseRoute = dirname($routeIndex); |
74
|
|
|
$toc = $this->meta[$baseRoute]["__toc__"]; |
|
|
|
|
75
|
|
|
$this->limitToc($toc, $meta); |
76
|
|
|
$views[$id]["data"]["toc"] = $toc; |
77
|
|
|
$views[$id]["data"]["meta"] = $meta; |
78
|
|
|
break; |
79
|
|
|
|
80
|
|
|
case "book-toc": |
81
|
|
|
$toc = $this->meta[$baseRoute]["__toc__"]; |
|
|
|
|
82
|
|
|
$views[$id]["data"]["toc"] = $toc; |
83
|
|
|
break; |
84
|
|
|
|
85
|
|
|
case "author": |
86
|
|
|
if (isset($views["main"]["data"]["author"])) { |
87
|
|
|
$views[$id]["data"]["author"] = $this->loadAuthorDetails($views["main"]["data"]["author"]); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case "copy": |
92
|
|
|
$viewToCopy = $views[$id]["data"]["meta"]["view"]; |
93
|
|
|
$views[$id]["data"] = $views[$viewToCopy]["data"]; |
94
|
|
|
break; |
95
|
|
|
|
96
|
|
|
default: |
97
|
|
|
throw new Exception(t("Unsupported data/meta/type '!TYPE' for additional content.", ["!TYPE" => $meta["type"]])); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Find next and previous links of current content. |
107
|
|
|
* |
108
|
|
|
* @param string $routeIndex target route to find next and previous for. |
109
|
|
|
* |
110
|
|
|
* @return array with next and previous if found. |
111
|
|
|
*/ |
112
|
|
|
private function findNextAndPrevious($routeIndex) |
113
|
|
|
{ |
114
|
|
|
$key = dirname($routeIndex); |
115
|
|
|
if (!isset($this->meta[$key]["__toc__"])) { |
116
|
|
|
return [null, null]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$toc = $this->meta[$key]["__toc__"]; |
120
|
|
|
if (!isset($toc[$routeIndex])) { |
121
|
|
|
return [null, null]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$index2Key = array_keys($toc); |
125
|
|
|
$keys = array_flip($index2Key); |
126
|
|
|
$values = array_values($toc); |
127
|
|
|
$count = count($keys); |
128
|
|
|
|
129
|
|
|
$current = $keys[$routeIndex]; |
130
|
|
|
$previous = null; |
131
|
|
View Code Duplication |
for ($i = $current - 1; $i >= 0; $i--) { |
|
|
|
|
132
|
|
|
$isSectionHeader = $values[$i]["sectionHeader"]; |
133
|
|
|
$isInternal = $values[$i]["internal"]; |
134
|
|
|
if ($isSectionHeader || $isInternal) { |
135
|
|
|
continue; |
136
|
|
|
} |
137
|
|
|
$previous = $values[$i]; |
138
|
|
|
$previous["route"] = $index2Key[$i]; |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$next = null; |
143
|
|
View Code Duplication |
for ($i = $current + 1; $i < $count; $i++) { |
|
|
|
|
144
|
|
|
$isSectionHeader = $values[$i]["sectionHeader"]; |
145
|
|
|
$isInternal = $values[$i]["internal"]; |
146
|
|
|
if ($isSectionHeader || $isInternal) { |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
$next = $values[$i]; |
150
|
|
|
$next["route"] = $index2Key[$i]; |
151
|
|
|
break; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return [$next, $previous]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Order toc items. |
161
|
|
|
* |
162
|
|
|
* @param string $baseRoute route to use to find __toc__. |
163
|
|
|
* @param string $meta on how to order toc. |
164
|
|
|
* |
165
|
|
|
* @return void. |
|
|
|
|
166
|
|
|
*/ |
167
|
|
|
private function orderToc($baseRoute, $meta) |
168
|
|
|
{ |
169
|
|
|
$defaults = [ |
170
|
|
|
"orderby" => "section", |
171
|
|
|
"orderorder" => "asc", |
172
|
|
|
]; |
173
|
|
|
$options = array_merge($defaults, $meta); |
174
|
|
|
$orderby = $options["orderby"]; |
175
|
|
|
$order = $options["orderorder"]; |
176
|
|
|
$toc = $this->meta[$baseRoute]["__toc__"]; |
177
|
|
|
|
178
|
|
|
uksort($toc, function ($a, $b) use ($toc, $orderby, $order) { |
179
|
|
|
$a = $toc[$a][$orderby]; |
180
|
|
|
$b = $toc[$b][$orderby]; |
181
|
|
|
|
182
|
|
|
$asc = $order == "asc" ? 1 : -1; |
183
|
|
|
|
184
|
|
|
if ($a == $b) { |
185
|
|
|
return 0; |
186
|
|
|
} elseif ($a > $b) { |
187
|
|
|
return $asc; |
188
|
|
|
} |
189
|
|
|
return -$asc; |
190
|
|
|
}); |
191
|
|
|
|
192
|
|
|
$this->meta[$baseRoute]["__toc__"] = $toc; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Limit and paginate toc items. |
198
|
|
|
* |
199
|
|
|
* @param string &$toc array with current toc. |
200
|
|
|
* @param string &$meta on how to order and limit toc. |
201
|
|
|
* |
202
|
|
|
* @return void. |
|
|
|
|
203
|
|
|
*/ |
204
|
|
|
private function limitToc(&$toc, &$meta) |
205
|
|
|
{ |
206
|
|
|
$defaults = [ |
207
|
|
|
"items" => 7, |
208
|
|
|
"offset" => 0, |
209
|
|
|
]; |
210
|
|
|
$options = array_merge($defaults, $meta); |
211
|
|
|
|
212
|
|
|
// Check if pagination is currently used |
213
|
|
|
if ($this->currentPage) { |
214
|
|
|
$options["offset"] = ($this->currentPage - 1) * $options["items"]; |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$meta["totalItems"] = count($toc); |
218
|
|
|
$meta["currentPage"] = (int) floor($options["offset"] / $options["items"]) + 1; |
219
|
|
|
$meta["totalPages"] = (int) floor($meta["totalItems"] / $options["items"] + 1); |
220
|
|
|
|
221
|
|
|
// Next and previous page |
222
|
|
|
$pagination = $this->config["pagination"]; |
|
|
|
|
223
|
|
|
$meta["nextPageUrl"] = null; |
224
|
|
|
$meta["previousPageUrl"] = null; |
225
|
|
|
|
226
|
|
|
if ($meta["currentPage"] > 1 && $meta["totalPages"] > 1) { |
227
|
|
|
$previousPage = $meta["currentPage"] - 1; |
228
|
|
|
$previous = ""; |
229
|
|
|
if ($previousPage != 1) { |
230
|
|
|
$previous = "/$pagination/$previousPage"; |
231
|
|
|
} |
232
|
|
|
$meta["previousPageUrl"] = $this->baseRoute . $previous; |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if ($meta["currentPage"] < $meta["totalPages"]) { |
236
|
|
|
$nextPage = $meta["currentPage"] + 1; |
237
|
|
|
$meta["nextPageUrl"] = $this->baseRoute . "/$pagination/$nextPage"; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
// Only use slice of toc |
242
|
|
|
$startSlice = ($meta["currentPage"] - 1) * $options["items"]; |
243
|
|
|
$toc = array_slice($toc, $startSlice, $options["items"]); |
244
|
|
|
$meta["displayedItems"] = count($toc); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.