1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Attachment\Object; |
4
|
|
|
|
5
|
|
|
// From Pimple |
6
|
|
|
use Pimple\Container as ServiceContainer; |
7
|
|
|
|
8
|
|
|
// From 'charcoal-config' |
9
|
|
|
use Charcoal\Config\ConfigurableInterface; |
10
|
|
|
|
11
|
|
|
// From 'charcoal-attachment' |
12
|
|
|
use Charcoal\Attachment\Interfaces\AttachmentAwareInterface; |
13
|
|
|
use Charcoal\Attachment\Interfaces\AttachmentContainerInterface; |
14
|
|
|
use Charcoal\Attachment\Traits\AttachmentAwareTrait; |
15
|
|
|
use Charcoal\Attachment\Traits\AttachmentContainerTrait; |
16
|
|
|
use Charcoal\Attachment\Traits\ConfigurableAttachmentsTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Gallery Attachment Type |
20
|
|
|
* |
21
|
|
|
* This type allows for nesting of additional attachment types. |
22
|
|
|
*/ |
23
|
|
|
class Container extends Attachment implements |
|
|
|
|
24
|
|
|
AttachmentAwareInterface, |
25
|
|
|
AttachmentContainerInterface, |
26
|
|
|
ConfigurableInterface |
27
|
|
|
{ |
28
|
|
|
use AttachmentAwareTrait; |
29
|
|
|
use AttachmentContainerTrait; |
30
|
|
|
use ConfigurableAttachmentsTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Inject dependencies from a DI Container. |
34
|
|
|
* |
35
|
|
|
* @param ServiceContainer $container A dependencies container instance. |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
protected function setDependencies(ServiceContainer $container) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
parent::setDependencies($container); |
41
|
|
|
|
42
|
|
|
if (isset($container['attachments/config'])) { |
43
|
|
|
$this->setConfig($container['attachments/config']); |
44
|
|
|
} elseif (isset($container['config']['attachments'])) { |
45
|
|
|
$this->setConfig($container['config']['attachments']); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Retrieve the objects associated to the current object. |
51
|
|
|
* |
52
|
|
|
* @param mixed ...$args Filter the attachments; |
53
|
|
|
* options accepted by {@see AttachmentAwareTrait::attachments()}. |
54
|
|
|
* @return Collection|Attachment[] |
55
|
|
|
*/ |
56
|
|
|
public function attachments(...$args) |
57
|
|
|
{ |
58
|
|
|
$attachables = $this->attachableObjects(); |
59
|
|
|
$attachments = call_user_func_array([ $this, 'getAttachments' ], $args); |
60
|
|
|
|
61
|
|
|
foreach ($attachments as $attachment) { |
62
|
|
|
if (isset($attachables[$attachment->objType()])) { |
63
|
|
|
$attachment->attachmentType = $attachables[$attachment->objType()]; |
64
|
|
|
} else { |
65
|
|
|
$attachment->attachmentType = []; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $attachments; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Event called before _deleting_ the attachment. |
74
|
|
|
* |
75
|
|
|
* @return boolean |
76
|
|
|
* @see Charcoal\Attachment\Traits\AttachmentAwareTrait::removeJoins |
77
|
|
|
* @see Charcoal\Source\StorableTrait::preDelete() For the "create" Event. |
78
|
|
|
*/ |
79
|
|
|
public function preDelete() |
80
|
|
|
{ |
81
|
|
|
// Delete nested attachments |
82
|
|
|
array_map(function($attachment) { |
83
|
|
|
$attachment->delete(); |
84
|
|
|
}, $this->attachments()->values()); |
85
|
|
|
|
86
|
|
|
return parent::preDelete(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|