$model, 'id' => $id ], JWT::SECRET_KEY); */ return $model . '_' . $id; } public static function decodeKey($key) { $result = explode('_', $key); $decode = ['model' => 'app\\models\\' . $result[0], 'id' => $result[1]]; return (object)$decode; //return JWT::decode($key, JWT::SECRET_KEY); } public static function add() { $cart_data = 'cart_data'; $cart_key = 'cart_key'; if (empty($_COOKIE[$cart_key])) { setcookie($cart_key, uniqid('.' . $_SERVER['REMOTE_ADDR'] . '.' . time()), time() + (86400 * self::EXP_DAYS), "/"); } $data = json_decode(\Yii::$app->request->getRawBody()); $newCartElement = $data->cart; $cartData = !empty($_COOKIE[$cart_data]) ? json_decode($_COOKIE['cart_data']) : []; $duplicatesCount = 0; foreach ($cartData as $element) { if ($element == $data->cart) $duplicatesCount++; } $allow = true; if (!empty($data->max) && $data->max <= $duplicatesCount) $allow = false; if ($allow) { $cartData[] = $newCartElement; setcookie($cart_data, json_encode($cartData), time() + (86400 * self::EXP_DAYS), "/"); if (!empty($_COOKIE[$cart_key])) { setcookie($cart_key, $_COOKIE[$cart_key], time() + (86400 * self::EXP_DAYS), "/"); } } else { echo json_encode(['message' => Ts::get(122), 'error' => 1]); exit; } } public static function addBooking() { $cart_data = 'cart_data'; $cart_key = 'cart_key'; $key = uniqid($_SERVER['REMOTE_ADDR'] . '_' . time()); if (empty($_COOKIE[$cart_key])) { setcookie($cart_key, $key, time() + (86400 * self::EXP_DAYS), "/"); } $data = json_decode(\Yii::$app->request->getRawBody()); $allTickets = explode(',', $data->{'tickets'}); $tickets = PriceObject::find()->where(['IN', 'id', $allTickets])->all(); $data_to_cart = []; /** @var PriceObject $ticket */ foreach ($tickets as $ticket) { $ticketsCount = array_count_values($allTickets)[$ticket->id]; $booking = OrderBooking::find()->where(['ticket_id' => $ticket->id, 'datetime' => $data->{'date_time'}, 'cart_key' => $_COOKIE[$cart_key] ?? $key])->one(); if ($booking) { $booking->tickets_count = $booking->tickets_count + $ticketsCount; } else { $booking = new OrderBooking(); $booking->datetime = $data->{'date_time'}; $booking->name_bg = $ticket->text_bg; $booking->name_en = $ticket->text_en; $booking->description_bg = $ticket->event->title('bg'); $booking->description_en = $ticket->event->title('en'); $booking->single_price = $ticket->price; $booking->tickets_count = $ticketsCount; $booking->event_id = $ticket->event_id; $booking->ticket_id = $ticket->id; $booking->cart_key = $_COOKIE[$cart_key] ?? $key; } $booking->save(); for ($i = 0; $i < $ticketsCount; $i++) { $data_to_cart[] = $booking->getCartKey(); } } $cartData = !empty($_COOKIE[$cart_data]) ? json_decode($_COOKIE[$cart_data]) : []; foreach ($data_to_cart as $element) $cartData[] = $element; setcookie($cart_data, json_encode($cartData), time() + (86400 * self::EXP_DAYS), "/"); if (!empty($_COOKIE[$cart_data])) { setcookie($cart_key, $_COOKIE[$cart_key], time() + (86400 * self::EXP_DAYS), "/"); } } public static function remove() { $cart_data = 'cart_data'; $cart_key = 'cart_key'; $data = json_decode(\Yii::$app->request->getRawBody()); $cartData = !empty($_COOKIE[$cart_data]) ? json_decode($_COOKIE['cart_data']) : []; $reCountCartData = []; foreach ($cartData as $cartKey) { if ($cartKey == $data->cart) continue; $reCountCartData[] = $cartKey; } $cartItem = explode('_', $data->cart); if ($cartItem[0] == 'OrderBooking') { $booking = OrderBooking::findOne($cartItem[1]); if ($booking) { $booking->delete(); } } setcookie($cart_data, json_encode($reCountCartData), time() + (86400 * 30), "/"); if (sizeof($reCountCartData) > 0) { if (!empty($_COOKIE[$cart_key])) setcookie($cart_key, $_COOKIE[$cart_key], time() + (86400 * self::EXP_DAYS), "/"); } else { setcookie('cart_data', '', time() - 3600, "/"); setcookie('cart_key', time() - 3600, "/"); } } public static function clear() { setcookie('cart_data', '', time() - 3600, "/"); setcookie('invoice_data', '', time() - 3600, "/"); setcookie('payment_method', '', time() - 3600, "/"); setcookie('cart_key', '', time() - 3600, "/"); } public static function clearCartOnly() { setcookie('cart_data', '', time() - 3600, "/"); setcookie('cart_key', '', time() - 3600, "/"); } public static function getData() { return !empty($_COOKIE['cart_data']) ? json_decode($_COOKIE['cart_data']) : []; } public static function getGroupedData() { $groupedData = []; foreach (self::getData() as $cartKey) $groupedData[$cartKey] = empty($groupedData[$cartKey]) ? 1 : ++$groupedData[$cartKey]; return $groupedData; } public static function getFormatedData() { /** @var \app\models\parsed\CartModel[] $cartModels */ $cartModels = []; $total = 0; foreach (self::getGroupedData() as $cartKey => $quantity) { $data = self::decodeKey($cartKey); /** @var Subscriptions | Events $modelClass */ $modelClass = $data->model; $model = $modelClass::findOne($data->id); if ($model) { $cartModel = new CartModel(); $cartModel->quantity = $quantity; $cartModel->cartKey = $cartKey; $cm = $model->setCart($cartModel); $cartModels[] = $cm; $total += $quantity * $cm->price; } } return ['models' => $cartModels, 'total' => number_format($total, 2, ' . ', '')]; } public static function updateItemQuantity() { $cart_data = 'cart_data'; $cart_key = 'cart_key'; $data = json_decode(\Yii::$app->request->getRawBody()); $updatedData = []; foreach (self::getGroupedData() as $cartKey => $q) { if ($cartKey == $data->cart) $q = $data->quantity; for ($i = 0; $i < $q; $i++) { $updatedData[] = $cartKey; } } $cartItem = explode('_', $data->cart); if ($cartItem[0] == 'OrderBooking') { $booking = OrderBooking::findOne($cartItem[1]); if ($booking) { $booking->tickets_count = $data->quantity; $booking->save(); } } setcookie($cart_data, json_encode($updatedData), time() + (86400 * 30), "/"); if (!empty($_COOKIE[$cart_key])) setcookie($cart_key, $_COOKIE[$cart_key], time() + (86400 * self::EXP_DAYS), "/"); } }