1<?php
2
3
4namespace ComboStrap;
5
6
7class PageUrlType extends MetadataText
8{
9
10    public const CONF_CANONICAL_URL_TYPE = "pageUrlType";
11    public const CONF_CANONICAL_URL_TYPE_DEFAULT = self::CONF_VALUE_PAGE_PATH;
12    public const CONF_VALUE_PAGE_PATH = "page path";
13    private static $urlTypeInstanceCache = [];
14    public const CONF_VALUE_SLUG = "slug";
15    public const CONF_VALUE_CANONICAL_PATH = "canonical path";
16    public const CONF_VALUE_HIERARCHICAL_SLUG = "hierarchical slug";
17    public const CONF_VALUE_PERMANENT_PAGE_PATH = "permanent page path";
18    public const CONF_VALUE_PERMANENT_CANONICAL_PATH = "permanent canonical path";
19    public const CONF_VALUE_HOMED_SLUG = "homed slug";
20    public const CONF_VALUES = [
21        PageUrlType::CONF_VALUE_PAGE_PATH,
22        PageUrlType::CONF_VALUE_PERMANENT_PAGE_PATH,
23        PageUrlType::CONF_VALUE_CANONICAL_PATH,
24        PageUrlType::CONF_VALUE_PERMANENT_CANONICAL_PATH,
25        PageUrlType::CONF_VALUE_SLUG,
26        PageUrlType::CONF_VALUE_HOMED_SLUG,
27        PageUrlType::CONF_VALUE_HIERARCHICAL_SLUG
28    ];
29
30
31    public static function getOrCreateForPage(ResourceCombo $page): PageUrlType
32    {
33        $path = $page->getPath()->toString();
34        $urlType = self::$urlTypeInstanceCache[$path];
35        if ($urlType === null) {
36            $urlType = self::createFromPage($page);
37            self::$urlTypeInstanceCache[$path] = $urlType;
38        }
39        return $urlType;
40
41    }
42
43    public static function createFromPage(ResourceCombo $page): PageUrlType
44    {
45        return (new PageUrlType())
46            ->setResource($page);
47    }
48
49    public function getValue(): ?string
50    {
51
52        $resourceCombo = $this->getResource();
53        if (!$resourceCombo->exists()) {
54            return PageUrlType::CONF_VALUE_PAGE_PATH;
55        }
56        if (!($resourceCombo instanceof Page)) {
57            LogUtility::msg("The page type is only for page");
58            return PageUrlType::CONF_VALUE_PAGE_PATH;
59        }
60
61        $confCanonicalType = $this->getName();
62        $confDefaultValue = $this->getDefaultValue();
63        $urlType = PluginUtility::getConfValue($confCanonicalType, $confDefaultValue);
64        if (!in_array($urlType, self::CONF_VALUES)) {
65            LogUtility::msg("The canonical configuration ($confCanonicalType) value ($urlType) is unknown and was set to the default one", LogUtility::LVL_MSG_ERROR, PageUrlPath::PROPERTY_NAME);
66            return $confDefaultValue;
67        }
68
69        return $urlType;
70
71
72    }
73
74
75    public function getTab(): string
76    {
77        return MetaManagerForm::TAB_PAGE_VALUE;
78    }
79
80    public function getDescription(): string
81    {
82        return "The type of Url for pages";
83    }
84
85    public function getLabel(): string
86    {
87        return "Page Url";
88    }
89
90    static public function getName(): string
91    {
92        return PageUrlType::CONF_CANONICAL_URL_TYPE;
93    }
94
95    public function getPersistenceType(): string
96    {
97        return Metadata::PERSISTENT_METADATA;
98    }
99
100    public function getMutable(): bool
101    {
102        return true;
103    }
104
105    public function getDefaultValue(): string
106    {
107        return PageUrlType::CONF_CANONICAL_URL_TYPE_DEFAULT;
108    }
109}
110