public function getSettingsHTML($params = array()) { $values = $this->getSettings(); if (!empty($params['value'])) { $values = array_merge($values, $params['value']); } $view = wa()->getView(); $cm = new waCountryModel(); $view->assign('countires', $cm->all()); if (!empty($values['country'])) { $rm = new waRegionModel(); $view->assign('regions', $rm->getByCountry($values['country'])); } $namespace = ''; if (!empty($params['namespace'])) { if (is_array($params['namespace'])) { $namespace = array_shift($params['namespace']); while (($namspace_chunk = array_shift($params['namespace'])) !== null) { $namespace .= "[{$namspace_chunk}]"; } } else { $namespace = $params['namespace']; } } $view->assign('namespace', $namespace); $view->assign('values', $values); $view->assign('p', $this); $view->assign('xhr_url', wa()->getAppUrl('webasyst') . '?module=backend&action=regions'); $html = $view->fetch($this->path . '/templates/settings.html'); $html .= parent::getSettingsHTML($params); return $html; }
public static function settingRegionRatesControl($name, $params = array()) { foreach ($params as $field => $param) { if (strpos($field, 'wrapper')) { unset($params[$field]); } } if (empty($params['value']) || !is_array($params['value'])) { $params['value'] = array(); } $control = ''; waHtmlControl::addNamespace($params, $name); $cm = new waCountryModel(); $countries = $cm->all(); $rm = new waRegionModel(); if ($regions = $rm->getByCountry('rus')) { $control .= "<table class=\"zebra\"><thead>"; $control .= "<tr class=\"gridsheader\"><th colspan=\"3\">"; $control .= htmlentities('Распределите регионы по тарифным поясам Почты России', ENT_QUOTES, 'utf-8'); $control .= "</th>"; $control .= "<th>Только авиа</th>"; $control .= "</th></tr></thead><tbody>"; $params['control_wrapper'] = '<tr><td>%s</td><td>→</td><td>%s</td><td>%s</td></tr>'; $params['title_wrapper'] = '%s'; $params['description_wrapper'] = '%s'; $params['options'] = array(); $params['options'][0] = _wp('*** пояс не выбран ***'); for ($region = 1; $region <= 5; $region++) { $params['options'][$region] = sprintf(_wp('Пояс %d'), $region); } $avia_params = $params; $avia_params['control_wrapper'] = '%2$s'; $avia_params['description_wrapper'] = false; $avia_params['title_wrapper'] = false; foreach ($regions as $region) { $name = 'zone'; $id = $region['code']; if (empty($params['value'][$id])) { $params['value'][$id] = array(); } $params['value'][$id] = array_merge(array($name => 0, 'avia_only' => false), $params['value'][$id]); $region_params = $params; waHtmlControl::addNamespace($region_params, $id); $avia_params = array('namespace' => $region_params['namespace'], 'control_wrapper' => '%2$s', 'description_wrapper' => false, 'title_wrapper' => false, 'value' => $params['value'][$id]['avia_only']); $region_params['value'] = max(0, min(5, $params['value'][$id][$name])); $region_params['description'] = waHtmlControl::getControl(waHtmlControl::CHECKBOX, 'avia_only', $avia_params); $region_params['title'] = $region['name']; if ($region['code']) { $region_params['title'] .= " ({$region['code']})"; } $control .= waHtmlControl::getControl(waHtmlControl::SELECT, 'zone', $region_params); } $control .= "</tbody>"; $control .= "</table>"; } else { $control .= 'Не определено ни одной области. Для работы модуля необходимо определить хотя бы одну область в России (см. раздел «Страны и области»).'; } return $control; }
/** * @see waShipping::getSettingsHTML() * @param array $params * @return string HTML */ public function getSettingsHTML($params = array()) { $params += array('translate' => array(&$this, '_w')); $values = $this->getSettings(); if (!empty($params['value'])) { $values = array_merge($values, $params['value']); } if (!$values['rate_zone']['country']) { $l = substr(wa()->getUser()->getLocale(), 0, 2); if ($l == 'ru') { $values['rate_zone']['country'] = 'rus'; $values['rate_zone']['region'] = '77'; $values['city'] = ''; } else { $values['rate_zone']['country'] = 'usa'; } } $view = wa()->getView(); $cm = new waCountryModel(); $view->assign('countries', $cm->all()); if (!empty($values['rate_zone']['country'])) { $rm = new waRegionModel(); $view->assign('regions', $rm->getByCountry($values['rate_zone']['country'])); } if (!empty($values['rate'])) { self::sortRates($values['rate']); if ($values['rate_by'] == 'price') { $values['rate'] = array_reverse($values['rate']); } } else { $values['rate'] = array(); $values['rate'][] = array('limit' => 0, 'cost' => 0.0); } $app_config = wa()->getConfig(); if (method_exists($app_config, 'getCurrencies')) { $view->assign('currencies', $app_config->getCurrencies()); } $namespace = ''; if (!empty($params['namespace'])) { if (is_array($params['namespace'])) { $namespace = array_shift($params['namespace']); while (($namespace_chunk = array_shift($params['namespace'])) !== null) { $namespace .= "[{$namespace_chunk}]"; } } else { $namespace = $params['namespace']; } } $view->assign('namespace', $namespace); $view->assign('values', $values); $view->assign('p', $this); $html = ''; $view->assign('xhr_url', wa()->getAppUrl('webasyst') . '?module=backend&action=regions'); $view->assign('map_adapters', wa()->getMapAdapters()); $html .= $view->fetch($this->path . '/templates/settings.html'); $html .= parent::getSettingsHTML($params); return $html; }
public function execute() { $country_iso3 = waRequest::request('country'); if (!$country_iso3) { throw new waException('Country not specified.', 404); } $regions = array(); switch ($country_iso3) { case '%AL': $country = array('name' => _w('All countries')); break; case '%EU': $country = array('name' => _w('All European countries')); break; case '%RW': $country = array('name' => _w('Rest of world')); break; default: // Country $cm = new waCountryModel(); $country = $cm->get($country_iso3); if (!$country) { throw new waException('Country not found.', 404); } // Country regions $rm = new waRegionModel(); foreach ($rm->getByCountry($country_iso3) as $r) { $r['css_class'] = 'highlighted just-added hidden'; $r['tax_name'] = ''; $r['tax_value'] = ''; $r['params'] = array('tax_value_modifier' => '+'); $regions[] = $r; } break; } $this->view->assign('c', array('css_class' => 'highlighted just-added one_rate', 'name' => $country['name'], 'iso3' => $country_iso3, 'regions' => $regions, 'global_rate' => '')); }
/** * * Country/region dependent select boxes [+ city input] * * @param string $name * @param array $params * @return string * @example * Sample of params defined in proper settings.php * * 'region_zone' => array( * 'title' => 'Sender region', * 'control_type' => waHtmlControl::CUSTOM . ' waShipping::settingRegionZoneControl', * 'items' => array( * 'country' => array( * 'value' => 'usa', * 'description' => 'Represents the country from which the shipment will be originating' * ), * 'region' => array( * 'value' => 'NY', * 'description' => 'Represents the state/province from which the shipment will be originating.<br>Required for printing labels' * ), * 'city' => array( * 'value' => 'New York', * 'description' => Enter city name<br>Required for printing labels' * ), * ) * ), * * If 'city' is not missing, city input box is presented * */ public static function settingRegionZoneControl($name, $params = array()) { $html = ""; $plugin = $params['instance']; /** * @var waShipping $plugin */ $params['items']['country']['value'] = !empty($params['value']['country']) ? $params['value']['country'] : ''; $params['items']['region']['value'] = !empty($params['value']['region']) ? $params['value']['region'] : ''; if (isset($params['items']['city'])) { $params['items']['city']['value'] = !empty($params['value']['city']) ? $params['value']['city'] : ''; } // country section $cm = new waCountryModel(); $html .= "<div class='country'>"; $html .= "<select name='{$name}[country]'><option value=''></option>"; foreach ($cm->all() as $country) { $html .= "<option value='{$country['iso3letter']}'" . ($params['items']['country']['value'] == $country['iso3letter'] ? " selected='selected'" : "") . ">{$country['name']}</value>"; } $html .= "</select><br>"; $html .= "<span class='hint'>{$params['items']['country']['description']}</span></div><br>"; $regions = array(); if ($params['items']['country']['value']) { $rm = new waRegionModel(); $regions = $rm->getByCountry($params['items']['country']['value']); } // region section $html .= '<div class="region">'; $html .= '<i class="icon16 loading" style="display:none; margin-left: -23px;"></i>'; $html .= '<div class="empty"' . (!empty($regions) ? 'style="display:none;"' : '') . '><p class="small">' . $plugin->_w("Shipping will be restricted to the selected country") . "</p>"; $html .= "<input name='{$name}[region]' value='' type='hidden'" . (!empty($regions) ? 'disabled="disabled"' : '') . '></div>'; $html .= '<div class="not-empty" ' . (empty($regions) ? 'style="display:none;"' : '') . ">"; $html .= "<select name='{$name}[region]'" . (empty($regions) ? 'disabled="disabled"' : '') . '><option value=""></option>'; foreach ($regions as $region) { $html .= "<option value='{$region['code']}'" . ($params['items']['region']['value'] == $region['code'] ? ' selected="selected"' : "") . ">{$region['name']}</option>"; } $html .= "</select><br>"; $html .= "<span class='hint'>{$params['items']['region']['description']}</span></div><br>"; // city section if (isset($params['items']['city'])) { $html .= "<div class='city'>"; $html .= "<input name='{$name}[city]' value='" . (!empty($params['items']['city']['value']) ? $params['items']['city']['value'] : "") . "' type='text'>\n <br>"; $html .= "<span class='hint'>{$params['items']['city']['description']}</span></div>"; } $html .= "</div>"; $url = wa()->getAppUrl('webasyst') . '?module=backend&action=regions'; // container id for interaction with js purpose $id = preg_replace("![\\[\\]]{1,2}!", "-", $name); if ($id[strlen($id) - 1] == "-") { $id = substr($id, 0, -1); } // wrap to container $html = "<div id='{$id}'>{$html}</div>"; // javascript here $html .= <<<HTML <script type='text/javascript'> \$(function() { 'use strict'; var name = '{$name}[region]'; var url = '{$url}'; var container = \$('#{$id}'); var target = container.find("div.region"); var loader = container.find(".loading"); var old_val = target.find("select, input").val(); container.find('select[name\$="[country]"]').change(function() { loader.show(); \$.post(url, { country: this.value }, function(r) { if (r.data && r.data.options && r.data.oOrder && r.data.oOrder.length) { var select = \$( "<select name='" + name + "'>" + "<option value=''></option>" + "</select>" ); var o, selected = false; for (var i = 0; i < r.data.oOrder.length; i++) { o = \$("<option></option>").attr( "value", r.data.oOrder[i] ).text( r.data.options[r.data.oOrder[i]] ).attr( "disabled", r.data.oOrder[i] === "" ); if (!selected && old_val === r.data.oOrder[i]) { o.attr("selected", true); selected = true; } select.append(o); } target.find(".not-empty select").replaceWith(select); target.find(".not-empty").show(); target.find(".empty input").attr("disabled", true); target.find(".empty").hide(); } else { target.find(".empty input").attr("disabled", false); target.find(".empty").show(); target.find(".not-empty select").attr("disabled", true); target.find(".not-empty").hide(); } loader.hide(); }, "json"); }); container.on("change", 'select[name="' + name + '"]', function() { old_val = this.value; }); }); </script> HTML; return $html; }
protected function getTaxCountries($tax, $countries) { $tax_countries = array(); // Collect data for all countries that have tax rates set foreach ($this->trm->getByTax($tax['id']) as $r) { // Init country if (!isset($tax_countries[$r['country_iso3']])) { switch ($r['country_iso3']) { case '%AL': $c = array('name' => _w('All countries')); break; case '%EU': $c = array('name' => _w('All European countries')); break; case '%RW': $c = array('name' => _w('Rest of world')); break; default: $c = ifset($countries[$r['country_iso3']], array('name' => 'Unknown country: ' . $r['country_iso3'])); break; } $tax_countries[$r['country_iso3']] = array('css_class' => null, 'iso3' => $r['country_iso3'], 'name' => $c['name'], 'regions_data' => array(), 'global_rate' => null, 'regions' => array()); } // Remember regions data to process later if ($r['region_code'] === null) { $tax_countries[$r['country_iso3']]['global_rate'] = (double) str_replace(',', '.', $r['tax_value']); } else { $tax_countries[$r['country_iso3']]['regions_data'][$r['region_code']] = $r; } } // Init regions $rm = new waRegionModel(); foreach ($rm->getByCountry(array_keys($tax_countries)) as $r) { $c =& $tax_countries[$r['country_iso3']]; if (!$c['regions_data']) { $r['css_class'] = 'hidden'; } else { if ($c['global_rate'] === null) { $r['css_class'] = 'regions_simple'; } else { $r['css_class'] = 'regions_advanced'; } } if (empty($c['regions_data'][$r['code']])) { $r['tax_name'] = ''; $r['tax_value'] = ''; $r['params'] = array('tax_value_modifier' => '+'); } else { $r = $c['regions_data'][$r['code']] + $r; $r['params'] = $r['params'] ? unserialize($r['params']) : array('tax_value_modifier' => ''); $r['tax_value'] = (double) str_replace(',', '.', $r['tax_value']); } $c['regions'][] = $r; } unset($c); // Cleanup foreach ($tax_countries as &$c) { if (!$c['regions_data']) { $c['css_class'] = 'one_rate'; } else { if ($c['global_rate'] === null) { $c['css_class'] = 'regions_simple'; } else { $c['css_class'] = 'regions_advanced'; } } unset($c['regions_data']); } unset($c); // Sort countries by name uasort($tax_countries, array($this, 'sortHelper')); return $tax_countries; }
public function allowedAddress() { $cache = new waSerializeCache(__CLASS__ . __FUNCTION__, 86400, 'webasyst'); if (!($addresses = $cache->get())) { $addresses = array(); /* countries */ $countries = $this->request('ems.get.locations', array('type' => 'countries')); $country_model = new waCountryModel(); $map = $country_model->getAll('iso2letter'); $address = array('country' => array()); foreach ($countries['locations'] as $country) { if (ifset($country['type']) == 'countries' && ($value = strtolower(ifset($country['value']))) && isset($map[$value])) { $address['country'][] = $map[$value]['iso3letter']; } } $addresses[] = $address; /* regions */ $region_model = new waRegionModel(); $address = array('country' => 'rus', 'region' => array()); $map = $region_model->getByCountry('rus'); foreach ($map as $region) { if ($this->findTo(array('country' => 'rus', 'region_name' => $region['name']))) { $address['region'][] = $region['code']; } } $addresses[] = $address; $cache->set($addresses); } return $addresses; }