/
CSVPostTransformer.php
132 lines (109 loc) · 2.75 KB
/
CSVPostTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php defined('SYSPATH') or die('No direct script access');
/**
* Ushahidi CSV Transformer
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
use Ushahidi\Core\Tool\MappingTransformer;
use Ushahidi\Core\Entity\PostRepository;
class Ushahidi_Transformer_CSVPostTransformer implements MappingTransformer
{
protected $map;
protected $fixedValues;
protected $repo;
protected $unmapped;
public function setRepo(PostRepository $repo)
{
$this->repo = $repo;
}
// MappingTransformer
public function setMap(Array $map)
{
$this->map = $map;
}
// MappingTransformer
public function setFixedValues(Array $fixedValues)
{
$this->fixedValues = $fixedValues;
}
// Transformer
public function interact(Array $record)
{
$record = array_values($record);
$columns = $this->map;
// Don't import columns marked as NULL
foreach ($columns as $index => $column) {
if ($column === NULL) {
unset($columns[$index]);
unset($record[$index]);
}
}
// Remap record columns
$record = array_combine($columns, $record);
// Trim and remove empty values
foreach ($record as $key => $val)
{
$record[$key] = trim($val);
if (empty($record[$key])) {
unset($record[$key]);
}
}
// Merge multi-value columns
$this->mergeMultiValueFields($record);
// Filter post fields from the record
$post_entity = $this->repo->getEntity();
$post_fields = array_intersect_key($record, $post_entity->asArray());
// Remove post fields from the record and leave form values
foreach ($post_fields as $key => $val)
{
unset($record[$key]);
}
// Put values in array
array_walk($record, function (&$val) {
if ($this->isLocation($val)) {
$val = [$val];
}
if (! is_array($val)) {
$val = [$val];
}
});
$form_values = ['values' => $record];
return array_merge($post_fields,
$form_values,
$this->fixedValues);
}
/**
* Multi-value columns use dot notation to add sub-keys
* e.g. 'location.lat' refers to a field called 'location'
* and 'lat' is a sub-key of the field.
*
* @param array &$record
*/
private function mergeMultiValueFields(&$record)
{
foreach ($record as $column => $val)
{
$keys = explode('.', $column);
// Get column name
$column_name = array_shift($keys);
// Assign sub-key to multi-value column
if (! empty($keys))
{
unset($record[$column]);
foreach ($keys as $key)
{
$record[$column_name][$key] = $val;
}
}
}
}
private function isLocation($value)
{
return is_array($value) &&
array_key_exists('lon', $value) &&
array_key_exists('lat', $value);
}
}