change Asset->status to ->state for consistency

This commit is contained in:
crispycat 2024-09-28 02:54:55 -04:00
parent b8c302a50e
commit fc28a5d2cc
7 changed files with 52 additions and 52 deletions

View File

@ -18,11 +18,11 @@
use \Crispage\Auth\CorePermissions;
use \Crispage\Exceptions\APIException;
class APIAssetStatusAction extends \Crispage\Framework\Action
class APIAssetStateAction extends \Crispage\Framework\Action
implements \Crispage\Response\APIPermissions {
public static function getExtensionInfo(): array {
return [
"id" => "crispage.actions.api.assetstatus",
"id" => "crispage.actions.api.assetstate",
"version" => VERSION
];
}
@ -46,7 +46,7 @@
if (!$asset) throw new APIException("Not found", 404);
// Output
$out = ["_status" => $asset->_status];
$out = ["_state" => $asset->_state];
break;
}
@ -68,17 +68,17 @@
throw new APIException("Unauthorized", 401);
if ($asset->isTemporary())
throw new APIException("Cannot set status of temporary asset", 403);
throw new APIException("Cannot set state of temporary asset", 403);
$status = intval($this->app->request->json["status"] ?? 0);
$state = intval($this->app->request->json["state"] ?? 0);
if ($status != Asset::STATUS_TRASHED && $status != Asset::STATUS_PERMANENT)
throw new APIException("Invalid status", 400);
if ($state != Asset::STATE_TRASHED && $state != Asset::STATE_PERMANENT)
throw new APIException("Invalid state", 400);
// Change status
$asset = $this->app->assets->setStatus($asset, $status);
// Change state
$asset = $this->app->assets->setState($asset, $state);
$out = ["_status" => $asset->_status];
$out = ["_state" => $asset->_state];
break;
}

View File

