1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\Controller; |
14
|
|
|
|
15
|
|
|
use Api\Controller\ApiAppController; |
16
|
|
|
use App\Model\Table\DraftsTable; |
17
|
|
|
use Cake\Http\Exception\BadRequestException; |
18
|
|
|
use Cake\Http\Exception\NotFoundException; |
19
|
|
|
use Saito\Exception\SaitoForbiddenException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Endpoint for adding/POST and editing/PUT posting |
23
|
|
|
* |
24
|
|
|
* @property DraftsTable $Drafts |
25
|
|
|
*/ |
26
|
|
|
class DraftsController extends ApiAppController |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Adds a new draft |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function add(): void |
34
|
|
|
{ |
35
|
|
|
$data = $this->getRequest()->getData(); |
36
|
|
|
$data['user_id'] = $this->CurrentUser->getId(); |
37
|
|
|
$draft = $this->Drafts->newEntity( |
38
|
|
|
$data, |
39
|
|
|
['fields' => ['pid', 'subject', 'text', 'user_id']] |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$draft = $this->Drafts->save($draft); |
43
|
|
|
|
44
|
|
|
if (!$draft) { |
45
|
|
|
throw new BadRequestException(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$response = [ |
49
|
|
|
'type' => 'drafts', |
50
|
|
|
'id' => $draft->get('id'), |
51
|
|
|
'attributes' => [ |
52
|
|
|
'id' => $draft->get('id'), |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
$this->set('data', $response); |
56
|
|
|
$this->set('_serialize', ['data']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Updates an existing draft. |
61
|
|
|
* |
62
|
|
|
* @param string $id Id of the draft to be updated. |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function edit(string $id) |
66
|
|
|
{ |
67
|
|
|
$id = (int)$id; |
68
|
|
|
try { |
69
|
|
|
$draft = $this->Drafts->get($id); |
70
|
|
|
} catch (\Throwable $e) { |
71
|
|
|
throw new NotFoundException(sprintf('Draft %s not found', $id)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($draft->get('user_id') !== $this->CurrentUser->getId()) { |
75
|
|
|
throw new SaitoForbiddenException( |
76
|
|
|
sprintf('Attempt to access draft %s.', $id), |
77
|
|
|
['CurrentUser' => $this->CurrentUser] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$data = $this->getRequest()->getData(); |
82
|
|
|
if (empty($data['text']) && empty($data['subject'])) { |
83
|
|
|
/// Don't keep empty drafts. Empty data deletes the draft. |
84
|
|
|
$this->Drafts->delete($draft); |
85
|
|
|
|
86
|
|
|
// Clear out the draft-id in the frontend, so a potential restarted |
87
|
|
|
// draft is going to trigger an add() in the frontend which starts a |
88
|
|
|
// new draft. |
89
|
|
|
$response = [ |
90
|
|
|
'type' => 'drafts', |
91
|
|
|
'id' => null, |
92
|
|
|
'attributes' => [ |
93
|
|
|
'id' => null, |
94
|
|
|
] |
95
|
|
|
]; |
96
|
|
|
$this->set('data', $response); |
97
|
|
|
} else { |
98
|
|
|
$this->Drafts->patchEntity( |
99
|
|
|
$draft, |
100
|
|
|
$data, |
101
|
|
|
['fields' => ['subject', 'text']] |
102
|
|
|
); |
103
|
|
|
$this->Drafts->save($draft); |
104
|
|
|
$this->set('data', []); |
105
|
|
|
} |
106
|
|
|
$this->set('_serialize', ['data']); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|