diff --git a/code/Location.php b/code/Location.php index 503e022..f0cc45d 100644 --- a/code/Location.php +++ b/code/Location.php @@ -1,35 +1,36 @@ 'Varchar(255)', - 'Featured' => 'Boolean', - 'Website' => 'Varchar(255)', - 'Phone' => 'Varchar(40)', - 'EmailAddress' => 'Varchar(255)', - 'ShowInLocator' => 'Boolean', - ); - - static $has_one = array( - 'Category' => 'LocationCategory' - ); - - static $casting = array( - 'distance' => 'Int' - ); - - static $default_sort = 'Title'; - - static $defaults = array( - 'ShowInLocator' => true - ); - - public static $singular_name = "Location"; - public static $plural_name = "Locations"; - - // api access via Restful Server module - static $api_access = true; +class Location extends DataObject implements PermissionProvider +{ + + private static $db = array( + 'Title' => 'Varchar(255)', + 'Featured' => 'Boolean', + 'Website' => 'Varchar(255)', + 'Phone' => 'Varchar(40)', + 'EmailAddress' => 'Varchar(255)', + 'ShowInLocator' => 'Boolean', + ); + + private static $has_one = array( + 'Category' => 'LocationCategory' + ); + + private static $casting = array( + 'distance' => 'Int' + ); + + private static $default_sort = 'Title'; + + private static $defaults = array( + 'ShowInLocator' => true + ); + + private static $singular_name = "Location"; + private static $plural_name = "Locations"; + + // api access via Restful Server module + private static $api_access = true; // search fields for Model Admin private static $searchable_fields = array( @@ -46,117 +47,126 @@ class Location extends DataObject implements PermissionProvider{ 'Phone', 'EmailAddress' ); - - // columns for grid field - static $summary_fields = array( - 'Title', - 'Address', - 'Suburb', - 'State', - 'Postcode', - 'Country', - 'Category.Name', - 'ShowInLocator.NiceAsBoolean', - 'Featured.NiceAsBoolean', + + // columns for grid field + private static $summary_fields = array( + 'Title', + 'Address', + 'Suburb', + 'State', + 'Postcode', + 'Country', + 'Category.Name', + 'ShowInLocator.NiceAsBoolean', + 'Featured.NiceAsBoolean', 'Coords' - ); + ); - // Coords status for $summary_fields - public function getCoords() { - return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false'; - } + // Coords status for $summary_fields + public function getCoords() + { + return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false'; + } // custom labels for fields - function fieldLabels($includerelations = true) { - $labels = parent::fieldLabels(); - - $labels['Title'] = 'Name'; - $labels['Suburb'] = "City"; - $labels['Postcode'] = 'Postal Code'; - $labels['ShowInLocator'] = 'Show'; + function fieldLabels($includerelations = true) + { + $labels = parent::fieldLabels(); + + $labels['Title'] = 'Name'; + $labels['Suburb'] = "City"; + $labels['Postcode'] = 'Postal Code'; + $labels['ShowInLocator'] = 'Show'; $labels['ShowInLocator.NiceAsBoolean'] = 'Show'; - $labels['Category.Name'] = 'Category'; - $labels['EmailAddress'] = 'Email'; - $labels['Featured.NiceAsBoolean'] = 'Featured'; + $labels['Category.Name'] = 'Category'; + $labels['EmailAddress'] = 'Email'; + $labels['Featured.NiceAsBoolean'] = 'Featured'; $labels['Coords'] = 'Coords'; - return $labels; - } - - 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'); + return $labels; + } + + 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'); // allow to be extended via DataExtension - $this->extend('updateCMSFields', $fields); - - return $fields; - } + $this->extend('updateCMSFields', $fields); - public function validate() { + return $fields; + } + + public function validate() + { $result = parent::validate(); return $result; } - /** - * @param Member $member - * @return boolean - */ - public function canView($member = false) { - //return Permission::check('Location_VIEW'); - return true; - } - - public function canEdit($member = false) { - return Permission::check('Location_EDIT'); - } - - public function canDelete($member = false) { - return Permission::check('Location_DELETE'); - } - - public function canCreate($member = false) { - return Permission::check('Location_CREATE'); - } - - public function providePermissions() { - return array( - //'Location_VIEW' => 'Read a Location', - 'Location_EDIT' => 'Edit a Location', - 'Location_DELETE' => 'Delete a Location', - 'Location_CREATE' => 'Create a Location' - ); - } - - public function onBeforeWrite(){ + /** + * @param Member $member + * @return true + */ + public function canView($member = false) + { + return true; + } + + public function canEdit($member = false) + { + return Permission::check('Location_EDIT'); + } + + public function canDelete($member = false) + { + return Permission::check('Location_DELETE'); + } + + public function canCreate($member = false) + { + return Permission::check('Location_CREATE'); + } + + public function providePermissions() + { + return array( + 'Location_EDIT' => 'Edit a Location', + 'Location_DELETE' => 'Delete 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 ad4e87e..b49f0cf 100644 --- a/code/LocationAdmin.php +++ b/code/LocationAdmin.php @@ -1,34 +1,37 @@ 'LocationCsvBulkLoader', - 'LocationCategory' => 'CsvBulkLoader' + +class LocationAdmin extends ModelAdmin +{ + + private static $managed_models = array( + 'Location', + 'LocationCategory' ); - - static $menu_title = 'Locator'; - static $url_segment = 'locator'; - public function getExportFields() { + private static $model_importers = array( + 'Location' => 'LocationCsvBulkLoader', + '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' + 'Postcode' => 'Postal Code', + 'Country' => 'Country', + 'Website' => 'Website', + 'Phone' => 'Phone', + 'Fax' => 'Fax', + 'EmailAddress' => 'Email Address', + 'ShowInLocator' => 'Show', + 'Lat' => 'Lat', + 'Lng' => 'Lng' ); } } \ No newline at end of file diff --git a/code/LocationCategory.php b/code/LocationCategory.php index e8744cc..cd63604 100644 --- a/code/LocationCategory.php +++ b/code/LocationCategory.php @@ -1,18 +1,19 @@ 'Varchar(100)' - ); - - static $has_many = array( - 'Locations' => 'Location' - ); - - public static $singular_name = "Category"; - public static $plural_name = "Categories"; - - private static $default_sort = 'Name'; - +class LocationCategory extends DataObject +{ + + private static $db = array( + 'Name' => 'Varchar(100)' + ); + + private static $has_many = array( + 'Locations' => 'Location' + ); + + private static $singular_name = "Category"; + Private static $plural_name = "Categories"; + + private static $default_sort = 'Name'; + } \ No newline at end of file diff --git a/code/LocationCsvBulkLoader.php b/code/LocationCsvBulkLoader.php index c5f69eb..0a9c71c 100644 --- a/code/LocationCsvBulkLoader.php +++ b/code/LocationCsvBulkLoader.php @@ -1,30 +1,33 @@ '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' - ) - ); - - public static function getCatByName(&$obj, $val, $record) { - $val = Convert::raw2sql($val); - return LocationCategory::get()->filter('Name', $val)->First(); - //); - } + +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' + ) + ); + + public static function getCatByName(&$obj, $val, $record) + { + $val = Convert::raw2sql($val); + return LocationCategory::get()->filter('Name', $val)->First(); + //); + } } \ No newline at end of file diff --git a/code/Locator.php b/code/Locator.php index d9ae953..58dee02 100644 --- a/code/Locator.php +++ b/code/Locator.php @@ -1,117 +1,126 @@ 'Boolean', - 'ModalWindow' => 'Boolean', +class Locator extends Page +{ + + private static $db = array( + 'AutoGeocode' => 'Boolean', + 'ModalWindow' => 'Boolean', 'Unit' => 'Enum("km,m","m")' - ); + ); private static $has_many = array(); - - private static $defaults = array( - 'AutoGeocode' => true - ); - - private static $singular_name = "Locator"; + + private static $defaults = array( + 'AutoGeocode' => true + ); + + private static $singular_name = "Locator"; private static $plural_name = "Locators"; private static $description = 'Find locations on a map'; - - public function getCMSFields() { - $fields = parent::getCMSFields(); - - // Locations Grid Field - $config = GridFieldConfig_RecordEditor::create(); + + public function getCMSFields() + { + $fields = parent::getCMSFields(); + + // Locations Grid Field + $config = GridFieldConfig_RecordEditor::create(); $locations = Location::get(); - $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)); - - // Settings - $fields->addFieldsToTab('Root.Settings', array( - HeaderField::create('DisplayOptions', 'Display Options', 3), + $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)); + + // 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') - ->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') - )); - - $this->extend('updateCMSFields', $fields); - - return $fields; + 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') + )); + + $this->extend('updateCMSFields', $fields); + + return $fields; } - public static function getLocations($filter = array(), $exclude = array()){ - $filter['ShowInLocator'] = true; - return Location::get() - ->exclude($exclude) - ->exclude('Lat', 0) - ->filter($filter); - } + public static function getLocations($filter = array(), $exclude = array()) + { + $filter['ShowInLocator'] = true; + return Location::get() + ->exclude($exclude) + ->exclude('Lat', 0) + ->filter($filter); + } - public function getAreLocations(){ - return self::getLocations(); - } + public function getAreLocations() + { + return self::getLocations(); + } - public function getAllCategories(){ - return LocationCategory::get(); - } + public function getAllCategories() + { + return LocationCategory::get(); + } } -class Locator_Controller extends Page_Controller { - - // allowed actions - private static $allowed_actions = array('xml'); - - // Set Requirements based on input from CMS - public function init() { - parent::init(); - - $themeDir = SSViewer::get_theme_folder(); - - Requirements::javascript('framework/thirdparty/jquery/jquery.js'); - if(Locator::getLocations()){ - Requirements::javascript('http://maps.google.com/maps/api/js?sensor=false'); - Requirements::javascript('locator/thirdparty/handlebars/handlebars-v1.3.0.js'); - Requirements::javascript('locator/thirdparty/jquery-store-locator/js/jquery.storelocator.js'); - } - - Requirements::css('locator/css/map.css'); - - $featured = (Locator::getLocations(array('Featured' => 1))->count() > 0) ? - 'featuredLocations: true' : - 'featuredLocations: false'; - - // map config based on user input in Settings tab - // AutoGeocode or Full Map - $load = ($this->data()->AutoGeocode) ? - '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; - - $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' : - 'locator/templates/infowindow-description.html'; - - // in page or modal - $modal = ($this->data()->ModalWindow) ? 'modalWindow: true' : 'modalWindow: false'; +class Locator_Controller extends Page_Controller +{ + + // allowed actions + private static $allowed_actions = array('xml'); + + // Set Requirements based on input from CMS + public function init() + { + parent::init(); + + $themeDir = SSViewer::get_theme_folder(); + + Requirements::javascript('framework/thirdparty/jquery/jquery.js'); + if (Locator::getLocations()) { + Requirements::javascript('http://maps.google.com/maps/api/js?sensor=false'); + Requirements::javascript('locator/thirdparty/handlebars/handlebars-v1.3.0.js'); + Requirements::javascript('locator/thirdparty/jquery-store-locator/js/jquery.storelocator.js'); + } + + Requirements::css('locator/css/map.css'); + + $featured = (Locator::getLocations(array('Featured' => 1))->count() > 0) ? + 'featuredLocations: true' : + 'featuredLocations: false'; + + // map config based on user input in Settings tab + // AutoGeocode or Full Map + $load = ($this->data()->AutoGeocode) ? + '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; + + $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' : + 'locator/templates/infowindow-description.html'; + + // in page or modal + $modal = ($this->data()->ModalWindow) ? 'modalWindow: true' : 'modalWindow: false'; $kilometer = ($this->data()->Unit == 'km') ? 'lengthUnit: "km"' : 'lengthUnit: "m"'; $link = $this->Link() . "xml.xml"; - // init map - if(Locator::getLocations()) { + // init map + if (Locator::getLocations()) { Requirements::customScript(" $(function($) { $('#map-container').storeLocator({ @@ -135,70 +144,72 @@ public function init() { "); } - } - - /** - * Find all locations for map - * - * Will return a XML feed of all locations marked "show in locator". - * - * @access public - * @return XML file + } + + /** + * 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 - */ - public function xml(SS_HTTPRequest $request) { + * @todo allow $filter to run off of getVars key/val pair + */ + public function xml(SS_HTTPRequest $request) + { $filter = array(); - $Locations = Locator::getLocations($filter); - - return $this->customise(array( - 'Locations' => $Locations - ))->renderWith('LocationXML'); - - } - - - /** - * LocationSearch form. - * - * Search form for locations, updates map and results list via AJAX - * - * @access public - * @return Form - */ - public function LocationSearch() { - $fields = FieldList::create( - $address = TextField::create('address', '') - ); - $address->setAttribute('placeholder', 'address or zip code'); - - if (LocationCategory::get()->Count() > 0) { + $Locations = Locator::getLocations($filter); + + return $this->customise(array( + 'Locations' => $Locations + ))->renderWith('LocationXML'); + + } + + + /** + * LocationSearch form. + * + * Search form for locations, updates map and results list via AJAX + * + * @access public + * @return Form + */ + public function LocationSearch() + { + $fields = FieldList::create( + $address = TextField::create('address', '') + ); + $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){ - $categories->add($local->Category()); - } - - if($categories->count()>0){ - $fields->push( - DropdownField::create( - 'category', - '', - $categories->map('Title', 'Title') - )->setEmptyString('Select Category')); - } - } - - $actions = FieldList::create( - FormAction::create('', 'Search') - ); - - return Form::create($this, 'LocationSearch', $fields, $actions); - - } - + $locals = Locator::getLocations($filter, $exclude = array('CategoryID' => 0)); + //debug::show($locals); + $categories = ArrayList::create(); + + foreach ($locals as $local) { + $categories->add($local->Category()); + } + + if ($categories->count() > 0) { + $fields->push( + DropdownField::create( + 'category', + '', + $categories->map('Title', 'Title') + )->setEmptyString('Select Category')); + } + } + + $actions = FieldList::create( + FormAction::create('', 'Search') + ); + + return Form::create($this, 'LocationSearch', $fields, $actions); + + } + }