Crud
abstract class Crud extends Module
Generic CRUD module.
Actions:
updateForm
createForm
read
update
create
delete
softDelete
restore
Action read
Request:
{
post : {
onlyIds : <bool - return only ids, paging not applied>,
orderBy : <column name>,
direction : <'asc'|'desc'>,
page : <requested page number>,
resultsPerPage : <number of results per page>,
trash : <bool - query only soft deleted items>,
search : <search string>,
filters : {
<column name> : {op : <operator>, val : <value>},
<column name> : {op : <operator>, val : <value>},
...
}
}
}
Response (request.onlyIds == false):
{
totalResults : <total number of results on all pages>,
ids : [
<first item id>,
<second item id>,
...
],
rows : [
[<first column value>, <second column value>, ...],
[<first column value>, <second column value>, ...],
...
],
actions : [
[<first item actions>],
[<second item actions>],
...
]
}
Response (request.onlyIds == true):
[
<first item id>,
<second item id>,
...
]
Constants
ACTION_PREFIX |
Action methods prefix |
INFINITY |
Constant. Infinite number of results requested. |
ASC |
Constant. Ascending order. |
DESC |
Constant. Descending order. |
Properties
public | $defaults | Default crud request | |
protected | $title | override to set module's name | from Module |
protected | $icon | override to set module's icon | from Module |
protected | $views | Add views directory to module; | from Module |
protected | $roles | override to set authorized roles | from Module |
protected | $actionRoles | override to set authorized roles | from Module |
protected | $crudColumns | Columns chainset class | |
protected | $crudActions | Actions chainset class | |
protected | $crudFilters | Filters chainset class | |
private | $crudColumnsCached | Cached columns chainset | |
private | $crudActionsCached | Cached actions chainset; | |
private | $crudFiltersCached | Cached filters chainset. |
Public Methods
Each module has to be initialized with eladmin instance and its own elakey.
Each module has its elakey - index in modules array - used to address requests.
Check if user is authorized to do action, or athorized to access module at all.
Return url for this module.
Create asset url, file path relative to /assets directory. Default $version = time()
Get roles authorized to work with the module, or specific action. Empty array means any role is authorized.
We want action keys to be case insensitive. This converts action to lowercase.
Convinient method for plain text output. Sets HTTP header text/plain and echo $str.
Convinient method for html output. Sets HTTP header text/html and echo $str.
Convinient method for json output. Sets HTTP header application/json and echo serialized $json.
Protected Methods
Private Methods
Details
in Module at line 96
__construct($core, $elakey)
Each module has to be initialized with eladmin instance and its own elakey.
in Module at line 111
final string
elakey()
Each module has its elakey - index in modules array - used to address requests.
in Module at line 118
final bool
auth(string|null $action = null)
Check if user is authorized to do action, or athorized to access module at all.
in Module at line 133
string
title()
Get name of the module.
in Module at line 140
string
icon()
Get icon of the module.
in Module at line 147
final string
requestUrl(string|null $action = null, array $args = [])
Return url for this module.
in Module at line 160
final string
assetUrl(string $path, string|null $version = null)
Create asset url, file path relative to /assets directory. Default $version = time()
at line 394
void
prepare()
Runs before any action is executed.
in Module at line 174
final array|null
roles(string|null $action = null)
Get roles authorized to work with the module, or specific action. Empty array means any role is authorized.
in Module at line 195
private void
setRoles(array|null $roles, string|null $action = null)
Set roles authorized to work with the module, or specific action. Empty array means any role is authorized.
at line 294
protected array
views()
Extends views directory.
in Module at line 228
protected Blade
blade()
Return Blade instance
in Module at line 235
final string
render(string $template, array $args = [])
Return rendered view.
in Module at line 243
void
renderAsset(string $path)
Determinate asset content-type and render it.
in Module at line 250
final static array
actions()
Return array of all defined actions.
in Module at line 267
final static bool
hasAction(string $action)
Check if actions is defined.
in Module at line 274
final static bool
isAction(string $action)
Check if actions is executed.
in Module at line 281
final static string
parseAction(string $action)
We want action keys to be case insensitive. This converts action to lowercase.
in Module at line 288
final static void
renderText(string|null $str = null)
Convinient method for plain text output. Sets HTTP header text/plain and echo $str.
in Module at line 297
final static void
renderHtml(string|null $str = null)
Convinient method for html output. Sets HTTP header text/html and echo $str.
in Module at line 306
final static void
renderJson(array|null $json = null)
Convinient method for json output. Sets HTTP header application/json and echo serialized $json.
at line 128
bool
implementsSoftDeletes()
IMPLEMENT. Does CRUD use soft deletes?
at line 135
bool
implementsSearch()
IMPLEMENT. Does CRUD support searching?
at line 142
bool
implementsPaging()
IMPLEMENT. Does CRUD support paging?
at line 149
bool
implementsSorting()
IMPLEMENT. Does CRUD support sorting?
at line 156
bool
implementsFilters()
IMPLEMENT. Does CRUD support filtering?
at line 163
abstract string
primary()
IMPLEMENT. Name of primary key column. Default is 'id'.
at line 169
abstract protected void
create(array $values)
IMPLEMENT. Create item.
at line 176
abstract protected void
update(array $values, $id)
IMPLEMENT. Update item.
at line 182
protected array
default()
Read default row Returns row as an associative array ['columnName' => 'value']
at line 193
abstract protected array
get($id)
IMPLEMENT. Read one row.
Returns row as an associative array ['columnName' => 'value']
at line 198
abstract protected void
delete($id)
IMPLEMENT. Hard delete row.
at line 203
abstract protected void
softDelete($id)
IMPLEMENT. Soft delete row.
at line 208
abstract protected void
restore($id)
IMPLEMENT. Restore soft deleted row.
at line 215
abstract protected array
read(array $request, $totalResults)
IMPLEMENT. Fetch an array of rows.
Row is an associative array ['columnName' => 'value'] $totalResults needs to be set to number of results without paging applied
at line 220
protected
crudColumns()
IMPLEMENT. Default columns chainset. Override to configure columns.
at line 230
protected
crudActions()
Default actions chainset. Override to configure actions.
at line 258
protected
crudFilters()
Default filters chainset. Override to configure filters.
at line 267
final Columns
getCrudColumns()
Get columns chainset.
at line 276
final Actions
getCrudActions()
Get actions chainset.
at line 285
final Filters
getCrudFilters()
Get filters chainset.
at line 302
protected
id($throwIfNull = false)
Return ID of the row which should be affected by the action.
at line 311
private
rowValuesArray($row)
generate array of values for one row
at line 325
private
rowActionsArray($row)
generate array of actions for one item
at line 346
private void
unset(array $row)
unset columns which we shouldn't recieve before update
at line 358
private void
defaults(array $row)
set defaults
at line 370
private void
validate(array $row)
validate columns before update
at line 382
private void
modify(array $row)
modify values before update
at line 407
actionCreateForm()
ACTION. Show form - create entry.
at line 416
actionUpdateForm()
ACTION. Show form - edit entry.
at line 425
actionRead()
ACTION. List database entries.
at line 484
actionUpdate()
ACTION. Edit database entry.
at line 495
actionCreate()
ACTION. Create database entry.
at line 507
actionDelete()
ACTION. Hard delete.
at line 515
actionSoftDelete()
ACTION. Soft delete.
at line 523
actionRestore()
ACTION. Restore.