PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.7
Yatra – Travel Booking & Tour Operator Software v3.0.7
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Repositories / TripRevisionRepository.php

TripRevisionRepository.php in Yatra – Travel Booking & Tour Operator Software 3.0.7, at app/Repositories/TripRevisionRepository.php

191 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Repositories;
6
7 use Yatra\Database\Tables\TripRevisionsTable;
8
9 /**
10 * Trip Revision Repository
11 * Handles database operations for trip revisions
12 */
13 class TripRevisionRepository extends BaseRepository
14 {
15 /**
16 * Get table name
17 */
18 protected function getTableName(): string
19 {
20 return TripRevisionsTable::getTableName();
21 }
22
23 /**
24 * Find all revisions for a trip
25 */
26 public function findByTripId(int $tripId, array $args = []): array
27 {
28 $table = esc_sql($this->table);
29 $order = $this->buildOrderClause($args);
30 $limit = $this->buildLimitClause($args);
31
32 $query = $this->wpdb->prepare(
33 "SELECT * FROM `{$table}` WHERE trip_id = %d {$order} {$limit}",
34 $tripId
35 );
36
37 return $this->wpdb->get_results($query) ?: [];
38 }
39
40 /**
41 * Find revision by ID
42 */
43 public function findRevision(int $id): ?\stdClass
44 {
45 $table = esc_sql($this->table);
46 $result = $this->wpdb->get_row(
47 $this->wpdb->prepare(
48 "SELECT * FROM `{$table}` WHERE id = %d",
49 $id
50 )
51 );
52
53 return $result ?: null;
54 }
55
56 /**
57 * Get latest version number for a trip
58 */
59 public function getLatestVersion(int $tripId): int
60 {
61 $table = esc_sql($this->table);
62 $result = $this->wpdb->get_var(
63 $this->wpdb->prepare(
64 "SELECT MAX(version) FROM `{$table}` WHERE trip_id = %d",
65 $tripId
66 )
67 );
68
69 return (int) ($result ?: 0);
70 }
71
72 /**
73 * Create a new revision
74 *
75 * @param int $tripId Trip ID
76 * @param int $version Version number
77 * @param string $data Serialized trip data
78 * @param int $createdBy User ID who created the revision
79 * @param string $status Revision status ('inherit' for normal, 'restored' for restore operations)
80 * @return int Revision ID
81 */
82 public function createRevision(int $tripId, int $version, string $data, int $createdBy = 0, string $status = 'inherit'): int
83 {
84 $data_array = [
85 'trip_id' => $tripId,
86 'version' => $version,
87 'status' => $status,
88 'data' => $data,
89 'created_by' => $createdBy,
90 'created_at' => current_time('mysql'),
91 ];
92
93 $result = $this->wpdb->insert($this->table, $data_array);
94
95 if ($result === false) {
96 throw new \Exception('Failed to create revision: ' . $this->wpdb->last_error);
97 }
98
99 return $this->wpdb->insert_id;
100 }
101
102 /**
103 * Get revision limit (similar to WP_POST_REVISIONS)
104 * Default is unlimited, but can be set via filter
105 */
106 public function getRevisionLimit(): int
107 {
108 /**
109 * Filter the number of revisions to keep for each trip
110 *
111 * @param int $limit Number of revisions to keep. 0 = unlimited, -1 = no revisions
112 */
113 $limit = apply_filters('yatra_trip_revision_limit', 0);
114
115 return (int) $limit;
116 }
117
118 /**
119 * Clean up old revisions for a trip (keep only the most recent N revisions)
120 * Similar to WordPress's revision cleanup
121 */
122 public function cleanupRevisions(int $tripId): int
123 {
124 $limit = $this->getRevisionLimit();
125
126 // 0 or negative means unlimited, no cleanup needed
127 if ($limit <= 0) {
128 return 0;
129 }
130
131 // Get all revisions for this trip, ordered by version DESC
132 $revisions = $this->findByTripId($tripId, [
133 'order_by' => 'version',
134 'order' => 'DESC',
135 ]);
136
137 // If we have more revisions than the limit, delete the oldest ones
138 if (count($revisions) > $limit) {
139 $revisionsToDelete = array_slice($revisions, $limit);
140 $deleted = 0;
141
142 foreach ($revisionsToDelete as $revision) {
143 $result = $this->wpdb->delete(
144 $this->table,
145 ['id' => $revision->id],
146 ['%d']
147 );
148 if ($result !== false) {
149 $deleted++;
150 }
151 }
152
153 return $deleted;
154 }
155
156 return 0;
157 }
158
159 /**
160 * Get revision count for a trip
161 */
162 public function getRevisionCount(int $tripId): int
163 {
164 $table = esc_sql($this->table);
165 $count = $this->wpdb->get_var(
166 $this->wpdb->prepare(
167 "SELECT COUNT(*) FROM `{$table}` WHERE trip_id = %d",
168 $tripId
169 )
170 );
171
172 return (int) ($count ?: 0);
173 }
174
175 /**
176 * Delete revisions for a trip
177 */
178 public function deleteByTripId(int $tripId): bool
179 {
180 $table = esc_sql($this->table);
181 $result = $this->wpdb->delete(
182 $this->table,
183 ['trip_id' => $tripId],
184 ['%d']
185 );
186
187 return $result !== false;
188 }
189 }
190
191