| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Controllers; |
| 6 |
|
| 7 |
use Yatra\Services\TripListingService; |
| 8 |
|
| 9 |
/** |
| 10 |
* Trip Listing Controller |
| 11 |
* |
| 12 |
* Handles HTTP requests for trip listing pages and coordinates between |
| 13 |
* the service layer and presentation layer. Follows MVC pattern with |
| 14 |
* proper separation of concerns. |
| 15 |
*/ |
| 16 |
class TripListingController |
| 17 |
{ |
| 18 |
private TripListingService $tripListingService; |
| 19 |
|
| 20 |
public function __construct(?TripListingService $tripListingService = null) |
| 21 |
{ |
| 22 |
$this->tripListingService = $tripListingService ?? new TripListingService(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Handle main trip listing page requests |
| 27 |
* |
| 28 |
* @param array $requestParams Request parameters from $_GET |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function handleTripListing(array $requestParams = []): void |
| 32 |
{ |
| 33 |
// Get filtered trip data from service |
| 34 |
$tripData = $this->tripListingService->getFilteredTrips($requestParams); |
| 35 |
|
| 36 |
// Share data with template via globals (WordPress pattern) |
| 37 |
$GLOBALS['yatra_trip_list'] = $tripData; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Handle taxonomy-based trip listings (destination/activity pages) |
| 42 |
* |
| 43 |
* @param string $taxonomyType 'destination' or 'activity' |
| 44 |
* @param string $slug Taxonomy slug |
| 45 |
* @param int|null $limit Number of trips to show; null uses WordPress posts per page. |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function handleTaxonomyListing(string $taxonomyType, string $slug, ?int $limit = null): void |
| 49 |
{ |
| 50 |
// Get trips for this taxonomy |
| 51 |
$tripData = $this->tripListingService->getTripsByTaxonomy($taxonomyType, $slug, $limit); |
| 52 |
|
| 53 |
// Share data with template via globals |
| 54 |
$GLOBALS['yatra_taxonomy_context'] = [ |
| 55 |
'trips' => $tripData['trips'], |
| 56 |
'total' => $tripData['total'], |
| 57 |
'taxonomy_type' => $taxonomyType, |
| 58 |
'taxonomy_slug' => $slug |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Validate request and determine if it should be processed |
| 64 |
* |
| 65 |
* @param array $requestParams Request parameters |
| 66 |
* @return bool |
| 67 |
*/ |
| 68 |
public function shouldProcessRequest(array $requestParams): bool |
| 69 |
{ |
| 70 |
// Always process base listing page |
| 71 |
if (empty($requestParams)) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
// Process if valid search/filter request |
| 76 |
return $this->tripListingService->isValidSearchRequest($requestParams); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get current page context for debugging/logging |
| 81 |
* |
| 82 |
* @param array $requestParams Request parameters |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
public function getPageContext(array $requestParams): array |
| 86 |
{ |
| 87 |
return [ |
| 88 |
'controller' => 'TripListingController', |
| 89 |
'action' => 'handleTripListing', |
| 90 |
'filters_applied' => !empty($requestParams), |
| 91 |
'request_params' => $requestParams |
| 92 |
]; |
| 93 |
} |
| 94 |
} |
| 95 |
|