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