1<?php
2
3namespace ComboStrap\Meta\Field;
4
5use ComboStrap\ExceptionNotFound;
6use ComboStrap\FileSystems;
7use ComboStrap\MarkupPath;
8use ComboStrap\Meta\Api\Metadata;
9use ComboStrap\Meta\Api\MetadataImage;
10use ComboStrap\Meta\Store\MetadataDokuWikiStore;
11use ComboStrap\PageImageUsage;
12use ComboStrap\ResourceCombo;
13use ComboStrap\Site;
14use ComboStrap\WikiPath;
15
16/**
17 * A field that derived the featured image for the html page/blog
18 *
19 * (The featured image for a html page may be a svg
20 * while for a social network, it should not)
21 *
22 * This meta returns the first svg image found
23 * otherwise the raster one
24 */
25class FeaturedImage extends MetadataImage
26{
27
28
29    const PROPERTY_NAME = "featured-image";
30
31
32    public static function createFromResourcePage(MarkupPath $page): FeaturedImage
33    {
34        return (new FeaturedImage())->setResource($page);
35    }
36
37    static public function getDescription(): string
38    {
39        return "The image for a page/blog";
40    }
41
42    static public function getLabel(): string
43    {
44        return "Featured Image";
45    }
46
47    public static function getName(): string
48    {
49        return self::PROPERTY_NAME;
50    }
51
52    static public function getPersistenceType(): string
53    {
54        return Metadata::DERIVED_METADATA;
55    }
56
57
58    static public function isMutable(): bool
59    {
60        return false;
61    }
62
63    public function getValue(): WikiPath
64    {
65
66        $contextPage = $this->getResource();
67        return $this->getFeaturedImageBlogForContext($contextPage);
68
69    }
70
71    /**
72     * The image may not be the first otherwise, it will make a duplicate
73     * @param ResourceCombo $contextPage
74     * @return WikiPath
75     * @throws ExceptionNotFound
76     */
77    private function getFeaturedImageBlogForContext(ResourceCombo $contextPage): WikiPath
78    {
79        $featuredSvgImage = FeaturedSvgImage::createFromResourcePage($contextPage);
80        $featuredRasterImage = FeaturedRasterImage::createFromResourcePage($contextPage);
81        try {
82            return $featuredSvgImage->getValue();
83        } catch (ExceptionNotFound $e) {
84            try {
85                return $featuredRasterImage->getValue();
86            } catch (ExceptionNotFound $e) {
87                try {
88                    return $featuredSvgImage->getDefaultValue();
89                } catch (ExceptionNotFound $e) {
90                    return $featuredRasterImage->getDefaultValue();
91                }
92            }
93        }
94    }
95
96    static public function getDrive(): string
97    {
98        return WikiPath::MEDIA_DRIVE;
99    }
100
101    static public function isOnForm(): bool
102    {
103        return true;
104    }
105
106    public static function getCanonical(): string
107    {
108        return self::PROPERTY_NAME;
109    }
110
111
112}
113