Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import/Export Sync #63

Merged
merged 1 commit into from
Nov 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}