/
associations.php
190 lines (172 loc) · 8.14 KB
/
associations.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Concrete\Controller\SinglePage\Dashboard\System\Express\Entities;
use Concrete\Core\Page\Controller\DashboardPageController;
class Associations extends DashboardPageController
{
protected $repository;
protected $associationRepository;
protected $entity;
public function on_start()
{
parent::on_start();
$this->repository = $this->entityManager->getRepository('\Concrete\Core\Entity\Express\Entity');
$this->associationRepository = $this->entityManager->getRepository('\Concrete\Core\Entity\Express\Association');
}
public function add($id = null)
{
$entity = $this->repository->findOneById($id);
if (is_object($entity)) {
$this->set('entity', $entity);
$entities = $this->repository->findAll();
$this->set('entities', $entities);
$types = array('' => t('** Select Type'));
$validTypes = [
'ManyToOne' => t('Many to One'),
'OneToMany' => t('One to Many'),
'OneToOne' => t('One to One'),
'ManyToMany' => t('Many to Many'),
];
$types += $validTypes;
$this->set('types', $types);
$this->set('pageTitle', t('Add Association'));
if ($this->request->isMethod('POST')) {
$entityIDs = array();
foreach($entities as $targetEntity) {
$entityIDs[] = $targetEntity->getID();
}
if (!$this->token->validate('add_association')) {
$this->error->add($this->token->getErrorMessage());
}
if (!in_array($this->request->request->get('target_entity'), $entityIDs)) {
$this->error->add(t('Invalid target entity.'));
}
if (!in_array($this->request->request->get('type'), array_keys($validTypes))) {
$this->error->add(t('You must choose what type of association this is.'));
}
if (!$this->request->request->get('target_property_name')) {
$this->error->add(t('You must give your association a target property name.'));
}
if (!$this->error->has()) {
$builder = \Core::make('express/builder/association');
$targetEntity = $this->repository->findOneById($this->request->request->get('target_entity'));
/*
* @var $builder \Concrete\Core\Express\ObjectAssociationBuilder
*/
switch ($this->request->request->get('type')) {
case 'ManyToOne':
$builder->addManyToOne(
$entity, $targetEntity, $this->request->request->get('target_property_name'), $this->request->request->get('inversed_property_name')
);
break;
case 'OneToOne':
$builder->addOneToOne(
$entity, $targetEntity, $this->request->request->get('target_property_name'), $this->request->request->get('inversed_property_name')
);
break;
case 'ManyToMany':
$builder->addManyToMany(
$entity, $targetEntity, $this->request->request->get('target_property_name'), $this->request->request->get('inversed_property_name')
);
break;
case 'OneToMany':
$builder->addOneToMany(
$entity, $targetEntity, $this->request->request->get('target_property_name'), $this->request->request->get('inversed_property_name')
);
break;
}
$this->entityManager->persist($entity);
$this->entityManager->persist($targetEntity);
$this->entityManager->flush();
$this->flash('success', t('Association added successfully.'));
$this->redirect('/dashboard/system/express/entities/associations', $entity->getId());
}
}
$this->render('/dashboard/system/express/entities/associations/add');
} else {
$this->redirect('/dashboard/system/express/entities');
}
}
public function view($id = null)
{
$entity = $this->repository->findOneById($id);
if (is_object($entity)) {
$this->entity = $entity;
$this->set('entity', $entity);
$this->set('associations', $entity->getAssociations());
$this->set('pageTitle', t('Associations'));
} else {
$this->redirect('/dashboard/system/express/entities');
}
}
public function edit($id = null)
{
$this->view_association_details($id);
$this->render('/dashboard/system/express/entities/associations/edit');
}
public function delete_association($id = null)
{
$this->view($id);
$association = $this->associationRepository->findOneById($this->request->request->get('association_id'));
if (!is_object($association)) {
$this->error->add(t('Invalid association object.'));
}
if (!$this->token->validate('delete_association')) {
$this->error->add($this->token->getErrorMessage());
}
if (!$this->error->has()) {
$this->entityManager->remove($association);
$this->entityManager->flush();
$this->flash('success', t('Association deleted successfully.'));
$this->redirect('/dashboard/system/express/entities/associations', $id);
} else {
$this->view_association_details($this->request->request->get('association_id'));
}
}
public function save_association($id = null)
{
$this->view($id);
$association = $this->associationRepository->findOneById($this->request->request->get('association_id'));
if (!is_object($association)) {
$this->error->add(t('Invalid association object.'));
}
if (!$this->token->validate()) {
$this->error->add($this->token->getErrorMessage());
}
if (!$this->error->has()) {
$association->setInversedByPropertyName($this->request->request->get('inversed_property_name'));
$association->setTargetPropertyName($this->request->request->get('target_property_name'));
if ($this->request->request->get('is_owned_by_association')) {
$association->setIsOwnedByAssociation(true);
} else {
$association->setIsOwnedByAssociation(false);
}
if ($this->request->request->get('is_owning_association')) {
$association->setIsOwningAssociation(true);
} else {
$association->setIsOwningAssociation(false);
}
$this->entityManager->persist($association);
$this->entityManager->flush();
$this->flash('success', t('Association saved successfully.'));
$this->redirect('/dashboard/system/express/entities/associations', 'view_association_details', $association->getID());
} else {
$this->view_association_details($this->request->request->get('association_id'));
}
}
public function view_association_details($id = null)
{
$association = $this->associationRepository->findOneById($id);
if (is_object($association)) {
$entity = $association->getSourceEntity();
$this->set('entity', $entity);
$this->set('association', $association);
$this->set('formatter', $association->getFormatter());
$this->set('pageTitle', t('Association Details'));
$this->render('/dashboard/system/express/entities/associations/view_association');
}
if (is_object($entity)) {
} else {
$this->redirect('/dashboard/system/express/entities');
}
}
}