xref: /plugin/combo/ComboStrap/CreationDate.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\Meta\Api\Metadata;
8use ComboStrap\Meta\Api\MetadataDateTime;
9use ComboStrap\Meta\Store\MetadataDokuWikiStore;
10use DateTime;
11
12/**
13 * Class CacheExpirationFrequencyMeta
14 * @package ComboStrap
15 * Represents the creation date of a resource
16 */
17class CreationDate extends MetadataDateTime
18{
19
20
21    public const PROPERTY_NAME = 'date_created';
22    const DATE_DOKUWIKI_PROPERTY_NAME = 'date';
23    const DOKUWIKI_SUB_KEY = 'created';
24
25
26    public static function createForPage(ResourceCombo $page): CreationDate
27    {
28        return (new CreationDate())
29            ->setResource($page);
30    }
31
32    public static function create(): CreationDate
33    {
34        return new CreationDate();
35    }
36
37    public function getDefaultValue(): DateTime
38    {
39        $path = $this->getResource()->getPathObject();
40        return FileSystems::getCreationTime($path);
41    }
42
43    /**
44     */
45    public function buildFromReadStore(): MetadataDateTime
46    {
47
48        $store = $this->getReadStore();
49
50        if (!($store instanceof MetadataDokuWikiStore)) {
51            return parent::buildFromReadStore();
52        }
53
54        $fromName = $store->getFromName(self::DATE_DOKUWIKI_PROPERTY_NAME);
55        $createdMeta = $fromName[self::DOKUWIKI_SUB_KEY];
56        if (empty($createdMeta)) {
57            return $this;
58        }
59        // the data in dokuwiki is saved as timestamp
60        $datetime = new DateTime();
61        $datetime->setTimestamp($createdMeta);
62        $this->setValue($datetime);
63        return $this;
64    }
65
66    public function toStoreValue()
67    {
68        $store = $this->getWriteStore();
69        if (!($store instanceof MetadataDokuWikiStore)) {
70            return parent::toStoreValue();
71        }
72        $value = $this->getValue();
73
74        return array(
75            self::DATE_DOKUWIKI_PROPERTY_NAME => [self::DOKUWIKI_SUB_KEY => $value->getTimestamp()]
76        );
77    }
78
79
80    static public function getName(): string
81    {
82        return CreationDate::PROPERTY_NAME;
83    }
84
85
86    public static function getPersistenceType(): string
87    {
88        /**
89         * On windows, the creation time is not preserved when you copy
90         * a file
91         *
92         * If you copy a file from C:\fat16 to D:\NTFS,
93         * it keeps the same modified date and time but changes the created date
94         * and time to the current date and time.
95         * If you move a file from C:\fat16 to D:\NTFS,
96         * it keeps the same modified date and time
97         * and keeps the same created date and time
98         */
99        return Metadata::PERSISTENT_METADATA;
100    }
101
102
103    public static function getTab(): string
104    {
105        return MetaManagerForm::TAB_PAGE_VALUE;
106    }
107
108    static public function getDescription(): string
109    {
110        return "The creation date of the page";
111    }
112
113    static public function getLabel(): string
114    {
115        return "Creation Date";
116    }
117
118    static public function isMutable(): bool
119    {
120        /**
121         * Not sure, It should not be really mutable by the user
122         * but the date should be found in the frontmatter for instance
123         */
124        return false;
125    }
126
127    public static function getCanonical(): string
128    {
129        return Metadata::CANONICAL;
130    }
131
132    public static function isOnForm(): bool
133    {
134        return true;
135    }
136}
137