|
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
|
9 |
|
public function __construct(ObjectManager $om, RequestStack $requests = null) |
|
39
|
|
|
{ |
|
40
|
9 |
|
$this->om = $om; |
|
41
|
9 |
|
$this->requests = $requests; |
|
42
|
9 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function update(Content $content) |
|
48
|
|
|
{ |
|
49
|
2 |
|
$managed = true; |
|
50
|
|
|
|
|
51
|
2 |
|
if (!$this->om->contains($content)) { |
|
52
|
1 |
|
$managed = false; |
|
53
|
1 |
|
$content = $this->om->merge($content); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
$this->om->flush($content); |
|
57
|
|
|
|
|
58
|
2 |
|
if (!$managed) { |
|
59
|
1 |
|
$this->om->detach($content); |
|
60
|
1 |
|
} |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
7 |
|
public function get($name, $locale = null, $default = null) |
|
67
|
|
|
{ |
|
68
|
7 |
|
if (null === $locale) { |
|
69
|
2 |
|
if (null === $this->requests || !$request = $this->requests->getMasterRequest()) { |
|
70
|
1 |
|
throw new MissingLocaleException( |
|
71
|
|
|
'Could not get content because locale is missing. Try passing it as a parameter.' |
|
72
|
1 |
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$locale = $request->getLocale(); |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
6 |
|
$content = $this->om->getRepository(Content::class) |
|
79
|
6 |
|
->findOneBy( |
|
80
|
|
|
[ |
|
81
|
6 |
|
'name' => $name, |
|
82
|
6 |
|
'locale' => $locale, |
|
83
|
|
|
] |
|
84
|
6 |
|
); |
|
85
|
|
|
|
|
86
|
6 |
|
if (!$content) { |
|
87
|
4 |
|
if (null === $default) { |
|
88
|
2 |
|
$default = $name; |
|
89
|
2 |
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
$content = $this->create($name, $default, $locale); |
|
92
|
4 |
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
$this->om->detach($content); |
|
95
|
|
|
|
|
96
|
6 |
|
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
|
4 |
|
private function create($name, $text, $locale) |
|
117
|
|
|
{ |
|
118
|
4 |
|
$content = new Content(); |
|
119
|
4 |
|
$content->setName($name) |
|
120
|
4 |
|
->setText($text) |
|
121
|
4 |
|
->setLocale($locale); |
|
122
|
|
|
|
|
123
|
4 |
|
$this->om->persist($content); |
|
124
|
4 |
|
$this->om->flush($content); |
|
125
|
|
|
|
|
126
|
4 |
|
return $content; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|