|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Settings\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Auth\Entity\UserInterface; |
|
6
|
|
|
use Core\Entity\IdentifiableEntityInterface; |
|
7
|
|
|
use Doctrine\ODM\MongoDB\LockMode; |
|
8
|
|
|
use Interop\Container\ContainerInterface; |
|
9
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface; |
|
10
|
|
|
use Laminas\Mvc\MvcEvent; |
|
11
|
|
|
use Settings\Entity\SettingsContainer as SettingsEntity; |
|
12
|
|
|
use Core\Repository\AbstractRepository; |
|
13
|
|
|
|
|
14
|
|
|
class Settings extends AbstractRepository |
|
15
|
|
|
{ |
|
16
|
|
|
protected $userRepository; |
|
17
|
|
|
protected $settingsByUser; |
|
18
|
|
|
protected $container; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->settingsByUser = array(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function setContainer(ContainerInterface $container) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->container = $container; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function getContainer() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->container; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function setUserRepository($userRepository) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->userRepository = $userRepository; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function getUserRepository() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->userRepository; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* the $id for an entity 'setting' is the same as for the entity 'user' |
|
47
|
|
|
* @param UserInterface $user |
|
48
|
|
|
* @return mixed|SettingsEntity|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getSettingsByUser($user) |
|
51
|
|
|
{ |
|
52
|
|
|
$userId = $user; |
|
53
|
|
|
$userEntity = null; |
|
54
|
|
|
if ($user instanceof IdentifiableEntityInterface) { |
|
|
|
|
|
|
55
|
|
|
$userId = $user->getId(); |
|
56
|
|
|
$userEntity = $user; |
|
57
|
|
|
} |
|
58
|
|
|
if (isset($this->settingsByUser[$userId])) { |
|
59
|
|
|
return $this->settingsByUser[$user]; |
|
60
|
|
|
} |
|
61
|
|
|
if (empty($userEntity)) { |
|
62
|
|
|
$UserRepository = $this->getUserRepository(); |
|
63
|
|
|
$userEntity = $UserRepository->find($userId); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (isset($userEntity)) { |
|
67
|
|
|
$settingsData = $userEntity->getSettings(); |
|
68
|
|
|
$this->settingsByUser[$userId] = new SettingsEntity(); |
|
69
|
|
|
$this->settingsByUser[$userId]->setData($settingsData)->spawnAsEntities(); |
|
70
|
|
|
return $this->settingsByUser[$userId]; |
|
71
|
|
|
} |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function onPostDispatch(MvcEvent $e) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
//throw new \Exception("Test"); |
|
78
|
|
|
$UserRepository = $this->getUserRepository(); |
|
79
|
|
|
foreach ($this->settingsByUser as $user => $settings) { |
|
80
|
|
|
if (!empty($user) && $settings->hasChanged()) { |
|
81
|
|
|
$userEntity = $UserRepository->find($user); |
|
82
|
|
|
$a = $settings->toArray(); |
|
83
|
|
|
$userEntity->setSettings($a); |
|
84
|
|
|
$id = $UserRepository->save($userEntity); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function find($user, $lockMode = LockMode::NONE, ?int $lockVersion = null): ?object |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->getSettingsByUser($user); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getEntityByStrategy($namespace) |
|
95
|
|
|
{ |
|
96
|
|
|
$configAccess = $this->getContainer()->get('ConfigAccess'); |
|
97
|
|
|
$settings = $configAccess->getByKey('settings'); |
|
98
|
|
|
if (array_key_exists($namespace, $settings) && array_key_exists('entity', $settings[$namespace])) { |
|
99
|
|
|
$entity = new $settings[$namespace]['entity']; |
|
100
|
|
|
$entity->setConfig($settings[$namespace]); |
|
101
|
|
|
return $entity; |
|
102
|
|
|
} |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getFormular($formular = null) |
|
107
|
|
|
{ |
|
108
|
|
|
$form = null; |
|
109
|
|
|
if (isset($formular) && is_string($formular)) { |
|
110
|
|
|
$formElementManager = $this->getContainer()->get('FormElementManager'); |
|
111
|
|
|
if ($formElementManager->has($formular)) { |
|
112
|
|
|
$form = $formElementManager->get($formular); |
|
113
|
|
|
} |
|
114
|
|
|
if (!isset($form)) { |
|
115
|
|
|
if ($this->getContainer()->has($formular)) { |
|
116
|
|
|
$form = $this->getContainer()->get($formular); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
return $form; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|