2
2
3
3
namespace Charcoal \Sitemap \Service ;
4
4
5
+ use Charcoal \Factory \FactoryInterface ;
6
+ use Charcoal \Loader \CollectionLoader ;
7
+ use Charcoal \Object \CategoryInterface ;
8
+ use Charcoal \Object \HierarchicalInterface ;
9
+ use Charcoal \Object \RoutableInterface ;
10
+ use Charcoal \Translator \TranslatorAwareTrait ;
11
+ use Charcoal \View \ViewableInterface ;
5
12
use InvalidArgumentException ;
6
13
use RuntimeException ;
7
14
8
15
// From 'charcoal-factory'
9
- use Charcoal \Factory \FactoryInterface ;
10
16
11
17
// From 'charcoal-core'
12
- use Charcoal \Loader \CollectionLoader ;
13
18
14
19
// From 'charcoal-object'
15
- use Charcoal \Object \CategoryInterface ;
16
- use Charcoal \Object \HierarchicalInterface ;
17
- use Charcoal \Object \RoutableInterface ;
18
20
19
21
// From 'charcoal-translator'
20
- use Charcoal \Translator \TranslatorAwareTrait ;
21
22
22
23
// From 'charcoal-view'
23
- use Charcoal \View \ViewableInterface ;
24
24
25
25
/**
26
26
* Sitemap builder from object hierarchy
@@ -117,6 +117,7 @@ protected function defaultOptions()
117
117
'locale ' => $ this ->translator ()->getLocale (),
118
118
'l10n ' => true ,
119
119
'check_active_routes ' => true ,
120
+ 'relative_urls ' => true ,
120
121
'objects ' => [
121
122
'label ' => '{{title}} ' ,
122
123
'url ' => '{{url}} ' ,
@@ -175,6 +176,9 @@ public function build($ident = 'default')
175
176
if (!isset ($ options ['check_active_routes ' ])) {
176
177
$ options ['check_active_routes ' ] = $ defaults ['check_active_routes ' ];
177
178
}
179
+ if (!isset ($ options ['relative_urls ' ])) {
180
+ $ options ['relative_urls ' ] = $ defaults ['relative_urls ' ];
181
+ }
178
182
$ out [] = $ this ->buildObject ($ class , $ options );
179
183
}
180
184
@@ -198,11 +202,13 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
198
202
}
199
203
}
200
204
205
+ // Loadin the actual objects from the predefined settings
201
206
$ factory = $ this ->modelFactory ();
202
207
$ obj = $ factory ->create ($ class );
203
208
204
209
$ loader = $ this ->collectionLoader ()->setModel ($ obj );
205
210
211
+ // From the filters
206
212
if (isset ($ options ['filters ' ])) {
207
213
$ filters = $ options ['filters ' ];
208
214
if ($ parent ) {
@@ -211,6 +217,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
211
217
$ loader ->addFilters ($ filters );
212
218
}
213
219
220
+ // From the orders
214
221
if (isset ($ options ['orders ' ])) {
215
222
$ orders = $ options ['orders ' ];
216
223
if ($ parent ) {
@@ -219,6 +226,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
219
226
$ loader ->addOrders ($ orders );
220
227
}
221
228
229
+ // From the category / hierarchichal property
222
230
$ category = ($ obj instanceof CategoryInterface);
223
231
$ hierarchical = ($ obj instanceof HierarchicalInterface);
224
232
if ($ hierarchical || $ category ) {
@@ -229,21 +237,26 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
229
237
}
230
238
}
231
239
240
+ // Loading
232
241
$ list = $ loader ->load ();
233
242
243
+ // Processing the objects and rendering data
234
244
$ out = [];
235
245
$ children = isset ($ options ['children ' ]) ? $ options ['children ' ] : [];
236
246
$ level ++;
237
247
248
+ // Options
238
249
$ l10n = $ options ['l10n ' ];
239
- $ locale = $ options ['locale ' ];
250
+ $ defaultLocale = $ options ['locale ' ];
240
251
$ checkActiveRoutes = $ options ['check_active_routes ' ];
252
+ $ relativeUrls = $ options ['relative_urls ' ];
241
253
242
- $ availableLocales = $ l10n ? $ this ->translator ()->availableLocales () : [$ locale ];
254
+ // Locales
255
+ $ availableLocales = $ l10n ? $ this ->translator ()->availableLocales () : [$ defaultLocale ];
243
256
244
257
foreach ($ availableLocales as $ locale ) {
245
258
246
- $ currentLocale = $ locale ;
259
+ // Get opposite languages locales
247
260
$ oppositeLang = [];
248
261
foreach ($ availableLocales as $ l ) {
249
262
if ($ l == $ locale ) {
@@ -252,12 +265,15 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
252
265
$ oppositeLang [] = $ l ;
253
266
}
254
267
268
+ // Set the local to the current locale before looping the list.
255
269
$ this ->translator ()->setLocale ($ locale );
256
270
foreach ($ list as $ object ) {
271
+ // When checking active routes, do not display routes that are not active
257
272
if ($ checkActiveRoutes && $ object instanceof RoutableInterface && !$ object ->isActiveRoute ()) {
258
273
continue ;
259
274
}
260
275
276
+ // Hierarchical (children, when defined)
261
277
$ cs = [];
262
278
if (!empty ($ children )) {
263
279
foreach ($ children as $ cname => $ opts ) {
@@ -266,27 +282,34 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
266
282
}
267
283
}
268
284
285
+ // Url template, relative or absolute?
286
+ $ urlTemplate = $ relativeUrls ? $ options ['url ' ] : '{{#withBaseUrl}} ' . $ options ['url ' ] . '{{/withBaseUrl}} ' ;
287
+
269
288
$ tmp = [
270
289
'label ' => trim ($ this ->renderData ($ object , $ options ['label ' ])),
271
- 'url ' => trim ($ this ->renderData ($ object , ' {{#withBaseUrl}} ' . $ options [ ' url ' ] . ' {{/withBaseUrl}} ' )),
290
+ 'url ' => trim ($ this ->renderData ($ object , $ urlTemplate )),
272
291
'children ' => $ cs ,
273
292
'data ' => $ this ->renderData ($ object , $ options ['data ' ]),
274
293
'level ' => $ level ,
275
294
'lang ' => $ locale
276
295
];
277
296
297
+ // If you need a priority, fix your own rules
278
298
$ priority = '' ;
279
299
if (isset ($ options ['priority ' ]) && $ options ['priority ' ]) {
280
300
$ priority = $ this ->renderData ($ object , (string )$ options ['priority ' ]);
281
301
}
282
302
$ tmp ['priority ' ] = $ priority ;
283
303
304
+ // If you need a date of last modification, fix your own rules
284
305
$ last = '' ;
285
306
if (isset ($ options ['last_modified ' ]) && $ options ['last_modified ' ]) {
286
307
$ last = $ this ->renderData ($ object , $ options ['last_modified ' ]);
287
308
}
288
309
$ tmp ['last_modified ' ] = $ last ;
289
310
311
+ // Opposite Languages
312
+ // Meant to be alternate, thus the lack of data rendering
290
313
$ alternates = [];
291
314
foreach ($ oppositeLang as $ ol ) {
292
315
$ this ->translator ()->setLocale ($ ol );
@@ -302,15 +325,15 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
302
325
303
326
$ tmp ['alternates ' ] = $ alternates ;
304
327
305
- $ this ->translator ()->setLocale ($ currentLocale );
328
+ $ this ->translator ()->setLocale ($ locale );
306
329
$ out [] = $ tmp ;
307
330
}
308
331
}
309
332
310
333
311
334
return $ out ;
312
335
}
313
-
336
+
314
337
/**
315
338
* Recursive data renderer
316
339
*
0 commit comments