1<?php
2
3
4namespace ComboStrap;
5
6
7use ModificationDate;
8use ReplicationDate;
9use Slug;
10
11class MetaManagerForm
12{
13
14    public const TAB_PAGE_VALUE = "page";
15    public const TAB_TYPE_VALUE = "type";
16    public const TAB_CACHE_VALUE = "cache";
17    public const TAB_REDIRECTION_VALUE = "redirection";
18    public const TAB_LANGUAGE_VALUE = "language";
19    public const TAB_INTEGRATION_VALUE = "integration";
20    public const TAB_QUALITY_VALUE = "quality";
21    public const TAB_IMAGE_VALUE = "image";
22    private $page;
23
24    private const FORM_METADATA_LIST = [ResourceName::PROPERTY_NAME,
25        PageTitle::PROPERTY_NAME,
26        PageH1::PROPERTY_NAME,
27        PageDescription::PROPERTY_NAME,
28        PageKeywords::PROPERTY_NAME,
29        PagePath::PROPERTY_NAME,
30        Canonical::PROPERTY_NAME,
31        Slug::PROPERTY_NAME,
32        PageUrlPath::PROPERTY_NAME,
33        PageLayout::PROPERTY_NAME,
34        ModificationDate::PROPERTY_NAME,
35        PageCreationDate::PROPERTY_NAME,
36        PageImages::PROPERTY_NAME,
37        Aliases::PROPERTY_NAME,
38        PageType::PROPERTY_NAME,
39        PagePublicationDate::PROPERTY_NAME,
40        StartDate::PROPERTY_NAME,
41        EndDate::PROPERTY_NAME,
42        LdJson::PROPERTY_NAME,
43        LowQualityPageOverwrite::PROPERTY_NAME,
44        QualityDynamicMonitoringOverwrite::PROPERTY_NAME,
45        Locale::PROPERTY_NAME,
46        Lang::PROPERTY_NAME,
47        Region::PROPERTY_NAME,
48        ReplicationDate::PROPERTY_NAME,
49        PageId::PROPERTY_NAME,
50        CacheExpirationFrequency::PROPERTY_NAME,
51        CacheExpirationDate::PROPERTY_NAME
52    ];
53
54    /**
55     * @var MetadataFormDataStore
56     */
57    private $targetFormDataStore;
58
59    /**
60     * MetaManager constructor.
61     */
62    public function __construct($page)
63    {
64        $this->page = $page;
65        $this->targetFormDataStore = MetadataFormDataStore::getOrCreateFromResource($page);
66    }
67
68    public static function createForPage(Page $page): MetaManagerForm
69    {
70        return new MetaManagerForm($page);
71    }
72
73    /**
74     * @return FormMeta
75     */
76    function toFormMeta(): FormMeta
77    {
78
79        /**
80         * Case when the page was changed externally
81         * with a new frontmatter
82         * The frontmatter data should be first replicated into the metadata file
83         */
84        $instructions = $this->page->getInstructionsDocument();
85        if (!$instructions->shouldProcess()) {
86            $instructions->process();
87        }
88
89        /**
90         * Creation
91         */
92        $formMeta = FormMeta::create($this->page->getDokuwikiId())
93            ->setType(FormMeta::FORM_NAV_TABS_TYPE);
94
95
96        /**
97         * The manager
98         */
99        $dokuwikiFsStore = MetadataDokuWikiStore::getOrCreateFromResource($this->page);
100        foreach (self::FORM_METADATA_LIST as $formsMetaDatum) {
101
102            $metadata = Metadata::getForName($formsMetaDatum);
103            if ($metadata === null) {
104                LogUtility::msg("The metadata ($formsMetaDatum} was not found");
105                continue;
106            }
107            $metadata
108                ->setResource($this->page)
109                ->setReadStore($dokuwikiFsStore)
110                ->buildFromReadStore()
111                ->setWriteStore($this->targetFormDataStore);
112            $formMeta->addFormFieldFromMetadata($metadata);
113        }
114
115
116        /**
117         * Tabs (for whatever reason, javascript keep the order of the properties
118         * and therefore the order of the tabs)
119         */
120        $formMeta
121            ->addTab(
122                FormMetaTab::create(self::TAB_PAGE_VALUE)
123                    ->setLabel("Page")
124                    ->setWidthLabel(3)
125                    ->setWidthField(9)
126            )
127            ->addTab(
128                FormMetaTab::create(self::TAB_TYPE_VALUE)
129                    ->setLabel("Page Type")
130                    ->setWidthLabel(3)
131                    ->setWidthField(9)
132            )
133            ->addTab(
134                FormMetaTab::create(self::TAB_REDIRECTION_VALUE)
135                    ->setLabel("Redirection")
136                    ->setWidthLabel(3)
137                    ->setWidthField(9)
138            )
139            ->addTab(
140                FormMetaTab::create(self::TAB_IMAGE_VALUE)
141                    ->setLabel("Image")
142                    ->setWidthField(12)
143            )
144            ->addTab(
145                FormMetaTab::create(self::TAB_QUALITY_VALUE)
146                    ->setLabel("Quality")
147                    ->setWidthLabel(6)
148                    ->setWidthField(6)
149            )->addTab(
150                FormMetaTab::create(self::TAB_LANGUAGE_VALUE)
151                    ->setLabel("Language")
152                    ->setWidthLabel(2)
153                    ->setWidthField(10)
154            )->addTab(
155                FormMetaTab::create(self::TAB_INTEGRATION_VALUE)
156                    ->setLabel("Integration")
157                    ->setWidthLabel(4)
158                    ->setWidthField(8)
159            )->addTab(
160                FormMetaTab::create(self::TAB_CACHE_VALUE)
161                    ->setLabel("Cache")
162                    ->setWidthLabel(6)
163                    ->setWidthField(6)
164            );
165
166
167        return $formMeta;
168
169    }
170
171
172    public function toFormData(): array
173    {
174        return $this->toFormMeta()->toFormData();
175    }
176
177
178}
179