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
16/**
17 *
18 * Publication Date
19 * @package ComboStrap
20 *
21 */
22class PagePublicationDate extends MetadataDateTime
23{
24
25    /**
26     * The key that contains the published date
27     */
28    const PROPERTY_NAME = "date_published";
29    const OLD_META_KEY = "published";
30
31    /**
32     * Late publication protection
33     */
34    const LATE_PUBLICATION_PROTECTION_ACRONYM = "lpp";
35    const CONF_LATE_PUBLICATION_PROTECTION_MODE = "latePublicationProtectionMode";
36    const CONF_LATE_PUBLICATION_PROTECTION_ENABLE = "latePublicationProtectionEnable";
37    const LATE_PUBLICATION_CLASS_NAME = "late-publication";
38
39
40    public static function getLatePublicationProtectionMode()
41    {
42
43        if (PluginUtility::getConfValue(PagePublicationDate::CONF_LATE_PUBLICATION_PROTECTION_ENABLE, true)) {
44            return PluginUtility::getConfValue(PagePublicationDate::CONF_LATE_PUBLICATION_PROTECTION_MODE);
45        } else {
46            return false;
47        }
48
49    }
50
51    public static function isLatePublicationProtectionEnabled()
52    {
53        return PluginUtility::getConfValue(PagePublicationDate::CONF_LATE_PUBLICATION_PROTECTION_ENABLE, true);
54    }
55
56    public static function createFromPage(Page $page)
57    {
58        return (new PagePublicationDate())
59            ->setResource($page);
60    }
61
62
63    public function getTab(): string
64    {
65        return MetaManagerForm::TAB_TYPE_VALUE;
66    }
67
68    public function getDescription(): string
69    {
70        return "The publication date";
71    }
72
73    public function getLabel(): string
74    {
75        return "Publication Date";
76    }
77
78    static public function getName(): string
79    {
80        return self::PROPERTY_NAME;
81    }
82
83
84    public function buildFromStoreValue($value): Metadata
85    {
86        $store = $this->getReadStore();
87        if (!($store instanceof MetadataDokuWikiStore)) {
88            return parent::buildFromStoreValue($value);
89        }
90
91        if ($value === null) {
92            /**
93             * Old metadata key
94             */
95            $value = $store->getFromPersistentName(PagePublicationDate::OLD_META_KEY);
96        }
97
98        try {
99            $this->dateTimeValue = $this->fromPersistentDateTimeUtility($value);
100        } catch (ExceptionCombo $e) {
101            LogUtility::msg($e->getMessage(), LogUtility::LVL_MSG_ERROR, $e->getCanonical());
102        }
103
104        return $this;
105    }
106
107
108    public function getPersistenceType(): string
109    {
110        return Metadata::PERSISTENT_METADATA;
111    }
112
113    public function getMutable(): bool
114    {
115        return true;
116    }
117
118    public function getDefaultValue(): ?\DateTime
119    {
120        return PageCreationDate::create()
121            ->setResource($this->getResource())
122            ->getValueOrDefault();
123    }
124
125    static public function getOldPersistentNames(): array
126    {
127        return [PagePublicationDate::OLD_META_KEY];
128    }
129
130    public function getCanonical(): string
131    {
132        return "published";
133    }
134
135
136}
137