1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\FieldNotes\Form; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Oc\FieldNotes\Persistence\FieldNoteService; |
7
|
|
|
use Oc\GeoCache\Persistence\GeoCacheLog\GeoCacheLogService; |
8
|
|
|
|
9
|
|
|
class UploadFormDataFactory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var FieldNoteService |
|
|
|
|
13
|
|
|
*/ |
14
|
|
|
private $fieldNoteService; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var GeoCacheLogService |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
private $geoCacheLogService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param FieldNoteService $fieldNoteService |
|
|
|
|
23
|
|
|
* @param GeoCacheLogService $geoCacheLogService |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function __construct(FieldNoteService $fieldNoteService, GeoCacheLogService $geoCacheLogService) |
26
|
|
|
{ |
27
|
|
|
$this->fieldNoteService = $fieldNoteService; |
28
|
|
|
$this->geoCacheLogService = $geoCacheLogService; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates a UploadFormData by given user id. |
33
|
|
|
* |
34
|
|
|
* @param int $userId |
35
|
|
|
* |
36
|
|
|
* @return UploadFormData |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function create($userId) |
39
|
|
|
{ |
40
|
|
|
$uploadFormData = new UploadFormData(); |
41
|
|
|
|
42
|
|
|
$uploadFormData->userId = $userId; |
43
|
|
|
$uploadFormData->ignoreBeforeDate = $this->getLatestLogOrFieldNoteDate($userId); |
44
|
|
|
|
45
|
|
|
return $uploadFormData; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Fetches the latest log or field note date. |
50
|
|
|
* |
51
|
|
|
* @param int $userId |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
private function getLatestLogOrFieldNoteDate($userId) |
56
|
|
|
{ |
57
|
|
|
$fieldNoteDate = $this->getLatestFieldNoteDate($userId); |
58
|
|
|
|
59
|
|
|
$geoCacheLogDate = $this->getLatestLogDate($userId); |
60
|
|
|
|
61
|
|
|
return max($fieldNoteDate, $geoCacheLogDate); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @param $userId |
|
|
|
|
66
|
|
|
* |
67
|
|
|
* @return DateTime|null |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
private function getLatestLogDate($userId) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$geoCacheLogDate = null; |
72
|
|
|
$geoCacheLog = $this->geoCacheLogService->getLatestUserLog($userId); |
73
|
|
|
|
74
|
|
|
if ($geoCacheLog) { |
75
|
|
|
$geoCacheLogDate = $geoCacheLog->date; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $geoCacheLogDate; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
|
|
|
|
82
|
|
|
* Fetches the latest field note date. |
83
|
|
|
* |
84
|
|
|
* @param $userId |
|
|
|
|
85
|
|
|
* |
86
|
|
|
* @return DateTime|null |
|
|
|
|
87
|
|
|
*/ |
88
|
|
|
private function getLatestFieldNoteDate($userId) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$fieldNoteDate = null; |
91
|
|
|
$fieldNote = $this->fieldNoteService->getLatestUserFieldNote($userId); |
92
|
|
|
|
93
|
|
|
if ($fieldNote) { |
94
|
|
|
$fieldNoteDate = $fieldNote->date; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $fieldNoteDate; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|