Skip to content

Commit

Permalink
Import/Export Sync
Browse files Browse the repository at this point in the history
Matched fields in LocationAdmin::getExportFields() with LocationCsvBulkLoader.

Can now export easily from one Locator installation to another without data loss or reformatting column headers in CSV

fixes dynamic#59
  • Loading branch information
jsirish committed Nov 10, 2015
1 parent dd431b2 commit d00c341
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
43 changes: 24 additions & 19 deletions code/LocationAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,41 @@

class LocationAdmin extends ModelAdmin
{

private static $managed_models = array(
'Location',
'LocationCategory'
'LocationCategory',
);

private static $model_importers = array(
'Location' => 'LocationCsvBulkLoader',
'LocationCategory' => 'CsvBulkLoader'
'LocationCategory' => 'CsvBulkLoader',
);

private static $menu_title = 'Locator';
private static $url_segment = 'locator';

public function getExportFields()
{
return array(
'Title' => 'Name',
'Address' => 'Address',
'Suburb' => 'City',
'State' => 'State',
'Postcode' => 'Postal Code',
'Country' => 'Country',
'Website' => 'Website',
'Phone' => 'Phone',
'Fax' => 'Fax',
'EmailAddress' => 'Email Address',
'ShowInLocator' => 'Show',
'Lat' => 'Lat',
'Lng' => 'Lng'
);
if ($this->modelClass == 'Location') {
return array(
'Title' => 'Name',
'Address' => 'Address',
'Suburb' => 'City',
'State' => 'State',
'Postcode' => 'Postcode',
'Country' => 'Country',
'Website' => 'Website',
'Phone' => 'Phone',
'Fax' => 'Fax',
'EmailAddress' => 'EmailAddress',
'Category.Name' => 'Category',
'ShowInLocator' => 'ShowInLocator',
'Featured' => 'Featured',
'Lat' => 'Lat',
'Lng' => 'Lng',
);
}

return parent::getExportFields();
}
}
}
31 changes: 14 additions & 17 deletions code/LocationCsvBulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@ class LocationCsvBulkLoader extends CsvBulkLoader
{
public $columnMap = array(
'Name' => 'Title',
//'Address' => 'Address',
'City' => 'Suburb',
//'State' => 'State',
'Zip' => 'Postcode',
'Category' => 'Category.Name',
'Email' => 'EmailAddress'
);

public $duplicateChecks = array(
'Address' => 'Address',
//'Website' => 'Website'
);

public $relationCallbacks = array(
'Category.Name' => array(
'relationname' => 'Category',
'callback' => 'getCatByName'
)
'Category.Name' => array(
'relationname' => 'Category',
'callback' => 'getCategoryByName',
),
);

public static function getCatByName(&$obj, $val, $record)
public static function getCategoryByName(&$obj, $val, $record)
{
$val = Convert::raw2sql($val);
return LocationCategory::get()->filter('Name', $val)->First();
//);
$category = LocationCategory::get()->filter(array('Name' => $val))->First();
if (!$category) {
$category = LocationCategory::create();
$category->Name = $val;
$category->write();
}

return $category;
}
}
}

0 comments on commit d00c341

Please sign in to comment.