1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Step; |
10
|
|
|
|
11
|
|
|
use Cecil\Collection\Page\Page; |
12
|
|
|
use Cecil\Collection\Taxonomy\Collection as VocabulariesCollection; |
13
|
|
|
use Cecil\Collection\Taxonomy\Term as Term; |
14
|
|
|
use Cecil\Collection\Taxonomy\Vocabulary as Vocabulary; |
15
|
|
|
use Cecil\Exception\Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create taxonomies collection. |
19
|
|
|
*/ |
20
|
|
|
class TaxonomiesCreate extends AbstractStep |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var VocabulariesCollection |
24
|
|
|
*/ |
25
|
|
|
protected $vocabCollection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function init($options) |
31
|
|
|
{ |
32
|
|
|
/** @var \Cecil\Builder $builder */ |
33
|
|
|
if (is_dir($this->builder->getConfig()->getContentPath())) { |
34
|
|
|
$this->process = true; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function process() |
42
|
|
|
{ |
43
|
|
|
if ($this->config->get('taxonomies')) { |
44
|
|
|
$this->createVocabulariesCollection(); |
45
|
|
|
$this->collectTermsFromPages(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->builder->setTaxonomies($this->vocabCollection); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a collection from the vocabularies configuration. |
53
|
|
|
*/ |
54
|
|
|
protected function createVocabulariesCollection() |
55
|
|
|
{ |
56
|
|
|
// create an empty a vocabularies collection |
57
|
|
|
$this->vocabCollection = new VocabulariesCollection('taxonomies'); |
58
|
|
|
/* |
59
|
|
|
* Adds each vocabulary to the collection. |
60
|
|
|
* ie: |
61
|
|
|
* taxonomies: |
62
|
|
|
* - tags: tag |
63
|
|
|
* - categories: category |
64
|
|
|
*/ |
65
|
|
|
foreach (array_keys((array) $this->config->get('taxonomies')) as $vocabulary) { |
66
|
|
|
/* |
67
|
|
|
* Disabled vocabulary? |
68
|
|
|
* ie: |
69
|
|
|
* taxonomies: |
70
|
|
|
* tags: disabled |
71
|
|
|
*/ |
72
|
|
|
if ($this->config->get("taxonomies.$vocabulary") == 'disabled') { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->vocabCollection->add(new Vocabulary($vocabulary)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Collects vocabularies/terms from pages frontmatter. |
82
|
|
|
*/ |
83
|
|
|
protected function collectTermsFromPages() |
84
|
|
|
{ |
85
|
|
|
/* @var $page Page */ |
86
|
|
|
$pages = $this->builder->getPages()->sortByDate(); |
87
|
|
|
foreach ($pages as $page) { |
88
|
|
|
// ie: tags |
89
|
|
|
foreach ($this->vocabCollection as $vocabulary) { |
90
|
|
|
$plural = $vocabulary->getId(); |
91
|
|
|
/* |
92
|
|
|
* ie: |
93
|
|
|
* tags: Tag 1, Tag 2 |
94
|
|
|
*/ |
95
|
|
|
if ($page->hasVariable($plural)) { |
96
|
|
|
// converts a string list to an array |
97
|
|
|
if (!is_array($page->getVariable($plural))) { |
98
|
|
|
$page->setVariable($plural, [$page->getVariable($plural)]); |
99
|
|
|
} |
100
|
|
|
// adds each term to the vocabulary collection... |
101
|
|
|
foreach ($page->getVariable($plural) as $termName) { |
102
|
|
|
if (null === $termName) { |
103
|
|
|
throw new Exception(\sprintf( |
104
|
|
|
'Taxonomy "%s" of "%s" can\'t be empty.', |
105
|
|
|
$plural, |
106
|
|
|
$page->getId() |
107
|
|
|
)); |
108
|
|
|
} |
109
|
|
|
$termId = Page::slugify($termName); |
110
|
|
|
$term = (new Term($termId))->setName($termName); |
111
|
|
|
$this->vocabCollection |
112
|
|
|
->get($plural) |
113
|
|
|
->add($term); |
|
|
|
|
114
|
|
|
// ... and adds page to the term collection |
115
|
|
|
$this->vocabCollection |
116
|
|
|
->get($plural) |
117
|
|
|
->get($termId) |
|
|
|
|
118
|
|
|
->add($page); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|