| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Shortcodes; |
| 6 |
|
| 7 |
/** |
| 8 |
* Trip Shortcode |
| 9 |
* |
| 10 |
* Displays trips with various filtering options using trip-listing-card.php template |
| 11 |
*/ |
| 12 |
class TripShortcode extends BaseShortcode |
| 13 |
{ |
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
parent::__construct('yatra_tour', [ |
| 17 |
'order' => 'asc', |
| 18 |
'featured' => '0', |
| 19 |
'per_page' => '10', |
| 20 |
'category' => '', |
| 21 |
'destination' => '', |
| 22 |
'activity' => '', |
| 23 |
'difficulty' => '', |
| 24 |
'price_min' => '', |
| 25 |
'price_max' => '', |
| 26 |
'duration_min' => '', |
| 27 |
'duration_max' => '', |
| 28 |
'search' => '', |
| 29 |
'columns' => '3', |
| 30 |
'show_pagination' => 'yes', |
| 31 |
'title' => 'Our Trips' |
| 32 |
]); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Register the shortcode |
| 37 |
*/ |
| 38 |
public function register(): void |
| 39 |
{ |
| 40 |
// Register both shortcode tags |
| 41 |
add_shortcode('yatra_tour', [$this, 'render']); |
| 42 |
add_shortcode('yatra_trip', [$this, 'render']); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Render the trip shortcode content |
| 47 |
*/ |
| 48 |
protected function renderContent(array $atts): string |
| 49 |
{ |
| 50 |
// Use the shared BlockDataService method |
| 51 |
return \Yatra\Services\BlockDataService::renderTrip($atts); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get trips using Yatra's service |
| 56 |
*/ |
| 57 |
public function getTrips(array $atts): array |
| 58 |
{ |
| 59 |
try { |
| 60 |
$tripService = new \Yatra\Services\TripService(); |
| 61 |
|
| 62 |
// Get current page from query string or attributes (for AJAX) |
| 63 |
$current_page = isset($atts['current_page']) ? (int) $atts['current_page'] : (isset($_GET['trip_page']) ? (int) $_GET['trip_page'] : 1); |
| 64 |
// Use per_page parameter only |
| 65 |
$per_page = (int) $atts['per_page']; |
| 66 |
$offset = ($current_page - 1) * $per_page; |
| 67 |
|
| 68 |
// Start with very basic arguments to ensure we get trips |
| 69 |
$args = [ |
| 70 |
'limit' => $per_page, |
| 71 |
'offset' => $offset, |
| 72 |
'order_by' => 'created_at', |
| 73 |
'order' => $atts['order'] === 'asc' ? 'ASC' : 'DESC' |
| 74 |
]; |
| 75 |
|
| 76 |
// Add featured filter if requested |
| 77 |
if ($atts['featured'] === '1') { |
| 78 |
$args['where']['is_featured'] = 1; |
| 79 |
} |
| 80 |
|
| 81 |
// Get total count for pagination |
| 82 |
$count_args = $args; |
| 83 |
unset($count_args['limit']); |
| 84 |
unset($count_args['offset']); |
| 85 |
$total_trips = $tripService->count($count_args); |
| 86 |
|
| 87 |
// Get trips using the service |
| 88 |
$trips_data = $tripService->getActiveTrips($args); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
$trips = []; |
| 93 |
foreach ($trips_data as $tripData) { |
| 94 |
// Convert to Trip model |
| 95 |
$trip = \Yatra\Models\Trip::fromStdClass($tripData); |
| 96 |
|
| 97 |
// Add basic data needed for the card |
| 98 |
// Note: reviews are loaded elsewhere; bookings_count is attached below |
| 99 |
$trip->reviews = []; |
| 100 |
|
| 101 |
$trips[] = $trip; |
| 102 |
} |
| 103 |
|
| 104 |
// Attach bookings_count (computed from bookings table) for just these trips |
| 105 |
$tripIds = array_map(static function ($t) { |
| 106 |
return isset($t->id) ? (int) $t->id : 0; |
| 107 |
}, $trips); |
| 108 |
$tripIds = array_values(array_filter($tripIds)); |
| 109 |
if (!empty($tripIds)) { |
| 110 |
$bookingsCountMap = $tripService->getBookingsCountMap($tripIds); |
| 111 |
foreach ($trips as $t) { |
| 112 |
$tId = isset($t->id) ? (int) $t->id : 0; |
| 113 |
if ($tId > 0) { |
| 114 |
$t->bookings_count = (int) ($bookingsCountMap[$tId] ?? 0); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Calculate pagination data |
| 120 |
$max_pages = $per_page > 0 ? ceil($total_trips / $per_page) : 1; |
| 121 |
|
| 122 |
return [ |
| 123 |
'trips' => $trips, |
| 124 |
'max_pages' => $max_pages, |
| 125 |
'current_page' => $current_page, |
| 126 |
'total_found' => $total_trips, |
| 127 |
'debug_info' => [ |
| 128 |
'args_used' => $args, |
| 129 |
'raw_count' => count($trips_data) |
| 130 |
] |
| 131 |
]; |
| 132 |
|
| 133 |
} catch (\Exception $e) { |
| 134 |
|
| 135 |
|
| 136 |
return [ |
| 137 |
'trips' => [], |
| 138 |
'max_pages' => 1, |
| 139 |
'current_page' => 1, |
| 140 |
'total_found' => 0 |
| 141 |
]; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
|