change Asset->status to ->state for consistency
This commit is contained in:
parent
b8c302a50e
commit
fc28a5d2cc
@ -18,11 +18,11 @@
|
|||||||
use \Crispage\Auth\CorePermissions;
|
use \Crispage\Auth\CorePermissions;
|
||||||
use \Crispage\Exceptions\APIException;
|
use \Crispage\Exceptions\APIException;
|
||||||
|
|
||||||
class APIAssetStatusAction extends \Crispage\Framework\Action
|
class APIAssetStateAction extends \Crispage\Framework\Action
|
||||||
implements \Crispage\Response\APIPermissions {
|
implements \Crispage\Response\APIPermissions {
|
||||||
public static function getExtensionInfo(): array {
|
public static function getExtensionInfo(): array {
|
||||||
return [
|
return [
|
||||||
"id" => "crispage.actions.api.assetstatus",
|
"id" => "crispage.actions.api.assetstate",
|
||||||
"version" => VERSION
|
"version" => VERSION
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@
|
|||||||
if (!$asset) throw new APIException("Not found", 404);
|
if (!$asset) throw new APIException("Not found", 404);
|
||||||
|
|
||||||
// Output
|
// Output
|
||||||
$out = ["_status" => $asset->_status];
|
$out = ["_state" => $asset->_state];
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -68,17 +68,17 @@
|
|||||||
throw new APIException("Unauthorized", 401);
|
throw new APIException("Unauthorized", 401);
|
||||||
|
|
||||||
if ($asset->isTemporary())
|
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)
|
if ($state != Asset::STATE_TRASHED && $state != Asset::STATE_PERMANENT)
|
||||||
throw new APIException("Invalid status", 400);
|
throw new APIException("Invalid state", 400);
|
||||||
|
|
||||||
// Change status
|
// Change state
|
||||||
$asset = $this->app->assets->setStatus($asset, $status);
|
$asset = $this->app->assets->setState($asset, $state);
|
||||||
|
|
||||||
$out = ["_status" => $asset->_status];
|
$out = ["_state" => $asset->_state];
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
@ -37,7 +37,7 @@
|
|||||||
|
|
||||||
// Get asset
|
// Get asset
|
||||||
$this->data["asset_id"] = intval($this->app->request->params["asset_id"] ?? 0);
|
$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
|
// Check that asset exists
|
||||||
if (!$this->data["asset"]) {
|
if (!$this->data["asset"]) {
|
||||||
|
@ -27,10 +27,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function run(): void {
|
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 = [];
|
$assets = [];
|
||||||
while ($id = $res->fetchColumn()) {
|
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;
|
if ($asset) $assets[] = $asset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +170,7 @@
|
|||||||
return true;
|
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
|
// Check class is valid
|
||||||
if (!class_exists($class))
|
if (!class_exists($class))
|
||||||
throw new AssetException("Class $class does not exist");
|
throw new AssetException("Class $class does not exist");
|
||||||
@ -185,28 +185,28 @@
|
|||||||
if (!$row) return null;
|
if (!$row) return null;
|
||||||
|
|
||||||
// Return asset from data
|
// Return asset from data
|
||||||
$row["_status"] = $status;
|
$row["_state"] = $state;
|
||||||
$row["_table"] = $table;
|
$row["_table"] = $table;
|
||||||
return new $class($this->app, $row);
|
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
|
// Get asset info from table
|
||||||
$res = $this->app->database->select("assets", null, ["id" => $id]);
|
$res = $this->app->database->select("assets", null, ["id" => $id]);
|
||||||
$row = $res->fetch();
|
$row = $res->fetch();
|
||||||
|
|
||||||
// Check that data exists
|
// Check that data exists
|
||||||
if (!$row) return null;
|
if (!$row) return null;
|
||||||
// Check status matches
|
// Check state matches
|
||||||
if ($req_status !== null && $row["status"] != $req_status) return null;
|
if ($req_state !== null && $row["state"] != $req_state) return null;
|
||||||
|
|
||||||
// Return asset
|
// Return asset
|
||||||
return $this->constructAsset(
|
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
|
// Get asset info from table
|
||||||
$res = $this->app->database->select(
|
$res = $this->app->database->select(
|
||||||
"assets", null, ["classname" => $classname, "slug" => $slug]
|
"assets", null, ["classname" => $classname, "slug" => $slug]
|
||||||
@ -215,16 +215,16 @@
|
|||||||
|
|
||||||
// Check that data exists
|
// Check that data exists
|
||||||
if (!$row) return null;
|
if (!$row) return null;
|
||||||
// Check status matches
|
// Check state matches
|
||||||
if ($req_status !== null && $row["status"] != $req_status) return null;
|
if ($req_state !== null && $row["state"] != $req_state) return null;
|
||||||
|
|
||||||
// Return asset
|
// Return asset
|
||||||
return $this->constructAsset(
|
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
|
// Get asset info from table
|
||||||
$res = $this->app->database->select(
|
$res = $this->app->database->select(
|
||||||
"assets", null,
|
"assets", null,
|
||||||
@ -233,18 +233,18 @@
|
|||||||
|
|
||||||
// Walk through and yield assets
|
// Walk through and yield assets
|
||||||
while (($row = $res->fetch()) !== false) {
|
while (($row = $res->fetch()) !== false) {
|
||||||
// Check status matches
|
// Check state matches
|
||||||
if ($req_status !== null && $row["status"] != $req_status) continue;
|
if ($req_state !== null && $row["state"] != $req_state) continue;
|
||||||
|
|
||||||
yield $this->constructAsset(
|
yield $this->constructAsset(
|
||||||
$row["tablename"], $row["classname"], $row["id"], $row["status"]
|
$row["tablename"], $row["classname"], $row["id"], $row["state"]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAllFiltered(
|
public function getAllFiltered(
|
||||||
string $classname, array $filters = [], string $sortby = null,
|
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 {
|
): \Generator {
|
||||||
// Check class is valid
|
// Check class is valid
|
||||||
if (!class_exists($classname))
|
if (!class_exists($classname))
|
||||||
@ -265,10 +265,10 @@
|
|||||||
$ares = $this->app->database->select("assets", null, ["id" => $row["id"]]);
|
$ares = $this->app->database->select("assets", null, ["id" => $row["id"]]);
|
||||||
$arow = $ares->fetch();
|
$arow = $ares->fetch();
|
||||||
if (!$arow) continue;
|
if (!$arow) continue;
|
||||||
// Check status matches
|
// Check state matches
|
||||||
if ($req_status !== null && $arow["status"] != $req_status) continue;
|
if ($req_state !== null && $arow["state"] != $req_state) continue;
|
||||||
// Yield asset
|
// Yield asset
|
||||||
$row["_status"] = $arow["status"];
|
$row["_state"] = $arow["state"];
|
||||||
$row["_table"] = $table;
|
$row["_table"] = $table;
|
||||||
yield new $classname($this->app, $row);
|
yield new $classname($this->app, $row);
|
||||||
}
|
}
|
||||||
@ -292,7 +292,7 @@
|
|||||||
if (!is_a($classname, "\\Crispage\\Framework\\Asset", true))
|
if (!is_a($classname, "\\Crispage\\Framework\\Asset", true))
|
||||||
throw new AssetException("Class $classname is not an Asset");
|
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["_table"] = $this->getTable($classname);
|
||||||
$data["slug"] ??= uniqid("asset_");
|
$data["slug"] ??= uniqid("asset_");
|
||||||
|
|
||||||
@ -318,7 +318,7 @@
|
|||||||
"classname" => "\\" . $asset::class,
|
"classname" => "\\" . $asset::class,
|
||||||
"tablename" => $asset->_table,
|
"tablename" => $asset->_table,
|
||||||
"slug" => $asset->slug,
|
"slug" => $asset->slug,
|
||||||
"status" => $asset->_status
|
"state" => $asset->_state
|
||||||
]);
|
]);
|
||||||
$this->app->database->insert(
|
$this->app->database->insert(
|
||||||
$asset->_table, $asset->toArray()
|
$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
|
// Update the asset in the assets table
|
||||||
$this->app->database->update(
|
$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 = $asset->toArray();
|
||||||
$data["_status"] = $status;
|
$data["_state"] = $state;
|
||||||
$asset = new ($asset::class)($this->app, $data);
|
$asset = new ($asset::class)($this->app, $data);
|
||||||
|
|
||||||
$this->app->dispatcher->trigger(
|
$this->app->dispatcher->trigger(
|
||||||
"crispage.assets.asset_status_set", $asset, $status
|
"crispage.assets.asset_state_set", $asset, $state
|
||||||
);
|
);
|
||||||
return $asset;
|
return $asset;
|
||||||
}
|
}
|
||||||
@ -389,14 +389,14 @@
|
|||||||
// Do not work on temporary or already trashed assets
|
// Do not work on temporary or already trashed assets
|
||||||
if ($asset->isTemporary() || $asset->isTrashed()) return $asset;
|
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 {
|
public function untrash(Asset $asset): Asset {
|
||||||
// Do not work on temporary or non-trashed assets
|
// Do not work on temporary or non-trashed assets
|
||||||
if ($asset->isTemporary() || !$asset->isTrashed()) return $asset;
|
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 {
|
public function makePermanent(Asset $asset): Asset {
|
||||||
@ -405,7 +405,7 @@
|
|||||||
$data = $asset->toArray();
|
$data = $asset->toArray();
|
||||||
$data["id"] = $this->getNewID();
|
$data["id"] = $this->getNewID();
|
||||||
$data["_table"] = $this->getTable("\\" . $asset::class);
|
$data["_table"] = $this->getTable("\\" . $asset::class);
|
||||||
$data["_status"] = Asset::STATUS_PERMANENT;
|
$data["_state"] = Asset::STATE_PERMANENT;
|
||||||
|
|
||||||
$passet = new ($asset::class)($this->app, $data);
|
$passet = new ($asset::class)($this->app, $data);
|
||||||
|
|
||||||
@ -415,7 +415,7 @@
|
|||||||
"classname" => "\\" . $passet::class,
|
"classname" => "\\" . $passet::class,
|
||||||
"tablename" => $passet->_table,
|
"tablename" => $passet->_table,
|
||||||
"slug" => $passet->slug,
|
"slug" => $passet->slug,
|
||||||
"status" => $passet->_status
|
"state" => $passet->_state
|
||||||
]);
|
]);
|
||||||
$this->app->database->insert(
|
$this->app->database->insert(
|
||||||
$passet->_table, $passet->toArray()
|
$passet->_table, $passet->toArray()
|
||||||
|
@ -178,8 +178,8 @@
|
|||||||
"action", []
|
"action", []
|
||||||
),
|
),
|
||||||
new Extension(
|
new Extension(
|
||||||
"crispage.actions.api.assetstatus", VERSION, "crispage.core",
|
"crispage.actions.api.assetstate", VERSION, "crispage.core",
|
||||||
"\\Crispage\\Actions\\API\\AssetStatusAction",
|
"\\Crispage\\Actions\\API\\AssetStateAction",
|
||||||
"action", []
|
"action", []
|
||||||
),
|
),
|
||||||
new Extension(
|
new Extension(
|
||||||
|
@ -19,9 +19,9 @@
|
|||||||
use \Crispage\Backend\AssetListActionButton;
|
use \Crispage\Backend\AssetListActionButton;
|
||||||
|
|
||||||
abstract class Asset {
|
abstract class Asset {
|
||||||
public const STATUS_TRASHED = -1;
|
public const STATE_TRASHED = -1;
|
||||||
public const STATUS_TEMPORARY = 0;
|
public const STATE_TEMPORARY = 0;
|
||||||
public const STATUS_PERMANENT = 1;
|
public const STATE_PERMANENT = 1;
|
||||||
|
|
||||||
public static abstract function getExtensionInfo(): array;
|
public static abstract function getExtensionInfo(): array;
|
||||||
|
|
||||||
@ -120,7 +120,7 @@
|
|||||||
|
|
||||||
public \Crispage $_app;
|
public \Crispage $_app;
|
||||||
public readonly ?string $_table;
|
public readonly ?string $_table;
|
||||||
public readonly int $_status;
|
public readonly int $_state;
|
||||||
public readonly int $id;
|
public readonly int $id;
|
||||||
public string $slug;
|
public string $slug;
|
||||||
public int $ctime;
|
public int $ctime;
|
||||||
@ -130,7 +130,7 @@
|
|||||||
|
|
||||||
protected function __construct(\Crispage $app, array $props) {
|
protected function __construct(\Crispage $app, array $props) {
|
||||||
$this->_app = $app;
|
$this->_app = $app;
|
||||||
$this->_status = $props["_status"] ?? 0;
|
$this->_state = $props["_state"] ?? 0;
|
||||||
$this->_table = $props["_table"] ?? null;
|
$this->_table = $props["_table"] ?? null;
|
||||||
$this->id = $props["id"] ?? 0;
|
$this->id = $props["id"] ?? 0;
|
||||||
$this->slug = $props["slug"] ?? "";
|
$this->slug = $props["slug"] ?? "";
|
||||||
@ -169,11 +169,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function isTemporary(): bool {
|
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 {
|
public function isTrashed(): bool {
|
||||||
return $this->_status <= self::STATUS_TRASHED;
|
return $this->_state <= self::STATE_TRASHED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOwnerId(): int {
|
public function getOwnerId(): int {
|
||||||
|
@ -67,7 +67,7 @@
|
|||||||
new Route("asset", "\\Crispage\\Actions\\API\\APIAssetAction", static::RDATA_API),
|
new Route("asset", "\\Crispage\\Actions\\API\\APIAssetAction", static::RDATA_API),
|
||||||
new Route("asset/slug", "\\Crispage\\Actions\\API\\APIAssetBySlugAction", 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/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", "\\Crispage\\Actions\\API\\APIAssetsAction", static::RDATA_API),
|
||||||
new Route("assets/search", "\\Crispage\\Actions\\API\\APIAssetSearchAction", static::RDATA_API),
|
new Route("assets/search", "\\Crispage\\Actions\\API\\APIAssetSearchAction", static::RDATA_API),
|
||||||
new Route("comment", "\\Crispage\\Actions\\API\\APICommentAction", static::RDATA_API),
|
new Route("comment", "\\Crispage\\Actions\\API\\APICommentAction", static::RDATA_API),
|
||||||
|
Loading…
Reference in New Issue
Block a user