PluginProbe
Packeta / 1.5.2
Packeta v1.5.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Carrier / CountryListingPage.php

CountryListingPage.php in Packeta 1.5.2, at src/Packetery/Module/Carrier/CountryListingPage.php

336 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class CountryListingPage
4 *
5 * @package Packetery\Carrier
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Carrier;
11
12 use Packetery\Core\Log\Record;
13 use Packetery\Module\CronService;
14 use Packetery\Module\Log;
15 use Packetery\Module\Options\Provider;
16 use PacketeryLatte\Engine;
17 use PacketeryNette\Http\Request;
18 use Packetery\Module\Plugin;
19
20 /**
21 * Class CountryListingPage
22 *
23 * @package Packetery\Carrier
24 */
25 class CountryListingPage {
26
27 public const TRANSIENT_CARRIER_CHANGES = 'packetery_carrier_changes';
28
29 /**
30 * PacketeryLatteEngine.
31 *
32 * @var Engine PacketeryLatte engine.
33 */
34 private $latteEngine;
35
36 /**
37 * Carrier repository.
38 *
39 * @var Repository Carrier repository.
40 */
41 private $carrierRepository;
42
43 /**
44 * Carrier downloader.
45 *
46 * @var Downloader
47 */
48 private $downloader;
49
50 /**
51 * Http request.
52 *
53 * @var Request
54 */
55 private $httpRequest;
56
57 /**
58 * Options provider
59 *
60 * @var Provider
61 */
62 private $optionsProvider;
63
64 /**
65 * Log Page
66 *
67 * @var Log\Page
68 */
69 private $logPage;
70
71 /**
72 * Internal pickup points config.
73 *
74 * @var PacketaPickupPointsConfig
75 */
76 private $pickupPointsConfig;
77
78 /**
79 * Carrier entity repository.
80 *
81 * @var EntityRepository
82 */
83 private $carrierEntityRepository;
84
85 /**
86 * CountryListingPage constructor.
87 *
88 * @param Engine $latteEngine PacketeryLatte engine.
89 * @param Repository $carrierRepository Carrier repository.
90 * @param Downloader $downloader Carrier downloader.
91 * @param Request $httpRequest Http request.
92 * @param Provider $optionsProvider Options provider.
93 * @param Log\Page $logPage Log page.
94 * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config.
95 * @param EntityRepository $carrierEntityRepository Carrier repository.
96 */
97 public function __construct(
98 Engine $latteEngine,
99 Repository $carrierRepository,
100 Downloader $downloader,
101 Request $httpRequest,
102 Provider $optionsProvider,
103 Log\Page $logPage,
104 PacketaPickupPointsConfig $pickupPointsConfig,
105 EntityRepository $carrierEntityRepository
106 ) {
107 $this->latteEngine = $latteEngine;
108 $this->carrierRepository = $carrierRepository;
109 $this->downloader = $downloader;
110 $this->httpRequest = $httpRequest;
111 $this->optionsProvider = $optionsProvider;
112 $this->logPage = $logPage;
113 $this->pickupPointsConfig = $pickupPointsConfig;
114 $this->carrierEntityRepository = $carrierEntityRepository;
115 }
116
117 /**
118 * Renders page.
119 */
120 public function render(): void {
121 $carriersUpdateParams = [];
122 if ( $this->httpRequest->getQuery( 'update_carriers' ) ) {
123 set_transient( 'packetery_run_update_carriers', true );
124 if ( wp_safe_redirect( add_query_arg( [ 'page' => OptionsPage::SLUG ], get_admin_url( null, 'admin.php' ) ) ) ) {
125 exit;
126 }
127 }
128 if ( get_transient( 'packetery_run_update_carriers' ) ) {
129 [ $carrierUpdaterResult, $carrierUpdaterClass ] = $this->downloader->run();
130 $carriersUpdateParams = [
131 'result' => $carrierUpdaterResult,
132 'resultClass' => $carrierUpdaterClass,
133 ];
134 delete_transient( 'packetery_run_update_carriers' );
135 }
136
137 $carriersUpdateParams['link'] = add_query_arg(
138 [
139 'page' => OptionsPage::SLUG,
140 'update_carriers' => '1',
141 ],
142 get_admin_url( null, 'admin.php' )
143 );
144 $carriersUpdateParams['lastUpdate'] = $this->getLastUpdate();
145
146 $isApiPasswordSet = false;
147 if ( null !== $this->optionsProvider->get_api_password() ) {
148 $isApiPasswordSet = true;
149 }
150
151 $nextScheduledRun = null;
152 $nextScheduledRunTimestamp = as_next_scheduled_action( CronService::CRON_CARRIERS_HOOK );
153 if ( is_int( $nextScheduledRunTimestamp ) ) {
154 $date = new \DateTime();
155 $date->setTimezone( wp_timezone() );
156 $date->setTimestamp( $nextScheduledRunTimestamp );
157 $nextScheduledRun = $date->format( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
158 }
159
160 $carrierChanges = get_transient( self::TRANSIENT_CARRIER_CHANGES );
161 $settingsChangedMessage = null;
162 if ( $carrierChanges ) {
163 $settingsChangedMessage = sprintf( // translators: 1: link start 2: link end.
164 esc_html__( 'The carrier settings have changed since the last carrier update. %1$sShow logs%2$s', 'packeta' ),
165 '<a href="' . $this->logPage->createLogListUrl( null, Record::ACTION_CARRIER_LIST_UPDATE ) . '">',
166 '</a>'
167 );
168 }
169
170 $countries = $this->getActiveCountries();
171 $this->latteEngine->render(
172 PACKETERY_PLUGIN_DIR . '/template/carrier/countries.latte',
173 [
174 'carriersUpdate' => $carriersUpdateParams,
175 'countries' => $countries,
176 'isApiPasswordSet' => $isApiPasswordSet,
177 'nextScheduledRun' => $nextScheduledRun,
178 'settingsChangedMessage' => $settingsChangedMessage,
179 'translations' => [
180 'packeta' => __( 'Packeta', 'packeta' ),
181 'title' => __( 'Carriers', 'packeta' ),
182 'carriersUpdate' => __( 'Carriers update', 'packeta' ),
183 'countries' => __( 'Countries', 'packeta' ),
184 'activeCarrier' => __( 'Active carrier', 'packeta' ),
185 'action' => __( 'Action', 'packeta' ),
186 'countryName' => __( 'Country name', 'packeta' ),
187 'countryCode' => __( 'Country code', 'packeta' ),
188 'setUp' => __( 'Set up', 'packeta' ),
189 'noActiveCountries' => __( 'No active countries.', 'packeta' ),
190 'lastCarrierUpdateDatetime' => __( 'Date of the last update of carriers', 'packeta' ),
191 'carrierListNeverDownloaded' => __( 'Carrier list was not yet downloaded. Continue by clicking the Run update of carriers button.', 'packeta' ),
192 'runCarrierUpdate' => __( 'Run update of carriers', 'packeta' ),
193 'pleaseCompleteSetupFirst' => __( 'Before updating the carriers, please complete the plugin setup first.', 'packeta' ),
194 'nextScheduledRunPlannedAt' => __( 'The next automatic update will occur at', 'packeta' ),
195 ],
196 ]
197 );
198 }
199
200 /**
201 * Returns Sorted data for template.
202 *
203 * @return array Data.
204 */
205 private function getActiveCountries(): array {
206 $countries = $this->carrierRepository->getCountries();
207
208 $internalCountries = $this->pickupPointsConfig->getInternalCountries();
209 $countries = array_unique( array_merge( $internalCountries, $countries ) );
210
211 $countriesFinal = [];
212 foreach ( $countries as $country ) {
213 $activeCarriers = $this->getActiveCarriersNamesByCountry( $country );
214 $wcCountries = \WC()->countries->get_countries();
215 $countriesFinal[] = [
216 'code' => $country,
217 'name' => $wcCountries[ strtoupper( $country ) ],
218 'url' => add_query_arg(
219 [
220 'page' => OptionsPage::SLUG,
221 'code' => $country,
222 ],
223 get_admin_url( null, 'admin.php' )
224 ),
225 'activeCarriers' => $activeCarriers,
226 'flag' => Plugin::buildAssetUrl( sprintf( 'public/images/flags/%s.png', $country ) ),
227 ];
228 }
229
230 usort(
231 $countriesFinal,
232 static function ( $a, $b ) {
233 if ( 'cz' === $a['code'] ) {
234 return - 1;
235 }
236 if ( 'cz' === $b['code'] ) {
237 return 1;
238 }
239 if ( 'sk' === $a['code'] ) {
240 return - 1;
241 }
242 if ( 'sk' === $b['code'] ) {
243 return 1;
244 }
245
246 return strnatcmp( (string) $a['name'], (string) $b['name'] );
247 }
248 );
249
250 return $countriesFinal;
251 }
252
253 /**
254 * Gets last update datetime.
255 *
256 * @return string|null
257 */
258 public function getLastUpdate(): ?string {
259 $lastCarrierUpdate = get_option( Downloader::OPTION_LAST_CARRIER_UPDATE );
260 if ( false !== $lastCarrierUpdate ) {
261 $date = \DateTime::createFromFormat( DATE_ATOM, $lastCarrierUpdate );
262 if ( false !== $date ) {
263 $date->setTimezone( wp_timezone() );
264
265 return $date->format( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
266 }
267 }
268
269 return null;
270 }
271
272 /**
273 * Gets options of active carriers.
274 *
275 * @return array
276 */
277 public function getCarriersForOptionsExport(): array {
278 $carriersWithSomeOptions = [];
279 $allCarriers = $this->carrierEntityRepository->getAllCarriersIncludingNonFeed();
280 foreach ( $allCarriers as $carrier ) {
281 $carrierId = $carrier->getId();
282 $optionId = OptionPrefixer::getOptionId( $carrierId );
283 $carrierOptions = get_option( $optionId );
284 if ( false !== $carrierOptions ) {
285 unset( $carrierOptions['id'] );
286 $originalName = $carrier->getName();
287 $cartName = $carrierOptions['name'];
288 unset( $carrierOptions['name'] );
289 $addition = [
290 'original_name' => $originalName,
291 'cart_name' => $cartName,
292 ];
293 $carrierOptions = array_merge( $addition, $carrierOptions );
294
295 $carrierOptions['count_of_orders'] = 0;
296 if ( $carrierId ) {
297 $orders = wc_get_orders(
298 [
299 'packetery_carrier_id' => $carrierId,
300 'nopaging' => true,
301 ]
302 );
303 if ( $orders ) {
304 $carrierOptions['count_of_orders'] = count( $orders );
305 }
306 }
307
308 $carriersWithSomeOptions[ $optionId ] = $carrierOptions;
309 }
310 }
311
312 return $carriersWithSomeOptions;
313 }
314
315 /**
316 * Gets array of active carriers names by country code.
317 *
318 * @param string $countryCode Country code.
319 *
320 * @return array
321 */
322 private function getActiveCarriersNamesByCountry( string $countryCode ): array {
323 $activeCarriers = [];
324 $countryCarriers = $this->carrierEntityRepository->getByCountryIncludingNonFeed( $countryCode );
325 foreach ( $countryCarriers as $carrier ) {
326 $optionId = OptionPrefixer::getOptionId( $carrier->getId() );
327 $carrierOptions = get_option( $optionId );
328 if ( false !== $carrierOptions && $carrierOptions['active'] ) {
329 $activeCarriers[] = $carrier->getName();
330 }
331 }
332
333 return $activeCarriers;
334 }
335 }
336