-
Notifications
You must be signed in to change notification settings - Fork 0
/
Records.php
315 lines (269 loc) · 10.6 KB
/
Records.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
namespace Bolt\Controller\Backend;
use Bolt\Asset\File\JavaScript;
use Bolt\Asset\Snippet\Snippet;
use Bolt\Asset\Target;
use Bolt\Common\Json;
use Bolt\Controller\Zone;
use Bolt\Exception\InvalidRepositoryException;
use Bolt\Form\FormType\ContentEditType;
use Bolt\Storage\ContentRequest\ListingOptions;
use Bolt\Translation\Translator as Trans;
use Silex\ControllerCollection;
use Symfony\Component\Form\Button;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Backend controller for record manipulation routes.
*
* Prior to v3.0 this functionality primarily existed in the monolithic
* Bolt\Controllers\Backend class.
*
* @author Gawain Lynch <gawain.lynch@gmail.com>
*/
class Records extends BackendBase
{
protected function addRoutes(ControllerCollection $c)
{
$c->method('GET|POST');
$c->match('/editcontent/{contenttypeslug}/{id}', 'edit')
->bind('editcontent')
->assert('id', '\d*')
->value('id', '');
$c->get('/overview/{contenttypeslug}', 'overview')
->bind('overview');
}
/**
* Edit a record, or create a new one.
*
* @param Request $request The Symfony Request
* @param string $contenttypeslug The content type slug
* @param int $id The content ID
*
* @return \Bolt\Response\TemplateResponse|\Symfony\Component\HttpFoundation\Response
*/
public function edit(Request $request, $contenttypeslug, $id)
{
$contentTypeKey = $contenttypeslug;
$duplicate = $request->query->getBoolean('duplicate');
$new = $duplicate ? false : empty($id);
// Override return redirect for singletons
$isSingleton = $this->getOption('contenttypes/' . $contentTypeKey . '/singleton');
$deleteRoute = $isSingleton ? 'editcontent' : 'overview';
// Test the access control
if ($response = $this->checkEditAccess($contentTypeKey, $id)) {
return $response;
}
// Get the ContentType object
$contentType = $this->getContentType($contentTypeKey);
$data = null;
$options = ['contenttype_name' => $contentType['singular_name']];
/** @var Form $form */
$form = $this->createFormBuilder(ContentEditType::class, $data, $options)
->getForm()
;
$form->handleRequest($request);
$button = $form->getClickedButton();
if ($form->isSubmitted() && $form->isValid() && $button !== null) {
// Save the POSTed record
$formValues = $request->request->all();
$returnTo = $this->getReturnTo($request, $button);
$editReferrer = $request->get('editreferrer');
if ($button->getName() === 'delete') {
$this->app['storage.request.modify']->action($contentTypeKey, [$id => ['delete' => true]]);
return $this->redirectToRoute($deleteRoute, ['contenttypeslug' => $contentTypeKey]);
} else {
$response = $this->recordSave()->action($formValues, $contentType, $id, $new || $duplicate, $returnTo, $editReferrer);
}
if ($response instanceof Response) {
return $response;
}
}
try {
// Get the record
$repo = $this->getRepository($contentTypeKey);
} catch (InvalidRepositoryException $e) {
$this->flashes()->error(Trans::__('contenttypes.generic.not-existing', ['%contenttype%' => $contentTypeKey]));
return $this->redirectToRoute('dashboard');
}
if ($new) {
$content = $repo->create(['contenttype' => $contentTypeKey, 'status' => $contentType['default_status']]);
} elseif ($duplicate) {
$source = $request->query->getInt('source');
$content = $repo->find($source);
} else {
$content = $repo->find($id);
}
if ($content === false) {
// Record not found, advise and redirect to the dashboard
$this->flashes()->error(Trans::__('contenttypes.generic.not-existing', ['%contenttype%' => $contentTypeKey]));
return $this->redirectToRoute('dashboard');
}
// Ensure custom entities have the legacy ContentType set
$this->app['storage.legacy_service']->setupContenttype($content);
$context = $this->recordEdit()->action($content, $content->getContenttype(), $duplicate);
$context['file_matcher'] = $this->app['filesystem.matcher'];
$context['form'] = $form->createView();
// Get the editreferrer
$referrer = $this->getEditReferrer($request);
if ($referrer) {
$content['editreferrer'] = $referrer;
}
// SBTODO: What to do with this?
if (isset($contentType['hierarchical']) && $contentType['hierarchical'] === true) {
// SBTODO: Move this JS file into core JS?
$jsFile = JavaScript::create('js/parent-change.js', 'bolt')
->setLate(true)
->setZone(Zone::BACKEND)
;
$json = Json::dump(
$this->app['storage.hierarchy']->getAllHierarchies($contenttypeslug, false),
Json::UNESCAPED | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
);
$callback = sprintf("<script>var hierarchies = JSON.parse('%s');</script>", $json);
$snippet = Snippet::create()
->setCallback($callback)
->setZone(Zone::BACKEND)
->setLocation(Target::BEFORE_HEAD_JS);
$this->app['asset.queue.file']->add($jsFile);
$this->app['asset.queue.snippet']->add($snippet);
}
// Lets delete the hierarchies cache if we're saving this record
$cacheKey = '_hierarchies_' . $contenttypeslug;
if ($request->query->has('returnto') && $this->app['cache']->contains($cacheKey)) {
$this->app['cache']->delete($cacheKey);
}
return $this->render('@bolt/editcontent/editcontent.twig', $context);
}
/**
* Calculate the parameter used to determine response.
*
* @internal to be removed when forms cut-over is complete
*
* @param Request $request
* @param Button $button
*
* @return string
*/
private function getReturnTo(Request $request, Button $button)
{
$name = $button->getName();
$isAjax = $request->isXmlHttpRequest();
if ($request->attributes->has('_test')) {
return 'test';
}
if ($name === 'save') {
return $isAjax ? 'ajax' : $name;
}
return $name;
}
/**
* Content type overview page.
*
* @param Request $request The Symfony Request
* @param string $contenttypeslug The content type slug
*
* @return \Bolt\Response\TemplateResponse|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function overview(Request $request, $contenttypeslug)
{
// Make sure the user is allowed to see this page, based on 'allowed contenttypes'
// for Editors.
if (!$this->isAllowed('contenttype:' . $contenttypeslug)) {
$this->flashes()->error(Trans::__('general.phrase.access-denied-privilege-view-page'));
return $this->redirectToRoute('dashboard');
}
$taxonomy = null;
foreach (array_keys($this->getOption('taxonomy', [])) as $taxonomyKey) {
if ($request->query->get('taxonomy-' . $taxonomyKey)) {
$taxonomy[$taxonomyKey] = $request->query->get('taxonomy-' . $taxonomyKey);
}
}
$options = (new ListingOptions())
->setOrder($request->query->get('order'))
->setPage($request->query->get('page_' . $contenttypeslug))
->setFilter($request->query->get('filter'))
->setTaxonomies($taxonomy)
->setGroupSort(true)
;
$context = [
'contenttype' => $this->getContentType($contenttypeslug),
'multiplecontent' => $this->recordListing()->action($contenttypeslug, $options),
'filter' => array_merge((array) $taxonomy, (array) $options->getFilter()),
'permissions' => $this->getContentTypeUserPermissions($contenttypeslug, $this->users()->getCurrentUser()),
];
return $this->render('@bolt/overview/overview.twig', $context);
}
/**
* Check that the user is allowed to edit the record.
*
* @param string $contenttypeslug
* @param int $id
*
* @return bool|\Symfony\Component\HttpFoundation\RedirectResponse
*/
private function checkEditAccess($contenttypeslug, $id)
{
// Is the record new or existing
$new = empty($id) ?: false;
/*
* Check the user is allowed to create/edit this record, based on:
* contenttype-all:
* contenttype-default:
* contenttypes:
* edit: []
* create: []
*/
$perm = $new ? "contenttype:$contenttypeslug:create" : "contenttype:$contenttypeslug:edit:$id";
if (!$this->isAllowed($perm)) {
$action = $new ? 'create' : 'edit';
$this->flashes()->error(Trans::__("You do not have the right privileges to $action that record."));
return $this->redirectToRoute('dashboard');
}
return false;
}
/**
* Set the editreferrer in twig if it was not set yet.
*
* @param Request $request
*
* @return string|null
*/
private function getEditReferrer(Request $request)
{
$tmp = parse_url($request->server->get('HTTP_REFERER'));
$referrer = $tmp['path'];
if (!empty($tmp['query'])) {
$referrer .= '?' . $tmp['query'];
}
if (strpos($referrer, '/overview/') !== false || ($referrer === $this->generateUrl('dashboard') . '/')) {
if ($this->getOption('general/compatibility/twig_globals', true)) {
$this->app['twig']->addGlobal('editreferrer', $referrer);
}
return $referrer;
}
return null;
}
/**
* @return \Bolt\Storage\ContentRequest\Edit
*/
protected function recordEdit()
{
return $this->app['storage.request.edit'];
}
/**
* @return \Bolt\Storage\ContentRequest\Listing
*/
protected function recordListing()
{
return $this->app['storage.request.listing'];
}
/**
* @return \Bolt\Storage\ContentRequest\Save
*/
protected function recordSave()
{
return $this->app['storage.request.save'];
}
}