Skip to content

Commit afa771b

Browse files
committed
[TASK] Add sorting to plugin configuration and repository
Related: #78
1 parent 56a9b45 commit afa771b

File tree

8 files changed

+344
-10
lines changed

8 files changed

+344
-10
lines changed

Classes/Controller/AddressController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public function searchAction()
5050
{
5151
/** @var AddressSearch $addressSearch */
5252
$addressSearch = $this->objectManager->get(AddressSearch::class);
53+
if ($this->settings['orderBy']) {
54+
$addressSearch->setOrderBy($this->settings['orderBy']);
55+
if ($this->settings['fallbackOrderBy']) {
56+
$addressSearch->setFallbackOrderBy($this->settings['fallbackOrderBy']);
57+
}
58+
}
5359

5460
if ($this->request->hasArgument('tx_contacts_addresssearch')) {
5561
$addressSearchArgs = $this->request->getArgument('tx_contacts_addresssearch');

Classes/Domain/Model/Dto/AddressSearch.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ class AddressSearch
3737
*/
3838
protected $searchString = '';
3939

40+
/**
41+
* @var string
42+
*/
43+
protected $orderBy = '';
44+
45+
/**
46+
* @var string
47+
*/
48+
protected $fallbackOrderBy = '';
49+
4050
/**
4151
* @return float
4252
*/
@@ -116,4 +126,36 @@ public function setSearchString(string $searchString)
116126
{
117127
$this->searchString = $searchString;
118128
}
129+
130+
/**
131+
* @return string
132+
*/
133+
public function getOrderBy(): string
134+
{
135+
return $this->orderBy;
136+
}
137+
138+
/**
139+
* @param string $orderBy
140+
*/
141+
public function setOrderBy(string $orderBy): void
142+
{
143+
$this->orderBy = $orderBy;
144+
}
145+
146+
/**
147+
* @return string
148+
*/
149+
public function getFallbackOrderBy(): string
150+
{
151+
return $this->fallbackOrderBy;
152+
}
153+
154+
/**
155+
* @param string $fallbackOrderBy
156+
*/
157+
public function setFallbackOrderBy(string $fallbackOrderBy)
158+
{
159+
$this->fallbackOrderBy = $fallbackOrderBy;
160+
}
119161
}

Classes/Domain/Repository/AddressRepository.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ protected function findAddressesByDistance(AddressSearch $addressSearch): array
295295
);
296296
}
297297

298+
if ($addressSearch->getOrderBy() !== 'distance') {
299+
$queryBuilder->orderBy($addressSearch->getOrderBy());
300+
} elseif ($addressSearch->getOrderBy() === 'distance' && $addressSearch->getFallbackOrderBy()) {
301+
$queryBuilder->orderBy($addressSearch->getFallbackOrderBy());
302+
}
303+
298304
$addresses = $queryBuilder->execute()->fetchAll();
299305

300306
if ($addressSearch->getLat() !== 0.0 && $addressSearch->getLon() !== 0.0 && $addressSearch->getRadius() !== 0) {
@@ -306,7 +312,13 @@ protected function findAddressesByDistance(AddressSearch $addressSearch): array
306312
if ($distance < $addressSearch->getRadius()) {
307313
$address['distance'] = $distance;
308314

309-
$addressesInDistance[(string)$distance] = $address;
315+
if ($addressSearch->getOrderBy() === 'distance' && $addressSearch->getFallbackOrderBy() === '') {
316+
$addressesInDistanceKey = (string)$distance;
317+
} else {
318+
$addressesInDistanceKey = $addressKey;
319+
}
320+
321+
$addressesInDistance[$addressesInDistanceKey] = $address;
310322
}
311323
}
312324

