-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkshopController.php
executable file
·464 lines (394 loc) · 9.96 KB
/
WorkshopController.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
namespace Statamic\Addons\Workshop;
use Statamic\API\URL;
use Statamic\API\Form;
use Statamic\API\Page;
use Statamic\API\Crypt;
use Statamic\API\Entry;
use Statamic\API\Content;
use Statamic\API\Request;
use Statamic\API\Fieldset;
use Statamic\API\User; //DANIELSON
use Statamic\Extend\Listener;
use Illuminate\Http\Response;
use Statamic\Extend\Controller;
use Stringy\StaticStringy as Stringy;
use Statamic\CP\Publish\ValidationBuilder;
class WorkshopController extends Controller
{
/**
* The factory object to work with.
*
* @var array
*/
public $factory;
/**
* The data with which to create a content file.
*
* @var array
*/
public $fields = [];
/**
* Fields that should be stripped out as meta data.
*
* @var array
*/
private $meta = [
'id',
'collection',
'date',
'fieldset',
'order',
'published',
'parent',
'redirect',
'slug',
'slugify',
'user', //DANIELSON
'username' //DANIELSON
];
/**
* The content's id
*
* @var string
*/
private $id;
/**
* An entry's optional date.
*
* @var string
*/
private $date;
/**
* The content's slug. By default will be a slugifed 'title'.
*
* @var string
*/
private $slug;
/**
* An entry's collection. Where it belongs.
*
* @var string
*/
private $collection;
/**
* The fieldset. The thing that rules them all.
*
* @var string
*/
private $fieldset;
/**
* An entry or page's Order key.
*
* @var string
*/
private $order;
/**
* A page's optional parent page.
*
* @var string
*/
private $parent = '/';
/**
* The published status of the content.
*
* @var string
*/
private $published = true;
/**
* The URL to redirect the user to upon success
*
* @var string
*/
private $redirect;
/**
* The field to slugify to create the slug.
*
* @var string
*/
private $slugify = 'title';
//DANIELSON
/**
* The user. By default will be current user.
*
* @var string
*/
private $user;
//DANIELSON
/**
* The username of the person being edited.
*
* @var string
*/
private $username;
/**
* Manipulate common request data across all types
* of content, big and small.
*
* @return void
*/
public function init()
{
if ( ! $this->isAllowed()) {
return redirect()->back();
}
$this->fields = Request::all();
$this->filter();
$this->startFactory();
$this->setFieldsetAndMore();
$this->slugify();
}
/**
* Create an entry in a collection.
*
* @return request
*/
public function postEntryCreate()
{
if ( ! $this->collection) {
// TODO: Throw an exception and/or return an error message
dd('Come on now. You need a collection.');
}
$validator = $this->runValidation();
if ($validator->fails()) {
return back()->withInput()->withErrors($validator, 'workshop');
}
$this->factory = Entry::create($this->slug)
->collection($this->collection->path())
->with($this->fields);
if ($this->collection->order() == 'date') {
$this->factory->date($this->date);
} elseif ($this->order) {
$this->factory->order($this->order);
}
$this->factory = $this->factory->get();
return $this->save();
}
/**
* Update an entry in a collection.
*
* @return request
*/
public function postEntryUpdate()
{
return $this->update();
}
/**
* Create a page.
*
* @return request
*/
public function postPageCreate()
{
$validator = $this->runValidation();
if ($validator->fails()) {
return back()->withInput()->withErrors($validator);
}
$url = URL::assemble($this->parent, $this->slug);
$this->factory = Page::create($url)
->with($this->fields)
->get();
return $this->save();
}
/**
* Update a page.
*
* @return request
*/
public function postPageUpdate()
{
return $this->update();
}
//DANIELSON
/**
* Update a user.
*
* @return request
*/
public function postUserUpdate()
{
return $this->update();
}
/**
* Update a global.
*
* @return request
*/
public function postGlobalUpdate()
{
return $this->update();
}
/**
* Update a content file with new data.
*
* @return request
*/
private function update()
{
$validator = $this->runValidation();
if ($validator->fails()) {
return back()->withInput()->withErrors($validator);
}
if ($this->username) {//DANIELSON - just remove the if/else to reset this
$user = $this->getUser();
$data = array_merge($user->data(), $this->fields);
$user->data($data);
$user->save();
if ($this->redirect) {//DANIELSON - stole this bit from the save() function, below
return redirect($this->getRedirect());
};
return redirect()->back();
} else {
$data = array_merge($this->factory->data(), $this->fields);
$this->factory->data($data);
return $this->save();
}
}
/**
* Get the Validator instance
*
* @return mixed
*/
private function runValidation()
{
if ($this->username) {//DANIELSON
$fields = array_merge($this->fields, ['username' => 'required']);
} else {
$fields = array_merge($this->fields, ['slug' => 'required']);
}
$builder = new ValidationBuilder(['fields' => $fields], $this->fieldset);
$builder->build();
return \Validator::make(['fields' => $fields], $builder->rules());
}
/**
* Save the factory object, run the hook,
* and redirect as needed.
*
* @return mixed
*/
private function save()
{
$this->factory->ensureId();
$this->factory->save();
$this->flash->put('success', true);
if ($this->redirect) {
return redirect($this->getRedirect());
};
return redirect()->back();
}
private function startFactory()
{
if ($this->id) {
$this->factory = Content::uuidRaw($this->id);
}
}
//DANIELSON
/**
* Get user content by id, falling back to the current user
*
* @return Content
*/
private function getUser()
{
$username = $this->username;
if ($username) {
$user = User::whereUsername($username);
} else {
$user = User::getCurrent();
}
return $user;
}
/**
* Set the slug based on another field. Defaults to title.
*
* @return void
*/
private function slugify()
{
if ($this->username) {//DANIELSON
$slugify = 'username';
}
$sluggard = array_get($this->fields, $this->slugify, current($this->fields));
$this->slug = Stringy::slugify($sluggard);
}
/**
* Filter out any meta fields from the request object and
* and assign them to class variables, leaving you with
* a nice and clean $fields variable to work with.
*
* @return void
*/
private function filter()
{
// Filter the HTML form data first
foreach ($this->fields as $key => $field) {
if (in_array($key, $this->meta)) {
$this->{$key} = $field;
unset($this->fields[$key]);
}
}
// And override those with special meta fields set
// on the tag itself as parameters
if (array_get($this->fields, '_meta')) {
$meta = Crypt::decrypt($this->fields['_meta']);
foreach ($meta as $key => $field) {
if (in_array($key, $this->meta)) {
$this->{$key} = $field;
}
}
unset($this->fields['_meta']);
}
}
/**
* Find and set the Fieldset object, if there is one.
*
* @return void
*/
private function setFieldsetAndMore()
{
// Set that collection
if ($this->collection) {
$this->collection = Content::collection($this->collection);
}
// Set that fieldset
if ($this->fieldset) {
$this->fieldset = Fieldset::get($this->fieldset);
} elseif ($this->username) {//DANIELSON
$user = $this->getUser();
$this->fieldset = $user->fieldset();
} elseif ($this->factory) {
$this->fieldset = $this->factory->fieldset();
} elseif ($this->collection) {
$this->fieldset = $this->collection->fieldset();
}
// Drop any field that's not in the fieldset
if ($this->fieldset && $this->getConfig('whitelist')) {
$this->fields = array_intersect_key($this->fields, array_flip(array_keys($this->fieldset->fields())));
}
}
/**
* Find and set the redirect URL.
*
* @return void
*/
private function getRedirect()
{
if ($this->redirect == 'url') {
return $this->factory->urlPath();
}
return $this->redirect;
}
/**
* Checks to see if the user is allowed to use the Workshop.
* Not everyone is so lucky.
*
* @return bool
*/
private function isAllowed()
{
if ($this->getConfig('enforce_auth') && ! \Auth::check()) {
return false;
};
return true;
}
}