2018-04-15 23:53:46 +00:00
|
|
|
<?php
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Controller\Admin;
|
2018-04-15 23:53:46 +00:00
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2018-08-04 22:05:14 +00:00
|
|
|
use App\Entity;
|
2018-04-15 23:53:46 +00:00
|
|
|
use App\Http\Request;
|
|
|
|
use App\Http\Response;
|
|
|
|
|
|
|
|
class CustomFieldsController
|
|
|
|
{
|
|
|
|
/** @var EntityManager */
|
|
|
|
protected $em;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $form_config;
|
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
/** @var \Azura\Doctrine\Repository */
|
2018-04-15 23:53:46 +00:00
|
|
|
protected $record_repo;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $csrf_namespace = 'admin_custom_fields';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EntityManager $em
|
|
|
|
* @param array $form_config
|
2018-08-27 07:14:05 +00:00
|
|
|
* @see \App\Provider\AdminProvider
|
2018-04-15 23:53:46 +00:00
|
|
|
*/
|
2018-08-04 22:05:14 +00:00
|
|
|
public function __construct(EntityManager $em, array $form_config)
|
2018-04-15 23:53:46 +00:00
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
$this->form_config = $form_config;
|
|
|
|
|
|
|
|
$this->record_repo = $this->em->getRepository(Entity\CustomField::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function indexAction(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$records = $this->record_repo->fetchArray(true, 'name');
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
return $request->getView()->renderToResponse($response, 'admin/custom_fields/index', [
|
2018-04-15 23:53:46 +00:00
|
|
|
'records' => $records,
|
2018-08-04 22:05:14 +00:00
|
|
|
'csrf' => $request->getSession()->getCsrf()->generate($this->csrf_namespace)
|
2018-04-15 23:53:46 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function editAction(Request $request, Response $response, $id = null): Response
|
|
|
|
{
|
|
|
|
$form = new \AzuraForms\Form($this->form_config);
|
|
|
|
|
|
|
|
if (!empty($id)) {
|
|
|
|
$record = $this->record_repo->find((int)$id);
|
|
|
|
$record_defaults = $this->record_repo->toArray($record, true, true);
|
|
|
|
$form->populate($record_defaults);
|
|
|
|
} else {
|
|
|
|
$record = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($_POST) && $form->isValid($_POST)) {
|
|
|
|
$data = $form->getValues();
|
|
|
|
|
|
|
|
if (!($record instanceof Entity\CustomField)) {
|
|
|
|
$record = new Entity\CustomField;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->record_repo->fromArray($record, $data);
|
|
|
|
|
|
|
|
$this->em->persist($record);
|
|
|
|
$this->em->flush();
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
$request->getSession()->flash(sprintf(($id) ? __('%s updated.') : __('%s added.'), __('Custom Field')), 'green');
|
2018-04-15 23:53:46 +00:00
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
return $response->withRedirect($request->getRouter()->named('admin:custom_fields:index'));
|
2018-04-15 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
return $request->getView()->renderToResponse($response, 'system/form_page', [
|
2018-04-15 23:53:46 +00:00
|
|
|
'form' => $form,
|
|
|
|
'render_mode' => 'edit',
|
|
|
|
'title' => sprintf(($id) ? __('Edit %s') : __('Add %s'), __('Custom Field'))
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteAction(Request $request, Response $response, $id, $csrf_token): Response
|
|
|
|
{
|
2018-08-04 22:05:14 +00:00
|
|
|
$request->getSession()->getCsrf()->verify($csrf_token, $this->csrf_namespace);
|
2018-04-15 23:53:46 +00:00
|
|
|
|
|
|
|
$user = $this->record_repo->find((int)$id);
|
|
|
|
|
|
|
|
if ($user instanceof Entity\CustomField) {
|
|
|
|
$this->em->remove($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->em->flush();
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
$request->getSession()->flash('<b>' . __('%s deleted.', __('Custom Field')) . '</b>', 'green');
|
2018-04-15 23:53:46 +00:00
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
return $response->withRedirect($request->getRouter()->named('admin:custom_fields:index'));
|
2018-04-15 23:53:46 +00:00
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|