Configuration/FlexForms/AddressSearchPlugin.xml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,58 @@
2323
<settings.displayResultList>
2424
<TCEforms>
2525
<label>LLL:EXT:contacts/Resources/Private/Language/locallang_be.xlf:tx_contacts.plugin.addressSearch.settings.displayResultList</label>
26+
<onChange>reload</onChange>
2627
<config>
2728
<type>check</type>
2829
</config>
2930
</TCEforms>
3031
</settings.displayResultList>
3132

33+
<settings.orderBy>
34+
<TCEforms>
35+
<label>LLL:EXT:contacts/Resources/Private/Language/locallang_be.xlf:tx_contacts.plugin.addressSearch.settings.orderBy</label>
36+
<onChange>reload</onChange>
37+
<displayCond>FIELD:settings.displayResultList:=:1</displayCond>
38+
<config>
39+
<type>select</type>
40+
<items>
41+
<numIndex index="0" type="array">
42+
<numIndex index="0"></numIndex>
43+
<numIndex index="1"></numIndex>
44+
</numIndex>
45+
<numIndex index="1">
46+
<numIndex index="0">LLL:EXT:contacts/Resources/Private/Language/locallang_be.xlf:tx_contacts.plugin.addressSearch.settings.orderBy.distance</numIndex>
47+
<numIndex index="1">distance</numIndex>
48+
</numIndex>
49+
<numIndex index="2">
50+
<numIndex index="0">LLL:EXT:contacts/Resources/Private/Language/locallang_be.xlf:tx_contacts.plugin.addressSearch.settings.orderBy.title</numIndex>
51+
<numIndex index="1">title</numIndex>
52+
</numIndex>
53+
</items>
54+
</config>
55+
</TCEforms>
56+
</settings.orderBy>
57+
58+
<settings.fallbackOrderBy>
59+
<TCEforms>
60+
<label>LLL:EXT:contacts/Resources/Private/Language/locallang_be.xlf:tx_contacts.plugin.addressSearch.settings.fallbackOrderBy</label>
61+
<displayCond>FIELD:settings.orderBy:=:distance</displayCond>
62+
<config>
63+
<type>select</type>
64+
<items>
65+
<numIndex index="0" type="array">
66+
<numIndex index="0"></numIndex>
67+
<numIndex index="1"></numIndex>
68+
</numIndex>
69+
<numIndex index="2">
70+
<numIndex index="0">LLL:EXT:contacts/Resources/Private/Language/locallang_be.xlf:tx_contacts.plugin.addressSearch.settings.orderBy.title</numIndex>
71+
<numIndex index="1">title</numIndex>
72+
</numIndex>
73+
</items>
74+
</config>
75+
</TCEforms>
76+
</settings.fallbackOrderBy>
77+
3278
<settings.showPid>
3379
<TCEforms>
3480
<exclude>1</exclude>
@@ -53,4 +99,4 @@
5399
</ROOT>
54100
</sDEF>
55101
</sheets>
56-
</T3DataStructure>
102+
</T3DataStructure>

Resources/Private/Language/de.locallang_be.xlf

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
22
<xliff version="1.0">
3-
<file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2020-04-14T11:42:00Z" product-name="contacts">
3+
<file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2020-04-20T12:15:00Z" product-name="contacts">
44
<header/>
55
<body>
66

@@ -63,11 +63,27 @@
6363
<target>Google Maps</target>
6464
</trans-unit>
6565
<trans-unit id="tx_contacts.plugin.addressSearch.settings.displayResultList">
66-
<source>display result list</source>
66+
<source>Display Result List</source>
6767
<target>zeige Ergebnisliste</target>
6868
</trans-unit>
69+
<trans-unit id="tx_contacts.plugin.addressSearch.settings.orderBy">
70+
<source>Sort Order of Result List</source>
71+
<target>Sortierreihenfolge der Ergebnisliste</target>
72+
</trans-unit>
73+
<trans-unit id="tx_contacts.plugin.addressSearch.settings.fallbackOrderBy">
74+
<source>Sort Order of Result List without Radius Search</source>
75+
<target>Sortierreihenfolge der Ergebnisliste ohne Umkreissuche</target>
76+
</trans-unit>
77+
<trans-unit id="tx_contacts.plugin.addressSearch.settings.orderBy.distance">
78+
<source>Distance</source>
79+
<target>Entfernung</target>
80+
</trans-unit>
81+
<trans-unit id="tx_contacts.plugin.addressSearch.settings.orderBy.title">
82+
<source>Title</source>
83+
<target>Titel</target>
84+
</trans-unit>
6985
<trans-unit id="tx_contacts.plugin.addressSearch.settings.showPid">
70-
<source>detail page</source>
86+
<source>Detail Page</source>
7187
<target>Detailseite</target>
7288
</trans-unit>
7389

