1<?php
2
3namespace ComboStrap\Meta\Field;
4
5use ComboStrap\ExceptionNotFound;
6use ComboStrap\FileSystems;
7use ComboStrap\FirstRasterImage;
8use ComboStrap\MarkupPath;
9use ComboStrap\Meta\Api\Metadata;
10use ComboStrap\Meta\Api\MetadataImage;
11use ComboStrap\Meta\Store\MetadataDokuWikiStore;
12use ComboStrap\PageImageUsage;
13use ComboStrap\WikiPath;
14
15
16class FeaturedRasterImage extends MetadataImage
17{
18
19
20    const PROPERTY_NAME = "featured-raster-image";
21    const FEATURED_IMAGE_PARSED = "featured-raster-image-parsed";
22
23    public static function getComboStrapLogo(): WikiPath
24    {
25        return WikiPath::createComboResource(":images:apple-touch-icon.png");
26    }
27
28    public static function createFromResourcePage(MarkupPath $page): FeaturedRasterImage
29    {
30        return (new FeaturedRasterImage())->setResource($page);
31    }
32
33    static public function getDescription(): string
34    {
35        return "A featured image in raster format";
36    }
37
38    static public function getLabel(): string
39    {
40        return "Featured Raster Image";
41    }
42
43    public static function getName(): string
44    {
45        return self::PROPERTY_NAME;
46    }
47
48    static public function getPersistenceType(): string
49    {
50        return Metadata::PERSISTENT_METADATA;
51    }
52
53    public function setFromStoreValueWithoutException($value): Metadata
54    {
55
56        if ($value === null) {
57            $pageImages = PageImages::createForPage($this->getResource())
58                ->setReadStore($this->getReadStore())
59                ->getValueAsPageImages();
60            foreach ($pageImages as $pageImage) {
61                $wikiPath = $pageImage->getImagePath();
62                try {
63                    $mime = FileSystems::getMime($wikiPath);
64                    if (!$mime->isSupportedRasterImage()) {
65                        continue;
66                    }
67                } catch (ExceptionNotFound $e) {
68                    continue;
69                }
70                $value = $wikiPath->toAbsoluteId();
71                if (in_array(PageImageUsage::ALL, $pageImage->getUsages())) {
72                    break;
73                }
74            }
75        }
76        return parent::setFromStoreValueWithoutException($value);
77
78    }
79
80    static public function isMutable(): bool
81    {
82        return true;
83    }
84
85    public function getDefaultValue(): WikiPath
86    {
87
88        /**
89         * Parsed Feature Images
90         */
91        return $this->getParsedValue();
92
93
94    }
95
96    public function setParsedValue(string $path = null): FeaturedRasterImage
97    {
98        $store = $this->getWriteStore();
99        if ($store instanceof MetadataDokuWikiStore) {
100            $store->setFromPersistentName(self::FEATURED_IMAGE_PARSED, $path);
101        }
102        return $this;
103    }
104
105    /**
106     * @throws ExceptionNotFound
107     */
108    public function getValueOrParsed(): WikiPath
109    {
110        try {
111            return $this->getValue();
112        } catch (ExceptionNotFound $e) {
113            return $this->getParsedValue();
114        }
115    }
116
117    /**
118     * @throws ExceptionNotFound
119     */
120    private function getParsedValue(): WikiPath
121    {
122        /**
123         * @var MarkupPath $markupPath
124         */
125        $markupPath = $this->getResource();
126        $isIndex = $markupPath->isIndexPage();
127        if ($isIndex) {
128            $parsedValue = $this->getReadStore()->getFromName(FirstRasterImage::PROPERTY_NAME);
129        } else {
130            $parsedValue = $this->getReadStore()->getFromName(self::FEATURED_IMAGE_PARSED);
131        }
132        if ($parsedValue === null) {
133            throw new ExceptionNotFound();
134        }
135        return WikiPath::createMediaPathFromPath($parsedValue);
136
137    }
138
139    static public function getDrive(): string
140    {
141        return WikiPath::MEDIA_DRIVE;
142    }
143
144    static public function isOnForm(): bool
145    {
146        return true;
147    }
148
149    public static function getCanonical(): string
150    {
151        return FeaturedImage::getCanonical();
152    }
153
154
155}
156