1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: wechsler |
5
|
|
|
* Date: 28/01/2017 |
6
|
|
|
* Time: 16:08 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Phase\TakeATicketBundle\Controller; |
10
|
|
|
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
class AjaxController extends BaseController |
16
|
|
|
{ |
17
|
|
|
const MANAGER_REQUIRED_ROLE = 'ROLE_ADMIN'; |
18
|
|
|
const BAND_IDENTIFIER_BAND_NAME = 1; |
19
|
|
|
const BAND_IDENTIFIER_PERFORMERS = 2; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int Whether to identify band by its name or a list of performers |
23
|
|
|
*/ |
24
|
|
|
protected $bandIdentifier = self::BAND_IDENTIFIER_PERFORMERS; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return JsonResponse |
28
|
|
|
* |
29
|
|
|
* FIXME does not honour config.displayOptions.songInPreview - always shows track! |
30
|
|
|
*/ |
31
|
|
|
public function nextSongsAction() |
32
|
|
|
{ |
33
|
|
|
// $this->setJsonErrorHandler(); |
|
|
|
|
34
|
|
|
// $includePrivate = $this->app['security']->isGranted(self::MANAGER_REQUIRED_ROLE); |
|
|
|
|
35
|
|
|
$includePrivate = false; |
36
|
|
|
$next = $this->getDataStore()->fetchUpcomingTickets($includePrivate); |
37
|
|
View Code Duplication |
if ($includePrivate) { |
|
|
|
|
38
|
|
|
$show = $next; |
39
|
|
|
} else { |
40
|
|
|
$show = []; |
41
|
|
|
foreach ($next as $k => $ticket) { |
42
|
|
|
$show[$k] = $ticket; |
43
|
|
|
if ($ticket['blocking']) { |
44
|
|
|
break; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return new JsonResponse($show); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function songSearchAction(Request $request) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$searchString = $request->get('searchString'); |
55
|
|
|
$searchCount = 10; |
56
|
|
|
if ($request->get('searchCount')) { |
57
|
|
|
$searchCount = $request->get('searchCount'); |
58
|
|
|
} |
59
|
|
|
$songs = $this->getDataStore()->findSongsBySearchString($searchString, $searchCount); |
60
|
|
|
|
61
|
|
|
$jsonResponse = new JsonResponse(['ok' => 'ok', 'searchString' => $searchString, 'songs' => $songs]); |
62
|
|
|
|
63
|
|
|
return $jsonResponse; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function useTicketAction(Request $request) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
// $this->setJsonErrorHandler(); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->denyAccessUnlessGranted(self::MANAGER_REQUIRED_ROLE); |
71
|
|
|
|
72
|
|
|
$id = $request->get('ticketId'); |
73
|
|
|
$res = $this->getDataStore()->markTicketUsedById($id); |
74
|
|
|
//FIXME fetch ticket with extra data |
75
|
|
|
if ($res) { |
76
|
|
|
$jsonResponse = new JsonResponse(['ok' => 'ok']); |
77
|
|
|
} else { |
78
|
|
|
$jsonResponse = new JsonResponse(['ok' => 'fail'], 500); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $jsonResponse; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function remotesRedirectAction() |
85
|
|
|
{ |
86
|
|
|
return new JsonResponse($this->getDataStore()->getSetting('remotesUrl')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function saveTicketAction(Request $request) |
90
|
|
|
{ |
91
|
|
|
// $this->setJsonErrorHandler(); |
|
|
|
|
92
|
|
|
$this->denyAccessUnlessGranted(self::MANAGER_REQUIRED_ROLE); |
93
|
|
|
|
94
|
|
|
$title = $request->get('title'); |
95
|
|
|
$songKey = $request->get('songId'); |
96
|
|
|
$band = $request->get('band') ?: []; // band must be array even if null (as passed by AJAX if no performers) |
97
|
|
|
$private = $request->get('private') === 'true' ? 1 : 0; |
98
|
|
|
$blocking = $request->get('blocking') === 'true' ? 1 : 0; |
99
|
|
|
$existingTicketId = $request->get('existingTicketId'); |
100
|
|
|
|
101
|
|
|
$song = null; |
102
|
|
|
$songId = null; |
103
|
|
|
|
104
|
|
View Code Duplication |
if (preg_match('/^[a-f0-9]{6}$/i', $songKey)) { |
|
|
|
|
105
|
|
|
$song = $this->getDataStore()->fetchSongByKey($songKey); |
106
|
|
|
} elseif (preg_match('/^\d+$/', $songKey)) { |
107
|
|
|
$song = $this->getDataStore()->fetchSongById($songKey); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($song) { |
111
|
|
|
$songId = $song['id']; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!$title) { |
115
|
|
|
$title = null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($existingTicketId) { |
119
|
|
|
$ticketId = $existingTicketId; |
120
|
|
|
} else { |
121
|
|
|
$ticketId = $this->getDataStore()->storeNewTicket($title, $songId); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// update even new tickets so that we can add any new columns easily |
125
|
|
|
$updated = ['title' => $title, 'songId' => $songId, 'blocking' => $blocking, 'private' => $private]; |
126
|
|
|
|
127
|
|
|
// $this->app['logger']->debug("Updating ticket", $updated); |
|
|
|
|
128
|
|
|
$this->getDataStore()->updateTicketById( |
129
|
|
|
$ticketId, |
130
|
|
|
$updated |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
if ($this->bandIdentifier === self::BAND_IDENTIFIER_PERFORMERS) { |
134
|
|
|
$this->getDataStore()->storeBandToTicket($ticketId, $band); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$ticket = $this->getDataStore()->fetchTicketById($ticketId); |
138
|
|
|
|
139
|
|
|
$ticket = $this->getDataStore()->expandTicketData($ticket); |
140
|
|
|
|
141
|
|
|
$responseData = [ |
142
|
|
|
'ticket' => $ticket, |
143
|
|
|
'performers' => $this->getDataStore()->generatePerformerStats(), |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
if ($ticketId) { |
147
|
|
|
$jsonResponse = new JsonResponse($responseData); |
148
|
|
|
} else { |
149
|
|
|
$jsonResponse = new JsonResponse($responseData, 500); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $jsonResponse; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function deleteTicketAction(Request $request) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
// $this->setJsonErrorHandler(); |
|
|
|
|
158
|
|
|
|
159
|
|
|
$this->denyAccessUnlessGranted(self::MANAGER_REQUIRED_ROLE); |
160
|
|
|
|
161
|
|
|
$id = $request->get('ticketId'); |
162
|
|
|
$res = $this->getDataStore()->deleteTicketById($id); |
163
|
|
|
if ($res) { |
164
|
|
|
$jsonResponse = new JsonResponse(['ok' => 'ok']); |
165
|
|
|
} else { |
166
|
|
|
$jsonResponse = new JsonResponse(['ok' => 'fail'], 500); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $jsonResponse; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function newTicketOrderAction(Request $request) |
173
|
|
|
{ |
174
|
|
|
// $this->setJsonErrorHandler(); |
|
|
|
|
175
|
|
|
|
176
|
|
|
$this->denyAccessUnlessGranted(self::MANAGER_REQUIRED_ROLE); |
177
|
|
|
|
178
|
|
|
$idOrder = $request->get('idOrder'); |
179
|
|
|
|
180
|
|
|
if (!is_array($idOrder)) { |
181
|
|
|
throw new \InvalidArgumentException('Order must be array!'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// $this->app['logger']->debug('New order: '.print_r($idOrder, true)); |
|
|
|
|
185
|
|
|
|
186
|
|
|
$res = true; |
187
|
|
|
foreach ($idOrder as $offset => $id) { |
188
|
|
|
$res = $res && $this->getDataStore()->updateTicketOffsetById($id, $offset); |
189
|
|
|
} |
190
|
|
|
if ($res) { |
191
|
|
|
$jsonResponse = new JsonResponse(['ok' => 'ok']); |
192
|
|
|
} else { |
193
|
|
|
// $this->app['logger']->warn('Failed to store track order: '.print_r($idOrder, true)); |
|
|
|
|
194
|
|
|
$jsonResponse = new JsonResponse(['ok' => 'fail', 'message' => 'Failed to store new sort order'], 500); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $jsonResponse; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.