/
Search.php
149 lines (117 loc) · 4.45 KB
/
Search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Igniter\Local\Components;
use Admin\Models\Addresses_model;
use Admin\Models\Customers_model;
use Exception;
use Igniter\Flame\Exception\ApplicationException;
use Igniter\Local\Facades\Location;
use Main\Facades\Auth;
class Search extends \System\Classes\BaseComponent
{
use \Igniter\Local\Traits\SearchesNearby;
use \Main\Traits\UsesPage;
protected $defaultAddress;
protected $savedAddresses;
public function defineProperties()
{
return [
'hideSearch' => [
'label' => 'lang:igniter.local::default.label_location_search_mode',
'type' => 'switch',
'comment' => 'lang:igniter.local::default.help_location_search_mode',
'validationRule' => 'required|boolean',
],
'menusPage' => [
'label' => 'Menu Page',
'type' => 'select',
'default' => 'local'.DIRECTORY_SEPARATOR.'menus',
'options' => [static::class, 'getThemePageOptions'],
'validationRule' => 'required|regex:/^[a-z0-9\-_\/]+$/i',
],
];
}
public function showAddressPicker()
{
return Auth::customer() && $this->getDefaultAddress();
}
public function getSavedAddresses()
{
if (!is_null($this->savedAddresses))
return $this->savedAddresses;
if (!$customer = Auth::customer())
return null;
return $this->savedAddresses = $customer->addresses()->get();
}
public function showDeliveryCoverageAlert()
{
if (!Location::orderTypeIsDelivery())
return FALSE;
if (!Location::requiresUserPosition())
return FALSE;
return Location::userPosition()->hasCoordinates()
&& !Location::checkDeliveryCoverage();
}
public function onRun()
{
$this->addJs('js/local.js', 'local-module-js');
$this->prepareVars();
}
public function onSetSavedAddress()
{
if (!$customer = Auth::customer())
return null;
if (!is_numeric($addressId = post('addressId')))
throw new ApplicationException(lang('igniter.local::default.alert_address_id_required'));
if (!$address = $customer->addresses()->find($addressId))
throw new ApplicationException(lang('igniter.local::default.alert_address_not_found'));
Customers_model::withoutEvents(function () use ($customer, $address) {
$customer->address_id = $address->address_id;
$customer->save();
});
$customer->reload();
$this->controller->pageCycle();
$this->prepareVars();
return [
'#local-search-container' => $this->renderPartial('@container'),
];
}
protected function prepareVars()
{
$this->page['menusPage'] = $this->property('menusPage');
$this->page['hideSearch'] = $this->property('hideSearch', FALSE);
$this->page['searchEventHandler'] = $this->getEventHandler('onSearchNearby');
$this->page['pickerEventHandler'] = $this->getEventHandler('onSetSavedAddress');
$this->page['searchQueryPosition'] = Location::instance()->userPosition();
$this->page['searchDefaultAddress'] = $this->updateNearbyAreaFromSavedAddress(
$this->getDefaultAddress()
);
}
protected function updateNearbyAreaFromSavedAddress($address)
{
if (!$address instanceof Addresses_model)
return $address;
$searchQuery = format_address($address->toArray(), FALSE);
if ($searchQuery == Location::getSession('searchQuery'))
return $address;
try {
$userLocation = $this->geocodeSearchQuery($searchQuery);
Location::searchByCoordinates($userLocation->getCoordinates())
->first(function ($location) use ($userLocation) {
if ($area = $location->searchDeliveryArea($userLocation->getCoordinates())) {
Location::updateNearbyArea($area);
return $area;
}
});
}
catch (Exception $ex) {
}
return $address;
}
protected function getDefaultAddress()
{
if (!is_null($this->defaultAddress))
return $this->defaultAddress;
return $this->defaultAddress = optional(Auth::customer())->address
?? optional($this->getSavedAddresses())->first();
}
}