This repository has been archived on 2024-04-19. You can view files and clone it, but cannot push or open issues or pull requests.
crispage-lite/patch/core/app/assets/Asset.php

188 lines
4.2 KiB
PHP
Raw Normal View History

2023-12-07 14:07:15 -05:00
<?php
/*
Crispage CMS
crispycat <the@crispy.cat>
https://crispy.cat/software/crispage
Crispage is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
namespace Crispage;
defined("ROOT") or die();
use \Crispage\Auth\CorePermissions;
class Asset {
public static function getNames(): array {
return ["Asset", "Assets"];
}
public static function getPermissions(): array {
return [
"create" => CorePermissions::NEVER,
"modify" => CorePermissions::NEVER,
"modify_own"=> CorePermissions::NEVER,
"delete" => CorePermissions::NEVER,
"delete_own"=> CorePermissions::NEVER
];
}
public static function getRoutingInfo(): array {
return [
"action" => null,
"data" => []
];
}
public static function getSortingInfo(): array {
return [
"field" => "mtime",
"desc" => true
];
}
public static function getListColumns(): array {
global $Crispage;
return [
"id" => ["label" => "{%ID}"],
"slug" => [
"label" => "{%SLUG}",
"filter" => "\\Crispage\\Utils\\TextUtils::codeify"
],
"ctime" => [
"label" => "{%CREATED}",
"filter" => [$Crispage->i18n, "datetime"]
],
"mtime" => [
"label" => "{%MODIFIED}",
"filter" => [$Crispage->i18n, "datetime"]
]
];
}
public static function getListActions(): array {
return [
[
"route" => "assets/editor",
"label" => "{%CREATE_NEW}",
"color" => "success"
]
];
}
public static function getListItemActions(): array {
return [
[
"route" => "assets/editor",
"label" => "{%EDIT}",
"color" => "primary",
"fields" => [
"asset_id" => "id"
]
],
[
"route" => "assets/delete",
"label" => "{%DELETE}",
"color" => "danger",
"fields" => [
"asset_id" => "id"
]
]
];
}
public static function getEditorFields(): array {
return [
"slug" => [
"column" => 1,
"component" => "\\Crispage\\Components\\Fields\\TextFieldComponent",
"com_data" => [
"label" => "{%SLUG}:"
],
"filter" => "strval"
]
];
}
public static function getOptionFields(): array {
return [];
}
public static function slug(string $text = null): string {
if (empty($text)) return strval(time());
$slug = strtolower($text);
$slug = preg_replace("/[^a-z\\d\\-_]+/", "-", $slug);
$slug = preg_replace("/-{2,}/", "-", $slug);
$slug = trim($slug, "-");
if (empty($slug)) return strval(time());
return $slug;
}
public ?string $_table;
public int $id;
public string $slug;
public int $ctime;
public int $mtime;
public string $options;
protected array $_options_array;
protected function __construct(array $props) {
$this->_table = $props["_table"] ?? null;
$this->id = $props["id"] ?? 0;
$this->slug = $props["slug"] ?? "";
$this->ctime = $props["ctime"] ?? time();
$this->mtime = $props["mtime"] ?? time();
$this->options = $props["options"] ?? "[]";
$this->_options_array = json_decode($this->options, true);
}
public function toArray(): array {
$arr = [];
foreach ($this as $k => $v) {
if ($k[0] == "_") continue;
if (is_bool($v)) $v = intval($v);
$arr[$k] = $v;
}
return $arr;
}
public function getOption(string $key, $default = null) {
return $this->_options_array[$key] ?? $default;
}
public function getOptions(): array {
return $this->_options_array;
}
public function setOption(string $key, $value): void {
$this->_options_array[$key] = $value;
$this->options = json_encode($this->_options_array);
}
public function setOptions(array $data): void {
$this->_options_array = array_merge($this->_options_array, $data);
$this->options = json_encode($this->_options_array);
}
public function isTemporary(): bool {
return $this->_table === null;
}
public function getOwnerId(): int {
return 0;
}
public function getName(): string {
return $this->slug;
}
public function getParentId(): int {
return 0;
}
}
?>