1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\Meta\Api\Metadata;
8use ComboStrap\Meta\Api\MetadataMultiple;
9
10class PageKeywords extends MetadataMultiple
11{
12
13    public const PROPERTY_NAME = "keywords";
14
15
16    public static function createForPage(MarkupPath $page)
17    {
18        return (new PageKeywords())
19            ->setResource($page);
20    }
21
22    static public function getTab(): string
23    {
24        return MetaManagerForm::TAB_PAGE_VALUE;
25    }
26
27
28    static public function getDataType(): string
29    {
30        // in a form, we send a list of words
31        return DataType::TEXT_TYPE_VALUE;
32    }
33
34
35    static public function getDescription(): string
36    {
37        return "The keywords added to your page (separated by a comma)";
38    }
39
40    static public function getLabel(): string
41    {
42        return "Keywords";
43    }
44
45    static public function getName(): string
46    {
47        return self::PROPERTY_NAME;
48    }
49
50
51    /**
52     * The default of dokuwiki is the recursive parts of all {@link ResourceName page name}
53     * in the hierarchy.
54     * @return string[]|null
55     */
56    public function getDefaultValue(): ?array
57    {
58
59        $resource = $this->getResource();
60        if (!($resource instanceof MarkupPath)) {
61            return null;
62        }
63        $keyWords = explode(" ", $resource->getNameOrDefault());
64        $parentPage = $resource;
65        while (true) {
66            try {
67                $parentPage = $parentPage->getParent();
68            } catch (ExceptionNotFound $e) {
69                break;
70            }
71            if (!$parentPage->isRootHomePage()) {
72                $parentKeyWords = explode(" ", $parentPage->getNameOrDefault());
73                $keyWords = array_merge($keyWords, $parentKeyWords);
74            }
75        }
76        $keyWords = array_map(function ($element) {
77            return strtolower($element);
78        }, $keyWords);
79        return array_unique($keyWords);
80    }
81
82    static public function getPersistenceType(): string
83    {
84        return Metadata::PERSISTENT_METADATA;
85    }
86
87    static public function isMutable(): bool
88    {
89        return true;
90    }
91
92
93    public function setFromStoreValueWithoutException($value): Metadata
94    {
95        try {
96            $this->array = $this->toArrayOrNull($value);
97        } catch (ExceptionCompile $e) {
98            LogUtility::msg($e->getMessage(), LogUtility::LVL_MSG_ERROR, $e->getMessage());
99        }
100        return $this;
101    }
102
103
104    static public function getCanonical(): string
105    {
106        return self::PROPERTY_NAME;
107    }
108
109
110    static public function isOnForm(): bool
111    {
112        return true;
113    }
114}
115