-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentGroupsEditor.php
More file actions
152 lines (131 loc) · 3.51 KB
/
DocumentGroupsEditor.php
File metadata and controls
152 lines (131 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Created by PhpStorm.
* User: administrator
* Date: 23.08.17
* Time: 15:46
*/
namespace Dsv\PortalBundle\Model\Document;
use Dsv\PortalBundle\Entity\DocumentsGroups;
use Dsv\PortalBundle\Entity\Portal;
use Dsv\PortalBundle\Repository\DocumentsGroupsRepository;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class DocumentGroupsEditor
* @package Dsv\PortalBundle\Model\Document
*/
class DocumentGroupsEditor
{
/**
* @var DocumentsGroupsRepository
*/
private $documentsGroupsRepository;
/**
* @var Portal
*/
private $portal;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var array
*/
private $parameters;
/**
* DocumentGroupsEditor constructor.
* @param DocumentsGroupsRepository $documentsGroupsRepository
* @param Portal $portal
* @param EntityManager $entityManager
*/
public function __construct(
DocumentsGroupsRepository $documentsGroupsRepository,
Portal $portal,
EntityManager $entityManager
) {
$this->documentsGroupsRepository = $documentsGroupsRepository;
$this->portal = $portal;
$this->entityManager = $entityManager;
$this->setParameters([]);
}
/**
* @return array
*/
public function getData()
{
$groups = $this->documentsGroupsRepository->getAllPortalDocumentGroups($this->portal);
$ret = [];
foreach ($groups as $group) {
$ret['rows'][] = array(
'id' => $group['id'],
'cell' => array('', $group['title'], $group['position']),
);
}
return $ret;
}
/**
* @param array $parameters
* @return $this
*/
public function setParameters(array $parameters)
{
$resolver = new OptionsResolver();
$resolver->setDefaults(
[
'id' => '',
'title' => '',
'position' => 0,
'oper' => 0,
]);
$this->parameters = $resolver->resolve($parameters);
return $this;
}
/**
*
*/
public function edit()
{
$this->setupEntity($this->findDocumentGroup());
$this->entityManager->flush();
}
/**
*
*/
public function add()
{
$documentsGroups = new DocumentsGroups($this->portal);
$this->setupEntity($documentsGroups);
$this->entityManager->persist($documentsGroups);
$this->entityManager->flush();
}
/**
*
*/
public function del()
{
$this->entityManager->remove($this->findDocumentGroup());
$this->entityManager->flush();
}
/**
* @return DocumentsGroups
*/
private function findDocumentGroup()
{
/** @var DocumentsGroups $documentsGroup */
$documentsGroup = $this->documentsGroupsRepository->find($this->parameters['id']);
if (!$documentsGroup) {
throw new NotFoundHttpException('Could not find a document group');
}
return $documentsGroup;
}
/**
* @param DocumentsGroups $documentsGroup
*/
private function setupEntity(DocumentsGroups $documentsGroup)
{
$documentsGroup->setTitle($this->parameters['title']);
$documentsGroup->setPosition($this->parameters['position']);
}
}