1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[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 ONGR\SettingsBundle\Controller; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\Result\DocumentIterator; |
15
|
|
|
use ONGR\ElasticsearchBundle\Service\Repository; |
16
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation; |
17
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\TopHitsAggregation; |
18
|
|
|
use ONGR\ElasticsearchDSL\Search; |
19
|
|
|
use ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue; |
20
|
|
|
use ONGR\SettingsBundle\Document\Setting; |
21
|
|
|
use ONGR\SettingsBundle\Service\SettingsManager; |
22
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
23
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class ProfileController. Placeholder for settings bundle profiles page. |
29
|
|
|
*/ |
30
|
|
|
class ProfilesController extends Controller |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Renders profiles page. |
34
|
|
|
* |
35
|
|
|
* @param Request $request |
36
|
|
|
* |
37
|
|
|
* @return Response |
38
|
|
|
*/ |
39
|
|
|
public function listAction(Request $request) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
return $this->render( |
42
|
|
|
'ONGRSettingsBundle:Profiles:list.html.twig', |
43
|
|
|
[] |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns a json list of profiles |
49
|
|
|
* |
50
|
|
|
* @param Request $request |
51
|
|
|
* |
52
|
|
|
* @return Response |
53
|
|
|
*/ |
54
|
|
|
public function getAllProfilesAction(Request $request) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$profiles = []; |
57
|
|
|
/** @var Repository $repo */ |
58
|
|
|
$repo = $this->get($this->getParameter('ongr_settings.repo')); |
59
|
|
|
|
60
|
|
|
/** @var DocumentIterator $result */ |
61
|
|
|
$result = $repo->execute( |
62
|
|
|
($repo->createSearch())->addAggregation(new TermsAggregation('profiles', 'profile')) |
63
|
|
|
); |
64
|
|
|
/** @var AggregationValue $agg */ |
65
|
|
|
foreach ($result->getAggregation('profiles') as $agg) { |
|
|
|
|
66
|
|
|
$profiles[] = $agg->getValue('key'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (empty($profiles) || !in_array('default', $profiles)) array_unshift($profiles, 'default'); |
70
|
|
|
|
71
|
|
|
return new JsonResponse($profiles); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a json list of profiles |
76
|
|
|
* |
77
|
|
|
* @return Response |
78
|
|
|
*/ |
79
|
|
|
public function getFullProfilesAction() |
80
|
|
|
{ |
81
|
|
|
$profiles = $this->get('ongr_settings.settings_manager')->getAllProfiles(); |
82
|
|
|
|
83
|
|
|
return new JsonResponse( |
84
|
|
|
['count' => count($profiles), 'documents' => $profiles] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function toggleProfileAction(Request $request) |
89
|
|
|
{ |
90
|
|
|
$settingName = $this->getParameter('ongr_settings.active_profiles'); |
91
|
|
|
$manager = $this->get('ongr_settings.settings_manager'); |
92
|
|
|
|
93
|
|
|
#TODO Not sure where is the right place to put active profiles setting initiating |
94
|
|
|
if (!$manager->has($settingName)) { |
95
|
|
|
$manager->create( |
96
|
|
|
[ |
97
|
|
|
'name' => $settingName, |
98
|
|
|
'value' => [], |
99
|
|
|
'type' => 'hidden', |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$profileName = $request->get('name'); |
105
|
|
|
$activeProfiles = (array)$manager->getValue($settingName, []); |
106
|
|
|
|
107
|
|
|
$key = array_search($profileName, $activeProfiles); |
108
|
|
|
if ($key === false) { |
109
|
|
|
$activeProfiles[] = $profileName; |
110
|
|
|
} else { |
111
|
|
|
unset($activeProfiles[$key]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$manager->update($settingName, [ |
115
|
|
|
'value' => $activeProfiles |
116
|
|
|
]); |
117
|
|
|
$this->get('ong_settings.cache_provider')->deleteAll(); |
118
|
|
|
|
119
|
|
|
return new JsonResponse(['error' => false]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.