|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: wechsler |
|
6
|
|
|
* Date: 21/08/15 |
|
7
|
|
|
* Time: 06:53 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Phase\TakeATicket; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\DBAL\Connection; |
|
13
|
|
|
use Iterator; |
|
14
|
|
|
use Phase\TakeATicket\DataSource\Factory; |
|
15
|
|
|
use Phase\TakeATicket\SongLoader\RclRowMapper; |
|
16
|
|
|
|
|
17
|
|
|
class SongLoader |
|
18
|
|
|
{ |
|
19
|
|
|
const CODE_LENGTH = 6; // min to avoid clashes |
|
20
|
|
|
|
|
21
|
|
|
protected $startRow = 2; |
|
22
|
|
|
|
|
23
|
|
|
protected $showProgress = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Store contents of specified XLS file to the given database handle |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $sourceFile Path to file |
|
29
|
|
|
* @param Connection $dbConn DB connection |
|
30
|
|
|
* @return int Number of non-duplicate songs stored |
|
31
|
|
|
* @throws \PHPExcel_Exception |
|
32
|
|
|
* @throws \PHPExcel_Reader_Exception |
|
33
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function run($sourceFile, Connection $dbConn) |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
$objPHPExcel = \PHPExcel_IOFactory::load($sourceFile); |
|
39
|
|
|
$dataStore = Factory::datasourceFromDbConnection($dbConn); |
|
40
|
|
|
// empty table - reset autoincrement if it has one |
|
41
|
|
|
$dataStore->resetCatalogue(); //FIXME clear tickets here too? |
|
42
|
|
|
$mapper = $this->getRowMapper($dataStore); |
|
43
|
|
|
$mapper->init(); |
|
44
|
|
|
|
|
45
|
|
|
$iterator = $objPHPExcel->getSheet()->getRowIterator($this->startRow); |
|
46
|
|
|
|
|
47
|
|
|
$i = 1; |
|
48
|
|
|
foreach ($iterator as $sourceRow) { |
|
49
|
|
|
$flattenedRow = []; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var \PHPExcel_Worksheet_Row $sourceRow |
|
52
|
|
|
*/ |
|
53
|
|
|
$cells = $sourceRow->getCellIterator(); |
|
54
|
|
|
/** |
|
55
|
|
|
* @var Iterator $cells |
|
56
|
|
|
*/ |
|
57
|
|
|
foreach ($cells as $cell) { |
|
58
|
|
|
/** |
|
59
|
|
|
* @var \PHPExcel_Cell $cell |
|
60
|
|
|
*/ |
|
61
|
|
|
$column = $cell->getColumn(); |
|
62
|
|
|
$content = $cell->getFormattedValue(); |
|
63
|
|
|
|
|
64
|
|
|
$flattenedRow[$column] = trim($content); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (implode($flattenedRow, '') !== '') { |
|
68
|
|
|
$storedOk = $mapper->storeRawRow($flattenedRow); |
|
69
|
|
|
if ($storedOk) { |
|
70
|
|
|
$this->printProgressMarker($i); |
|
71
|
|
|
} else { |
|
72
|
|
|
print('x'); |
|
73
|
|
|
} |
|
74
|
|
|
$i++; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$total = $i - 1; |
|
79
|
|
|
if ($this->showProgress) { |
|
80
|
|
|
echo "\nImported $total songs\n"; |
|
81
|
|
|
} |
|
82
|
|
|
return $total; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param bool $showProgress |
|
87
|
|
|
* @return SongLoader |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setShowProgress($showProgress) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->showProgress = $showProgress; |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $i |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function printProgressMarker($i) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->showProgress) { |
|
101
|
|
|
if (!($i % 100)) { |
|
102
|
|
|
echo $i; |
|
103
|
|
|
} else { |
|
104
|
|
|
if (!($i % 10)) { |
|
105
|
|
|
echo '.'; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
if (!($i % 1000)) { |
|
109
|
|
|
echo "\n"; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* FIXME redefine as return RowMapperInterface |
|
116
|
|
|
* |
|
117
|
|
|
* @param $dataStore |
|
118
|
|
|
* @return RclRowMapper |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getRowMapper($dataStore) |
|
121
|
|
|
{ |
|
122
|
|
|
return new RclRowMapper($dataStore); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|