xref: /template/strap/ComboStrap/PageDescription.php (revision c3437056399326d621a01da73b649707fbb0ae69)
1*c3437056SNickeau<?php
2*c3437056SNickeau
3*c3437056SNickeau
4*c3437056SNickeaunamespace ComboStrap;
5*c3437056SNickeau
6*c3437056SNickeau
7*c3437056SNickeauuse syntax_plugin_combo_frontmatter;
8*c3437056SNickeau
9*c3437056SNickeauclass PageDescription extends MetadataText
10*c3437056SNickeau{
11*c3437056SNickeau    /**
12*c3437056SNickeau     * The description sub key in the dokuwiki meta
13*c3437056SNickeau     * that has the description text
14*c3437056SNickeau     */
15*c3437056SNickeau    const ABSTRACT_KEY = "abstract";
16*c3437056SNickeau    public const PROPERTY_NAME = "description";
17*c3437056SNickeau    const DESCRIPTION_ORIGIN = "origin";
18*c3437056SNickeau    const PLUGIN_DESCRIPTION_META = "plugin_description";
19*c3437056SNickeau
20*c3437056SNickeau
21*c3437056SNickeau    /**
22*c3437056SNickeau     * @var string - the origin of the description
23*c3437056SNickeau     */
24*c3437056SNickeau    private $descriptionOrigin;
25*c3437056SNickeau
26*c3437056SNickeau
27*c3437056SNickeau    public const DESCRIPTION_PROPERTY = "description";
28*c3437056SNickeau    /**
29*c3437056SNickeau     * To indicate from where the description comes
30*c3437056SNickeau     * This is when it's the original dokuwiki description
31*c3437056SNickeau     */
32*c3437056SNickeau    public const DESCRIPTION_DOKUWIKI_ORIGIN = "dokuwiki";
33*c3437056SNickeau    /**
34*c3437056SNickeau     * The origin of the description was set to frontmatter
35*c3437056SNickeau     * due to historic reason to say to it comes from combo
36*c3437056SNickeau     * (You may set it via the metadata manager and get this origin)
37*c3437056SNickeau     */
38*c3437056SNickeau    public const DESCRIPTION_COMBO_ORIGIN = syntax_plugin_combo_frontmatter::CANONICAL;
39*c3437056SNickeau    private $defaultValue;
40*c3437056SNickeau
41*c3437056SNickeau    public static function createForPage($page): PageDescription
42*c3437056SNickeau    {
43*c3437056SNickeau        return (new PageDescription())
44*c3437056SNickeau            ->setResource($page);
45*c3437056SNickeau    }
46*c3437056SNickeau
47*c3437056SNickeau    public function getTab(): string
48*c3437056SNickeau    {
49*c3437056SNickeau        return MetaManagerForm::TAB_PAGE_VALUE;
50*c3437056SNickeau    }
51*c3437056SNickeau
52*c3437056SNickeau    public function getDescription(): string
53*c3437056SNickeau    {
54*c3437056SNickeau        return "The description is a paragraph that describe your page. It's advertised to external application and used in templating.";
55*c3437056SNickeau    }
56*c3437056SNickeau
57*c3437056SNickeau    public function getLabel(): string
58*c3437056SNickeau    {
59*c3437056SNickeau        return "Description";
60*c3437056SNickeau    }
61*c3437056SNickeau
62*c3437056SNickeau    static public function getName(): string
63*c3437056SNickeau    {
64*c3437056SNickeau        return self::DESCRIPTION_PROPERTY;
65*c3437056SNickeau    }
66*c3437056SNickeau
67*c3437056SNickeau    public function getPersistenceType(): string
68*c3437056SNickeau    {
69*c3437056SNickeau        return MetadataDokuWikiStore::PERSISTENT_METADATA;
70*c3437056SNickeau    }
71*c3437056SNickeau
72*c3437056SNickeau    public function getDataType(): string
73*c3437056SNickeau    {
74*c3437056SNickeau        return DataType::PARAGRAPH_TYPE_VALUE;
75*c3437056SNickeau    }
76*c3437056SNickeau
77*c3437056SNickeau
78*c3437056SNickeau    public function getMutable(): bool
79*c3437056SNickeau    {
80*c3437056SNickeau        return true;
81*c3437056SNickeau    }
82*c3437056SNickeau
83*c3437056SNickeau    /**
84*c3437056SNickeau     * @return string|null - the dokuwiki calculated description
85*c3437056SNickeau     *
86*c3437056SNickeau     */
87*c3437056SNickeau    public function getDefaultValue(): ?string
88*c3437056SNickeau    {
89*c3437056SNickeau
90*c3437056SNickeau        $this->buildCheck();
91*c3437056SNickeau        return $this->defaultValue;
92*c3437056SNickeau
93*c3437056SNickeau    }
94*c3437056SNickeau
95*c3437056SNickeau
96*c3437056SNickeau    public function buildFromStoreValue($value): Metadata
97*c3437056SNickeau    {
98*c3437056SNickeau
99*c3437056SNickeau        $metaDataStore = $this->getReadStore();
100*c3437056SNickeau        if (!($metaDataStore instanceof MetadataDokuWikiStore)) {
101*c3437056SNickeau            parent::buildFromStoreValue($value);
102*c3437056SNickeau            return $this;
103*c3437056SNickeau        }
104*c3437056SNickeau
105*c3437056SNickeau        if ($value !== null && is_array($value)) {
106*c3437056SNickeau            $description = $value[self::ABSTRACT_KEY];
107*c3437056SNickeau            if ($description !== null) {
108*c3437056SNickeau                $this->descriptionOrigin = $value[self::DESCRIPTION_ORIGIN];
109*c3437056SNickeau                parent::buildFromStoreValue($description);
110*c3437056SNickeau                return $this;
111*c3437056SNickeau            }
112*c3437056SNickeau        }
113*c3437056SNickeau        /**
114*c3437056SNickeau         * Plugin Plugin Description Integration
115*c3437056SNickeau         */
116*c3437056SNickeau        $value = $metaDataStore->getFromPersistentName(self::PLUGIN_DESCRIPTION_META);
117*c3437056SNickeau        if ($value !== null) {
118*c3437056SNickeau            $keywords = $value["keywords"];
119*c3437056SNickeau            if ($keywords !== null) {
120*c3437056SNickeau                parent::buildFromStoreValue($keywords);
121*c3437056SNickeau                $this->descriptionOrigin = self::PLUGIN_DESCRIPTION_META;
122*c3437056SNickeau                return $this;
123*c3437056SNickeau            }
124*c3437056SNickeau        }
125*c3437056SNickeau
126*c3437056SNickeau        /**
127*c3437056SNickeau         * No description set, null
128*c3437056SNickeau         */
129*c3437056SNickeau        parent::buildFromStoreValue(null);
130*c3437056SNickeau
131*c3437056SNickeau        /**
132*c3437056SNickeau         * Default value is derived from the meta store
133*c3437056SNickeau         * We need to set it at build time because the store may change
134*c3437056SNickeau         * after the build
135*c3437056SNickeau         */
136*c3437056SNickeau        $this->defaultValue = $this->getGeneratedValueFromDokuWikiStore($metaDataStore);
137*c3437056SNickeau
138*c3437056SNickeau        return $this;
139*c3437056SNickeau    }
140*c3437056SNickeau
141*c3437056SNickeau
142*c3437056SNickeau    public function setValue($value): Metadata
143*c3437056SNickeau    {
144*c3437056SNickeau
145*c3437056SNickeau        if ($value === "" || $value === null) {
146*c3437056SNickeau
147*c3437056SNickeau            if ($this->getReadStore() instanceof MetadataDokuWikiStore) {
148*c3437056SNickeau                // we need to know the origin of the actual description
149*c3437056SNickeau                if ($this->descriptionOrigin === null) {
150*c3437056SNickeau                    /**
151*c3437056SNickeau                     * we don't do {@link Metadata::buildCheck() build check} otherwise we get a loop
152*c3437056SNickeau                     * because it will use back this method {@link Metadata::setValue()}
153*c3437056SNickeau                     */
154*c3437056SNickeau                    $this->buildFromReadStore();
155*c3437056SNickeau                }
156*c3437056SNickeau                if ($this->descriptionOrigin === PageDescription::DESCRIPTION_COMBO_ORIGIN) {
157*c3437056SNickeau                    throw new ExceptionCombo("The description cannot be empty", PageDescription::DESCRIPTION_PROPERTY);
158*c3437056SNickeau                } else {
159*c3437056SNickeau                    // The original description is from Dokuwiki, we don't send an error
160*c3437056SNickeau                    // otherwise all page without a first description would get an error
161*c3437056SNickeau                    // (What fucked up is fucked up)
162*c3437056SNickeau                    return $this;
163*c3437056SNickeau                }
164*c3437056SNickeau            }
165*c3437056SNickeau
166*c3437056SNickeau        }
167*c3437056SNickeau
168*c3437056SNickeau        parent::setValue($value);
169*c3437056SNickeau        return $this;
170*c3437056SNickeau    }
171*c3437056SNickeau
172*c3437056SNickeau    /**
173*c3437056SNickeau     * @return string|array
174*c3437056SNickeau     */
175*c3437056SNickeau    public function toStoreValue()
176*c3437056SNickeau    {
177*c3437056SNickeau        $metaDataStore = $this->getWriteStore();
178*c3437056SNickeau        if (!($metaDataStore instanceof MetadataDokuWikiStore)) {
179*c3437056SNickeau            return parent::toStoreValue();
180*c3437056SNickeau        }
181*c3437056SNickeau        /**
182*c3437056SNickeau         * For dokuwiki, this is an array
183*c3437056SNickeau         */
184*c3437056SNickeau        return array(
185*c3437056SNickeau            self::ABSTRACT_KEY => $this->getValue(),
186*c3437056SNickeau            self::DESCRIPTION_ORIGIN => PageDescription::DESCRIPTION_COMBO_ORIGIN
187*c3437056SNickeau        );
188*c3437056SNickeau    }
189*c3437056SNickeau
190*c3437056SNickeau    public function getCanonical(): string
191*c3437056SNickeau    {
192*c3437056SNickeau        return $this->getName();
193*c3437056SNickeau    }
194*c3437056SNickeau
195*c3437056SNickeau    public function getDescriptionOrigin(): string
196*c3437056SNickeau    {
197*c3437056SNickeau        return $this->descriptionOrigin;
198*c3437056SNickeau    }
199*c3437056SNickeau
200*c3437056SNickeau    private function getGeneratedValueFromDokuWikiStore(MetadataDokuWikiStore $metaDataStore): ?string
201*c3437056SNickeau    {
202*c3437056SNickeau
203*c3437056SNickeau        /**
204*c3437056SNickeau         * The generated is in the current metadata
205*c3437056SNickeau         */
206*c3437056SNickeau        $descriptionArray = $metaDataStore->getCurrentFromName(self::PROPERTY_NAME);
207*c3437056SNickeau        if (empty($descriptionArray)) {
208*c3437056SNickeau            return null;
209*c3437056SNickeau        }
210*c3437056SNickeau        if (!array_key_exists(self::ABSTRACT_KEY, $descriptionArray)) {
211*c3437056SNickeau            return null;
212*c3437056SNickeau        }
213*c3437056SNickeau        $value = $descriptionArray[self::ABSTRACT_KEY];
214*c3437056SNickeau
215*c3437056SNickeau
216*c3437056SNickeau        /**
217*c3437056SNickeau         * Dokuwiki description
218*c3437056SNickeau         * With some trick
219*c3437056SNickeau         * TODO: Generate our own description ?
220*c3437056SNickeau         */
221*c3437056SNickeau        // suppress the carriage return
222*c3437056SNickeau        $description = str_replace("\n", " ", $value);
223*c3437056SNickeau        // suppress the h1
224*c3437056SNickeau        $resourceCombo = $this->getResource();
225*c3437056SNickeau        if ($resourceCombo instanceof Page) {
226*c3437056SNickeau            $description = str_replace($resourceCombo->getH1OrDefault(), "", $description);
227*c3437056SNickeau        }
228*c3437056SNickeau        // Suppress the star, the tab, About
229*c3437056SNickeau        $description = preg_replace('/(\*|\t|About)/im', "", $description);
230*c3437056SNickeau        // Suppress all double space and trim
231*c3437056SNickeau        return trim(preg_replace('/  /m', " ", $description));
232*c3437056SNickeau    }
233*c3437056SNickeau
234*c3437056SNickeau
235*c3437056SNickeau}
236