1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\CsvBulkLoader; |
6
|
|
|
use SilverStripe\Core\Convert; |
7
|
|
|
use SilverStripe\i18n\Data\Intl\IntlLocales; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class LocationCsvBulkLoader |
11
|
|
|
* @package Dynamic\Locator |
12
|
|
|
*/ |
13
|
|
|
class LocationCsvBulkLoader extends CsvBulkLoader |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
public $columnMap = array( |
20
|
|
|
'Name' => 'Title', |
21
|
|
|
'EmailAddress' => 'Email', |
22
|
|
|
'Categories' => '->getCategoryByName', |
23
|
|
|
'Import_ID' => 'Import_ID', |
24
|
|
|
'Country' => '->getCountryByName', |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
public $duplicateChecks = array( |
31
|
|
|
'Import_ID' => 'Import_ID' |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $val |
36
|
|
|
* @return string|string[]|null |
37
|
|
|
*/ |
38
|
|
|
public function getEscape($val) |
39
|
|
|
{ |
40
|
|
|
return preg_replace("/\r|\n/", "", $val); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $obj |
45
|
|
|
* @param $val |
46
|
|
|
* @param $record |
47
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
48
|
|
|
*/ |
49
|
|
|
public static function getCategoryByName(&$obj, $val, $record) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$val = Convert::raw2sql($val); |
52
|
|
|
$parts = explode(', ', $val); |
|
|
|
|
53
|
|
|
|
54
|
|
|
foreach ($parts as $part) { |
55
|
|
|
$category = LocationCategory::get()->filter(array('Name' => $part))->first(); |
56
|
|
|
if (!$category) { |
57
|
|
|
$category = LocationCategory::create(); |
58
|
|
|
$category->Name = $part; |
59
|
|
|
$category->write(); |
60
|
|
|
} |
61
|
|
|
$obj->Categories()->add($category); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $obj |
67
|
|
|
* @param $val |
68
|
|
|
* @param $record |
69
|
|
|
*/ |
70
|
|
|
public function getCountryByName(&$obj, $val, $record) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$val = $this->getEscape($val); |
73
|
|
|
$countries = IntlLocales::singleton()->getCountries(); |
74
|
|
|
|
75
|
|
|
if (strlen((string)$val) == 2) { |
76
|
|
|
$val = strtolower($val); |
|
|
|
|
77
|
|
|
if (array_key_exists($val, $countries)) { |
78
|
|
|
$obj->Country = $val; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (in_array($val, $countries)) { |
83
|
|
|
$obj->Country = array_search($val, $countries); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $record |
89
|
|
|
* @param array $columnMap |
90
|
|
|
* @param \SilverStripe\Dev\BulkLoader_Result $results |
91
|
|
|
* @param bool $preview |
92
|
|
|
* @return int|void |
93
|
|
|
*/ |
94
|
|
|
protected function processRecord($record, $columnMap, &$results, $preview = false) |
95
|
|
|
{ |
96
|
|
|
$objID = parent::processRecord($record, $columnMap, $results, $preview = false); |
97
|
|
|
|
98
|
|
|
$location = Location::get()->byID($objID); |
99
|
|
|
$location->publishSingle(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.