Skip to content

Commit

Permalink
ENHANCEMENT default visibility of locations and form (#239)
Browse files Browse the repository at this point in the history
* ENHANCEMNET default visibility of locations and form

* UPDATE test to reflect change to Locator::locator_categories_by_locator()
muskie9 authored Mar 16, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 3597ace commit f6a9735
Showing 3 changed files with 37 additions and 12 deletions.
36 changes: 29 additions & 7 deletions src/Page/Locator.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
use Dynamic\Locator\Model\LocationCategory;
use Dynamic\SilverStripeGeocoder\AddressDataExtension;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\HeaderField;
@@ -15,6 +16,7 @@
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\ArrayList;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\View\ArrayData;
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;

@@ -24,6 +26,8 @@
* @property bool $AutoGeocode
* @property bool $ModalWindow
* @property string $Unit
* @property bool $ShowResultsDefault
* @property bool $ShowFormDefault
* @method Categories|ManyManyList $Categories
*/
class Locator extends \Page
@@ -48,6 +52,8 @@ class Locator extends \Page
*/
private static $db = [
'Unit' => 'Enum("m,km","m")',
'ShowResultsDefault' => 'Boolean',
'ShowFormDefault' => 'Boolean',
];

/**
@@ -81,6 +87,15 @@ class Locator extends \Page
LocationPage::class,
];

/**
* @var array
*/
private static $defaults = [
'ShowResultsDefault' => false,
'ShowFormDefault' => true,
'ShowInMenus' => false,
];

/**
* @return FieldList
*/
@@ -91,6 +106,14 @@ public function getCMSFields()
$fields->addFieldsToTab('Root.Settings', [
HeaderField::create('DisplayOptions', 'Display Options', 3),
OptionsetField::create('Unit', 'Unit of measure', ['m' => 'Miles', 'km' => 'Kilometers']),
DropdownField::create('ShowResultsDefault')
->setTitle('Show results by default')
->setDescription('This will display results if no filters are applied to the locator')
->setSource([false => 'No', true => 'Yes']),
DropdownField::create('ShowFormDefault')
->setTitle('Show filter form by default')
->setDescription('This will display the filter form for the locator')
->setSource([false => 'No', true => 'Yes']),
]);

// Filter categories
@@ -147,30 +170,29 @@ public static function get_all_categories()
}

/**
* @return bool
* @return ArrayList|ManyManyList
*/
public function getPageCategories()
public function getPageCategories(): ArrayList|ManyManyList
{
return self::locator_categories_by_locator($this->ID);
}

/**
* @param int $id
*
* @return bool|
* @return ManyManyList|ArrayList
*/
public static function locator_categories_by_locator($id = 0)
public static function locator_categories_by_locator($id = 0): ArrayList|ManyManyList
{
if ($id == 0) {
return false;
return ArrayList::create();
}

/** @var Locator $locator */
if ($locator = static::get()->byID($id)) {
return $locator->getUsedCategories();
}

return false;
return ArrayList::create();
}

/**
11 changes: 7 additions & 4 deletions src/Page/LocatorController.php
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ public function init()

// prevent init of map if no query
$request = Controller::curr()->getRequest();
if (!$this->getTrigger($request)) {
if (!$this->getTrigger($request) && !$this->ShowResultsDefault) {
return;
}

@@ -299,7 +299,7 @@ public function getAddressSearchCoords()
*/
public function index(HTTPRequest $request)
{
if ($this->getTrigger($request)) {
if ($this->getTrigger($request) || $this->ShowResultsDefault) {
$locations = $this->getLocations();
} else {
$locations = ArrayList::create();
@@ -347,10 +347,13 @@ public function json()
*
* Search form for locations, updates map and results list via AJAX
*
* @return \SilverStripe\Forms\Form
* @return LocatorForm|null
*/
public function LocationSearch()
public function LocationSearch(): ?LocatorForm
{
if (!$this->ShowFormDefault) {
return null;
}

$form = LocatorForm::create($this, 'LocationSearch');

2 changes: 1 addition & 1 deletion tests/Page/LocatorTest.php
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ public function testGetPageCategories()
public function testLocator_categories_by_locator()
{
$categories = Locator::locator_categories_by_locator(0);
$this->assertFalse($categories);
$this->assertEquals(0, $categories->count());
}

/**

0 comments on commit f6a9735

Please sign in to comment.