sibling = &$sibling; } } /** * render the test PAI form group controls * @return mixed */ private function renderTestApi() { Context::getContext()->smarty->assign(array()); return $this->sibling->display($this->sibling->module_file, 'views/templates/admin/config/distance_test_api.tpl'); } /** * render the test PAI form group controls * @return mixed */ private function renderGoogleMapsInfo() { Context::getContext()->smarty->assign(array()); return $this->sibling->display($this->sibling->module_file, 'views/templates/admin/config/distance_google_maps_info.tpl'); } public function renderForm() { $id_carrier = (int)Tools::getValue('id_carrier'); $id_shop = (int)Tools::getValue('id_shop'); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($id_carrier, $id_shop); $carrier_distance = DDWCarrierDistanceHelper::getByCarrier($id_carrier, $id_shop); $countries = Country::getCountries($this->default_lang); $fields_form = array(); $fields_form[0]['form'] = array( 'input' => array( array( 'type' => 'hidden', 'name' => 'id_carrier' ), array( 'type' => 'switch', 'label' => $this->sibling->l('Limit carrier availability by distance?', $this->route), 'name' => 'distance_enabled', 'desc' => $this->sibling->l('Limit carrier availability by distance', $this->route), 'values' => array( array( 'id' => 'distance_enabled_on', 'value' => 1, 'label' => $this->trans('Yes', array(), 'Admin.Global'), ), array( 'id' => 'distance_enabled_off', 'value' => 0, 'label' => $this->trans('No', array(), 'Admin.Global'), ), ), ), array( 'type' => 'select', 'label' => $this->sibling->l('Maps API', $this->route), 'name' => 'maps_api_service', 'desc' => $this->sibling->l('Select an Maps API Service to use', $this->route), 'options' => array( 'query' => array( array( 'maps_api_service' => 'openmaps', 'name' => 'Open Maps API (No API Key Required)' ), array( 'maps_api_service' => 'googlemaps', 'name' => 'Google Map API (Key Required)' ) ), 'id' => 'maps_api_service', 'name' => 'name', ) ), array( 'type' => 'text', 'label' => $this->sibling->l('Google Maps API Key', $this->route), 'name' => 'google_api_key', 'desc' => $this->sibling->l('Enter your google Maps API Key here', $this->route), 'size' => 64, 'class' => 'fixed-width-xl', ), array( 'type' => 'html', 'label' => '', 'html_content' => $this->renderGoogleMapsInfo(), 'desc' => '', ), array( 'type' => 'text', 'label' => $this->sibling->l('Delivery radius from', $this->route), 'name' => 'radius_from_km', 'suffix' => $this->sibling->l('km', $this->route), 'size' => 4, 'class' => 'fixed-width-xl', ), array( 'type' => 'text', 'label' => $this->sibling->l('Delivery radius to', $this->route), 'name' => 'radius_to_km', 'desc' => $this->sibling->l('Radius in km of the delivery distance from this carrier depot', $this->route), 'suffix' => $this->sibling->l('km', $this->route), 'size' => 4, 'class' => 'fixed-width-xl', ), array( 'type' => 'text', 'label' => $this->sibling->l('Carrier Postal code', $this->route), 'name' => 'carrier_postal_code', 'desc' => $this->sibling->l('This is the postal code from where your deliveries are dispatched for this carrier. This will be used to calculate radius to customer.', $this->route), 'size' => 12, 'class' => 'fixed-width-xl', ), array( 'type' => 'select', 'label' => $this->sibling->l('Carrier country', $this->route), 'name' => 'carrier_country_iso', 'options' => array( 'query' => $countries, 'id' => 'iso_code', 'name' => 'name', ) ), array( 'type' => 'html', 'label' => '', 'html_content' => $this->renderTestApi(), 'desc' => $this->sibling->l('If your Maps API Service has been set up correctly then the address for your carrier (based on the post code and country above) should be displayed below when you press the test button ', $this->route), ) ), 'submit' => array( 'title' => $this->sibling->l('Save', $this->route), 'class' => 'btn btn-ddw-distance-save pull-right' ) ); $helper = new HelperForm(); $this->setupHelperConfigForm($helper, $this->route, 'process'); $helper->fields_value['id_carrier'] = $id_carrier; $helper->fields_value['google_api_key'] = DDWMapsHelper::getApiKey(); $helper->fields_value['maps_api_service'] = DDWMapsHelper::getMapsApiService(); if ($carrier_distance->id_ddw_carrier_distance > 0) { $helper->fields_value['id_carrier'] = $carrier_distance->id_carrier; $helper->fields_value['distance_enabled'] = $carrier_distance->distance_enabled; $helper->fields_value['radius_from_km'] = $carrier_distance->radius_from_km; $helper->fields_value['radius_to_km'] = $carrier_distance->radius_to_km; $helper->fields_value['carrier_postal_code'] = $carrier_distance->carrier_postal_code; $helper->fields_value['carrier_country_iso'] = $carrier_distance->carrier_country_iso; } Context::getContext()->smarty->assign(array( 'module_config_url' => $this->module_config_url, 'hour_values' => range(0, 23), 'min_values' => range(0, 59), 'form' => $helper->generateForm($fields_form) )); return $this->sibling->display($this->sibling->module_file, 'views/templates/admin/config/distance.tpl'); } /** * process the form */ public function processForm() { $id_carrier = Tools::getValue('id_carrier'); $id_shop = Context::getContext()->shop->id; $carrier_distance = DDWCarrierDistanceHelper::getByCarrier($id_carrier, $id_shop); $carrier_distance->id_carrier = (int)$id_carrier; $carrier_distance->id_shop = (int)Context::getContext()->shop->id; $carrier_distance->distance_enabled = (int)Tools::getValue('distance_enabled'); $carrier_distance->radius_from_km = (float)Tools::getValue('radius_from_km'); $carrier_distance->radius_to_km = (float)Tools::getValue('radius_to_km'); $carrier_distance->carrier_postal_code = pSQL(Tools::getValue('carrier_postal_code')); $carrier_distance->carrier_country_iso = pSQL(Tools::getValue('carrier_country_iso')); $carrier_distance->save(); DDWMapsHelper::setApiKey(Tools::getValue('google_api_key')); DDWMapsHelper::setMapsApiService(Tools::getValue('maps_api_service')); } /** * test the maps API set up */ public function processTestApi() { if (Tools::getValue('maps_api_service') == 'openmaps') { $ddw_maps_helper = new DDWMapsOSMHelper(); } if (Tools::getValue('maps_api_service') == 'googlemaps') { $ddw_maps_helper = new DDWMapsGoogleHelper(); } $address = $ddw_maps_helper->getAddress(Tools::getValue('carrier_postal_code'), Tools::getValue('carrier_country_iso')); if ($address == '') { $address = $this->sibling->l('Unable to determine an address for the post code and country provided', $this->route); } return $address; } public function route() { switch (Tools::getValue('action')) { case 'renderform': die($this->renderForm()); case 'processform': die($this->processForm()); case 'processtestapi': die($this->processTestApi()); } } }