Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 80b17f46

Přidáno uživatelem Jan Šedivý před téměř 6 roky(ů)

Re #7335 Formulář pro správu typu objektů v administraci

Zobrazit rozdíly:

app/AdminModule/component/Object/ObjectTypeEditForm.latte
1
<div class="row">
2
    <div class="col-4">
3
        {control form}
4
    </div>
5
</div>
app/AdminModule/component/Object/ObjectTypeEditForm.php
1
<?php
2

  
3
namespace App\AdminModule\Components;
4

  
5

  
6
use App\Enum\EFlashMessage;
7
use App\Model\Repository\ObjectTypeRepository;
8
use App\Utils\Form;
9
use Nette\Application\UI\Control;
10

  
11
class ObjectTypeEditForm extends Control
12
{
13
    /** @var ObjectTypeRepository */
14
    private $objectTypeRepository;
15

  
16
    private $typeId;
17

  
18
    /**
19
     * ObjectTypeEditForm constructor.
20
     * @param ObjectTypeRepository $objectTypeRepository
21
     * @param $typeId null|int
22
     */
23
    public function __construct($typeId, ObjectTypeRepository $objectTypeRepository)
24
    {
25
        $this->objectTypeRepository = $objectTypeRepository;
26
        $this->typeId = $typeId;
27

  
28
        parent::__construct();
29
    }
30

  
31
    public function render()
32
    {
33
        $this->template->setFile(__DIR__ . '/ObjectTypeEditForm.latte');
34
        $this->template->render();
35
    }
36

  
37
    public function createComponentForm()
38
    {
39
        $form = new Form();
40

  
41
        $form->addText(ObjectTypeRepository::COLUMN_OBJECT_TYPE, 'Object Tyoe')
42
            ->addRule(Form::REQUIRED, 'Field %label is required.');
43

  
44
        $form->addSubmit('save', 'Save');
45

  
46
        if($this->typeId)
47
        {
48
            $form->setDefaults($this->objectTypeRepository->findRow($this->typeId));
49
        }
50

  
51
        $form->onSuccess[] = [$this, 'formSuccess'];
52

  
53
        return $form;
54
    }
55

  
56
    public function formSuccess(Form $form)
57
    {
58
        $result = $this->objectTypeRepository->save($form->getValues(true), $this->typeId);
59

  
60
        if ($result)
61
        {
62
            $this->presenter->flashMessage('Object type was successfully saved.', EFlashMessage::SUCCESS);
63
            $this->presenter->redirect('Object:');
64
        } else
65
        {
66
            $this->presenter->flashMessage('Object Type could not be saved.', EFlashMessage::ERROR);
67
        }
68
    }
69
}
70

  
71
interface IObjectTypeEditFormFactory
72
{
73
    /**
74
     * @param $typeId null|int
75
     * @return ObjectTypeEditForm
76
     */
77
    public function create($typeId);
78
}
app/AdminModule/presenters/ObjectPresenter.php
3 3
namespace App\AdminModule\Presenters;
4 4

  
5 5

  
6
use App\AdminModule\Components\IObjectTypeEditFormFactory;
6 7
use App\AdminModule\Components\IObjectTypeGridFactory;
8
use App\Enum\EFlashMessage;
7 9
use App\Model\Repository\ObjectTypeRepository;
8 10

  
9 11
class ObjectPresenter extends BaseUserPresenter
......
14 16
    /** @var IObjectTypeGridFactory */
15 17
    private $objectTypeGridFactory;
16 18

  
19
    /** @var IObjectTypeEditFormFactory */
20
    private $objectTypeEditFormFactory;
21

  
22
    private $typeId;
23

  
17 24
    /**
18 25
     * ObjectPresenter constructor.
19 26
     * @param ObjectTypeRepository $objectTypeRepository
27
     * @param IObjectTypeGridFactory $objectTypeGridFactory
28
     * @param IObjectTypeEditFormFactory $objectTypeEditFormFactory
20 29
     */
