Skip to content

Commit 5430288

Browse files
commit
1 parent 08dd99c commit 5430288

File tree

1 file changed

+340
-0
lines changed

1 file changed

+340
-0
lines changed
Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
<?php
2+
3+
/**
4+
* SiteBase
5+
* PHP Version 8.3
6+
*
7+
* @category CMS / Framework
8+
* @package Degami\Sitebase
9+
* @author Mirko De Grandis <degami@github.com>
10+
* @license MIT https://opensource.org/licenses/mit-license.php
11+
* @link https://github.com/degami/sitebase
12+
*/
13+
14+
namespace App\Base\Controllers\Admin\Commerce;
15+
16+
use App\App;
17+
use Psr\Container\ContainerInterface;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use App\Base\Routing\RouteInfo;
20+
use Degami\Basics\Exceptions\BasicException;
21+
use DI\DependencyException;
22+
use DI\NotFoundException;
23+
use Exception;
24+
use App\Base\Abstracts\Controllers\AdminManageFrontendModelsPage;
25+
use Degami\PHPFormsApi as FAPI;
26+
use App\Base\Models\OrderShipment as OrderShipmentModel;
27+
use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException;
28+
use Symfony\Component\HttpFoundation\Response;
29+
use App\Base\Abstracts\Controllers\BasePage;
30+
use App\Base\Models\Order;
31+
32+
/**
33+
* "Order Shipments" Admin Page
34+
*/
35+
class OrderShipments extends AdminManageFrontendModelsPage
36+
{
37+
public function __construct(
38+
protected ContainerInterface $container,
39+
protected ?Request $request = null,
40+
protected ?RouteInfo $route_info = null,
41+
bool $asGrid = false,
42+
) {
43+
parent::__construct($container, $request, $route_info, $asGrid);
44+
45+
if ($this->getEnvironment()->getVariable('GOOGLE_API_KEY')) {
46+
$this->getAssets()->addHeadJs('https://maps.googleapis.com/maps/api/js?v=3.exp&amp&amp;libraries=geometry,places&amp;key='. $this->getEnvironment()->getVariable('GOOGLE_API_KEY'));
47+
} else if ($this->getEnvironment()->getVariable('MAPBOX_API_KEY')) {
48+
$this->getAssets()->addHeadJs('https://unpkg.com/leaflet@1.3.4/dist/leaflet.js', [
49+
'integrity' => "sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==",
50+
'crossorigin' => "1",
51+
]);
52+
53+
$this->getAssets()->addHeadCss('https://unpkg.com/leaflet@1.3.4/dist/leaflet.css', [
54+
'integrity' => "sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==",
55+
'crossorigin' => "1",
56+
]);
57+
}
58+
}
59+
60+
/**
61+
* @var string page title
62+
*/
63+
protected ?string $page_title = 'Order Shipments';
64+
65+
/**
66+
* {@inheritdoc}
67+
*
68+
* @return string
69+
*/
70+
public function getTemplateName(): string
71+
{
72+
return 'base_admin_page';
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*
78+
* @return string
79+
*/
80+
public static function getAccessPermission(): string
81+
{
82+
return 'administer_orders';
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*
88+
* @return string
89+
*/
90+
public static function getObjectClass(): string
91+
{
92+
return OrderShipmentModel::class;
93+
}
94+
95+
/**
96+
* {@inheritdoc}
97+
*
98+
* @return string
99+
*/
100+
protected function getObjectIdQueryParam(): string
101+
{
102+
return 'shipment_id';
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*
108+
* @return array|null
109+
*/
110+
public static function getAdminPageLink() : array|null
111+
{
112+
return [
113+
'permission_name' => static::getAccessPermission(),
114+
'route_name' => static::getPageRouteName(),
115+
'icon' => 'navigation',
116+
'text' => 'Order Shipments',
117+
'section' => 'commerce',
118+
'order' => 30,
119+
];
120+
}
121+
122+
/**
123+
* {@inheritdoc}
124+
*
125+
* @param FAPI\Form $form
126+
* @param array &$form_state
127+
* @return FAPI\Form
128+
* @throws BasicException
129+
* @throws DependencyException
130+
* @throws NotFoundException
131+
* @throws PhpfastcacheSimpleCacheException
132+
*/
133+
public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\Form
134+
{
135+
$type = $this->getRequest()->query->get('action') ?? 'list';
136+
137+
/**
138+
* @var OrderShipmentModel $orderShipment
139+
*/
140+
$orderShipment = $this->getObject();
141+
142+
$form->addField('action', [
143+
'type' => 'value',
144+
'value' => $type,
145+
]);
146+
147+
$websites = $this->getUtils()->getWebsitesSelectOptions();
148+
149+
switch ($type) {
150+
case 'view' :
151+
152+
153+
break;
154+
case 'edit':
155+
case 'new':
156+
157+
$locationFieldType = 'geolocation'; $mapOptions = [];
158+
if ($this->getEnvironment()->getVariable('GOOGLE_API_KEY')) {
159+
$locationFieldType = 'gmaplocation';
160+
}
161+
else if ($this->getEnvironment()->getVariable('MAPBOX_API_KEY')) {
162+
$locationFieldType = 'leafletlocation';
163+
$mapOptions = [
164+
'accessToken' => $this->getEnvironment()->getVariable('MAPBOX_API_KEY'),
165+
'scrollwheel' => true,
166+
'maptype' => 'mapbox/streets-v12',
167+
];
168+
}
169+
170+
$form
171+
->addField('shipping_method', [
172+
'type' => 'textfield',
173+
'title' => 'Shipping Method',
174+
'required' => true,
175+
'default_value' => $orderShipment->getShippingMethod(),
176+
])
177+
->addField('shipment_code', [
178+
'type' => 'textfield',
179+
'title' => 'Shipment Code',
180+
'required' => true,
181+
'default_value' => $orderShipment->getShipmentCode(),
182+
])
183+
->addField('status', [
184+
'type' => 'textfield',
185+
'title' => 'Status',
186+
'required' => true,
187+
'default_value' => $orderShipment->getStatus(),
188+
])
189+
->addField('location', [
190+
'type' => $locationFieldType,
191+
'title' => 'Location',
192+
'default_value' => $orderShipment->getCurrentLocation(),
193+
] + $mapOptions)
194+
->addField('website_id', [
195+
'type' => 'select',
196+
'title' => 'Website',
197+
'options' => $websites,
198+
'required' => true,
199+
'default_value' => $orderShipment->getWebsiteId(),
200+
]);
201+
202+
$this->addSubmitButton($form);
203+
204+
break;
205+
206+
case 'delete':
207+
$this->fillConfirmationForm('Do you confirm the deletion of the selected element?', $form);
208+
break;
209+
}
210+
211+
return $form;
212+
}
213+
214+
/**
215+
* {@inheritdoc}
216+
*
217+
* @param FAPI\Form $form
218+
* @param array &$form_state
219+
* @return bool|string
220+
*/
221+
public function formValidate(FAPI\Form $form, &$form_state): bool|string
222+
{
223+
//$values = $form->values();
224+
// @todo : check if page language is in page website languages?
225+
return true;
226+
}
227+
228+
/**
229+
* {@inheritdoc}
230+
*
231+
* @param FAPI\Form $form
232+
* @param array &$form_state
233+
* @return mixed
234+
* @throws BasicException
235+
* @throws DependencyException
236+
* @throws NotFoundException
237+
*/
238+
public function formSubmitted(FAPI\Form $form, &$form_state): mixed
239+
{
240+
/**
241+
* @var OrderShipmentModel $orderShipment
242+
*/
243+
$orderShipment = $this->getObject();
244+
245+
$values = $form->values();
246+
247+
switch ($values['action']) {
248+
case 'new':
249+
250+
// intentional fall trough
251+
// no break
252+
case 'edit':
253+
254+
255+
break;
256+
case 'delete':
257+
$orderShipment->delete();
258+
259+
$this->setAdminActionLogData('Deleted order shipment ' . $orderShipment->getId());
260+
261+
$this->addInfoFlashMessage($this->getUtils()->translate("Order shipment Deleted."));
262+
263+
break;
264+
}
265+
return $this->refreshPage();
266+
}
267+
268+
/**
269+
* {@inheritdoc}
270+
*
271+
* @return array
272+
*/
273+
protected function getTableHeader(): ?array
274+
{
275+
return [
276+
'ID' => 'id',
277+
'Website' => ['order' => 'website_id', 'foreign' => 'website_id', 'table' => $this->getModelTableName(), 'view' => 'site_name'],
278+
'Order' => ['order' => 'order_id', 'foreign' => 'order_id', 'table' => $this->getModelTableName(), 'view' => 'order_number'],
279+
'Shipping Method' => ['order' => 'shipping_method'],
280+
'Shipment Code' => ['order' => 'shipment_code'],
281+
'actions' => null,
282+
];
283+
}
284+
285+
/**
286+
* {@inheritdoc}
287+
*
288+
* @param array $data
289+
* @param array $options
290+
* @return array
291+
* @throws BasicException
292+
* @throws Exception
293+
*/
294+
protected function getTableElements(array $data, array $options = []): array
295+
{
296+
return array_map(
297+
function ($orderShipment) {
298+
return [
299+
'ID' => $orderShipment->id,
300+
'Website' => $orderShipment->getWebsiteId() == null ? 'All websites' : $orderShipment->getWebsite()->domain,
301+
'Order' => $orderShipment->getOrder()->getOrderNumber(),
302+
'Shipping Method' => $orderShipment->getShippingMethod(),
303+
'Shipment Code' => $orderShipment->getShipmentCode(),
304+
'actions' => [
305+
static::VIEW_BTN => $this->getViewButton($orderShipment->id),
306+
static::EDIT_BTN => $this->getEditButton($orderShipment->id),
307+
static::DELETE_BTN => $this->getDeleteButton($orderShipment->id),
308+
],
309+
];
310+
},
311+
$data
312+
);
313+
}
314+
315+
/**
316+
* gets edit button html
317+
*
318+
* @param int $object_id
319+
* @return string
320+
* @throws DependencyException
321+
* @throws NotFoundException
322+
*/
323+
public function getViewButton(int $object_id): string
324+
{
325+
return $this->getActionButton('view', $object_id, 'secondary', 'zoom-in', 'View');
326+
}
327+
328+
protected function beforeRender(): BasePage|Response
329+
{
330+
if (App::getInstance()->getEnvironment()->getVariable('ENABLE_COMMERCE', false) == false) {
331+
$this->addWarningFlashMessage($this->getUtils()->translate("Commerce functionallity is currently disabled"), true);
332+
}
333+
return parent::beforeRender();
334+
}
335+
336+
public static function exposeDataToDashboard() : mixed
337+
{
338+
return null;
339+
}
340+
}

0 commit comments

Comments
 (0)