1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16use ComboStrap\Meta\Api\Metadata;
17use ComboStrap\Meta\Api\MetadataDateTime;
18use ComboStrap\Meta\Store\MetadataDokuWikiStore;
19use DateTime;
20
21/**
22 *
23 * Publication Date
24 * @package ComboStrap
25 *
26 */
27class PagePublicationDate extends MetadataDateTime
28{
29
30    /**
31     * The key that contains the published date
32     */
33    const PROPERTY_NAME = "date_published";
34    const OLD_META_KEY = "published";
35
36    /**
37     * Late publication protection
38     */
39    const LATE_PUBLICATION_PROTECTION_ACRONYM = "lpp";
40    const CONF_LATE_PUBLICATION_PROTECTION_MODE = "latePublicationProtectionMode";
41    const CONF_LATE_PUBLICATION_PROTECTION_ENABLE = "latePublicationProtectionEnable";
42    const LATE_PUBLICATION_CLASS_PREFIX_NAME = "late-publication";
43
44
45    public static function getLatePublicationProtectionMode()
46    {
47
48        if (SiteConfig::getConfValue(PagePublicationDate::CONF_LATE_PUBLICATION_PROTECTION_ENABLE, true)) {
49            return SiteConfig::getConfValue(PagePublicationDate::CONF_LATE_PUBLICATION_PROTECTION_MODE);
50        } else {
51            return false;
52        }
53
54    }
55
56    public static function isLatePublicationProtectionEnabled()
57    {
58        return SiteConfig::getConfValue(PagePublicationDate::CONF_LATE_PUBLICATION_PROTECTION_ENABLE, true);
59    }
60
61    public static function createFromPage(MarkupPath $page)
62    {
63        return (new PagePublicationDate())
64            ->setResource($page);
65    }
66
67
68    static public function getTab(): string
69    {
70        return MetaManagerForm::TAB_TYPE_VALUE;
71    }
72
73    static public function getDescription(): string
74    {
75        return "The publication date";
76    }
77
78    static public function getLabel(): string
79    {
80        return "Publication Date";
81    }
82
83    static public function getName(): string
84    {
85        return self::PROPERTY_NAME;
86    }
87
88
89    public function setFromStoreValueWithoutException($value): Metadata
90    {
91        $store = $this->getReadStore();
92        if (!($store instanceof MetadataDokuWikiStore)) {
93            return parent::setFromStoreValueWithoutException($value);
94        }
95
96        if ($value === null) {
97            /**
98             * Old metadata key
99             */
100            $value = $store->getFromName(PagePublicationDate::OLD_META_KEY);
101        }
102
103        try {
104            $this->dateTimeValue = $this->fromPersistentDateTimeUtility($value);
105        } catch (ExceptionCompile $e) {
106            LogUtility::msg($e->getMessage(), LogUtility::LVL_MSG_ERROR, $e->getCanonical());
107        }
108
109        return $this;
110    }
111
112
113    static public function getPersistenceType(): string
114    {
115        return Metadata::PERSISTENT_METADATA;
116    }
117
118    static public function isMutable(): bool
119    {
120        return true;
121    }
122
123    public function getDefaultValue(): DateTime
124    {
125        $lastName = $this->getResource()->getPathObject()->getLastNameWithoutExtension();
126        $result = preg_match("/(\d{4}-\d{2}-\d{2}).*/i", $lastName, $matches);
127        if ($result === 1) {
128            $date = $matches[1];
129            try {
130                return Iso8601Date::createFromString($date)->getDateTime();
131            } catch (ExceptionBadSyntax $e) {
132                // should not happen
133                LogUtility::error("Internal Error: the date format is not valid. Error: {$e->getMessage()}", self::CANONICAL);
134            }
135        }
136
137        return CreationDate::create()
138            ->setResource($this->getResource())
139            ->getValueOrDefault();
140    }
141
142    static public function getOldPersistentNames(): array
143    {
144        return [PagePublicationDate::OLD_META_KEY];
145    }
146
147    static public function getCanonical(): string
148    {
149        return "published";
150    }
151
152
153    static public function isOnForm(): bool
154    {
155        return true;
156    }
157}
158