@ -37,7 +37,7 @@
// Get asset
$this->data["asset_id"] = intval($this->app->request->params["asset_id"] ?? 0);
$this->data["asset"] = $this->app->assets->get($this->data["asset_id"], Asset::STATUS_TRASHED);
$this->data["asset"] = $this->app->assets->get($this->data["asset_id"], Asset::STATE_TRASHED);
// Check that asset exists
if (!$this->data["asset"]) {

View File

@ -27,10 +27,10 @@
}
public function run(): void {
$res = $this->app->database->select("assets", ["id"], ["status" => Asset::STATUS_TRASHED]);
$res = $this->app->database->select("assets", ["id"], ["state" => Asset::STATE_TRASHED]);
$assets = [];
while ($id = $res->fetchColumn()) {
$asset = $this->app->assets->get($id, Asset::STATUS_TRASHED);
$asset = $this->app->assets->get($id, Asset::STATE_TRASHED);
if ($asset) $assets[] = $asset;
}

View File

@ -170,7 +170,7 @@
return true;
}
private function constructAsset(string $table, string $class, int $id, int $status = Asset::STATUS_TEMPORARY): ?Asset {
private function constructAsset(string $table, string $class, int $id, int $state = Asset::STATE_TEMPORARY): ?Asset {
// Check class is valid
if (!class_exists($class))
throw new AssetException("Class $class does not exist");
@ -185,28 +185,28 @@
if (!$row) return null;
// Return asset from data
$row["_status"] = $status;
$row["_state"] = $state;
$row["_table"] = $table;
return new $class($this->app, $row);
}
public function get(int $id, ?int $req_status = Asset::STATUS_PERMANENT): ?Asset {
public function get(int $id, ?int $req_state = Asset::STATE_PERMANENT): ?Asset {
// Get asset info from table
$res = $this->app->database->select("assets", null, ["id" => $id]);
$row = $res->fetch();
// Check that data exists
if (!$row) return null;
// Check status matches
if ($req_status !== null && $row["status"] != $req_status) return null;
// Check state matches
if ($req_state !== null && $row["state"] != $req_state) return null;
// Return asset
return $this->constructAsset(
$row["tablename"], $row["classname"], $id, $row["status"]
$row["tablename"], $row["classname"], $id, $row["state"]
);
}
public function getBySlug(string $classname, string $slug, ?int $req_status = Asset::STATUS_PERMANENT): ?Asset {
public function getBySlug(string $classname, string $slug, ?int $req_state = Asset::STATE_PERMANENT): ?Asset {
// Get asset info from table
$res = $this->app->database->select(
"assets", null, ["classname" => $classname, "slug" => $slug]
@ -215,16 +215,16 @@
// Check that data exists
if (!$row) return null;
// Check status matches
if ($req_status !== null && $row["status"] != $req_status) return null;
// Check state matches
if ($req_state !== null && $row["state"] != $req_state) return null;
// Return asset
return $this->constructAsset(
$row["tablename"], $row["classname"], $row["id"], $row["status"]
$row["tablename"], $row["classname"], $row["id"], $row["state"]
);
}
public function getAll(string $classname = null, ?int $req_status = Asset::STATUS_PERMANENT): \Generator {
public function getAll(string $classname = null, ?int $req_state = Asset::STATE_PERMANENT): \Generator {
// Get asset info from table
$res = $this->app->database->select(
"assets", null,
@ -233,18 +233,18 @@
// Walk through and yield assets
while (($row = $res->fetch()) !== false) {
// Check status matches
if ($req_status !== null && $row["status"] != $req_status) continue;
// Check state matches
if ($req_state !== null && $row["state"] != $req_state) continue;
yield $this->constructAsset(
$row["tablename"], $row["classname"], $row["id"], $row["status"]
$row["tablename"], $row["classname"], $row["id"], $row["state"]
);
}
}
public function getAllFiltered(
string $classname, array $filters = [], string $sortby = null,
bool $desc = false, ?int $req_status = Asset::STATUS_PERMANENT
bool $desc = false, ?int $req_state = Asset::STATE_PERMANENT
): \Generator {
// Check class is valid
if (!class_exists($classname))
@ -265,10 +265,10 @@
$ares = $this->app->database->select("assets", null, ["id" => $row["id"]]);
$arow = $ares->fetch();
if (!$arow) continue;
// Check status matches
if ($req_status !== null && $arow["status"] != $req_status) continue;
// Check state matches
if ($req_state !== null && $arow["state"] != $req_state) continue;
// Yield asset
$row["_status"] = $arow["status"];
$row["_state"] = $arow["state"];
$row["_table"] = $table;
yield new $classname($this->app, $row);
}
@ -292,7 +292,7 @@
if (!is_a($classname, "\\Crispage\\Framework\\Asset", true))
throw new AssetException("Class $classname is not an Asset");
$data["_status"] = ($temp) ? Asset::STATUS_TEMPORARY : Asset::STATUS_PERMANENT;
$data["_state"] = ($temp) ? Asset::STATE_TEMPORARY : Asset::STATE_PERMANENT;
$data["_table"] = $this->getTable($classname);
$data["slug"] ??= uniqid("asset_");
@ -318,7 +318,7 @@
"classname" => "\\" . $asset::class,
"tablename" => $asset->_table,
"slug" => $asset->slug,
"status" => $asset->_status
"state" => $asset->_state
]);
$this->app->database->insert(
$asset->_table, $asset->toArray()
@ -368,19 +368,19 @@
);
}
private function setStatus(Asset $asset, int $status): Asset {
private function setState(Asset $asset, int $state): Asset {
// Update the asset in the assets table
$this->app->database->update(
"assets", ["status" => $status], ["id" => $asset->id]
"assets", ["state" => $state], ["id" => $asset->id]
);
// Return a new asset object with updated status
// Return a new asset object with updated state
$data = $asset->toArray();
$data["_status"] = $status;
$data["_state"] = $state;
$asset = new ($asset::class)($this->app, $data);
$this->app->dispatcher->trigger(
"crispage.assets.asset_status_set", $asset, $status
"crispage.assets.asset_state_set", $asset, $state
);
return $asset;
}
@ -389,14 +389,14 @@
// Do not work on temporary or already trashed assets
if ($asset->isTemporary() || $asset->isTrashed()) return $asset;
return $this->setStatus($asset, Asset::STATUS_TRASHED);
return $this->setState($asset, Asset::STATE_TRASHED);
}
public function untrash(Asset $asset): Asset {
// Do not work on temporary or non-trashed assets
if ($asset->isTemporary() || !$asset->isTrashed()) return $asset;
return $this->setStatus($asset, Asset::STATUS_PERMANENT);
return $this->setState($asset, Asset::STATE_PERMANENT);
}
public function makePermanent(Asset $asset): Asset {
@ -405,7 +405,7 @@
$data = $asset->toArray();
$data["id"] = $this->getNewID();
$data["_table"] = $this->getTable("\\" . $asset::class);
$data["_status"] = Asset::STATUS_PERMANENT;
$data["_state"] = Asset::STATE_PERMANENT;
$passet = new ($asset::class)($this->app, $data);
@ -415,7 +415,7 @@
"classname" => "\\" . $passet::class,
"tablename" => $passet->_table,
"slug" => $passet->slug,
"status" => $passet->_status
"state" => $passet->_state
]);
$this->app->database->insert(
$passet->_table, $passet->toArray()

View File

@ -178,8 +178,8 @@
"action", []
),
new Extension(
"crispage.actions.api.assetstatus", VERSION, "crispage.core",
"\\Crispage\\Actions\\API\\AssetStatusAction",
"crispage.actions.api.assetstate", VERSION, "crispage.core",
"\\Crispage\\Actions\\API\\AssetStateAction",
"action", []
),
new Extension(

View File

@ -19,9 +19,9 @@
use \Crispage\Backend\AssetListActionButton;
abstract class Asset {
public const STATUS_TRASHED = -1;
public const STATUS_TEMPORARY = 0;
public const STATUS_PERMANENT = 1;
public const STATE_TRASHED = -1;
public const STATE_TEMPORARY = 0;
public const STATE_PERMANENT = 1;
public static abstract function getExtensionInfo(): array;
@ -120,7 +120,7 @@
public \Crispage $_app;
public readonly ?string $_table;
public readonly int $_status;
public readonly int $_state;
public readonly int $id;
public string $slug;
public int $ctime;
@ -130,7 +130,7 @@
protected function __construct(\Crispage $app, array $props) {
$this->_app = $app;
$this->_status = $props["_status"] ?? 0;
$this->_state = $props["_state"] ?? 0;
$this->_table = $props["_table"] ?? null;
$this->id = $props["id"] ?? 0;
$this->slug = $props["slug"] ?? "";
@ -169,11 +169,11 @@
}
public function isTemporary(): bool {
return $this->_status == self::STATUS_TEMPORARY || $this->id <= 0;
return $this->_state == self::STATE_TEMPORARY || $this->id <= 0;
}
public function isTrashed(): bool {
return $this->_status <= self::STATUS_TRASHED;
return $this->_state <= self::STATE_TRASHED;
}
public function getOwnerId(): int {

View File

@ -67,7 +67,7 @@
new Route("asset", "\\Crispage\\Actions\\API\\APIAssetAction", static::RDATA_API),
new Route("asset/slug", "\\Crispage\\Actions\\API\\APIAssetBySlugAction", static::RDATA_API),
new Route("asset/options", "\\Crispage\\Actions\\API\\APIAssetOptionsAction", static::RDATA_API),
new Route("asset/status", "\\Crispage\\Actions\\API\\APIAssetStatusAction", static::RDATA_API),
new Route("asset/state", "\\Crispage\\Actions\\API\\APIAssetStateAction", static::RDATA_API),
new Route("assets", "\\Crispage\\Actions\\API\\APIAssetsAction", static::RDATA_API),
new Route("assets/search", "\\Crispage\\Actions\\API\\APIAssetSearchAction", static::RDATA_API),
new Route("comment", "\\Crispage\\Actions\\API\\APICommentAction", static::RDATA_API),