1<?php
2
3
4namespace ComboStrap\Meta\Field;
5
6
7use ComboStrap\ExceptionNotFound;
8use ComboStrap\Meta\Api\Metadata;
9use ComboStrap\Meta\Store\MetadataDokuWikiStore;
10use ComboStrap\Meta\Api\MetadataText;
11use ComboStrap\MetaManagerForm;
12use ComboStrap\PageTitle;
13use ComboStrap\ResourceName;
14
15class PageH1 extends MetadataText
16{
17
18
19    public const H1_PARSED = "h1_parsed";
20    public const PROPERTY_NAME = "h1";
21
22    public static function createForPage($page): PageH1
23    {
24        return (new PageH1())
25            ->setResource($page);
26    }
27
28    public static function getTab(): string
29    {
30        return MetaManagerForm::TAB_PAGE_VALUE;
31    }
32
33    public static function getDescription(): string
34    {
35        return "The heading 1 (or H1) is the first heading of your page. It may be used in template to make a difference with the title.";
36    }
37
38    public static function getLabel(): string
39    {
40        return "H1 (Heading 1)";
41    }
42
43    static public function getName(): string
44    {
45        return self::PROPERTY_NAME;
46    }
47
48    public static function getPersistenceType(): string
49    {
50        return Metadata::PERSISTENT_METADATA;
51    }
52
53
54    public static function isMutable(): bool
55    {
56        return true;
57    }
58
59    /**
60     * @return string
61     */
62    public function getDefaultValue(): string
63    {
64        $store = $this->getReadStore();
65        if ($store instanceof MetadataDokuWikiStore) {
66            $h1Parsed = $store->getFromName(self::H1_PARSED);
67            if (!empty($h1Parsed)) {
68                return $h1Parsed;
69            }
70            // dokuwiki store title in the current
71            $h1 = $store->getCurrentFromName("title");
72            if (!empty($h1)) {
73                return $h1;
74            }
75        }
76        try {
77            return PageTitle::createForMarkup($this->getResource())
78                ->getValue();
79        } catch (ExceptionNotFound $e) {
80            // ok
81        }
82
83        return ResourceName::createForResource($this->getResource())
84            ->getValueOrDefault();
85
86    }
87
88    /**
89     * @return string
90     */
91    public function getValueOrDefault(): string
92    {
93        try {
94            return $this->getValue();
95        } catch (ExceptionNotFound $e) {
96            return $this->getDefaultValue();
97        }
98    }
99
100
101    public static function getCanonical(): string
102    {
103        return static::getName();
104    }
105
106    public function setDefaultValue(string $defaultValue): PageH1
107    {
108        $store = $this->getWriteStore();
109        if ($store instanceof MetadataDokuWikiStore) {
110            $store->setFromPersistentName(self::H1_PARSED, $defaultValue);
111        }
112        return $this;
113
114    }
115
116
117    public static function isOnForm(): bool
118    {
119        return true;
120    }
121}
122