|
1
|
|
|
<?php |
|
2
|
|
|
namespace VideoGamesRecords\CoreBundle\Service; |
|
3
|
|
|
|
|
4
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Group; |
|
5
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Chart; |
|
6
|
|
|
use VideoGamesRecords\CoreBundle\Entity\ChartLib; |
|
7
|
|
|
|
|
8
|
|
|
class Game |
|
9
|
|
|
{ |
|
10
|
|
|
private $em; |
|
11
|
|
|
private $directory; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(\Doctrine\ORM\EntityManager $em, $rootDir) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->em = $em; |
|
16
|
|
|
$this->directory = $rootDir . '/../var/data/game'; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Add groups and charts from a csv file |
|
22
|
|
|
*/ |
|
23
|
|
|
public function addFromCsv() |
|
24
|
|
|
{ |
|
25
|
|
|
$files = array_diff(scandir($this->directory . '/in'), array('..', '.')); |
|
26
|
|
|
foreach ($files as $file) { |
|
27
|
|
|
if (substr($file, 0, 8) != 'game-add') { |
|
28
|
|
|
continue; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$fileIn = $this->directory . '/in/' . $file; |
|
32
|
|
|
$fileOut = $this->directory . '/out/' . $file; |
|
33
|
|
|
$handle = fopen($fileIn, 'r'); |
|
34
|
|
|
$idGame = substr($file, 8, -4); |
|
35
|
|
|
|
|
36
|
|
|
if (!is_numeric($idGame)) { |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @var \VideoGamesRecords\CoreBundle\Entity\Game $game */ |
|
41
|
|
|
$game = $this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Game', $idGame); |
|
42
|
|
|
|
|
43
|
|
|
if ($game === null) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$group = null; |
|
48
|
|
|
$types = null; |
|
49
|
|
|
while (($row = fgetcsv($handle, null, ';')) !== false) { |
|
|
|
|
|
|
50
|
|
|
$libEn = $row[1]; |
|
51
|
|
|
$libFr = $row[2]; |
|
52
|
|
|
if ((in_array($row[0], array('group', 'chart'))) && (isset($row[3]))) { |
|
53
|
|
|
if ($row[3] != null) { |
|
54
|
|
|
$types = explode('|', $row[3]); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
switch ($row[0]) { |
|
58
|
|
|
case 'object': |
|
59
|
|
|
// DO nohting |
|
60
|
|
|
break; |
|
61
|
|
|
case 'group': |
|
62
|
|
|
$group = new Group(); |
|
63
|
|
|
$group->translate('en', false)->setName($libEn); |
|
64
|
|
|
$group->translate('fr', false)->setName($libFr); |
|
65
|
|
|
$group->setGame($game); |
|
66
|
|
|
$group->mergeNewTranslations(); |
|
67
|
|
|
$this->em->persist($group); |
|
68
|
|
|
break; |
|
69
|
|
|
case 'chart': |
|
70
|
|
|
$chart = new Chart(); |
|
71
|
|
|
$chart->translate('en', false)->setName($libEn); |
|
72
|
|
|
$chart->translate('fr', false)->setName($libFr); |
|
73
|
|
|
$chart->setGroup($group); |
|
74
|
|
|
$chart->mergeNewTranslations(); |
|
75
|
|
|
|
|
76
|
|
|
if ($types != null) { |
|
77
|
|
|
foreach ($types as $idType) { |
|
78
|
|
|
var_dump($idType); |
|
|
|
|
|
|
79
|
|
|
$chartLib = new ChartLib(); |
|
80
|
|
|
$chartLib |
|
81
|
|
|
->setChart($chart) |
|
82
|
|
|
->setType($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\ChartType', $idType)); |
|
83
|
|
|
$chart->addLib($chartLib); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->em->persist($chart); |
|
88
|
|
|
break; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
$this->em->flush(); |
|
92
|
|
|
rename($fileIn, $fileOut); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Update groups and charts from a csv file |
|
99
|
|
|
*/ |
|
100
|
|
|
public function updateFromCsv() |
|
101
|
|
|
{ |
|
102
|
|
|
$files = array_diff(scandir($this->directory . '/in'), array('..', '.')); |
|
103
|
|
|
foreach ($files as $file) { |
|
104
|
|
|
if (substr($file, 0, 11) != 'game-update') { |
|
105
|
|
|
continue; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$fileIn = $this->directory . '/in/' . $file; |
|
109
|
|
|
$fileOut = $this->directory . '/out/' . $file; |
|
|
|
|
|
|
110
|
|
|
$handle = fopen($fileIn, 'r'); |
|
111
|
|
|
$idGame = substr($file, 11, -4); |
|
112
|
|
|
|
|
113
|
|
|
if (!is_numeric($idGame)) { |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$group = null; |
|
118
|
|
|
$types = null; |
|
|
|
|
|
|
119
|
|
|
while (($row = fgetcsv($handle, null, ';')) !== false) { |
|
|
|
|
|
|
120
|
|
|
$id = $row[1]; |
|
121
|
|
|
$libEn = $row[2]; |
|
122
|
|
|
$libFr = $row[3]; |
|
123
|
|
|
switch ($row[0]) { |
|
124
|
|
|
case 'object': |
|
125
|
|
|
// DO nohting |
|
126
|
|
|
break; |
|
127
|
|
|
case 'group': |
|
128
|
|
|
$group = $this->em->getRepository('VideoGamesRecordsCoreBundle:Group')->find($id); |
|
129
|
|
|
$group->translate('en', false)->setName($libEn); |
|
130
|
|
|
$group->translate('fr', false)->setName($libFr); |
|
131
|
|
|
$group->mergeNewTranslations(); |
|
132
|
|
|
$this->em->persist($group); |
|
133
|
|
|
break; |
|
134
|
|
|
case 'chart': |
|
135
|
|
|
$chart = $this->em->getRepository('VideoGamesRecordsCoreBundle:Chart')->find($id); |
|
136
|
|
|
$chart->translate('en', false)->setName($libEn); |
|
137
|
|
|
$chart->translate('fr', false)->setName($libFr); |
|
138
|
|
|
$chart->mergeNewTranslations(); |
|
139
|
|
|
$this->em->persist($chart); |
|
140
|
|
|
break; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
$this->em->flush(); |
|
144
|
|
|
//rename($fileIn, $fileOut); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|