|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Update map coordinates handler file. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Handler |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright YetiForce S.A. |
|
9
|
|
|
* @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
10
|
|
|
* @author Arkadiusz Sołek <[email protected]> |
|
11
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
use App\Fields\MapCoordinates; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Update map coordinates handler class. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Vtiger_UpdateMapCoordinates_Handler |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* EntityAfterSave function. |
|
23
|
|
|
* |
|
24
|
|
|
* @param App\EventHandler $eventHandler |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function entityAfterSave(App\EventHandler $eventHandler): void |
|
29
|
|
|
{ |
|
30
|
|
|
$recordModel = $eventHandler->getRecordModel(); |
|
31
|
|
|
$recordId = $recordModel->getId(); |
|
32
|
|
|
$isNew = $recordModel->isNew(); |
|
33
|
|
|
foreach ($recordModel->getModule()->getFieldsByType(['mapCoordinates'], true) as $fieldName => $fieldModel) { |
|
34
|
|
|
if ($isNew || false !== $recordModel->getPreviousValue($fieldName)) { |
|
35
|
|
|
$value = $recordModel->get($fieldName); |
|
36
|
|
|
if (!\App\Json::isEmpty($value)) { |
|
37
|
|
|
$value = \App\Json::decode($value); |
|
38
|
|
|
$coordinate = MapCoordinates::convert($value['type'], MapCoordinates::DECIMAL, $value['value']); |
|
39
|
|
|
} |
|
40
|
|
|
if (!empty($coordinate['lat']) && !empty($coordinate['lon'])) { |
|
41
|
|
|
if ((new \App\Db\Query())->from(\OpenStreetMap_Module_Model::COORDINATES_TABLE_NAME) |
|
42
|
|
|
->where(['crmid' => $recordId, 'type' => $fieldName])->exists()) { |
|
43
|
|
|
MapCoordinates::updateMapCoordinates($recordId, $fieldName, 'update', $coordinate); |
|
44
|
|
|
} else { |
|
45
|
|
|
MapCoordinates::updateMapCoordinates($recordId, $fieldName, 'insert', $coordinate); |
|
46
|
|
|
} |
|
47
|
|
|
} elseif (!$isNew && empty($coordinate['lat']) && empty($coordinate['lon'])) { |
|
48
|
|
|
MapCoordinates::updateMapCoordinates($recordId, $fieldName, 'delete'); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* EditViewChangeValue function. |
|
56
|
|
|
* |
|
57
|
|
|
* @param App\EventHandler $eventHandler |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public function editViewChangeValue(App\EventHandler $eventHandler): array |
|
62
|
|
|
{ |
|
63
|
|
|
$return = []; |
|
64
|
|
|
$recordModel = $eventHandler->getRecordModel(); |
|
65
|
|
|
$coordinates = $recordModel->get('coordinates'); |
|
66
|
|
|
if (!\App\Json::isEmpty($coordinates)) { |
|
67
|
|
|
$coordinates = \App\Json::decode($coordinates); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
$coordinatesConnector = \App\Map\Coordinates::getInstance(); |
|
70
|
|
|
$values = []; |
|
71
|
|
|
if (!empty($coordinatesData = $coordinatesConnector->getCoordinates(\App\Map\Coordinates::getAddressParams($recordModel, 'a')))) { |
|
72
|
|
|
$coordinatesData = reset($coordinatesData); |
|
73
|
|
|
foreach ([MapCoordinates::DECIMAL, MapCoordinates::DEGREES, MapCoordinates::CODE_PLUS] as $type) { |
|
74
|
|
|
$values[$type][] = \App\Fields\MapCoordinates::convert(MapCoordinates::DECIMAL, $type, ['lat' => $coordinatesData['lat'], 'lon' => $coordinatesData['lon']]); |
|
75
|
|
|
} |
|
76
|
|
|
if (isset($values['decimal'][0]['lat'], $values['decimal'][0]['lon'])) { |
|
77
|
|
|
$return['changeValues'][] = ['fieldName' => 'coordinates[decimal][lat]', 'value' => $values['decimal'][0]['lat']]; |
|
78
|
|
|
$return['changeValues'][] = ['fieldName' => 'coordinates[decimal][lon]', 'value' => $values['decimal'][0]['lon']]; |
|
79
|
|
|
$recordModel->set('coordinates[decimal][lat]', $values['decimal'][0]['lat']); |
|
80
|
|
|
$recordModel->set('coordinates[decimal][lon]', $values['decimal'][0]['lon']); |
|
81
|
|
|
} |
|
82
|
|
|
if (isset($values['degrees'][0]['lat'], $values['degrees'][0]['lon'])) { |
|
83
|
|
|
$return['changeValues'][] = ['fieldName' => 'coordinates[degrees][lat]', 'value' => $values['degrees'][0]['lat']]; |
|
84
|
|
|
$return['changeValues'][] = ['fieldName' => 'coordinates[degrees][lon]', 'value' => $values['degrees'][0]['lon']]; |
|
85
|
|
|
$recordModel->set('coordinates[degrees][lat]', $values['degrees'][0]['lat']); |
|
86
|
|
|
$recordModel->set('coordinates[degrees][lon]', $values['degrees'][0]['lon']); |
|
87
|
|
|
} |
|
88
|
|
|
if (isset($values['codeplus'][0])) { |
|
89
|
|
|
$return['changeValues'][] = ['fieldName' => 'coordinates[codeplus]', 'value' => $values['codeplus'][0]]; |
|
90
|
|
|
$recordModel->set('coordinates[codeplus]', $values['codeplus'][0]['lat']); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return $return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get variables for the current event. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $name |
|
100
|
|
|
* @param array $params |
|
101
|
|
|
* @param string $moduleName |
|
102
|
|
|
* |
|
103
|
|
|
* @return array|null |
|
104
|
|
|
*/ |
|
105
|
|
|
public function vars(string $name, array $params, string $moduleName): ?array |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
if (\App\EventHandler::EDIT_VIEW_CHANGE_VALUE === $name) { |
|
108
|
|
|
return ['addresslevel1a', 'addresslevel2a', 'addresslevel3a', 'addresslevel5a', 'addresslevel8a', 'buildingnumbera', 'addresslevel8a']; |
|
109
|
|
|
} |
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|