Newer
Older

Yassine Doghri
committed
declare(strict_types=1);
* @copyright 2021 Ad Aures
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/

Yassine Doghri
committed
namespace Modules\Admin\Controllers;
use App\Entities\Person;
use App\Models\PersonModel;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\RedirectResponse;
use Modules\Media\Models\MediaModel;
class PersonController extends BaseController
public function _remap(string $method, string ...$params): mixed
return $this->{$method}();

Yassine Doghri
committed
($person = (new PersonModel())->getPersonById((int) $params[0])) instanceof Person

Yassine Doghri
committed
return $this->{$method}($person);

Yassine Doghri
committed
}
throw PageNotFoundException::forPageNotFound();

Yassine Doghri
committed
public function list(): string
$data = [
'persons' => (new PersonModel())->orderBy('full_name')
->findAll(),
];

Yassine Doghri
committed
$this->setHtmlHead(lang('Person.all_persons'));
return view('person/list', $data);

Yassine Doghri
committed
public function view(Person $person): string
$data = [

Yassine Doghri
committed
'person' => $person,
];

Yassine Doghri
committed
$this->setHtmlHead($person->full_name);
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $person->full_name,
]);
return view('person/view', $data);

Yassine Doghri
committed
public function createView(): string
{
helper(['form']);

Yassine Doghri
committed
$this->setHtmlHead(lang('Person.create'));
return view('person/create');

Yassine Doghri
committed
public function createAction(): RedirectResponse
{
$rules = [
'avatar' => 'is_image[avatar]|ext_in[avatar,jpg,jpeg,png]|min_dims[avatar,400,400]|is_image_ratio[avatar,1,1]',
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}

Yassine Doghri
committed
$db = db_connect();
$db->transStart();
$person = new Person([
'created_by' => user_id(),
'updated_by' => user_id(),
'full_name' => $this->request->getPost('full_name'),
'unique_name' => $this->request->getPost('unique_name'),
'information_url' => $this->request->getPost('information_url'),
'avatar' => $this->request->getFile('avatar'),
]);
$personModel = new PersonModel();
if (! $personModel->insert($person)) {

Yassine Doghri
committed
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $personModel->errors());
}

Yassine Doghri
committed
$db->transComplete();
return redirect()->route('person-list')
->with('message', lang('Person.messages.createSuccess'));

Yassine Doghri
committed
public function editView(Person $person): string
{
helper('form');
$data = [

Yassine Doghri
committed
'person' => $person,

Yassine Doghri
committed
$this->setHtmlHead(lang('Person.edit'));
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $person->full_name,
]);
return view('person/edit', $data);

Yassine Doghri
committed
public function editAction(Person $person): RedirectResponse
{
$rules = [
'avatar' => 'is_image[avatar]|ext_in[avatar,jpg,jpeg,png]|min_dims[avatar,400,400]|is_image_ratio[avatar,1,1]',
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}

Yassine Doghri
committed
$person->updated_by = user_id();
$person->full_name = $this->request->getPost('full_name');
$person->unique_name = $this->request->getPost('unique_name');
$person->information_url = $this->request->getPost('information_url');
$person->setAvatar($this->request->getFile('avatar'));
$personModel = new PersonModel();

Yassine Doghri
committed
if (! $personModel->update($person->id, $person)) {
return redirect()
->back()
->withInput()
->with('errors', $personModel->errors());
}

Yassine Doghri
committed
return redirect()->route('person-edit', [$person->id])->with(

Yassine Doghri
committed
public function deleteAction(Person $person): RedirectResponse

Yassine Doghri
committed
if ($person->avatar_id !== null) {
// delete avatar to prevent collision if recreating person

Yassine Doghri
committed
(new MediaModel())->deleteMedia($person->avatar);

Yassine Doghri
committed
(new PersonModel())->delete($person->id);
return redirect()->route('person-list')
->with('message', lang('Person.messages.deleteSuccess'));