diff --git a/src/admin/LocationAdmin.php b/src/admin/LocationAdmin.php index 7d772ca..2856038 100644 --- a/src/admin/LocationAdmin.php +++ b/src/admin/LocationAdmin.php @@ -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; } /** @@ -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'); diff --git a/src/bulkloader/LocationCsvBulkLoader.php b/src/bulkloader/LocationCsvBulkLoader.php index dd03385..f2910aa 100644 --- a/src/bulkloader/LocationCsvBulkLoader.php +++ b/src/bulkloader/LocationCsvBulkLoader.php @@ -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 { @@ -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', ); /** @@ -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(); } } diff --git a/src/objects/Location.php b/src/objects/Location.php index 6b326e5..3c39b7f 100644 --- a/src/objects/Location.php +++ b/src/objects/Location.php @@ -2,6 +2,7 @@ namespace Dynamic\Locator; +use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\ManyManyList; use SilverStripe\Security\PermissionProvider; @@ -9,6 +10,7 @@ use SilverStripe\Forms\EmailField; use SilverStripe\Forms\DropdownField; use SilverStripe\Security\Permission; +use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; /** * Class Location @@ -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 */ @@ -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', ); /** @@ -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 * @@ -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; } @@ -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(); diff --git a/src/objects/LocationCategory.php b/src/objects/LocationCategory.php index 8afc99e..be19a26 100644 --- a/src/objects/LocationCategory.php +++ b/src/objects/LocationCategory.php @@ -9,6 +9,7 @@ use SilverStripe\ORM\DataObject; use SilverStripe\ORM\ManyManyList; use SilverStripe\Security\Permission; +use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; /** * Class LocationCategory @@ -63,6 +64,7 @@ public function getCMSFields() $fields->removeByName([ 'Locations', 'LocationSet', + 'Locators', 'LinkTracking', 'FileTracking', ]); @@ -70,13 +72,17 @@ public function getCMSFields() 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, )); }