Resources/Private/Language/locallang_be.xlf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
22
<xliff version="1.0">
3-
<file source-language="en" datatype="plaintext" original="messages" date="2020-04-14T11:42:00Z" product-name="contacts">
3+
<file source-language="en" datatype="plaintext" original="messages" date="2020-04-20T12:15:00Z" product-name="contacts">
44
<header/>
55
<body>
66

@@ -46,10 +46,22 @@
4646
<source>Google Maps</source>
4747
</trans-unit>
4848
<trans-unit id="tx_contacts.plugin.addressSearch.settings.displayResultList">
49-
<source>display result list</source>
49+
<source>Display Result List</source>
50+
</trans-unit>
51+
<trans-unit id="tx_contacts.plugin.addressSearch.settings.orderBy">
52+
<source>Sort Order of Result List</source>
53+
</trans-unit>
54+
<trans-unit id="tx_contacts.plugin.addressSearch.settings.fallbackOrderBy">
55+
<source>Sort Order of Result List without Radius Search</source>
56+
</trans-unit>
57+
<trans-unit id="tx_contacts.plugin.addressSearch.settings.orderBy.distance">
58+
<source>Distance</source>
59+
</trans-unit>
60+
<trans-unit id="tx_contacts.plugin.addressSearch.settings.orderBy.title">
61+
<source>Title</source>
5062
</trans-unit>
5163
<trans-unit id="tx_contacts.plugin.addressSearch.settings.showPid">
52-
<source>detail page</source>
64+
<source>Detail Page</source>
5365
</trans-unit>
5466

5567
<trans-unit id="tx_contacts.plugin.contacts.action.contact.list.categories">

Tests/Functional/Repository/AddressRepositoryTest.php

Lines changed: 152 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public function findByDistanceWithoutCoordinates(): void
9393
$addressSearch->setPids('2');
9494

9595
$addresses = $this->addressRepository->findByDistance($addressSearch);
96-
$this->assertSame(
96+
$this->assertCount(
9797
16,
98-
count($addresses)
98+
$addresses
9999
);
100100
}
101101

