Skip to content

Commit

Permalink
Fixes location search
Browse files Browse the repository at this point in the history
Fixes #394, #393, #392
  • Loading branch information
Tam committed Sep 10, 2024
1 parent d03bf99 commit ea5a384
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
111 changes: 44 additions & 67 deletions src/fields/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +35,7 @@
use Twig\Error\SyntaxError;
use Twig\Markup;
use yii\base\InvalidConfigException;
use yii\db\ExpressionInterface;
use yii\db\Schema;

/**
Expand Down Expand Up @@ -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
// -------------------------------------------------------------------------

Expand Down Expand Up @@ -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
// -------------------------------------------------------------------------

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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))
Expand All @@ -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();
Expand Down Expand Up @@ -417,9 +397,6 @@ public function getContentGqlMutationArgumentType (): Type|array
// Methods: Events
// -------------------------------------------------------------------------

/**
* @inheritdoc
*/
public function beforeSave (bool $isNew): bool
{
$this->lat = (float) $this->lat;
Expand All @@ -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))
Expand All @@ -447,20 +417,27 @@ 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);

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
// =========================================================================

Expand Down
7 changes: 4 additions & 3 deletions src/services/MapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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]]';
}

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

$query->subQuery->orderBy($nextOrder);
$query->orderBy($nextOrder);
}

Expand Down

0 comments on commit ea5a384

Please sign in to comment.