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 TFBCUtilities |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Load view data for additional route, merged with meta if any. |
13
|
|
|
* |
14
|
|
|
* @param string $route to load. |
15
|
|
|
* |
16
|
|
|
* @return array with view data details. |
17
|
|
|
*/ |
18
|
|
|
private function getDataForAdditionalRoute($route) |
19
|
|
|
{ |
20
|
|
|
// From configuration |
21
|
|
|
$filter = $this->config["textfilter"]; |
|
|
|
|
22
|
|
|
|
23
|
|
|
// Get filtered content from route |
24
|
|
|
list(, , $filtered) = |
25
|
|
|
$this->mapRoute2Content($route); |
|
|
|
|
26
|
|
|
|
27
|
|
|
// Set data to be content of frontmatter, merged with meta |
28
|
|
|
$meta = $this->getMetaForRoute($route); |
|
|
|
|
29
|
|
|
$data = $filtered->frontmatter; |
30
|
|
|
$data = array_merge_recursive_distinct($meta, $data); |
31
|
|
|
unset($data["__toc__"]); |
32
|
|
|
unset($data["views"]); |
33
|
|
|
|
34
|
|
|
// Do phase 2 processing |
35
|
|
|
$new = $this->di->get("textFilter")->parse($filtered->text, $filter); |
|
|
|
|
36
|
|
|
|
37
|
|
|
// Creates urls based on baseurl |
38
|
|
|
$baseurl = isset($data["baseurl"]) |
39
|
|
|
? isset($data["baseurl"]) |
40
|
|
|
: null; |
41
|
|
|
$this->addBaseurl2AnchorUrls($new, $baseurl); |
|
|
|
|
42
|
|
|
$data["content"] = $new->text; |
43
|
|
|
|
44
|
|
|
return $data; |
45
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Parse text, find and update all a href to use baseurl. |
52
|
|
|
* |
53
|
|
|
* @param object &$filtered with text and excerpt to process. |
54
|
|
|
* @param string $baseurl add as baseurl for all relative urls. |
55
|
|
|
* |
56
|
|
|
* @return void. |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
private function addBaseurl2AnchorUrls(&$filtered, $baseurl) |
59
|
|
|
{ |
60
|
|
|
$textf = $this->di->get("textFilter"); |
61
|
|
|
$url = $this->di->get("url"); |
62
|
|
|
|
63
|
|
|
// Use callback to url->create() instead of string concat |
64
|
|
|
$callback = function ($route) use ($url, $baseurl) { |
65
|
|
|
return $url->create($route, $baseurl); |
66
|
|
|
}; |
67
|
|
|
|
68
|
|
|
$filtered->text = |
69
|
|
|
$textf->addBaseurlToRelativeLinks($filtered->text, $baseurl, $callback); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get published date. |
76
|
|
|
* |
77
|
|
|
* @param array $frontmatter with details on dates. |
78
|
|
|
* |
79
|
|
|
* @return integer as time for publish time. |
80
|
|
|
*/ |
81
|
|
|
private function getPublishTime($frontmatter) |
82
|
|
|
{ |
83
|
|
|
list(, $date) = $this->di->get("view")->getPublishedDate($frontmatter); |
84
|
|
|
return strtotime($date); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: