Skip to content

Commit 227061f

Browse files
committed
Merge branch 'v5-dev' into v5
2 parents 4716067 + ea5a384 commit 227061f

File tree

3 files changed

+52
-70
lines changed

3 files changed

+52
-70
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.0.2 - 2024-09-10
2+
### Fixed
3+
- Fixes location search (Fixes #394, #393, #392)
4+
15
## 5.0.1 - 2024-06-27 [CRITICAL]
26
### Security
37
- Removed Polyfill.io (https://sansec.io/research/polyfill-supply-chain-attack)

src/fields/MapField.php

+44-67
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
use Craft;
1212
use craft\base\Element;
1313
use craft\base\ElementInterface;
14+
use craft\base\Event;
1415
use craft\base\Field;
1516
use craft\base\PreviewableFieldInterface;
17+
use craft\elements\db\ElementQuery;
1618
use craft\elements\db\ElementQueryInterface;
1719
use craft\errors\InvalidFieldException;
20+
use craft\events\CancelableEvent;
1821
use craft\helpers\Json;
1922
use ether\simplemap\enums\GeoService as GeoEnum;
2023
use ether\simplemap\enums\MapTiles;
@@ -32,6 +35,7 @@
3235
use Twig\Error\SyntaxError;
3336
use Twig\Markup;
3437
use yii\base\InvalidConfigException;
38+
use yii\db\ExpressionInterface;
3539
use yii\db\Schema;
3640

3741
/**
@@ -155,9 +159,22 @@ class MapField extends Field implements PreviewableFieldInterface
155159
*/
156160
public string $boundary = '""';
157161

162+
private static $searchParams = null;
163+
158164
// Methods
159165
// =========================================================================
160166

167+
public function init(): void
168+
{
169+
Event::on(
170+
ElementQuery::class,
171+
ElementQuery::EVENT_AFTER_PREPARE,
172+
[$this, 'afterPrepareElementQuery'],
173+
);
174+
175+
parent::init();
176+
}
177+
161178
// Methods: Static
162179
// -------------------------------------------------------------------------
163180

@@ -192,6 +209,19 @@ public static function supportedTranslationMethods (): array
192209
];
193210
}
194211

212+
public static function queryCondition(array $instances, mixed $value, array &$params): array|string|ExpressionInterface|false|null
213+
{
214+
if (empty($instances) || empty($value))
215+
return null;
216+
217+
self::$searchParams = [
218+
'field' => $instances[0],
219+
'value' => $value,
220+
];
221+
222+
return null;
223+
}
224+
195225
// Methods: Instance
196226
// -------------------------------------------------------------------------
197227

@@ -233,13 +263,6 @@ public function rules (): array
233263
return $rules;
234264
}
235265

236-
/**
237-
* @param mixed $value
238-
* @param ElementInterface|Element|null $element
239-
*
240-
* @return Map
241-
* @throws Exception
242-
*/
243266
public function normalizeValue (mixed $value, ElementInterface|Element $element = null): Map
244267
{
245268
if (is_string($value))
@@ -272,14 +295,6 @@ public function normalizeValue (mixed $value, ElementInterface|Element $element
272295
return $map;
273296
}
274297

275-
/**
276-
* @return string
277-
* @throws InvalidConfigException
278-
* @throws LoaderError
279-
* @throws RuntimeError
280-
* @throws SyntaxError
281-
* @throws \yii\base\Exception
282-
*/
283298
public function getSettingsHtml (): string
284299
{
285300
$value = new Map();
@@ -337,13 +352,6 @@ public function getSettingsHtml (): string
337352
]);
338353
}
339354

340-
/**
341-
* @param null $value
342-
* @param ElementInterface|null $element
343-
*
344-
* @return string
345-
* @throws InvalidConfigException
346-
*/
347355
public function getInputHtml ($value = null, ElementInterface $element = null): string
348356
{
349357
if ($element !== null && $element->hasEagerLoadedElements($this->handle))
@@ -355,39 +363,11 @@ public function getInputHtml ($value = null, ElementInterface $element = null):
355363
);
356364
}
357365

