1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\Meta\Api\Metadata;
8use ComboStrap\Meta\Api\MetadataText;
9
10class PageType extends MetadataText
11{
12
13
14    /**
15     * @link https://ogp.me/#types Facebook ogp
16     * @link https://www.dublincore.org/specifications/dublin-core/dcmi-terms/#http://purl.org/dc/elements/1.1/type Dublin Core
17     */
18    public const PROPERTY_NAME = "type";
19    public const BLOG_TYPE = "blog";
20    public const WEB_PAGE_TYPE = "webpage";
21    public const ARTICLE_TYPE = "article";
22    public const ORGANIZATION_TYPE = "organization";
23    public const NEWS_TYPE = "news";
24    public const OTHER_TYPE = "other";
25    public const WEBSITE_TYPE = "website";
26    public const HOME_TYPE = "home";
27    public const EVENT_TYPE = "event";
28    /**
29     * Default page type configuration
30     */
31    public const CONF_DEFAULT_PAGE_TYPE = "defaultPageType";
32    public const CONF_DEFAULT_PAGE_TYPE_DEFAULT = PageType::ARTICLE_TYPE;
33
34    public static function createForPage($page): PageType
35    {
36        return (new PageType())
37            ->setResource($page);
38    }
39
40    public function setWriteStore($store): PageType
41    {
42        // Just to return the good type
43        return parent::setWriteStore($store);
44    }
45
46    /**
47     * @return string
48     */
49    public function getValueOrDefault(): string
50    {
51        try {
52            return $this->getValue();
53        } catch (ExceptionNotFound $e) {
54            return $this->getDefaultValue();
55        }
56    }
57
58
59    public function setReadStore($store): PageType
60    {
61        // Just to return the good type
62        return parent::setReadStore($store);
63    }
64
65
66    static public function getTab(): string
67    {
68        return MetaManagerForm::TAB_TYPE_VALUE;
69    }
70
71    static public function getDescription(): string
72    {
73        return "The type of page";
74    }
75
76    static public function getLabel(): string
77    {
78        return "Page Type";
79    }
80
81    static public function getName(): string
82    {
83        return self::PROPERTY_NAME;
84    }
85
86    static public function getPersistenceType(): string
87    {
88        return Metadata::PERSISTENT_METADATA;
89    }
90
91    static public function isMutable(): bool
92    {
93        return true;
94    }
95
96    /**
97     * @return string
98     */
99    public function getDefaultValue(): string
100    {
101        $resource = $this->getResource();
102        if (!($resource instanceof MarkupPath)) {
103            return self::OTHER_TYPE;
104        }
105
106        if ($resource->isRootHomePage()) {
107            return PageType::WEBSITE_TYPE;
108        } else if ($resource->isIndexPage()) {
109            return PageType::HOME_TYPE;
110        } else {
111            $defaultPageTypeConf = SiteConfig::getConfValue(PageType::CONF_DEFAULT_PAGE_TYPE, PageType::CONF_DEFAULT_PAGE_TYPE_DEFAULT);
112            if (!empty($defaultPageTypeConf)) {
113                return $defaultPageTypeConf;
114            } else {
115                return self::ARTICLE_TYPE;
116            }
117        }
118    }
119
120    /**
121     * The canonical for page type
122     */
123    static public function getCanonical(): string
124    {
125        return "page:type";
126    }
127
128
129    public function getPossibleValues(): ?array
130    {
131        $types = [
132            self::ORGANIZATION_TYPE,
133            self::ARTICLE_TYPE,
134            self::NEWS_TYPE,
135            self::BLOG_TYPE,
136            self::WEBSITE_TYPE,
137            self::EVENT_TYPE,
138            self::HOME_TYPE,
139            self::WEB_PAGE_TYPE,
140            self::OTHER_TYPE
141        ];
142        sort($types);
143        return $types;
144    }
145
146    static public function isOnForm(): bool
147    {
148        return true;
149    }
150}
151