1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\Action; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DBALException; |
6
|
|
|
use eZ\Publish\API\Repository\Repository; |
7
|
|
|
use eZ\Publish\API\Repository\Values\User\User; |
8
|
|
|
use eZ\Publish\Core\Repository\Values\Content\Content; |
9
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollection; |
10
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Event\InformationCollected; |
11
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Exception\ActionFailedException; |
12
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Factory\FieldDataFactory; |
13
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository; |
14
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository; |
15
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Value\LegacyData; |
16
|
|
|
|
17
|
|
|
class DatabaseAction implements ActionInterface, CrucialActionInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var FieldDataFactory |
21
|
|
|
*/ |
22
|
|
|
protected $factory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EzInfoCollectionRepository |
26
|
|
|
*/ |
27
|
|
|
protected $infoCollectionRepository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EzInfoCollectionAttributeRepository |
31
|
|
|
*/ |
32
|
|
|
protected $infoCollectionAttributeRepository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Repository |
36
|
|
|
*/ |
37
|
|
|
protected $repository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* PersistToDatabaseAction constructor. |
41
|
|
|
* |
42
|
|
|
* @param FieldDataFactory $factory |
43
|
|
|
* @param EzInfoCollectionRepository $infoCollectionRepository |
44
|
|
|
* @param EzInfoCollectionAttributeRepository $infoCollectionAttributeRepository |
45
|
|
|
* @param Repository $repository |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
FieldDataFactory $factory, |
49
|
|
|
EzInfoCollectionRepository $infoCollectionRepository, |
50
|
|
|
EzInfoCollectionAttributeRepository $infoCollectionAttributeRepository, |
51
|
|
|
Repository $repository |
52
|
|
|
) { |
53
|
|
|
$this->factory = $factory; |
54
|
|
|
$this->infoCollectionRepository = $infoCollectionRepository; |
55
|
|
|
$this->infoCollectionAttributeRepository = $infoCollectionAttributeRepository; |
56
|
|
|
$this->repository = $repository; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function act(InformationCollected $event) |
63
|
|
|
{ |
64
|
|
|
$struct = $event->getInformationCollectionStruct(); |
65
|
|
|
$contentType = $event->getContentType(); |
66
|
|
|
$location = $event->getLocation(); |
67
|
|
|
|
68
|
|
|
/** @var Content $content */ |
69
|
|
|
$content = $this->repository->getContentService()->loadContent($location->contentInfo->id); |
70
|
|
|
|
71
|
|
|
/** @var User $currentUser */ |
72
|
|
|
$currentUser = $this->repository->getCurrentUser(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
/** @var EzInfoCollection $ezInfo */ |
75
|
|
|
$ezInfo = $this->infoCollectionRepository->createWithValues($location, $currentUser); |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
$this->infoCollectionRepository->save($ezInfo); |
79
|
|
|
} catch (DBALException $e) { |
80
|
|
|
throw new ActionFailedException('database', $e->getMessage()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var string |
85
|
|
|
* @var \eZ\Publish\Core\FieldType\Value $value |
86
|
|
|
*/ |
87
|
|
|
foreach ($struct->getCollectedFields() as $fieldDefIdentifier => $value) { |
88
|
|
|
/** @var LegacyData $value */ |
89
|
|
|
$value = $this->factory->getLegacyValue($value, $contentType->getFieldDefinition($fieldDefIdentifier)); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$ezInfoAttribute = $this->infoCollectionAttributeRepository |
92
|
|
|
->createWithValues($location, $ezInfo, $content->getField($fieldDefIdentifier)->id, $value); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$this->infoCollectionAttributeRepository->save($ezInfoAttribute); |
96
|
|
|
} catch (DBALException $e) { |
97
|
|
|
throw new ActionFailedException('database', $e->getMessage()); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.