@@ -294,4 +294,154 @@ public function findByDistanceWithSearchWord(): void
294294
$addresses
295295
);
296296
}
297+
298+
/**
299+
* @test
300+
*/
301+
public function findByDistanceWithoutRadiusAndSortingByTitle(): void
302+
{
303+
$addressSearch = new AddressSearch();
304+
$addressSearch->setPids('2');
305+
$addressSearch->setOrderBy('title');
306+
307+
$addresses = $this->addressRepository->findByDistance($addressSearch);
308+
$this->assertSame(
309+
'Abgeordnetenhaus von Berlin',
310+
$addresses[0]['title']
311+
);
312+
$this->assertSame(
313+
'Bayerischer Landtag',
314+
$addresses[1]['title']
315+
);
316+
$this->assertSame(
317+
'Bremische Bürgerschaft',
318+
$addresses[2]['title']
319+
);
320+
$this->assertSame(
321+
'Landtag von Baden-Württemberg',
322+
$addresses[10]['title']
323+
);
324+
$this->assertSame(
325+
'Thüringer Landtag',
326+
$addresses[15]['title']
327+
);
328+
}
329+
330+
/**
331+
* @test
332+
*/
333+
public function findByDistanceWithoutRadiusAndSortingByDistance(): void
334+
{
335+
$addressSearch = new AddressSearch();
336+
$addressSearch->setPids('2');
337+
$addressSearch->setOrderBy('distance');
338+
339+
$addresses = $this->addressRepository->findByDistance($addressSearch);
340+
$this->assertSame(
341+
'Landtag von Baden-Württemberg',
342+
$addresses[0]['title']
343+
);
344+
$this->assertSame(
345+
'Bayerischer Landtag',
346+
$addresses[1]['title']
347+
);
348+
$this->assertSame(
349+
'Abgeordnetenhaus von Berlin',
350+
$addresses[2]['title']
351+
);
352+
$this->assertSame(
353+
'Landtag Rheinland-Pfalz',
354+
$addresses[10]['title']
355+
);
356+
$this->assertSame(
357+
'Thüringer Landtag',
358+
$addresses[15]['title']
359+
);
360+
361+
$addressSearch->setFallbackOrderBy('title');
362+
$addresses = $this->addressRepository->findByDistance($addressSearch);
363+
$this->assertSame(
364+
'Abgeordnetenhaus von Berlin',
365+
$addresses[0]['title']
366+
);
367+
$this->assertSame(
368+
'Bayerischer Landtag',
369+
$addresses[1]['title']
370+
);
371+
$this->assertSame(
372+
'Bremische Bürgerschaft',
373+
$addresses[2]['title']
374+
);
375+
$this->assertSame(
376+
'Landtag von Baden-Württemberg',
377+
$addresses[10]['title']
378+
);
379+
$this->assertSame(
380+
'Thüringer Landtag',
381+
$addresses[15]['title']
382+
);
383+
}
384+
385+
/**
386+
* @test
387+
*/
388+
public function findByDistanceWithRadiusAndSortingByDistance(): void
389+
{
390+
// Berlin Alexanderplatz
391+
$addressSearch = new AddressSearch();
392+
$addressSearch->setLat(52.5225068);
393+
$addressSearch->setLon(13.4206053);
394+
$addressSearch->setRadius(200);
395+
$addressSearch->setPids('2');
396+
$addressSearch->setOrderBy('distance');
397+
398+
$addresses = $this->addressRepository->findByDistance($addressSearch);
399+
400+
$this->assertSame(
401+
'Abgeordnetenhaus von Berlin',
402+
$addresses['3.2643554966074']['title']
403+
);
404+
$this->assertSame(
405+
'Landtag Brandenburg',
406+
$addresses['28.651891958674']['title']
407+
);
408+
$this->assertSame(
409+
'Landtag Mecklenburg-Vorpommern',
410+
$addresses['181.56386274957']['title']
411+
);
412+
$this->assertSame(
413+
'Sächsischer Landtag',
414+
$addresses['164.39912286221']['title']
415+
);
416+
$this->assertSame(
417+
'Landtag von Sachsen-Anhalt',
418+
$addresses['129.23779803243']['title']
419+
);
420+
421+
$addressSearch->setOrderBy('distance');
422+
$addressSearch->setFallbackOrderBy('title');
423+
424+
$addresses = $this->addressRepository->findByDistance($addressSearch);
425+
426+
$this->assertSame(
427+
'Abgeordnetenhaus von Berlin',
428+
$addresses[0]['title']
429+
);
430+
$this->assertSame(
431+
'Landtag Brandenburg',
432+
$addresses[5]['title']
433+
);
434+
$this->assertSame(
435+
'Landtag Mecklenburg-Vorpommern',
436+
$addresses[6]['title']
437+
);
438+
$this->assertSame(
439+
'Landtag von Sachsen-Anhalt',
440+
$addresses[11]['title']
441+
);
442+
$this->assertSame(
443+
'Sächsischer Landtag',
444+
$addresses[14]['title']
445+
);
446+
}
297447
}

0 commit comments

Comments
 (0)