1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\TranslationBundle\Admin\Extension\Phpcr; |
13
|
|
|
|
14
|
|
|
use Doctrine\ODM\PHPCR\DocumentManager; |
15
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
16
|
|
|
use Sonata\TranslationBundle\Admin\Extension\AbstractTranslatableAdminExtension; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Nicolas Bastien <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class TranslatableAdminExtension extends AbstractTranslatableAdminExtension |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param AdminInterface $admin |
25
|
|
|
* |
26
|
|
|
* @return DocumentManager |
27
|
|
|
*/ |
28
|
|
|
protected function getDocumentManager(AdminInterface $admin) |
29
|
|
|
{ |
30
|
|
|
return $admin->getModelManager()->getDocumentManager(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function alterObject(AdminInterface $admin, $object) |
37
|
|
|
{ |
38
|
|
|
$locale = $this->getTranslatableLocale($admin); |
39
|
|
|
$documentManager = $this->getDocumentManager($admin); |
40
|
|
|
$unitOfWork = $documentManager->getUnitOfWork(); |
41
|
|
|
|
42
|
|
|
if ($this->getTranslatableChecker()->isTranslatable($object) && ($unitOfWork->getCurrentLocale($object) != $locale)) { |
43
|
|
|
$object = $this->getDocumentManager($admin)->findTranslation($admin->getClass(), $object->getId(), $locale); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function preUpdate(AdminInterface $admin, $object) |
51
|
|
|
{ |
52
|
|
|
$locale = $this->getTranslatableLocale($admin); |
53
|
|
|
$documentManager = $this->getDocumentManager($admin); |
54
|
|
|
$unitOfWork = $documentManager->getUnitOfWork(); |
55
|
|
|
|
56
|
|
|
if ($this->getTranslatableChecker()->isTranslatable($object) && ($unitOfWork->getCurrentLocale($object) != $locale)) { |
57
|
|
|
$documentManager->bindTranslation($object, $locale); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.