From 5e52dc904993904fc5c9bc096ce58b20c9ef9175 Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Thu, 17 Dec 2015 09:57:27 -0600 Subject: [PATCH] Location updates Location * overhauled CMSFields * new DB field Email * EmailAddress depreciated * Build task to migrate EmailAddress to Email Locator * code formatting cleanup * added Email to XML * added Email to `location-list-description.html` template LocationCSVBulkLoader * import field mapping accommodates depreciated EmailAddress LocationModelAdmin * updated export fields to use Email fixes #55 --- code/Location.php | 98 +++++++++++++----------- code/LocationAdmin.php | 2 +- code/LocationCsvBulkLoader.php | 1 + code/Locator.php | 93 ++++++++++------------ tasks/EmailAddressTask.php | 23 ++++++ templates/LocationXML.ss | 2 +- templates/location-list-description.html | 15 ++-- 7 files changed, 124 insertions(+), 110 deletions(-) create mode 100644 tasks/EmailAddressTask.php diff --git a/code/Location.php b/code/Location.php index f0cc45d..6d715be 100644 --- a/code/Location.php +++ b/code/Location.php @@ -2,32 +2,32 @@ class Location extends DataObject implements PermissionProvider { - private static $db = array( 'Title' => 'Varchar(255)', 'Featured' => 'Boolean', 'Website' => 'Varchar(255)', 'Phone' => 'Varchar(40)', + 'Email' => 'Varchar(255)', 'EmailAddress' => 'Varchar(255)', 'ShowInLocator' => 'Boolean', ); private static $has_one = array( - 'Category' => 'LocationCategory' + 'Category' => 'LocationCategory', ); private static $casting = array( - 'distance' => 'Int' + 'distance' => 'Int', ); private static $default_sort = 'Title'; private static $defaults = array( - 'ShowInLocator' => true + 'ShowInLocator' => true, ); - private static $singular_name = "Location"; - private static $plural_name = "Locations"; + private static $singular_name = 'Location'; + private static $plural_name = 'Locations'; // api access via Restful Server module private static $api_access = true; @@ -40,12 +40,12 @@ class Location extends DataObject implements PermissionProvider 'State', 'Postcode', 'Country', + 'Website', + 'Phone', + 'Email', 'Category.ID', 'ShowInLocator', 'Featured', - 'Website', - 'Phone', - 'EmailAddress' ); // columns for grid field @@ -59,7 +59,7 @@ class Location extends DataObject implements PermissionProvider 'Category.Name', 'ShowInLocator.NiceAsBoolean', 'Featured.NiceAsBoolean', - 'Coords' + 'Coords', ); // Coords status for $summary_fields @@ -69,17 +69,17 @@ public function getCoords() } // custom labels for fields - function fieldLabels($includerelations = true) + public function fieldLabels($includerelations = true) { $labels = parent::fieldLabels(); $labels['Title'] = 'Name'; - $labels['Suburb'] = "City"; + $labels['Suburb'] = 'City'; $labels['Postcode'] = 'Postal Code'; $labels['ShowInLocator'] = 'Show'; $labels['ShowInLocator.NiceAsBoolean'] = 'Show'; $labels['Category.Name'] = 'Category'; - $labels['EmailAddress'] = 'Email'; + $labels['Email'] = 'Email'; $labels['Featured.NiceAsBoolean'] = 'Featured'; $labels['Coords'] = 'Coords'; @@ -88,38 +88,33 @@ function fieldLabels($includerelations = true) public function getCMSFields() { - - $fields = parent::getCMSFields(); - - // remove Main tab - $fields->removeByName('Main'); - - // create and populate Info tab - $fields->addFieldsToTab('Root.Info', array( - HeaderField::create('InfoHeader', 'Contact Information'), - TextField::create('Website'), - TextField::create('EmailAddress'), - TextField::create('Phone') - )); - - // change label of Suburb from Addressable to City - $fields->removeByName('Suburb'); - $fields->insertBefore(TextField::create('Suburb', 'City'), 'State'); - - // If categories have been created, show category drop down - if (LocationCategory::get()->Count() > 0) { - $fields->insertAfter(DropDownField::create('CategoryID', 'Category', - LocationCategory::get()->map('ID', 'Title'))->setEmptyString('-- select --'), 'Phone'); - } - - // move Title and ShowInLocator fields to Address tab from Addressable - $fields->insertAfter(TextField::create('Title'), 'AddressHeader'); - $fields->insertAfter(CheckboxField::create('Featured', 'Featured'), 'Title'); - $fields->insertAfter(CheckboxField::create('ShowInLocator', 'Show on Map'), 'Country'); + $fields = FieldList::create( + new TabSet( + $name = 'Root', + new Tab( + $title = 'Main', + HeaderField::create('ContactHD', 'Contact Information'), + TextField::create('Title', 'Name'), + TextField::create('Phone'), + EmailField::create('Email', 'Email'), + TextField::create('Website') + ->setAttribute('placeholder', 'http://'), + DropDownField::create('CategoryID', 'Category', LocationCategory::get()->map('ID', 'Title')) + ->setEmptyString('-- select --'), + CheckboxField::create('ShowInLocator', 'Show in results') + ->setDescription('Location will be included in results list'), + CheckboxField::create('Featured') + ->setDescription('Location will show at/near the top of the results list') + ) + ) + ); // allow to be extended via DataExtension $this->extend('updateCMSFields', $fields); + // override Suburb field name + $fields->dataFieldByName('Suburb')->setTitle('City'); + return $fields; } @@ -130,9 +125,22 @@ public function validate() return $result; } + public function EmailAddress() + { + Deprecation::notice('3.0', 'Use "$Email" instead.'); + if ($this->Email) { + return $this->Email; + } elseif ($this->EmailAddress) { + return $this->EmailAddress; + } + + return false; + } + /** * @param Member $member - * @return true + * + * @return bool */ public function canView($member = false) { @@ -159,14 +167,12 @@ public function providePermissions() return array( 'Location_EDIT' => 'Edit a Location', 'Location_DELETE' => 'Delete a Location', - 'Location_CREATE' => 'Create a Location' + 'Location_CREATE' => 'Create a Location', ); } public function onBeforeWrite() { - parent::onBeforeWrite(); } - -} \ No newline at end of file +} diff --git a/code/LocationAdmin.php b/code/LocationAdmin.php index 899abe2..676ace1 100644 --- a/code/LocationAdmin.php +++ b/code/LocationAdmin.php @@ -28,7 +28,7 @@ public function getExportFields() 'Website' => 'Website', 'Phone' => 'Phone', 'Fax' => 'Fax', - 'EmailAddress' => 'EmailAddress', + 'Email' => 'Email', 'Category.Name' => 'Category', 'ShowInLocator' => 'ShowInLocator', 'Featured' => 'Featured', diff --git a/code/LocationCsvBulkLoader.php b/code/LocationCsvBulkLoader.php index bd1030c..3522606 100644 --- a/code/LocationCsvBulkLoader.php +++ b/code/LocationCsvBulkLoader.php @@ -5,6 +5,7 @@ class LocationCsvBulkLoader extends CsvBulkLoader public $columnMap = array( 'Name' => 'Title', 'City' => 'Suburb', + 'EmailAddress' => 'Email', 'Category' => 'Category.Name', ); diff --git a/code/Locator.php b/code/Locator.php index 58dee02..b9439a4 100644 --- a/code/Locator.php +++ b/code/Locator.php @@ -2,22 +2,19 @@ class Locator extends Page { - private static $db = array( 'AutoGeocode' => 'Boolean', 'ModalWindow' => 'Boolean', - 'Unit' => 'Enum("km,m","m")' + 'Unit' => 'Enum("m,km","m")', ); - private static $has_many = array(); - private static $defaults = array( - 'AutoGeocode' => true + 'AutoGeocode' => true, ); - private static $singular_name = "Locator"; - private static $plural_name = "Locators"; - private static $description = 'Find locations on a map'; + private static $singular_name = 'Locator'; + private static $plural_name = 'Locators'; + private static $description = 'Show locations on a map'; public function getCMSFields() { @@ -26,21 +23,19 @@ public function getCMSFields() // Locations Grid Field $config = GridFieldConfig_RecordEditor::create(); $locations = Location::get(); - $fields->addFieldToTab("Root.Locations", GridField::create("Locations", "Locations", $locations, $config)); + $fields->addFieldToTab('Root.Locations', GridField::create('Locations', 'Locations', $locations, $config)); // Location categories $config = GridFieldConfig_RecordEditor::create(); - $fields->addFieldToTab("Root.Categories", - GridField::create("Categories", "Categories", LocationCategory::get(), $config)); + $fields->addFieldToTab('Root.Categories', GridField::create('Categories', 'Categories', LocationCategory::get(), $config)); // Settings $fields->addFieldsToTab('Root.Settings', array( HeaderField::create('DisplayOptions', 'Display Options', 3), - OptionsetField::create('Unit', 'Unit of measure', array('km' => 'Kilometers', 'm' => 'Miles')), - CheckboxField::create('AutoGeocode', - 'Auto Geocode - Automatically filter map results based on user location') + OptionsetField::create('Unit', 'Unit of measure', array('m' => 'Miles', 'km' => 'Kilometers')), + CheckboxField::create('AutoGeocode', 'Auto Geocode - Automatically filter map results based on user location') ->setDescription('Note: if any locations are set as featured, the auto geocode is automatically disabled.'), - CheckboxField::create('ModalWindow', 'Modal Window - Show Map results in a modal window') + CheckboxField::create('ModalWindow', 'Modal Window - Show Map results in a modal window'), )); $this->extend('updateCMSFields', $fields); @@ -51,6 +46,7 @@ public function getCMSFields() public static function getLocations($filter = array(), $exclude = array()) { $filter['ShowInLocator'] = true; + return Location::get() ->exclude($exclude) ->exclude('Lat', 0) @@ -66,12 +62,10 @@ public function getAllCategories() { return LocationCategory::get(); } - } class Locator_Controller extends Page_Controller { - // allowed actions private static $allowed_actions = array('xml'); @@ -101,15 +95,14 @@ public function init() 'autoGeocode: true, fullMapStart: false,' : 'autoGeocode: false, fullMapStart: true, storeLimit: 1000, maxDistance: true,'; - $absoluteBase = getcwd();//get current working dir - $base = str_replace('/framework', '', $absoluteBase);//remove framework if .htaccess is working - $themePath = $base . "/" . $themeDir; + $base = Director::baseFolder(); + $themePath = $base.'/'.$themeDir; - $listTemplatePath = (file_exists($themePath . '/templates/location-list-description.html')) ? - $themeDir . '/templates/location-list-description.html' : + $listTemplatePath = (file_exists($themePath.'/templates/location-list-description.html')) ? + $themeDir.'/templates/location-list-description.html' : 'locator/templates/location-list-description.html'; - $infowindowTemplatePath = (file_exists($themePath . '/templates/infowindow-description.html')) ? - $themeDir . '/templates/infowindow-description.html' : + $infowindowTemplatePath = (file_exists($themePath.'/templates/infowindow-description.html')) ? + $themeDir.'/templates/infowindow-description.html' : 'locator/templates/infowindow-description.html'; // in page or modal @@ -117,42 +110,41 @@ public function init() $kilometer = ($this->data()->Unit == 'km') ? 'lengthUnit: "km"' : 'lengthUnit: "m"'; - $link = $this->Link() . "xml.xml"; + $link = $this->Link().'xml.xml'; // init map if (Locator::getLocations()) { Requirements::customScript(" $(function($) { - $('#map-container').storeLocator({ - " . $load . " - dataLocation: '" . $link . "', - listTemplatePath: '" . $listTemplatePath . "', - infowindowTemplatePath: '" . $infowindowTemplatePath . "', - originMarker: true, - " . $modal . ", - " . $featured . ", - slideMap: false, - zoomLevel: 0, - distanceAlert: 120, - formID: 'Form_LocationSearch', - inputID: 'Form_LocationSearch_address', - categoryID: 'Form_LocationSearch_category', - distanceAlert: -1, - " . $kilometer . " - }); + $('#map-container').storeLocator({ + ".$load." + dataLocation: '".$link."', + listTemplatePath: '".$listTemplatePath."', + infowindowTemplatePath: '".$infowindowTemplatePath."', + originMarker: true, + ".$modal.', + '.$featured.", + slideMap: false, + zoomLevel: 0, + distanceAlert: 120, + formID: 'Form_LocationSearch', + inputID: 'Form_LocationSearch_address', + categoryID: 'Form_LocationSearch_category', + distanceAlert: -1, + ".$kilometer.' + }); }); - "); + '); } - } /** - * Find all locations for map + * Find all locations for map. * * Will return a XML feed of all locations marked "show in locator". * - * @access public * @return XML file + * * @todo rename/refactor to allow for json/xml * @todo allow $filter to run off of getVars key/val pair */ @@ -162,18 +154,15 @@ public function xml(SS_HTTPRequest $request) $Locations = Locator::getLocations($filter); return $this->customise(array( - 'Locations' => $Locations + 'Locations' => $Locations, ))->renderWith('LocationXML'); - } - /** * LocationSearch form. * * Search form for locations, updates map and results list via AJAX * - * @access public * @return Form */ public function LocationSearch() @@ -184,10 +173,8 @@ public function LocationSearch() $address->setAttribute('placeholder', 'address or zip code'); if (LocationCategory::get()->Count() > 0) { - $filter = array(); $locals = Locator::getLocations($filter, $exclude = array('CategoryID' => 0)); - //debug::show($locals); $categories = ArrayList::create(); foreach ($locals as $local) { @@ -209,7 +196,5 @@ public function LocationSearch() ); return Form::create($this, 'LocationSearch', $fields, $actions); - } - } diff --git a/tasks/EmailAddressTask.php b/tasks/EmailAddressTask.php new file mode 100644 index 0000000..5114275 --- /dev/null +++ b/tasks/EmailAddressTask.php @@ -0,0 +1,23 @@ +update('DataObject', 'validation_enabled', false); + $ct = 0; + $updateEmail = function ($location) use (&$ct) { + if (!$location->Email && $location->EmailAddress) { + $location->Email = $location->EmailAddress; + $location->write(); + ++$ct; + } + }; + Location::get()->each($updateEmail); + Config::inst()->update('DataObject', 'validation_enabled', true); + echo '

'.$ct.' Locations updated

'; + } +} diff --git a/templates/LocationXML.ss b/templates/LocationXML.ss index d48b762..ebaa2a8 100644 --- a/templates/LocationXML.ss +++ b/templates/LocationXML.ss @@ -1,3 +1,3 @@ - <% loop Locations %><% end_loop %> + <% loop Locations %><% end_loop %> \ No newline at end of file diff --git a/templates/location-list-description.html b/templates/location-list-description.html index 1cb5388..b185655 100644 --- a/templates/location-list-description.html +++ b/templates/location-list-description.html @@ -4,16 +4,15 @@
{{name}}
-
{{address}}
-
{{address2}}
-
{{city}}, {{state}} {{postal}}
-
{{phone}}
- {{#if web}} - - {{/if}} +
{{address}}
+ {{#if address2}}
{{address2}}
{{/if}} +
{{city}}, {{state}} {{postal}}
+ {{#if phone}}
{{phone}}
{{/if}} + {{#if web}}{{/if}} + {{#if email}}{{/if}} {{#if distance}}
- {{distance}} {{length}} | + {{distance}} {{length}} | Directions
{{/if}}