diff --git a/CHANGELOG.md b/CHANGELOG.md index 236d4cf..8a286e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.0.2 - 2024-09-10 +### Fixed +- Fixes location search (Fixes #394, #393, #392) + ## 5.0.1 - 2024-06-27 ### Security - Removed Polyfill.io (https://sansec.io/research/polyfill-supply-chain-attack) diff --git a/src/fields/MapField.php b/src/fields/MapField.php index 6c266de..f6587e2 100644 --- a/src/fields/MapField.php +++ b/src/fields/MapField.php @@ -11,10 +11,13 @@ use Craft; use craft\base\Element; use craft\base\ElementInterface; +use craft\base\Event; use craft\base\Field; use craft\base\PreviewableFieldInterface; +use craft\elements\db\ElementQuery; use craft\elements\db\ElementQueryInterface; use craft\errors\InvalidFieldException; +use craft\events\CancelableEvent; use craft\helpers\Json; use ether\simplemap\enums\GeoService as GeoEnum; use ether\simplemap\enums\MapTiles; @@ -32,6 +35,7 @@ use Twig\Error\SyntaxError; use Twig\Markup; use yii\base\InvalidConfigException; +use yii\db\ExpressionInterface; use yii\db\Schema; /** @@ -155,9 +159,22 @@ class MapField extends Field implements PreviewableFieldInterface */ public string $boundary = '""'; + private static $searchParams = null; + // Methods // ========================================================================= + public function init(): void + { + Event::on( + ElementQuery::class, + ElementQuery::EVENT_AFTER_PREPARE, + [$this, 'afterPrepareElementQuery'], + ); + + parent::init(); + } + // Methods: Static // ------------------------------------------------------------------------- @@ -192,6 +209,19 @@ public static function supportedTranslationMethods (): array ]; } + public static function queryCondition(array $instances, mixed $value, array &$params): array|string|ExpressionInterface|false|null + { + if (empty($instances) || empty($value)) + return null; + + self::$searchParams = [ + 'field' => $instances[0], + 'value' => $value, + ]; + + return null; + } + // Methods: Instance // ------------------------------------------------------------------------- @@ -233,13 +263,6 @@ public function rules (): array return $rules; } - /** - * @param mixed $value - * @param ElementInterface|Element|null $element - * - * @return Map - * @throws Exception - */ public function normalizeValue (mixed $value, ElementInterface|Element $element = null): Map { if (is_string($value)) @@ -272,14 +295,6 @@ public function normalizeValue (mixed $value, ElementInterface|Element $element return $map; } - /** - * @return string - * @throws InvalidConfigException - * @throws LoaderError - * @throws RuntimeError - * @throws SyntaxError - * @throws \yii\base\Exception - */ public function getSettingsHtml (): string { $value = new Map(); @@ -337,13 +352,6 @@ public function getSettingsHtml (): string ]); } - /** - * @param null $value - * @param ElementInterface|null $element - * - * @return string - * @throws InvalidConfigException - */ public function getInputHtml ($value = null, ElementInterface $element = null): string { if ($element !== null && $element->hasEagerLoadedElements($this->handle)) @@ -355,39 +363,11 @@ public function getInputHtml ($value = null, ElementInterface $element = null): ); } - /** - * @inheritdoc - * - * @param mixed $value - * @param ElementInterface $element - * - * @return string - * @throws Exception - */ public function getTableAttributeHtml (mixed $value, ElementInterface $element): string { return $this->normalizeValue($value, $element)->address; } - /** - * @param ElementQueryInterface $query - * @param $value - * - * @return void - * @throws Exception - */ - public function modifyElementsQuery (ElementQueryInterface $query, $value): void - { - if (!SimpleMap::getInstance()) - return; - - SimpleMap::getInstance()->map->modifyElementsQuery($query, $value, $this); - } - - /** - * @inheritdoc - * @throws Exception - */ public function isValueEmpty ($value, ElementInterface $element): bool { return $this->normalizeValue($value)->isValueEmpty(); @@ -417,9 +397,6 @@ public function getContentGqlMutationArgumentType (): Type|array // Methods: Events // ------------------------------------------------------------------------- - /** - * @inheritdoc - */ public function beforeSave (bool $isNew): bool { $this->lat = (float) $this->lat; @@ -432,13 +409,6 @@ public function beforeSave (bool $isNew): bool return parent::beforeSave($isNew); } - /** - * @param ElementInterface $element - * @param bool $isNew - * - * @return bool - * @throws InvalidFieldException - */ public function beforeElementSave (ElementInterface $element, bool $isNew): bool { if (!SimpleMap::getInstance()->map->validateField($this, $element)) @@ -447,13 +417,6 @@ public function beforeElementSave (ElementInterface $element, bool $isNew): bool return parent::beforeElementSave($element, $isNew); } - /** - * @param ElementInterface $element - * @param bool $isNew - * - * @throws InvalidFieldException - * @throws Throwable - */ public function afterElementSave (ElementInterface $element, bool $isNew): void { SimpleMap::getInstance()->map->saveField($this, $element); @@ -461,6 +424,20 @@ public function afterElementSave (ElementInterface $element, bool $isNew): void parent::afterElementSave($element, $isNew); } + public function afterPrepareElementQuery (CancelableEvent $event): void + { + if (!self::$searchParams) return; + + /** @var ElementQueryInterface $query */ + $query = $event->sender; + + SimpleMap::getInstance()->map->modifyElementsQuery( + $query, + self::$searchParams['value'], + self::$searchParams['field'], + ); + } + // Helpers // ========================================================================= diff --git a/src/services/MapService.php b/src/services/MapService.php index baf7a29..ca9f3fc 100644 --- a/src/services/MapService.php +++ b/src/services/MapService.php @@ -375,7 +375,7 @@ private function _searchLocation (ElementQuery $query, mixed $value, string $tab // Filter the query $query ->subQuery - ->addSelect($search . ' as [[mapsCalculatedDistance]]') + ->addSelect($search . ' as [[distance]]') ->andWhere($restrict) ->andWhere([ 'not', @@ -385,9 +385,9 @@ private function _searchLocation (ElementQuery $query, mixed $value, string $tab if (Craft::$app->getDb()->driverName === 'pgsql') $query->subQuery->andWhere($search . ' <= ' . $radius); else - $query->subQuery->andHaving('[[mapsCalculatedDistance]] <= ' . $radius); + $query->subQuery->andHaving('[[distance]] <= ' . $radius); - return '[[mapsCalculatedDistance]]'; + return '[[distance]]'; } /** @@ -407,6 +407,7 @@ private function _replaceOrderBy (ElementQuery $query, string $search = "") elseif ($order !== 'distance') $nextOrder[$order] = $sort; } + $query->subQuery->orderBy($nextOrder); $query->orderBy($nextOrder); }