. * */ namespace Vvveb\Controller; use function Vvveb\__; use function Vvveb\model; use Vvveb\System\Traits\Crud as CrudTrait; class Crud extends Base { use CrudTrait { CrudTrait::delete as remove; CrudTrait::index as get; } protected $fullPost = true; function validate($method, $ignoreMissing = false) { $model = model($this->type); $data = $this->request->post ?? []; if ($model) { $errors = $model->validate($data, $method, $this->type, $ignoreMissing); if ($errors) { return $errors; } else { return true; } } $this->redirect = false; $this->fullPost = true; return [sprintf(__('%s not found!'), ucfirst($this->type))]; } function index() { $data = $this->get(); if (! $data || (! isset($data[$this->type . '_id']))) { return $this->notFound(sprintf(__('%s not found!'), ucfirst($this->type))); } return $data; } //create function post() { if (($errors = $this->validate('add')) === true) { $this->redirect = false; return $this->save(); } else { $this->notFound(['errors' => $errors], 500); } } //edit function patch() { if (($errors = $this->validate('edit')) === true) { $this->redirect = false; return $this->save(); } else { $this->notFound(['errors' => $errors], 500); } } //upsert function put() { //if id update else create if (($errors = $this->validate('edit', true)) === true) { return $this->save(); } else { $this->notFound(['errors' => $errors], 500); } } //delete function delete() { $type = $this->type; $type_id = $this->type_id ?? "{$type}_id"; $data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false; $response = ['deleted' => false]; if ($data_id) { if (is_numeric($data_id)) { $data_id = [$data_id]; } if (! isset($this->modelName)) { $modelName = $type; } $model = model($modelName); $options = [$type_id => $data_id] + $this->global; $result = $model->delete($options); $name = ucfirst(__($type)); if ($result && isset($result[$type])) { if ($result[$type]) { $response['deleted'] = true; } else { $response['message'] = 'No rows affected!'; return $this->notFound($response, 410); } } else { $response['message'] = 'Error deleting %s!'; return $this->notFound($response, 500); } } return $response; } }