sibling = &$sibling; } } public function setMedia() { if (Tools::getValue('controller') == 'order' || Tools::getValue('controller') == 'supercheckout' || Tools::getValue('controller') == 'amzpayments' || Tools::getValue('controller') == 'default') { $this->context->controller->addJquery(); $this->context->controller->addJqueryUI('ui.datepicker'); //$this->context->controller->registerJavascript('typewatch', Media::getJqueryPluginPath('ui.datepicker', null)['js']); $this->sibling->context->controller->registerJavascript('jquery_ui_localizations', 'js/jquery/ui/i18n/jquery.ui.datepicker-' . Context::getContext()->language->iso_code . '.js', null); $this->sibling->context->controller->addJS($this->sibling->_path . 'views/js/admin/lib/tools.js'); $this->sibling->context->controller->addJS($this->sibling->_path . 'views/js/front/DDWFrontCheckoutCalendarController.js'); $this->sibling->context->controller->addJS($this->sibling->_path . 'views/js/front/DDWFrontCheckoutController.js'); $this->sibling->context->controller->addJS($this->sibling->_path . 'views/js/front/DDWFrontCheckoutControllerCore.js'); $this->sibling->context->controller->addJS($this->sibling->_path . 'views/js/front/DDWFrontCheckoutTableController.js'); $this->sibling->context->controller->addJS($this->sibling->_path . 'views/js/front/DDWFrontCheckoutDayViewController.js'); $this->sibling->context->controller->addCSS($this->sibling->_path . 'views/css/front/front.css'); } if (Tools::getValue('controller') == 'product') { $this->sibling->context->controller->addCSS($this->sibling->_path . 'views/css/front/deliverydateswizard.css'); } } public function getWeekdayTranslated($id_weekday) { $weekdays = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); return $this->l($weekdays[$id_weekday]); } public function getMonthTranslated($id_month) { $months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); return $this->l($months[$id_month - 1]); } /** * get array of blocked dates for the calendar * @param null $override_scope * @return array */ public function getBlockedDates($override_scope = null) { if (empty($override_scope['id_carrier'])) { $id_carrier = (int)Tools::getValue('id_carrier'); } else { $id_carrier = (int)$override_scope['id_carrier']; } $id_shop = Context::getContext()->shop->id; $start_date = date('Y-m-d'); return DDWScheduleHelper::getBlockedDates($override_scope, $start_date, $start_date, $id_carrier, $id_shop); } /** * update DDW cart date and time */ public function updateDDWCart() { if ((int)Context::getContext()->cart->id > 0) { DDWModel::saveToCart(Tools::getValue('ddw_date'), Tools::getValue('ddw_time'), Context::getContext()->cart->id); } } /** * get the last cart date time for current cart session * @return array */ public function getCartDateTime() { if (Context::getContext()->cart->id != '') { return DDWCartHelper::getCartDDWDateTime(Context::getContext()->cart->id); } } /** * render the calendar widget * @return mixed */ public function renderWidgetCalendar() { $id_carrier = (int)Tools::getValue('id_carrier'); $id_shop = Context::getContext()->shop->id; $ddw_translations = DDWTranslationsModel::getTranslations(Context::getContext()->shop->id, false); $this->assignTranslations($ddw_translations); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($id_carrier, Context::getContext()->shop->id); if (!$ddw_model->enabled) { return ''; } if ((int)$ddw_model->max_days == 0) { $max_days = 30; } else { $max_days = $ddw_model->max_days; } $days = array( 'days' => array() ); // add translated weekdays foreach ($days['days'] as &$day) { $day['weekday_label'] = $this->getWeekdayTranslated($day['weekday']); } $this->sibling->smarty->assign(array( 'days' => $days, 'ddw_config' => json_encode($ddw_model) )); $return = array( 'ddw_config' => json_encode($ddw_model), 'html' => $this->sibling->display($this->sibling->file, 'views/templates/front/widget_calendar.tpl'), 'blocked_dates' => $this->getBlockedDates() ); return json_encode($return); } /** * render thw date in time table layout */ public function renderWidgetTable() { $id_carrier = (int)Tools::getValue('id_carrier'); $id_shop = Context::getContext()->shop->id; $ddw_translations = DDWTranslationsModel::getTranslations(Context::getContext()->shop->id, false); $this->assignTranslations($ddw_translations); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($id_carrier, Context::getContext()->shop->id); if (!$ddw_model->enabled) { return ''; } $ddw_override_helper = new DDWOverrideHelper(date('Y-m-d'), $id_carrier, $id_shop); if ((int)$ddw_override_helper->max_days == 0) { $max_days = 14; } else { $max_days = $ddw_override_helper->max_days; } $table_start_date = date('Y-m-d'); $table_end_date = date('Y-m-d', strtotime($table_start_date . ' + ' . $max_days . ' days')); $blocked_dates = DDWScheduleHelper::getBlockedDates(null, $table_start_date, $table_end_date, $id_carrier, $id_shop); $days = DDWScheduleHelper::getDatesAndTimeslots($blocked_dates, $table_start_date, $table_end_date, $id_carrier, $id_shop); // add translated weekdays foreach ($days['days'] as &$day) { $day['date_formatted'] = Tools::displayDate($day['date']); $day['weekday_label'] = $this->getWeekdayTranslated($day['weekday']); } $this->sibling->smarty->assign(array( 'days' => $days, 'ddw_config' => json_encode($ddw_model) )); $return = array( 'days' => $days, 'ddw_config' => json_encode($ddw_model), 'html' => $this->sibling->display($this->sibling->file, 'views/templates/front/widget_table.tpl'), 'blocked_dates' => $this->getBlockedDates() ); return json_encode($return); } /** * Render the day to day view */ public function renderWidgetDayView() { $id_carrier = (int)Tools::getValue('id_carrier'); $id_shop = Context::getContext()->shop->id; $timeslots = array(); $ddw_translations = DDWTranslationsModel::getTranslations(Context::getContext()->shop->id, false); $this->assignTranslations($ddw_translations); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($id_carrier, Context::getContext()->shop->id); if (!$ddw_model->enabled) { return ''; } $ddw_override_helper = new DDWOverrideHelper(date('Y-m-d'), $id_carrier, $id_shop); if ((int)$ddw_override_helper->max_days == 0) { $max_days = 14; } else { $max_days = $ddw_override_helper->max_days; } $start_date = date('Y-m-d'); $available_dates = DDWScheduleHelper::getAvailableDates(null, $start_date, $max_days, $id_carrier, $id_shop); $available_dates_result = array(); foreach ($available_dates as $available_date) { $available_dates_result[] = array( 'date' => $available_date, 'date_formatted' => Tools::displayDate($available_date), 'weekday_label' => $this->getWeekdayTranslated(date('w', strtotime($available_date))), 'day_label' => date('d', strtotime($available_date)), 'month_label' => $this->getMonthTranslated(date('m', strtotime($available_date))), 'year_label' => date('Y', strtotime($available_date)) ); } if (count($available_dates_result) > 0) { $first_date_available = $available_dates_result[0]['date']; $timeslots = DDWScheduleHelper::getTimeslotsForDate($first_date_available, $id_carrier, $id_shop); if (!empty($timeslots)) { $realcount = $timeslots[$first_date_available]['realcount']; $available = $timeslots[$first_date_available]['available']; $timeslots = $timeslots[$first_date_available]['timeslots']; } } $this->sibling->smarty->assign(array( 'realcount' => $realcount, 'available' => $available, 'timeslots' => $timeslots, )); $html_timeslots = $this->sibling->display($this->sibling->file, 'views/templates/front/widget_dayview_timeslots.tpl'); $this->sibling->smarty->assign(array( 'days' => $available_dates_result, 'ddw_config' => json_encode($ddw_model), 'html_timeslots' => $html_timeslots )); $return = array( 'html' => $this->sibling->display($this->sibling->file, 'views/templates/front/widget_dayview.tpl'), ); return json_encode($return); } /** * render the appropriate layout */ public function render() { $layout = Tools::getValue('layout'); switch ($layout) { case 'table': return $this->renderWidgetTable(); case 'dayview': return $this->renderWidgetDayView(); default: die($this->renderWidgetCalendar()); } } /** * render the widget during checkout * @return mixed */ public function renderFrontWidget($params) { $link = new Link(); $id_shop = Context::getContext()->shop->id; $ddw_translations = DDWTranslationsModel::getTranslations($id_shop, false); $ddw_ajax_url = $link->getModuleLink('deliverydateswizardpro', 'ajax', array(), Configuration::get('PS_SSL_ENABLED')); $this->assignTranslations($ddw_translations); $ddw_entries = DDWModelHelper::getAllByShop($id_shop); $ddw_entries_json = array(); foreach ($ddw_entries as $ddw_entry) { $ddw_entries_json[$ddw_entry['id_carrier']] = $ddw_entry; } $ddw_cart_date = DDWCartHelper::getCartDDWDateTime(Context::getContext()->cart->id); $this->sibling->smarty->assign(array( 'controller_name' => Context::getContext()->controller->php_self, 'controller' => Tools::getValue('controller'), 'ddw_entries_json' => json_encode($ddw_entries_json), 'id_lang' => Context::getContext()->language->id, 'ddw_date_format' => Context::getContext()->language->date_format_lite, 'ddw_ajax_url' => $ddw_ajax_url, 'ddw_cart_date' => $ddw_cart_date['ddw_order_date'], 'ddw_cart_time' => $ddw_cart_date['ddw_order_time'], 'ddw_cart_time_id' => $ddw_cart_date['ddw_order_time_id'], 'baseDir' => __PS_BASE_URI__ )); return $this->sibling->display($this->sibling->file, 'views/templates/front/widget.tpl'); } /** * @param string $date * @param bool $return_view * @return string * @throws PrestaShopDatabaseException * @throws PrestaShopException */ public function renderTimeSlots($date = '', $return_view = true) { $id_shop = Context::getContext()->shop->id; $id_carrier = (int)Tools::getValue('id_carrier'); $json_return = array(); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($id_carrier, $id_shop); if ($date == '') { $date = Tools::getValue('date'); } $date = date('Y-m-d', strtotime($date)); $day_of_week = date('w', strtotime($date)); $ddw_timeslots = new DDWTimeslotModel(); $ddw_specificdate_model = new DDWSpecificDateModel(); $ddw_specificdate_model->loadByDate(date('Y-m-d', strtotime($date)), $id_carrier, $id_shop); if ($ddw_specificdate_model->id_ddw_specificdate > 0 && $ddw_specificdate_model->enabled) { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForSpecificDate($ddw_specificdate_model->id_ddw_specificdate, $id_carrier, $id_shop); } else { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForWeekday($id_carrier, $day_of_week, $id_shop); } $ddw_override_helper = new DDWOverrideHelper($date, $id_carrier, $id_shop); $timeslots = DDWScheduleHelper::filterTimeslots($timeslots_collection_unfiltered, $date, $ddw_override_helper->timeslots_prep_minutes, $id_carrier, $id_shop); $timeslot_realcount = 0; foreach ($timeslots as $timeslot) { if ($timeslot->enabled) { $timeslot_realcount++; } } $json_return['timeslot_realcount'] = $timeslot_realcount; if (!$return_view) { return $timeslots; } $this->sibling->smarty->assign(array( 'timeslots' => $timeslots )); $json_return['html'] = $this->sibling->display($this->sibling->file, 'views/templates/front/timeslots.tpl'); return Tools::jsonEncode($json_return); } /** * @return string */ public function renderTimeSlotsDayView() { $id_shop = Context::getContext()->shop->id; $id_carrier = (int)Tools::getValue('id_carrier'); $date = Tools::getValue('date'); $timeslots = DDWScheduleHelper::getTimeslotsForDate($date, $id_carrier, $id_shop); if (!empty($timeslots[$date]['timeslots'])) { $realcount = $timeslots[$date]['realcount']; $available = $timeslots[$date]['available']; $timeslots = $timeslots[$date]['timeslots']; } else { return ''; } $this->sibling->smarty->assign(array( 'timeslots' => $timeslots, 'realcount' => $realcount, 'available' => $available )); return $this->sibling->display($this->sibling->file, 'views/templates/front/widget_dayview_timeslots.tpl'); } /* Hooks */ /** * Save the selected delivery date timeslot ID to the cart * @param $params * @return bool|void */ public function hookActionCarrierProcess($params) { $ddw_order_time = ''; $ddw_timeslot_id = ''; /* check if time slot is available */ if (Tools::getIsset('ddw_order_time')) { $ddw_timeslot = new DDWTimeslotModel(Tools::getValue('ddw_order_time')); $ddw_order_time = $ddw_timeslot->time_start . ' - ' . $ddw_timeslot->time_end; $ddw_timeslot_id = $ddw_timeslot->id; if ($ddw_timeslot->order_limit > 0) { $count = DDWOrderHelper::getOrderCountInTimeWindow(Tools::getValue('ddw_order_date'), $ddw_order_time, $this->context->shop->id); if ($count >= $ddw_timeslot->order_limit) { $ddw_timeslot->id = ''; } } $cart = $params['cart']; if (!($cart instanceof Cart)) { return; } DDWModel::saveToCart(Tools::getValue('ddw_order_date'), $ddw_timeslot_id, $cart->id); $cart->ddw_order_date = Tools::getValue('ddw_order_date'); $cart->ddw_order_time = $ddw_timeslot_id; } } /** * Display date and time on order confirmation page * @param $params */ public function hookDisplayOrderConfirmation($params) { if (empty($params['order'])) { return ''; } $order = $params['order']; $ddw_datetime = DDWModel::getOrderDDWDateTime($order->id); if (DDWDateTimeHelper::isStrDateTimeEpoch($ddw_datetime['ddw_order_date'])) { return ''; } $block_html = ''; if ($ddw_datetime['ddw_order_date'] != '') { $block_html .= '
' . Translate::getModuleTranslation('deliverydateswizardpro', 'Delivery Date:', 'ORDER_DETAIL_BLOCK') . ' ' . Tools::displayDate($ddw_datetime['ddw_order_date']); } if ($ddw_datetime['ddw_order_time'] != '' && $ddw_datetime['ddw_order_time'] != 0) { $block_html .= '
' . Translate::getModuleTranslation('deliverydateswizardpro', 'Delivery Time:', 'ORDER_DETAIL_BLOCK') . ' ' . $ddw_datetime['ddw_order_time']; } return $block_html; } /** * Display on the order details page in customer account * @param $params * @return string */ public function hookDisplayOrderDetail($params) { if (empty($params['order'])) { return ''; } $order = $params['order']; $ddw_datetime = DDWModel::getOrderDDWDateTime($order->id); $this->sibling->smarty->assign(array( 'ddw_order_date' => Tools::displayDate($ddw_datetime['ddw_order_date']), 'ddw_order_time' => $ddw_datetime['ddw_order_time'] )); return $this->sibling->display($this->sibling->file, 'views/templates/front/orderdetail.tpl'); } /** * Make sure shipping step is shown again during checkout if previously selected date and is no longer valid * @param $params */ public function hookActionDispatcher($params) { $available = true; $cookie = Context::getContext()->cookie; $id_cart = (int)$cookie->id_cart; $id_shop = (int)Context::getContext()->shop->id; $checkout_session_data = DDWCartHelper::getPersistData($cookie->id_cart); if ($id_cart == 0 || $id_shop == 0) { return false; } $cart = new Cart($id_cart); $cart_ddw = DDWCartHelper::getCartDDWDateTime($id_cart, true); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($cart->id_carrier, $id_shop); if (empty($ddw_model->id_ddw)) { return false; } if (!$ddw_model->required) { return false; } if (!empty($checkout_session_data)) { $json = json_decode($checkout_session_data, true); if ($ddw_model->required) { if ($cart_ddw['ddw_order_date'] == '') { $available = false; } } // check if cart ddw date and time is still available if ($available == true) { $available = DDWScheduleHelper::isDateTimeAvailable($cart_ddw['ddw_order_date'], $cart_ddw['ddw_order_time'], $cart_ddw['ddw_order_time_id'], $cart->id_carrier, $id_cart); } // this will make sure shipping step is the active step during checkout if (!$available && !empty($json['checkout-delivery-step'])) { if ($json['checkout-delivery-step']['step_is_complete'] == 1) { $json['checkout-addresses-step']['step_is_reachable'] = 1; $json['checkout-addresses-step']['step_is_complete'] = 0; $json['checkout-delivery-step']['step_is_reachable'] = 0; $json['checkout-delivery-step']['step_is_complete'] = 0; $json['checkout-payment-step']['step_is_reachable'] = 0; $json['checkout-payment-step']['step_is_complete'] = 0; $checkout_session_data = json_encode($json); DDWCartHelper::setPersistData($cookie->id_cart, $checkout_session_data); } } } } /** * route */ public function route() { switch (Tools::getValue('action')) { case 'render': die($this->render()); case 'rendertimeslots': die($this->renderTimeSlots()); case 'rendertimeslotsdayview': die($this->renderTimeSlotsDayView()); case 'updatecartdatetime': die($this->updateDDWCart()); /*case 'processdatetimeavailable': die($this->processDateTimeAvailable());*/ case 'getcartdatetime': die(json_encode($this->getCartDateTime())); } } }