-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveJoinAction.php
98 lines (79 loc) · 2.59 KB
/
RemoveJoinAction.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
<?php
namespace Charcoal\Admin\Action;
use Exception;
// From PSR-7
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
// From 'charcoal-admin'
use Charcoal\Admin\AdminAction;
// From 'charcoal-core'
use Charcoal\Loader\CollectionLoader;
// From 'charcoal-attachment'
use Charcoal\Attachment\Object\Attachment;
use Charcoal\Attachment\Object\Join;
/**
* Disconnect two objects
*/
class RemoveJoinAction extends AdminAction
{
/**
* @param RequestInterface $request A PSR-7 compatible Request instance.
* @param ResponseInterface $response A PSR-7 compatible Response instance.
* @return ResponseInterface
*/
public function run(RequestInterface $request, ResponseInterface $response)
{
$params = $request->getParams();
if (
!isset($params['attachment_id']) ||
!isset($params['obj_id']) ||
!isset($params['obj_type']) ||
!isset($params['group'])
) {
$this->setSuccess(false);
return $response;
}
$attachmentId = $params['attachment_id'];
$objId = $params['obj_id'];
$objType = $params['obj_type'];
$group = $params['group'];
// Try loading the object
try {
$obj = $this->modelFactory()->create($objType)->load($objId);
} catch (Exception $e) {
$this->setSuccess(false);
return $response;
}
$joinProto = $this->modelFactory()->create(Join::class);
if (!$joinProto->source()->tableExists()) {
$joinProto->source()->createTable();
}
$loader = new CollectionLoader([
'logger' => $this->logger,
'factory' => $this->modelFactory()
]);
$loader
->setModel($joinProto)
->addFilter('object_type', $objType)
->addFilter('object_id', $objId)
->addFilter('attachment_id', $attachmentId)
->addFilter('group', $group);
$existingJoins = $loader->load();
// Should be just one, tho.
foreach ($existingJoins as $joinModel) {
$joinModel->delete();
}
// Try loading the attachment
try {
$attachment = $this->modelFactory()->create(Attachment::class)->load($attachmentId);
if ($attachment['id'] !== null) {
$attachment->delete();
}
} catch (Exception $error) {
$this->setSuccess(false);
return $response;
}
$this->setSuccess(true);
return $response;
}
}