Skip to content

Commit b52073a

Browse files
committed
API Move logic from silverstripe/cms into central place
1 parent 6b33b5a commit b52073a

File tree

8 files changed

+360
-39
lines changed

8 files changed

+360
-39
lines changed

src/Model/ModelData.php

+41
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,38 @@ class ModelData
7474

7575
private array $objCache = [];
7676

77+
private $_cache_statusFlags = null;
78+
7779
public function __construct()
7880
{
7981
// no-op
8082
}
8183

84+
/**
85+
* Flags provides the user with additional data about the current page status.
86+
*
87+
* Mostly this is used for versioning, but can be used for other purposes (e.g. localisation).
88+
* Each page can have more than one status flag.
89+
*
90+
* Returns an associative array of a unique key to a (localized) title for the flag.
91+
* The unique key can be reused as a CSS class.
92+
*
93+
* Example (simple):
94+
* "deletedonlive" => "Deleted"
95+
*
96+
* Example (with optional title attribute):
97+
* "deletedonlive" => ['text' => "Deleted", 'title' => 'This page has been deleted']
98+
*/
99+
public function getStatusFlags(bool $cached = true): array
100+
{
101+
if (!$this->_cache_statusFlags || !$cached) {
102+
$flags = [];
103+
$this->extend('updateStatusFlags', $flags);
104+
$this->_cache_statusFlags = $flags;
105+
}
106+
return $this->_cache_statusFlags;
107+
}
108+
82109
// -----------------------------------------------------------------------------------------------------------------
83110

84111
// FIELD GETTERS & SETTERS -----------------------------------------------------------------------------------------
@@ -545,6 +572,20 @@ public function Debug(): ModelData|string
545572
return ModelDataDebugger::create($this);
546573
}
547574

575+
/**
576+
* Clears record-specific cached data.
577+
*
578+
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
579+
* When false will just clear session-local cached data
580+
*/
581+
public function flushCache(bool $persistent = true): static
582+
{
583+
$this->objCacheClear();
584+
$this->_cache_statusFlags = null;
585+
$this->extend('onFlushCache');
586+
return $this;
587+
}
588+
548589
/**
549590
* Generate the cache name for a field
550591
*/

src/ORM/DataObject.php

+34-16
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,19 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
116116
{
117117
/**
118118
* Human-readable singular name.
119-
* @var string
120-
* @config
121119
*/
122-
private static $singular_name = null;
120+
private static ?string $singular_name = null;
123121

124122
/**
125123
* Human-readable plural name
126-
* @var string
127-
* @config
128124
*/
129-
private static $plural_name = null;
125+
private static ?string $plural_name = null;
126+
127+
/**
128+
* Description of the class.
129+
* Used in some areas of the CMS, e.g. when selecting what type of record to create.
130+
*/
131+
private static ?string $class_description = null;
130132

131133
/**
132134
* @config
@@ -946,6 +948,27 @@ public function i18n_plural_name()
946948
return _t(static::class . '.PLURALNAME', $this->plural_name());
947949
}
948950

951+
/**
952+
* Get description for this class
953+
*/
954+
public function classDescription(): ?string
955+
{
956+
return static::config()->get('class_description');
957+
}
958+
959+
/**
960+
* Get localised description for this class
961+
*/
962+
public function i18nClassDescription(): ?string
963+
{
964+
$placeholder = 'PLACEHOLDER_DESCRIPTION';
965+
$description = _t(static::class.'.CLASS_DESCRIPTION', $this->classDescription() ?? $placeholder);
966+
if ($description === $placeholder) {
967+
return null;
968+
}
969+
return $description;
970+
}
971+
949972
/**
950973
* Standard implementation of a title/label for a specific
951974
* record. Tries to find properties 'Title' or 'Name',
@@ -3509,14 +3532,11 @@ public static function get_one($callerClass = null, $filter = "", $cache = true,
35093532
}
35103533

35113534
/**
3512-
* Flush the cached results for all relations (has_one, has_many, many_many)
3513-
* Also clears any cached aggregate data.
3535+
* @inheritDoc
35143536
*
3515-
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
3516-
* When false will just clear session-local cached data
3517-
* @return static $this
3537+
* Also flush the cached results for all relations (has_one, has_many, many_many)
35183538
*/
3519-
public function flushCache($persistent = true)
3539+
public function flushCache(bool $persistent = true): static
35203540
{
35213541
if (static::class == DataObject::class) {
35223542
DataObject::$_cache_get_one = [];
@@ -3530,11 +3550,9 @@ public function flushCache($persistent = true)
35303550
}
35313551
}
35323552

3533-
$this->extend('onFlushCache');
3534-
35353553
$this->components = [];
35363554
$this->eagerLoadedData = [];
3537-
return $this;
3555+
return parent::flushCache($persistent);
35383556
}
35393557

35403558
/**
@@ -3561,7 +3579,7 @@ public static function flush_and_destroy_cache()
35613579
*/
35623580
public static function reset()
35633581
{
3564-
DBEnum::flushCache();
3582+
DBEnum::clearStaticCache();
35653583
ClassInfo::reset_db_cache();
35663584
static::getSchema()->reset();
35673585
DataObject::$_cache_get_one = [];

src/ORM/FieldType/DBEnum.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DBEnum extends DBString
3838
/**
3939
* Clear all cached enum values.
4040
*/
41-
public static function flushCache(): void
41+
public static function clearStaticCache(): void
4242
{
4343
DBEnum::$enum_cache = [];
4444
}
@@ -176,7 +176,7 @@ public function getEnum(): array
176176
* If table or name are not set, or if it is not a valid field on the given table,
177177
* then only known enum values are returned.
178178
*
179-
* Values cached in this method can be cleared via `DBEnum::flushCache();`
179+
* Values cached in this method can be cleared via `DBEnum::clearStaticCache();`
180180
*/
181181
public function getEnumObsolete(): array
182182
{

0 commit comments

Comments
 (0)