xref: /plugin/combo/ComboStrap/MetaManagerForm.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\Meta\Api\Metadata;
8use ComboStrap\Meta\Api\MetadataSystem;
9use ComboStrap\Meta\Field\Aliases;
10use ComboStrap\Meta\Field\AncestorImage;
11use ComboStrap\Meta\Field\FacebookImage;
12use ComboStrap\Meta\Field\FeaturedImage;
13use ComboStrap\Meta\Field\SocialCardImage;
14use ComboStrap\Meta\Field\FeaturedRasterImage;
15use ComboStrap\Meta\Field\FeaturedSvgImage;
16use ComboStrap\Meta\Field\PageH1;
17use ComboStrap\Meta\Field\PageImages;
18use ComboStrap\Meta\Field\PageTemplateName;
19use ComboStrap\Meta\Field\Region;
20use ComboStrap\Meta\Field\TwitterImage;
21use ComboStrap\Meta\Form\FormMeta;
22use ComboStrap\Meta\Form\FormMetaTab;
23use ComboStrap\Meta\Store\MetadataDokuWikiStore;
24
25class MetaManagerForm
26{
27
28    public const TAB_PAGE_VALUE = "page";
29    public const TAB_TYPE_VALUE = "type";
30    public const TAB_CACHE_VALUE = "cache";
31    public const TAB_REDIRECTION_VALUE = "redirection";
32    public const TAB_LANGUAGE_VALUE = "language";
33    public const TAB_INTEGRATION_VALUE = "integration";
34    public const TAB_QUALITY_VALUE = "quality";
35    public const TAB_IMAGE_VALUE = "image";
36    private MarkupPath $page;
37
38
39    private const META_ORDERS = [ResourceName::PROPERTY_NAME,
40        PageTitle::PROPERTY_NAME,
41        Lead::PROPERTY_NAME,
42        PageH1::PROPERTY_NAME,
43        Label::PROPERTY_NAME,
44        PageDescription::PROPERTY_NAME,
45        PageKeywords::PROPERTY_NAME,
46        PagePath::PROPERTY_NAME,
47        Canonical::PROPERTY_NAME,
48        Slug::PROPERTY_NAME,
49        PageUrlPath::PROPERTY_NAME,
50        PageTemplateName::PROPERTY_NAME,
51        ModificationDate::PROPERTY_NAME,
52        CreationDate::PROPERTY_NAME,
53        FeaturedImage::PROPERTY_NAME,
54        FeaturedRasterImage::PROPERTY_NAME,
55        FeaturedSvgImage::PROPERTY_NAME,
56        FeaturedIcon::PROPERTY_NAME,
57        TwitterImage::PROPERTY_NAME,
58        FacebookImage::PROPERTY_NAME,
59        AncestorImage::PROPERTY_NAME,
60        FirstImage::PROPERTY_NAME,
61        Aliases::PROPERTY_NAME,
62        PageType::PROPERTY_NAME,
63        PagePublicationDate::PROPERTY_NAME,
64        StartDate::PROPERTY_NAME,
65        EndDate::PROPERTY_NAME,
66        LdJson::PROPERTY_NAME,
67        LowQualityPageOverwrite::PROPERTY_NAME,
68        QualityDynamicMonitoringOverwrite::PROPERTY_NAME,
69        Locale::PROPERTY_NAME,
70        Lang::PROPERTY_NAME,
71        Region::PROPERTY_NAME,
72        ReplicationDate::PROPERTY_NAME,
73        PageId::PROPERTY_NAME,
74        CacheExpirationFrequency::PROPERTY_NAME,
75        CacheExpirationDate::PROPERTY_NAME,
76        PageLevel::PROPERTY_NAME
77    ];
78
79    /**
80     * @var MetadataFormDataStore
81     */
82    private $targetFormDataStore;
83
84    /**
85     * MetaManager constructor.
86     */
87    public function __construct($page)
88    {
89        $this->page = $page;
90        $this->targetFormDataStore = MetadataFormDataStore::getOrCreateFromResource($page);
91    }
92
93    public static function createForPage(MarkupPath $page): MetaManagerForm
94    {
95        return new MetaManagerForm($page);
96    }
97
98    /**
99     * @return FormMeta
100     */
101    function toFormMeta(): FormMeta
102    {
103
104        /**
105         * Case when the page was changed externally
106         * with a new frontmatter
107         * The frontmatter data should be first replicated into the metadata file
108         */
109        $fetcherMarkup = $this->page->getInstructionsDocument();
110        $fetcherMarkup->getInstructions();
111
112
113        /**
114         * Creation
115         */
116        $name = $this->page->getPathObject()->toAbsoluteId();
117        $formMeta = FormMeta::create($name)
118            ->setType(FormMeta::FORM_NAV_TABS_TYPE);
119
120
121        /**
122         * The manager
123         */
124        $dokuwikiFsStore = MetadataDokuWikiStore::getOrCreateFromResource($this->page);
125        $metadataNameInOrder = self::META_ORDERS;
126
127
128        foreach ($metadataNameInOrder as $metadataName) {
129            try {
130                $metadataObject = MetadataSystem::getForName($metadataName);
131            } catch (ExceptionNotFound $e) {
132                LogUtility::internalError("The metadata ($metadataName) was not found");
133                continue;
134            }
135            if(!$metadataObject::isOnForm()){
136                LogUtility::internalError("This metadata should not be on the order list as it's not for the form");
137                continue;
138            }
139            $metadataObject
140                ->setResource($this->page)
141                ->setReadStore($dokuwikiFsStore)
142                ->buildFromReadStore()
143                ->setWriteStore($this->targetFormDataStore);
144            $formMeta->addFormFieldFromMetadata($metadataObject);
145        }
146
147        /**
148         * Metadata that are not in the order list
149         */
150        foreach (MetadataSystem::getMetadataClasses() as $metadata) {
151            if (!$metadata::isOnForm()) {
152                continue;
153            }
154            if (!in_array($metadata::getName(), $metadataNameInOrder)) {
155                $metadataObject = (new $metadata())
156                    ->setResource($this->page)
157                    ->setReadStore($dokuwikiFsStore)
158                    ->buildFromReadStore()
159                    ->setWriteStore($this->targetFormDataStore);
160                $formMeta->addFormFieldFromMetadata($metadataObject);
161            }
162        }
163
164
165        /**
166         * Tabs (for whatever reason, javascript keep the order of the properties
167         * and therefore the order of the tabs)
168         */
169        $formMeta
170            ->addTab(
171                FormMetaTab::create(self::TAB_PAGE_VALUE)
172                    ->setLabel("Page")
173                    ->setWidthLabel(3)
174                    ->setWidthField(9)
175            )
176            ->addTab(
177                FormMetaTab::create(self::TAB_TYPE_VALUE)
178                    ->setLabel("Page Type")
179                    ->setWidthLabel(3)
180                    ->setWidthField(9)
181            )
182            ->addTab(
183                FormMetaTab::create(self::TAB_REDIRECTION_VALUE)
184                    ->setLabel("Redirection")
185                    ->setWidthLabel(3)
186                    ->setWidthField(9)
187            )
188            ->addTab(
189                FormMetaTab::create(self::TAB_IMAGE_VALUE)
190                    ->setLabel("Images")
191                    ->setWidthLabel(3)
192                    ->setWidthField(9)
193            )
194            ->addTab(
195                FormMetaTab::create(self::TAB_QUALITY_VALUE)
196                    ->setLabel("Quality")
197                    ->setWidthLabel(6)
198                    ->setWidthField(6)
199            )->addTab(
200                FormMetaTab::create(self::TAB_LANGUAGE_VALUE)
201                    ->setLabel("Language")
202                    ->setWidthLabel(2)
203                    ->setWidthField(10)
204            )->addTab(
205                FormMetaTab::create(self::TAB_INTEGRATION_VALUE)
206                    ->setLabel("Integration")
207                    ->setWidthLabel(4)
208                    ->setWidthField(8)
209            )->addTab(
210                FormMetaTab::create(self::TAB_CACHE_VALUE)
211                    ->setLabel("Cache")
212                    ->setWidthLabel(6)
213                    ->setWidthField(6)
214            );
215
216
217        return $formMeta;
218
219    }
220
221
222    public function toFormData(): array
223    {
224        return $this->toFormMeta()->toFormData();
225    }
226
227
228}
229