|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\FieldNote; |
|
6
|
|
|
use AppBundle\Exception\WrongFileFormatException; |
|
7
|
|
|
use AppBundle\Service\Traits\ErrorTrait; |
|
8
|
|
|
use AppBundle\Util\ArrayUtil; |
|
9
|
|
|
use DateTime; |
|
10
|
|
|
use DateTimeZone; |
|
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
12
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
13
|
|
|
|
|
14
|
|
|
class FieldNoteService |
|
15
|
|
|
{ |
|
16
|
|
|
use ErrorTrait; |
|
17
|
|
|
|
|
18
|
|
|
const FIELD_NOTE_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z'; |
|
19
|
|
|
const LOG_TYPE = [ |
|
20
|
|
|
'Found it' => 1, |
|
21
|
|
|
"Didn't find it" => 2, |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $entityManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Symfony\Component\Translation\TranslatorInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $translator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* FieldNoteService constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param \Doctrine\ORM\EntityManagerInterface $entityManager |
|
38
|
|
|
* @param \Symfony\Component\Translation\TranslatorInterface $translator |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(EntityManagerInterface $entityManager, TranslatorInterface $translator) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->entityManager = $entityManager; |
|
43
|
|
|
$this->translator = $translator; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $fileName |
|
48
|
|
|
* @param int $userId |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
* @throws \AppBundle\Exception\WrongFileFormatException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function importFromFile($fileName, $userId) |
|
54
|
|
|
{ |
|
55
|
|
|
$content = file_get_contents($fileName); |
|
56
|
|
|
$content = mb_convert_encoding($content, 'UTF-8', 'UCS-2LE'); |
|
57
|
|
|
$rows = ArrayUtil::trimExplode("\n", $content); |
|
58
|
|
|
foreach ($rows as $row) { |
|
59
|
|
|
$data = str_getcsv($row, ',', '"', '""'); |
|
60
|
|
|
if (count($data) !== 4) { |
|
61
|
|
|
throw new WrongFileFormatException( |
|
62
|
|
|
$this->translator->trans('This file seems not to be a field notes file.') |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (!array_key_exists($data[2], self::LOG_TYPE)) { |
|
67
|
|
|
$this->addError( |
|
68
|
|
|
$this->translator->trans('Log type "%type%" is not implemented', ['%type%' => $data[2]]) |
|
69
|
|
|
); |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
$type = self::LOG_TYPE[$data[2]]; |
|
73
|
|
|
|
|
74
|
|
|
$geocache = $this->entityManager->getRepository('AppBundle:Geocache')->findOneBy(['wpOc' => $data[0]]); |
|
75
|
|
|
if (!$geocache) { |
|
76
|
|
|
$this->addError( |
|
77
|
|
|
$this->translator->trans('Geocache "%code%" not found', ['%code%' => $data[0]]) |
|
78
|
|
|
); |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$date = DateTime::createFromFormat( |
|
83
|
|
|
self::FIELD_NOTE_DATETIME_FORMAT, |
|
84
|
|
|
$data[1], |
|
85
|
|
|
new DateTimeZone('UTC') |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
$fieldNote = new FieldNote(); |
|
89
|
|
|
$fieldNote->setUser($this->entityManager->getReference('AppBundle:User', $userId)); |
|
90
|
|
|
$fieldNote->setGeocache($geocache); |
|
91
|
|
|
$fieldNote->setDate($date); |
|
92
|
|
|
$fieldNote->setType($type); |
|
93
|
|
|
$fieldNote->setText($data[3]); |
|
94
|
|
|
$this->entityManager->persist($fieldNote); |
|
95
|
|
|
} |
|
96
|
|
|
$this->entityManager->flush(); |
|
97
|
|
|
|
|
98
|
|
|
if ($this->hasErrors()) { |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|