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        if ($fromName === null) {
56            return $this;
57        }
58        $createdMeta = $fromName[self::DOKUWIKI_SUB_KEY] ?? null;
59        if (empty($createdMeta)) {
60            return $this;
61        }
62        // the data in dokuwiki is saved as timestamp
63        $datetime = new DateTime();
64        $datetime->setTimestamp($createdMeta);
65        $this->setValue($datetime);
66        return $this;
67    }
68
69    public function toStoreValue()
70    {
71        $store = $this->getWriteStore();
72        if (!($store instanceof MetadataDokuWikiStore)) {
73            return parent::toStoreValue();
74        }
75        $value = $this->getValue();
76
77        return array(
78            self::DATE_DOKUWIKI_PROPERTY_NAME => [self::DOKUWIKI_SUB_KEY => $value->getTimestamp()]
79        );
80    }
81
82
83    static public function getName(): string
84    {
85        return CreationDate::PROPERTY_NAME;
86    }
87
88
89    public static function getPersistenceType(): string
90    {
91        /**
92         * On windows, the creation time is not preserved when you copy
93         * a file
94         *
95         * If you copy a file from C:\fat16 to D:\NTFS,
96         * it keeps the same modified date and time but changes the created date
97         * and time to the current date and time.
98         * If you move a file from C:\fat16 to D:\NTFS,
99         * it keeps the same modified date and time
100         * and keeps the same created date and time
101         */
102        return Metadata::PERSISTENT_METADATA;
103    }
104
105
106    public static function getTab(): string
107    {
108        return MetaManagerForm::TAB_PAGE_VALUE;
109    }
110
111    static public function getDescription(): string
112    {
113        return "The creation date of the page";
114    }
115
116    static public function getLabel(): string
117    {
118        return "Creation Date";
119    }
120
121    static public function isMutable(): bool
122    {
123        /**
124         * Not sure, It should not be really mutable by the user
125         * but the date should be found in the frontmatter for instance
126         */
127        return false;
128    }
129
130    public static function getCanonical(): string
131    {
132        return Metadata::CANONICAL;
133    }
134
135    public static function isOnForm(): bool
136    {
137        return true;
138    }
139}
140