1<?php
2
3
4namespace ComboStrap;
5
6
7class ResourceName extends MetadataText
8{
9
10
11    public const PROPERTY_NAME = "name";
12
13    public static function createForResource($page): ResourceName
14    {
15        return (new ResourceName())
16            ->setResource($page);
17    }
18
19    public function getTab(): string
20    {
21        return MetaManagerForm::TAB_PAGE_VALUE;
22    }
23
24    public function getDescription(): string
25    {
26        $resourceCombo = $this->getResource();
27        $resourceType = $resourceCombo->getType();
28        $desc = "The $resourceType name is the shortest $resourceType description. It should be at maximum a couple of words long.";
29        if ($resourceType === Page::TYPE) {
30            $desc = $desc . " It's used mainly in navigational components.";
31        }
32        return $desc;
33    }
34
35    public function getLabel(): string
36    {
37        return "Name";
38    }
39
40    static public function getName(): string
41    {
42        return self::PROPERTY_NAME;
43    }
44
45    public function getPersistenceType(): string
46    {
47        return MetadataDokuWikiStore::PERSISTENT_METADATA;
48    }
49
50    public function getMutable(): bool
51    {
52        return true;
53    }
54
55    public function getDefaultValue(): string
56    {
57
58        $resourceCombo = $this->getResource();
59
60        $pathName = $resourceCombo->getPath()->getLastNameWithoutExtension();
61        switch ($resourceCombo->getType()) {
62
63            case Page::TYPE:
64                /**
65                 * If this is a home page, the default
66                 * is the parent path name
67                 */
68                if ($pathName === Site::getHomePageName()) {
69                    $names = $resourceCombo->getPath()->getNames();
70                    $namesCount = sizeof($names);
71                    if ($namesCount >= 2) {
72                        $pathName = $names[$namesCount - 2];
73                    }
74                }
75                break;
76
77        }
78
79        $words = preg_split("/\s/", preg_replace("/-|_/", " ", $pathName));
80        $wordsUc = [];
81        foreach ($words as $word) {
82            $wordsUc[] = ucfirst($word);
83        }
84        return implode(" ", $wordsUc);
85    }
86
87    public function getCanonical(): string
88    {
89        return $this->getName();
90    }
91
92
93}
94