1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivoaz ContentEditable bundle. |
5
|
|
|
* |
6
|
|
|
* (c) Ivo Azirjans <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivoaz\Bundle\ContentEditableBundle\Manager; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
15
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Exception\MissingLocaleException; |
16
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Model\Content; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Ivo Azirjans <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ContentManager implements ContentManagerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ObjectManager |
26
|
|
|
*/ |
27
|
|
|
private $om; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RequestStack |
31
|
|
|
*/ |
32
|
|
|
private $requests; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ObjectManager $om |
36
|
|
|
* @param RequestStack $requests |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ObjectManager $om, RequestStack $requests = null) |
39
|
|
|
{ |
40
|
|
|
$this->om = $om; |
41
|
|
|
$this->requests = $requests; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function update(Content $content) |
48
|
|
|
{ |
49
|
|
|
$managed = true; |
50
|
|
|
|
51
|
|
|
if (!$this->om->contains($content)) { |
52
|
|
|
$managed = false; |
53
|
|
|
$content = $this->om->merge($content); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->om->flush($content); |
57
|
|
|
|
58
|
|
|
if (!$managed) { |
59
|
|
|
$this->om->detach($content); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function get($name, $locale = null, $default = null) |
67
|
|
|
{ |
68
|
|
|
if (null === $locale) { |
69
|
|
|
if (null === $this->requests || !$request = $this->requests->getMasterRequest()) { |
70
|
|
|
throw new MissingLocaleException( |
71
|
|
|
'Could not get content because locale is missing. Try passing it as a parameter.' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$locale = $request->getLocale(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$content = $this->om->getRepository(Content::class) |
79
|
|
|
->findOneBy( |
80
|
|
|
[ |
81
|
|
|
'name' => $name, |
82
|
|
|
'locale' => $locale, |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
if (!$content) { |
87
|
|
|
if (null === $default) { |
88
|
|
|
$default = $name; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$content = $this->create($name, $default, $locale); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->om->detach($content); |
95
|
|
|
|
96
|
|
|
return $content; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function find($id) |
103
|
|
|
{ |
104
|
|
|
$repository = $this->om->getRepository(Content::class); |
105
|
|
|
|
106
|
|
|
return $repository->find($id); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
* @param string $text |
112
|
|
|
* @param string $locale |
113
|
|
|
* |
114
|
|
|
* @return Content |
115
|
|
|
*/ |
116
|
|
|
private function create($name, $text, $locale) |
117
|
|
|
{ |
118
|
|
|
$content = new Content(); |
119
|
|
|
$content->setName($name) |
120
|
|
|
->setText($text) |
121
|
|
|
->setLocale($locale); |
122
|
|
|
|
123
|
|
|
$this->om->persist($content); |
124
|
|
|
$this->om->flush($content); |
125
|
|
|
|
126
|
|
|
return $content; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|