21 30
    public function __construct(
22 31
        ObjectTypeRepository $objectTypeRepository,
23
        IObjectTypeGridFactory $objectTypeGridFactory
32
        IObjectTypeGridFactory $objectTypeGridFactory,
33
        IObjectTypeEditFormFactory $objectTypeEditFormFactory
24 34
    )
25 35
    {
26 36
        $this->objectTypeRepository = $objectTypeRepository;
27 37
        $this->objectTypeGridFactory = $objectTypeGridFactory;
38
        $this->objectTypeEditFormFactory = $objectTypeEditFormFactory;
28 39

  
29 40
        parent::__construct();
30 41
    }
......
36 47

  
37 48
    public function actionEditType(int $id)
38 49
    {
50
        if(!$this->objectTypeRepository->findRow($id))
51
        {
52
            $this->flashMessage('Object type was not found.', EFlashMessage::ERROR);
53
            $this->redirect('Object:');
54
        }
39 55

  
56
        $this->typeId = $id;
57
        $this->template->id = $id;
40 58
    }
41 59

  
42 60
    public function actionDeleteType(int $id)
43 61
    {
44

  
62
        if($this->objectTypeRepository->delete($id))
63
        {
64
            $this->flashMessage('Object type was deleted.', EFlashMessage::SUCCESS);
65
        }
66
        else
67
        {
68
            $this->flashMessage('Object type was not found.', EFlashMessage::ERROR);
69
        }
70
        $this->redirect('Object:');
45 71
    }
46 72

  
47 73
    public function createComponentObjectTypeGrid()
......
49 75
        return $this->objectTypeGridFactory->create();
50 76
    }
51 77

  
78
    public function createComponentObjectTypeEditForm()
79
    {
80
        return $this->objectTypeEditFormFactory->create($this->typeId);
81
    }
52 82
}
app/AdminModule/templates/Object/addType.latte
1
{block content}
2
    <div class="row">
3
        <div class="col-10">
4
            <div class="display-5">New Object Type</div>
5
        </div>
6
        <div class="col-2 text-right">
7
            <a n:href="Object:default" class="btn btn-sm btn-success"><span class="fa fa-fw fa-list"></span> Object Types</a>
8
        </div>
9
    </div>
10
    {control objectTypeEditForm}
11
{/block}
app/AdminModule/templates/Object/default.latte
4 4
            <div class="display-5">Object Type List</div>
5 5
        </div>
6 6
        <div class="col-1">
7
            <a n:href="User:add" class="btn btn-sm btn-success"><span class="fa fa-fw fa-plus"></span> New Object Type</a>
7
            <a n:href="Object:addType" class="btn btn-sm btn-success"><span class="fa fa-fw fa-plus"></span> New Object Type</a>
8 8
        </div>
9 9
    </div>
10 10
    <div class="row">
app/AdminModule/templates/Object/editType.latte
1
{block content}
2
    <div class="row">
3
        <div class="col-10">
4
            <div class="display-5">Edit Object Type</div>
5
        </div>
6
        <div class="col-2 text-right">
7
            <a n:href="Object:deleteType $id" class="btn btn-sm btn-danger" onclick="return confirm('Do you really want to delete this type?')"><span class="fa fa-fw fa-trash"></span> Delete Type</a>
8
            <a n:href="Object:default" class="btn btn-sm btn-success"><span class="fa fa-fw fa-list"></span> Object Types</a>
9
        </div>
10
    </div>
11
    {control objectTypeEditForm}
12
{/block}
app/config/components.neon
9 9
    - App\FrontModule\Components\ILoginFormFactory
10 10
    - App\AdminModule\Components\IUserEditFormFactory
11 11
    - App\FrontModule\Components\ITransliterationSearchFormFactory
12
    - App\AdminModule\Components\IMuseumEditFormFactory
12
    - App\AdminModule\Components\IMuseumEditFormFactory
13
    - App\AdminModule\Components\IObjectTypeEditFormFactory
www/css/admin/style.css
30 30
    font-size: 2rem;
31 31
    font-weight: 300;
32 32
    line-height: 1.2;
33
    margin-bottom: 1rem;
33 34
}
34 35

  
35 36
.content {

Také k dispozici: Unified diff