358-
/**
359-
* @inheritdoc
360-
*
361-
* @param mixed $value
362-
* @param ElementInterface $element
363-
*
364-
* @return string
365-
* @throws Exception
366-
*/
367366
public function getTableAttributeHtml (mixed $value, ElementInterface $element): string
368367
{
369368
return $this->normalizeValue($value, $element)->address;
370369
}
371370

372-
/**
373-
* @param ElementQueryInterface $query
374-
* @param $value
375-
*
376-
* @return void
377-
* @throws Exception
378-
*/
379-
public function modifyElementsQuery (ElementQueryInterface $query, $value): void
380-
{
381-
if (!SimpleMap::getInstance())
382-
return;
383-
384-
SimpleMap::getInstance()->map->modifyElementsQuery($query, $value, $this);
385-
}
386-
387-
/**
388-
* @inheritdoc
389-
* @throws Exception
390-
*/
391371
public function isValueEmpty ($value, ElementInterface $element): bool
392372
{
393373
return $this->normalizeValue($value)->isValueEmpty();
@@ -417,9 +397,6 @@ public function getContentGqlMutationArgumentType (): Type|array
417397
// Methods: Events
418398
// -------------------------------------------------------------------------
419399

420-
/**
421-
* @inheritdoc
422-
*/
423400
public function beforeSave (bool $isNew): bool
424401
{
425402
$this->lat = (float) $this->lat;
@@ -432,13 +409,6 @@ public function beforeSave (bool $isNew): bool
432409
return parent::beforeSave($isNew);
433410
}
434411

435-
/**
436-
* @param ElementInterface $element
437-
* @param bool $isNew
438-
*
439-
* @return bool
440-
* @throws InvalidFieldException
441-
*/
442412
public function beforeElementSave (ElementInterface $element, bool $isNew): bool
443413
{
444414
if (!SimpleMap::getInstance()->map->validateField($this, $element))
@@ -447,20 +417,27 @@ public function beforeElementSave (ElementInterface $element, bool $isNew): bool
447417
return parent::beforeElementSave($element, $isNew);
448418
}
449419

450-
/**
451-
* @param ElementInterface $element
452-
* @param bool $isNew
453-
*
454-
* @throws InvalidFieldException
455-
* @throws Throwable
456-
*/
457420
public function afterElementSave (ElementInterface $element, bool $isNew): void
458421
{
459422
SimpleMap::getInstance()->map->saveField($this, $element);
460423

461424
parent::afterElementSave($element, $isNew);
462425
}
463426

427+
public function afterPrepareElementQuery (CancelableEvent $event): void
428+
{
429+
if (!self::$searchParams) return;
430+
431+
/** @var ElementQueryInterface $query */
432+
$query = $event->sender;
433+
434+
SimpleMap::getInstance()->map->modifyElementsQuery(
435+
$query,
436+
self::$searchParams['value'],
437+
self::$searchParams['field'],
438+
);
439+
}
440+
464441
// Helpers
465442
// =========================================================================
466443

src/services/MapService.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ private function _searchLocation (ElementQuery $query, mixed $value, string $tab
375375
// Filter the query
376376
$query
377377
->subQuery
378-
->addSelect($search . ' as [[mapsCalculatedDistance]]')
378+
->addSelect($search . ' as [[distance]]')
379379
->andWhere($restrict)
380380
->andWhere([
381381
'not',
@@ -385,9 +385,9 @@ private function _searchLocation (ElementQuery $query, mixed $value, string $tab
385385
if (Craft::$app->getDb()->driverName === 'pgsql')
386386
$query->subQuery->andWhere($search . ' <= ' . $radius);
387387
else
388-
$query->subQuery->andHaving('[[mapsCalculatedDistance]] <= ' . $radius);
388+
$query->subQuery->andHaving('[[distance]] <= ' . $radius);
389389

390-
return '[[mapsCalculatedDistance]]';
390+
return '[[distance]]';
391391
}
392392

393393
/**
@@ -407,6 +407,7 @@ private function _replaceOrderBy (ElementQuery $query, string $search = "")
407407
elseif ($order !== 'distance') $nextOrder[$order] = $sort;
408408
}
409409

410+
$query->subQuery->orderBy($nextOrder);
410411
$query->orderBy($nextOrder);
411412
}
412413

0 commit comments

Comments
 (0)