1 | <?php |
||
2 | |||
3 | namespace Apps\Model\Front\Sitemap; |
||
4 | |||
5 | use Ffcms\Core\App; |
||
6 | use Ffcms\Core\Arch\Model; |
||
7 | use Ffcms\Core\Helper\Date; |
||
8 | use Ffcms\Core\Helper\FileSystem\File; |
||
9 | use Ffcms\Core\Helper\Type\Any; |
||
10 | use Ffcms\Templex\Url\Url; |
||
11 | |||
12 | /** |
||
13 | * Class EntityBuildMap. Build sitemap data and save to file in xml format |
||
14 | * @package Apps\Model\Front\Sitemap |
||
15 | */ |
||
16 | class EntityBuildMap extends Model |
||
17 | { |
||
18 | private $data; |
||
19 | private $langs; |
||
20 | |||
21 | /** |
||
22 | * EntityBuildMap constructor. Pass available languages and data as array inside. |
||
23 | * @param array|null $langs |
||
24 | * @param array|null $data |
||
25 | */ |
||
26 | public function __construct(?array $langs = null, ?array $data = null) |
||
27 | { |
||
28 | $this->langs = $langs; |
||
29 | |||
30 | if (!$data && count($data) > 0) { |
||
31 | foreach ($data as $item) { |
||
32 | if (!Any::isArray($item) || !isset($item['uri'], $item['lastmod'])) { |
||
33 | continue; |
||
34 | } |
||
35 | |||
36 | $this->add($item['uri'], $item['lastmod'], $item['freq'], $item['priority']); |
||
37 | } |
||
38 | } |
||
39 | parent::__construct(); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Add uri/url item to sitemap builder |
||
44 | * @param string $uri |
||
45 | * @param int|string $lastmod |
||
46 | * @param string $freq |
||
47 | * @param float $priority |
||
48 | */ |
||
49 | public function add(?string $uri, $lastmod, string $freq = 'weekly', $priority = 0.5) |
||
50 | { |
||
51 | // generate multi-language files |
||
52 | if ($this->langs && Any::isArray($this->langs) && count($this->langs) > 0) { |
||
53 | foreach ($this->langs as $lang) { |
||
54 | // set data to local attribute |
||
55 | $this->data[$lang][] = [ |
||
56 | 'uri' => Url::stringUrl($uri, $lang), |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
57 | 'lastmod' => Date::convertToDatetime($lastmod, 'c'), |
||
58 | 'freq' => (string)$freq, |
||
59 | 'priority' => (float)$priority |
||
60 | ]; |
||
61 | } |
||
62 | } else { // only one language, multilanguage is disabled |
||
63 | $this->data[App::$Properties->get('singleLanguage')][] = [ |
||
64 | 'uri' => Url::stringUrl($uri), |
||
65 | 'lastmod' => Date::convertToDatetime($lastmod, 'c'), |
||
66 | 'freq' => (string)$freq, |
||
67 | 'priority' => (float)$priority |
||
68 | ]; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Build xml output and save it into sitemap folder |
||
74 | * @param string $uniqueName |
||
75 | * @return bool |
||
76 | */ |
||
77 | public function save(string $uniqueName): bool |
||
78 | { |
||
79 | // check if data exists |
||
80 | if (!$this->data || !Any::isArray($this->data)) { |
||
81 | return false; |
||
82 | } |
||
83 | |||
84 | // list data each every language, render xml output and write into file |
||
85 | foreach ($this->data as $lang => $items) { |
||
86 | $xml = App::$View->render('sitemap/urlset', [ |
||
87 | 'items' => $items |
||
88 | ]); |
||
89 | |||
90 | File::write(EntityIndexList::INDEX_PATH . '/' . $uniqueName . '.' . $lang . '.xml', $xml); |
||
91 | } |
||
92 | return true; |
||
93 | } |
||
94 | } |
||
95 |