Skip to content

Commit

Permalink
REFACTOR Location data import and export (#210)
Browse files Browse the repository at this point in the history
Import/Export should work both ways using default spreadsheet header row

Location
* CMS Design
* Updated $summary_fields

LocationCategory
* CMS Design
  • Loading branch information
jsirish authored Sep 24, 2019
1 parent 4e9ab40 commit e35aa4e
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 38 deletions.
24 changes: 16 additions & 8 deletions src/admin/LocationAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,34 @@ class LocationAdmin extends ModelAdmin
*/
public function getExportFields()
{
if ($this->modelClass == 'Location') {
return array(
if ($this->modelClass == Location::class) {
$fields = [
'Title' => 'Name',
'Address' => 'Address',
'Address2' => 'Address2',
'City' => 'City',
'State' => 'State',
'PostalCode' => 'PostalCode',
'Country' => 'Country',
'Website' => 'Website',
'CountryCode' => 'Country',
'Phone' => 'Phone',
'Fax' => 'Fax',
'Email' => 'Email',
'ShowInLocator' => 'ShowInLocator',
'Website' => 'Website',
'Featured' => 'Featured',
'CategoryList' => 'Categories',
'Lat' => 'Lat',
'Lng' => 'Lng',
);
'Import_ID' => 'Import_ID',
];
}

return parent::getExportFields();
if (!isset($fields)) {
$fields = parent::getExportFields();
}

$this->extend('updateGetExportFields', $fields);

return $fields;
}

/**
Expand All @@ -73,7 +81,7 @@ public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);
$class = $this->sanitiseClassName($this->modelClass);
if ($class == 'Location') {
if ($class == Location::class) {
$gridField = $form->Fields()->fieldByName($class);
$config = $gridField->getConfig();
$config->removeComponentsByType('GridFieldDeleteAction');
Expand Down
73 changes: 57 additions & 16 deletions src/bulkloader/LocationCsvBulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use SilverStripe\Dev\CsvBulkLoader;
use SilverStripe\Core\Convert;
use SilverStripe\i18n\Data\Intl\IntlLocales;

/**
* Class LocationCsvBulkLoader
* @package Dynamic\Locator
*/
class LocationCsvBulkLoader extends CsvBulkLoader
{
Expand All @@ -16,9 +18,10 @@ class LocationCsvBulkLoader extends CsvBulkLoader
*/
public $columnMap = array(
'Name' => 'Title',
'City' => 'Suburb',
'EmailAddress' => 'Email',
'Categories' => '->getCategoryByName',
'Import_ID' => 'Import_ID',
'Country' => '->getCountryByName',
);

/**
Expand All @@ -29,32 +32,70 @@ class LocationCsvBulkLoader extends CsvBulkLoader
);

/**
* @var array
* @param $val
* @return string|string[]|null
*/
/*
public $relationCallbacks = array(
'Category.Name' => array(
'relationname' => 'Category',
'callback' => 'getCategoryByName',
),
);*/
public function getEscape($val)
{
return preg_replace("/\r|\n/", "", $val);
}

/**
* @param $obj
* @param $val
* @param $record
* @return \SilverStripe\ORM\DataObject|static
* @throws \SilverStripe\ORM\ValidationException
*/
public static function getCategoryByName(&$obj, $val, $record)
{
$val = Convert::raw2sql($val);
$category = LocationCategory::get()->filter(array('Name' => $val))->First();
if (!$category) {
$category = LocationCategory::create();
$category->Name = $val;
$category->write();
$parts = explode(', ', $val);

foreach ($parts as $part) {
$category = LocationCategory::get()->filter(array('Name' => $part))->first();
if (!$category) {
$category = LocationCategory::create();
$category->Name = $part;
$category->write();
}
$obj->Categories()->add($category);
}
}

/**
* @param $obj
* @param $val
* @param $record
*/
public function getCountryByName(&$obj, $val, $record)
{
$val = $this->getEscape($val);
$countries = IntlLocales::singleton()->getCountries();

if (strlen((string)$val) == 2) {
$val = strtolower($val);
if (array_key_exists($val, $countries)) {
$obj->Country = $val;
}
}

if (in_array($val, $countries)) {
$obj->Country = array_search($val, $countries);
}
}

/**
* @param array $record
* @param array $columnMap
* @param \SilverStripe\Dev\BulkLoader_Result $results
* @param bool $preview
* @return int|void
*/
protected function processRecord($record, $columnMap, &$results, $preview = false)
{
$objID = parent::processRecord($record, $columnMap, $results, $preview = false);

return $category;
$location = Location::get()->byID($objID);
$location->publishSingle();
}
}
59 changes: 49 additions & 10 deletions src/objects/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Dynamic\Locator;

use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\Security\PermissionProvider;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Security\Permission;
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;

/**
* Class Location
Expand Down Expand Up @@ -36,11 +38,6 @@ class Location extends DataObject implements PermissionProvider
*/
private static $plural_name = 'Locations';

/**
* @var bool
*/
private static $versioned_gridfield_extensions = true;

/**
* @var array
*/
Expand Down Expand Up @@ -108,12 +105,20 @@ class Location extends DataObject implements PermissionProvider
private static $summary_fields = array(
'Title',
'Address',
'Address2',
'City',
'State',
'PostalCode',
'Country',
'Featured.NiceAsBoolean',
'Coords',
'CountryCode',
'Phone' => 'Phone',
'Fax' => 'Fax',
'Email' => 'Email',
'Website' => 'Website',
'Featured',
'CategoryList',
'Lat',
'Lng',
'Import_ID',
);

/**
Expand All @@ -126,6 +131,29 @@ public function getCoords()
return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false';
}

/**
* @return string
*/
public function getCategoryList()
{
if ($this->Categories()->count()) {
return implode(', ', $this->Categories()->column('Name'));
}

return '';
}

/**
* @return bool|string
*/
public function getCountryCode()
{
if ($this->Country) {
return strtoupper($this->Country);
}
return false;
}

/**
* custom labels for fields
*
Expand All @@ -136,9 +164,9 @@ public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels($includerelations);
$labels['Title'] = 'Name';
$labels['Address2'] = 'Address 2';
$labels['PostalCode'] = 'Postal Code';
$labels['Category.Name'] = 'Category';
$labels['Category.ID'] = 'Category';
$labels['Categories.Name'] = 'Categories';
$labels['Featured.NiceAsBoolean'] = 'Featured';
return $labels;
}
Expand Down Expand Up @@ -166,6 +194,17 @@ public function getCMSFields()
'Fax',
$featured
);

if ($this->ID) {
$categories = $fields->dataFieldByName('Categories');
$config = $categories->getConfig();
$config->removeComponentsByType([
GridFieldAddExistingAutocompleter::class
])
->addComponents([
new GridFieldAddExistingSearchButton()
]);
}
});

$fields = parent::getCMSFields();
Expand Down
14 changes: 10 additions & 4 deletions src/objects/LocationCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\Security\Permission;
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;

/**
* Class LocationCategory
Expand Down Expand Up @@ -63,20 +64,25 @@ public function getCMSFields()
$fields->removeByName([
'Locations',
'LocationSet',
'Locators',
'LinkTracking',
'FileTracking',
]);

if ($this->ID) {
// Locations
$config = GridFieldConfig_RelationEditor::create();
$config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
$config->addComponent(new GridFieldAddExistingAutocompleter());
$config->removeComponentsByType(GridFieldAddNewButton::class);
$config->removeComponentsByType([
GridFieldAddExistingAutocompleter::class,
GridFieldAddNewButton::class
])
->addComponents([
new GridFieldAddExistingSearchButton(),
]);
$locations = $this->Locations();
$locationField = GridField::create('Locations', 'Locations', $locations, $config);

$fields->addFieldsToTab('Root.Locations', array(
$fields->addFieldsToTab('Root.Main', array(
$locationField,
));
}
Expand Down

0 comments on commit e35aa4e

Please sign in to comment.