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