1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: wechsler |
5
|
|
|
* Date: 25/02/2017 |
6
|
|
|
* Time: 16:54 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Phase\TakeATicket\SongLoader; |
10
|
|
|
|
11
|
|
|
use Phase\TakeATicket\DataSource\AbstractSql; |
12
|
|
|
use Phase\TakeATicket\Model\Instrument; |
13
|
|
|
use Phase\TakeATicket\Model\Platform; |
14
|
|
|
use Phase\TakeATicket\Model\Song; |
15
|
|
|
use Phase\TakeATicket\Model\Source; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Input file row mapper for the Rock Club London XLS file format |
19
|
|
|
* |
20
|
|
|
* To load other file formats, extend / replace this class and implement |
21
|
|
|
*/ |
22
|
|
|
class RclRowMapper implements RowMapperInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var AbstractSql |
26
|
|
|
*/ |
27
|
|
|
protected $dataStore; |
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected $manualIds; |
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $lastId; |
36
|
|
|
|
37
|
|
|
const RESULT_OK = 0; |
38
|
|
|
const RESULT_ERROR = 1; |
39
|
|
|
const RESULT_DUPLICATE = 2; |
40
|
|
|
|
41
|
|
|
const INPUT_FIELD_ARTIST = 'artist'; |
42
|
|
|
const INPUT_FIELD_TITLE = 'title'; |
43
|
|
|
const INPUT_FIELD_HAS_HARMONY = 'hasHarmony'; |
44
|
|
|
const INPUT_FIELD_HAS_KEYS = 'hasKeys'; |
45
|
|
|
const INPUT_FIELD_SOURCE = 'source'; |
46
|
|
|
const INPUT_FIELD_IN_RB3 = 'inRb3'; |
47
|
|
|
const INPUT_FIELD_IN_RB4 = 'inRb4'; |
48
|
|
|
const INPUT_FIELD_DURATION_MMSS = 'duration_mmss'; |
49
|
|
|
const INPUT_FIELD_DURATION = 'duration'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Column map for input file |
53
|
|
|
* |
54
|
|
|
* @var string[] |
55
|
|
|
*/ |
56
|
|
|
protected $fileFields = [ |
57
|
|
|
'B' => self::INPUT_FIELD_ARTIST, |
58
|
|
|
'C' => self::INPUT_FIELD_TITLE, |
59
|
|
|
'D' => self::INPUT_FIELD_HAS_HARMONY, |
60
|
|
|
'E' => self::INPUT_FIELD_HAS_KEYS, |
61
|
|
|
'I' => self::INPUT_FIELD_SOURCE, |
62
|
|
|
'F' => self::INPUT_FIELD_IN_RB3, |
63
|
|
|
'G' => self::INPUT_FIELD_IN_RB4, |
64
|
|
|
'H' => self::INPUT_FIELD_DURATION_MMSS, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
protected $fieldLookup = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* RclRowMapper constructor. |
71
|
|
|
* @param $dataStore |
72
|
|
|
*/ |
73
|
|
|
public function __construct(AbstractSql $dataStore) |
74
|
|
|
{ |
75
|
|
|
$this->dataStore = $dataStore; |
76
|
|
|
$this->manualIds = $dataStore->getDbConn()->getDriver() === 'pdo_mysql'; |
77
|
|
|
$this->lastId = 0; |
78
|
|
|
$this->fieldLookup = array_flip($this->fileFields); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Perform any once-only operations |
83
|
|
|
*/ |
84
|
|
|
public function init() |
85
|
|
|
{ |
86
|
|
|
//TODO Either load icons from config or remove config option |
87
|
|
|
$instruments = [ |
88
|
|
|
(new Instrument())->setId(1)->setName('Vocals')->setAbbreviation('V') |
89
|
|
|
->setIconHtml('<img src="local/rb-mic.png" class="instrumentIcon"/>'), |
90
|
|
|
(new Instrument())->setId(2)->setName('Guitar')->setAbbreviation('G') |
91
|
|
|
->setIconHtml('<img src="local/rb-guitar.png" class="instrumentIcon"/>'), |
92
|
|
|
(new Instrument())->setId(3)->setName('Bass')->setAbbreviation('B') |
93
|
|
|
->setIconHtml('<img src="local/rb-bass.png" class="instrumentIcon"/>'), |
94
|
|
|
(new Instrument())->setId(4)->setName('Drums')->setAbbreviation('D') |
95
|
|
|
->setIconHtml('<img src="local/rb-drums.png" class="instrumentIcon"/>'), |
96
|
|
|
(new Instrument())->setId(5)->setName('Keyboard')->setAbbreviation('K') |
97
|
|
|
->setIconHtml('<img src="local/rb-keys.png" class="instrumentIcon"/>'), |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
foreach ($instruments as $instrument) { |
101
|
|
|
$this->dataStore->storeInstrument($instrument); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$platforms = [ |
105
|
|
|
new Platform('RB3'), |
106
|
|
|
new Platform('RB4'), |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
foreach ($platforms as $platform) { |
110
|
|
|
$this->dataStore->storePlatform($platform); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Takes a row with column indexes and stores it to the database |
116
|
|
|
* |
117
|
|
|
* Primary table is songs (one per input line) |
118
|
|
|
* Must also manage instruments, platforms, sources |
119
|
|
|
* |
120
|
|
|
* @param array $flattenedRow |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function storeRawRow(array $flattenedRow) |
124
|
|
|
{ |
125
|
|
|
$song = new Song(); |
126
|
|
|
$song |
127
|
|
|
->setArtist($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_ARTIST]]) |
128
|
|
|
->setTitle($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_TITLE]]); |
129
|
|
|
|
130
|
|
|
$durationMS = trim($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_DURATION_MMSS]]); |
131
|
|
|
if ($durationMS && preg_match('/^\s*(\d+):(\d+)\s*$/', $durationMS, $matches)) { |
132
|
|
|
$song->setDuration(($matches[1] * 60) + $matches[2]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$sourceName = trim($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_SOURCE]]); |
136
|
|
|
if ($sourceName) { |
137
|
|
|
$source = $this->dataStore->fetchSourceByName($sourceName); |
138
|
|
|
if (!$source) { |
139
|
|
|
$source = new Source($sourceName); |
140
|
|
|
$this->dataStore->storeSource($source); |
141
|
|
|
} |
142
|
|
|
if ($source) { |
143
|
|
|
$song->setSourceId($source->getId()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->dataStore->storeSong($song); // Store song before all xrefs as we need ID |
148
|
|
|
|
149
|
|
|
// Platforms |
150
|
|
|
$platformIds = []; |
151
|
|
|
$platformFields = [ |
152
|
|
|
self::INPUT_FIELD_IN_RB3 => 'RB3', |
153
|
|
|
self::INPUT_FIELD_IN_RB4 => 'RB4', |
154
|
|
|
]; |
155
|
|
|
foreach ($platformFields as $field => $platformName) { |
156
|
|
|
if (trim($flattenedRow[$this->fieldLookup[$field]])) { |
157
|
|
|
$platform = $this->dataStore->fetchPlatformByName($platformName); |
158
|
|
|
if (!$platform) { |
159
|
|
|
$platform = new Platform($platformName); |
160
|
|
|
$this->dataStore->storePlatform($platform); |
161
|
|
|
} |
162
|
|
|
$platformIds[] = $platform->getId(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
$this->dataStore->storeSongPlatformLinks($song->getId(), $platformIds); |
166
|
|
|
|
167
|
|
|
// Instruments - all stored at init(); // TODO add harmony |
168
|
|
|
$instruments = ['Vocals', 'Guitar', 'Bass', 'Drums']; |
169
|
|
|
if (trim($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_HAS_KEYS]])) { |
170
|
|
|
$instruments[] = 'Keyboard'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$datastore = $this->dataStore; |
174
|
|
|
$instrumentIds = array_map( |
175
|
|
|
function ($name) use ($datastore) { |
176
|
|
|
$instrument = $datastore->fetchInstrumentByName($name); |
177
|
|
|
return $instrument ? $instrument->getId() : null; |
178
|
|
|
}, |
179
|
|
|
$instruments |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
$this->dataStore->storeSongInstrumentLinks($song->getId(), $instrumentIds); |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function getFormatterName() |
189
|
|
|
{ |
190
|
|
|
return 'RCL XLS formatter'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param $row |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
protected function rowToStorable($row) |
200
|
|
|
{ |
201
|
|
|
$directMapFields = [ |
202
|
|
|
self::INPUT_FIELD_ARTIST, |
203
|
|
|
self::INPUT_FIELD_TITLE, |
204
|
|
|
self::INPUT_FIELD_HAS_HARMONY, |
205
|
|
|
self::INPUT_FIELD_HAS_KEYS, |
206
|
|
|
self::INPUT_FIELD_SOURCE, |
207
|
|
|
self::INPUT_FIELD_DURATION, |
208
|
|
|
]; |
209
|
|
|
|
210
|
|
|
$trueIfPresentFields = [ |
211
|
|
|
self::INPUT_FIELD_HAS_HARMONY, |
212
|
|
|
self::INPUT_FIELD_HAS_KEYS, |
213
|
|
|
self::INPUT_FIELD_IN_RB3, |
214
|
|
|
self::INPUT_FIELD_IN_RB4, |
215
|
|
|
]; |
216
|
|
|
|
217
|
|
|
$storable = []; |
218
|
|
|
|
219
|
|
|
foreach ($directMapFields as $k) { |
220
|
|
|
$storable[$k] = empty($row[$k]) ? null : $row[$k]; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
foreach ($trueIfPresentFields as $k) { |
224
|
|
|
$storable[$k] = empty($row[$k]) ? 0 : 1; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if (isset($row[self::INPUT_FIELD_DURATION_MMSS]) |
228
|
|
|
&& preg_match('/^\s*(\d+):(\d+)\s*$/', $row[self::INPUT_FIELD_DURATION_MMSS], $matches) |
229
|
|
|
) { |
230
|
|
|
$storable[self::INPUT_FIELD_DURATION] = ($matches[1] * 60) + $matches[2]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $storable; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|