@@ -149,6 +149,7 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For
149149 switch ($ type ) {
150150 case 'view ' :
151151
152+ $ form ->addMarkup ($ this ->renderShipmentInfo ($ orderShipment ));
152153
153154 break ;
154155 case 'edit ' :
@@ -251,7 +252,29 @@ public function formSubmitted(FAPI\Form $form, &$form_state): mixed
251252 // no break
252253 case 'edit ' :
253254
254-
255+ $ orderShipment ->setShippingMethod ($ values ['shipping_method ' ]);
256+ $ orderShipment ->setShipmentCode ($ values ['shipment_code ' ]);
257+ $ orderShipment ->setStatus ($ values ['status ' ]);
258+ $ orderShipment ->setWebsiteId ($ values ['website_id ' ]);
259+
260+ $ changedData = $ orderShipment ->getChangedData ();
261+
262+ if (is_numeric ($ values ['location ' ]['latitude ' ]) && is_numeric ($ values ['location ' ]['longitude ' ])) {
263+ $ changedData += [
264+ 'latitude ' => $ values ['location ' ]['latitude ' ],
265+ 'longitude ' => $ values ['location ' ]['longitude ' ],
266+ ];
267+
268+ // update position, saving history if needed (this also persists the object)
269+ $ orderShipment ->updatePosition ($ values ['location ' ]['latitude ' ], $ values ['location ' ]['longitude ' ]);
270+ }
271+
272+ $ this ->setAdminActionLogData ($ changedData );
273+
274+ $ orderShipment ->persist ();
275+
276+ $ this ->addSuccessFlashMessage ($ this ->getUtils ()->translate ("Order Shipment Saved. " ));
277+
255278 break ;
256279 case 'delete ' :
257280 $ orderShipment ->delete ();
@@ -276,8 +299,9 @@ protected function getTableHeader(): ?array
276299 'ID ' => 'id ' ,
277300 'Website ' => ['order ' => 'website_id ' , 'foreign ' => 'website_id ' , 'table ' => $ this ->getModelTableName (), 'view ' => 'site_name ' ],
278301 '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 ' ],
302+ 'Shipping Method ' => ['order ' => 'shipping_method ' , 'search ' => 'shipping_method ' ],
303+ 'Shipment Code ' => ['order ' => 'shipment_code ' , 'search ' => 'shipment_code ' ],
304+ 'Status ' => ['order ' => 'status ' ],
281305 'actions ' => null ,
282306 ];
283307 }
@@ -301,6 +325,7 @@ function ($orderShipment) {
301325 'Order ' => $ orderShipment ->getOrder ()->getOrderNumber (),
302326 'Shipping Method ' => $ orderShipment ->getShippingMethod (),
303327 'Shipment Code ' => $ orderShipment ->getShipmentCode (),
328+ 'Status ' => $ orderShipment ->getStatus (),
304329 'actions ' => [
305330 static ::VIEW_BTN => $ this ->getViewButton ($ orderShipment ->id ),
306331 static ::EDIT_BTN => $ this ->getEditButton ($ orderShipment ->id ),
@@ -337,4 +362,90 @@ public static function exposeDataToDashboard() : mixed
337362 {
338363 return null ;
339364 }
365+
366+ protected function renderShipmentInfo (OrderShipmentModel $ orderShipment ) : string
367+ {
368+ $ locationScript = '' ;
369+ $ latitude = $ orderShipment ->getLatitude ();
370+ $ longitude = $ orderShipment ->getLongitude ();
371+ if ($ this ->getEnvironment ()->getVariable ('GOOGLE_API_KEY ' )) {
372+ $ locationScript = "<script type= \"text/javascript \">
373+ var latlng = {
374+ lat: " .$ latitude .",
375+ lng: " .$ longitude ."
376+ };
377+ var map = new google.maps.Map(document.getElementById('shipment-history-map'), {
378+ zoom: 4,
379+ center: latlng
380+ });
381+
382+ var marker = new google.maps.Marker({
383+ position: latlng,
384+ map: map,
385+ draggable: false
386+ });
387+ </script> " ;
388+ } else if ($ this ->getEnvironment ()->getVariable ('MAPBOX_API_KEY ' )) {
389+ $ locationScript = "<script type= \"text/javascript \">
390+ var latlng = {
391+ lat: " .$ latitude .",
392+ lng: " .$ longitude ."
393+ };
394+ var map = L.map('shipment-history-map').setView([latlng.lat,latlng.lng],4);
395+ L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
396+ attribution:
397+ 'Map data © <a href= \"https://www.openstreetmap.org/ \">OpenStreetMap</a> contributors,'+
398+ '<a href= \"https://creativecommons.org/licenses/by-sa/2.0/ \">CC-BY-SA</a>,'+
399+ ' Imagery © <a href= \"https://www.mapbox.com/ \">Mapbox</a>',
400+ maxZoom: 18,
401+ id: 'mapbox/streets-v12',
402+ accessToken: ' {$ this ->getEnvironment ()->getVariable ('MAPBOX_API_KEY ' )}'
403+ }).addTo(map);
404+
405+ var marker = L.marker([latlng.lat, latlng.lng],{
406+ draggable: false
407+ }).addTo(map);
408+ </script> " ;
409+ }
410+
411+ return
412+ '<h2> ' .$ this ->getUtils ()->translate ('Shipment %s for order %s ' , [$ orderShipment ->getShipmentCode (),$ orderShipment ->getOrder ()?->getOrderNumber()]).'</h2><hr/> ' .
413+ '<div id="shipment-history-map" style="height:400px;margin-bottom:20px;"></div> ' .
414+ $ locationScript .
415+ ($ orderShipment ->getLatitude () && $ orderShipment ->getLongitude () ?
416+ '<script type="text/javascript">
417+ document.addEventListener("DOMContentLoaded", function() {
418+ var mapOptions = {
419+ center: { lat: ' .$ orderShipment ->getLatitude ().', lng: ' .$ orderShipment ->getLongitude ().' },
420+ zoom: 12
421+ };
422+ var map = new google.maps.Map(document.querySelector(".shipment-history-map"), mapOptions);
423+ var marker = new google.maps.Marker({
424+ position: { lat: ' .$ orderShipment ->getLatitude ().', lng: ' .$ orderShipment ->getLongitude ().' },
425+ map: map,
426+ title: "Current Location"
427+ });
428+ });
429+ </script> '
430+ : '<p> ' .$ this ->getUtils ()->translate ('No current location available ' ).'</p> '
431+ ).
432+
433+ ($ orderShipment ->getPositionHistory () === [] ?
434+ '<p> ' .$ this ->getUtils ()->translate ('No position history available ' ).'</p> '
435+ :
436+ '<h3> ' .$ this ->getUtils ()->translate ('Position History ' ).'</h3> ' .
437+ '<ul class="list-group"><li class="list-group-item"> ' .implode ('</li><li class="list-group-item"> ' , array_map (
438+ function ($ historyItem ) {
439+ $ data = [
440+ 'latitude ' => $ historyItem ->getLatitude (),
441+ 'longitude ' => $ historyItem ->getLongitude (),
442+ 'when ' => $ historyItem ->getCreatedAt ()
443+ ];
444+
445+ return $ this ->getHtmlRenderer ()->renderArrayOnTable ($ data );
446+ },
447+ $ orderShipment ->getPositionHistory ()
448+ ))."</li></ul> "
449+ );
450+ }
340451}
0 commit comments