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>&rarr;</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;
 }
 public function getOptions($id = null)
 {
     if (!$this->model) {
         $this->model = new waCountryModel();
     }
     if ($id) {
         if (!($result = $this->model->name($id))) {
             throw new Exception('Unknown country ISO-3 code: ' . $id);
         }
         return $result;
     }
     $result = $this->model->all();
     foreach ($result as &$row) {
         $row = $row['name'];
     }
     return $result;
 }
 /**
  * @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 getOptions($id = null)
 {
     if (isset($this->options['options']) && is_array($this->options['options'])) {
         return $this->options['options'];
     }
     if (!$this->model) {
         $this->model = new waCountryModel();
     }
     if ($id) {
         if (!($result = $this->model->name($id))) {
             throw new Exception('Unknown country ISO-3 code: ' . $id);
         }
         return $result;
     }
     $result = $this->model->all();
     foreach ($result as &$row) {
         $row = $row['name'];
     }
     // Config option to show subset of countries only
     if (isset($this->options['iso_codes']) && is_array($this->options['iso_codes'])) {
         $result = array_intersect_key($result, array_fill_keys($this->options['iso_codes'], true));
     }
     return $result;
 }
 public function execute()
 {
     if (waRequest::post()) {
         $app_settings = new waAppSettingsModel();
         foreach ($this->getData() as $name => $value) {
             $app_settings->set('shop', $name, $value);
         }
         $sms = waRequest::post('sms', array());
         $path = $this->getConfig()->getPath('config', 'sms');
         $save = array();
         foreach ($sms as $s) {
             $from = $s['from'];
             $adapter = $s['adapter'];
             unset($s['from']);
             unset($s['adapter']);
             $empty = true;
             foreach ($s as $v) {
                 if ($v) {
                     $empty = false;
                     break;
                 }
             }
             if (!$empty) {
                 if (!$from) {
                     $from = '*';
                 }
                 foreach (explode("\n", $from) as $from) {
                     $from = trim($from);
                     $save[$from] = $s;
                     $save[$from]['adapter'] = $adapter;
                 }
             }
         }
         waUtils::varExportToFile($save, $path);
     }
     $cm = new waCountryModel();
     $this->view->assign('countries', $cm->all());
     $this->view->assign($this->getConfig()->getGeneralSettings());
     $workhours = wa()->getSetting('workhours', null);
     if ($workhours) {
         $workhours = json_decode($workhours, true);
     }
     $this->view->assign('workhours', $workhours);
     $sms_adapters = $this->getSMSAdapters();
     $this->view->assign('sms_adapters', $sms_adapters);
     $this->view->assign('saved', waRequest::post());
 }
    /**
     *
     * 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 searchPrepare($query, $auto_title = true)
 {
     if ($auto_title || !isset($this->alias_index['data'])) {
         $this->alias_index['data'] = 0;
     }
     //$query = urldecode($query);   // sometime this urldecode broke query, better make urldecode (if needed) outside the searchPrepare
     // `&` can be escaped in search request. Need to split by not escaped ones only.
     $escapedBS = 'ESCAPED_BACKSLASH';
     while (FALSE !== strpos($query, $escapedBS)) {
         $escapedBS .= rand(0, 9);
     }
     $escapedAmp = 'ESCAPED_AMPERSAND';
     while (FALSE !== strpos($query, $escapedAmp)) {
         $escapedAmp .= rand(0, 9);
     }
     $query = str_replace('\\&', $escapedAmp, str_replace('\\\\', $escapedBS, $query));
     $query = explode('&', $query);
     $model = $this->getModel();
     $title = array();
     foreach ($query as $part) {
         if (!($part = trim($part))) {
             continue;
         }
         $part = str_replace(array($escapedBS, $escapedAmp), array('\\', '&'), $part);
         $parts = preg_split("/(\\\$=|\\^=|\\*=|==|!=|>=|<=|=|>|<|@=)/uis", $part, 2, PREG_SPLIT_DELIM_CAPTURE);
         if ($parts) {
             if ($parts[0] === 'name' && $parts[1] === '*=') {
                 $t_a = preg_split("/\\s+/", $parts[2]);
                 $cond = array();
                 foreach ($t_a as $t) {
                     $t = trim($t);
                     if ($t) {
                         $t = $model->escape($t, 'like');
                         $cond[] = "c.name LIKE '%{$t}%'";
                     }
                 }
                 $this->addWhere(implode(" AND ", $cond));
                 $title[] = _ws('Name') . $parts[1] . $parts[2];
             } else {
                 if ($parts[0] == 'email') {
                     if (!isset($this->joins['email'])) {
                         $this->joins['email'] = array('table' => 'wa_contact_emails', 'alias' => 'e');
                     }
                     $title[] = waContactFields::get($parts[0])->getName() . $parts[1] . $parts[2];
                     $this->where[] = 'e.email' . $this->getExpression($parts[1], $parts[2]);
                 } else {
                     if ($model->fieldExists($parts[0])) {
                         if ($f = waContactFields::get($parts[0])) {
                             $title[] = $f->getName() . $parts[1] . $parts[2];
                         } else {
                             $title[] = $parts[0] . $parts[1] . $parts[2];
                         }
                         $this->where[] = 'c.' . $parts[0] . $this->getExpression($parts[1], $parts[2]);
                     } else {
                         if ($parts[0] == 'category') {
                             if (!isset($this->joins['categories'])) {
                                 $this->joins['categories'] = array('table' => 'wa_contact_categories', 'alias' => 'cc');
                             }
                             $title[] = _ws('Category') . $parts[1] . $parts[2];
                             $this->where[] = 'cc.category_id' . $this->getExpression($parts[1], $parts[2]);
                         } else {
                             $field_parts = explode('.', $parts[0]);
                             $f = $field_parts[0];
                             if ($fo = waContactFields::get($f)) {
                                 $title[] = $fo->getName() . $parts[1] . $parts[2];
                             }
                             $ext = isset($field_parts[1]) ? $field_parts[1] : null;
                             $on = ":table.contact_id = c.id AND :table.field = '" . $model->escape($f) . "'";
                             $this->where_fields[] = $f;
                             $op = $parts[1];
                             $term = $parts[2];
                             if ($f === 'address:country') {
                                 $al1 = $this->addJoin('wa_contact_data', $on);
                                 $whr = "{$al1}.value " . $this->getExpression($op, $term);
                                 if ($ext !== null) {
                                     $whr .= " AND {$al1}.ext = '" . $model->escape($ext) . "'";
                                     $whr = "({$whr})";
                                 }
                                 // search by l18n name of countries
                                 if ($op === '*=') {
                                     if (wa()->getLocale() === 'en_US') {
                                         $al2 = $this->addLeftJoin('wa_country', ":table.iso3letter = {$al1}.value");
                                         $whr .= " OR {$al2}.name " . $this->getExpression($parts[1], $parts[2]);
                                     } else {
                                         if (wa()->getLocale() !== 'en_US') {
                                             $iso3letters = array();
                                             $country_model = new waCountryModel();
                                             $countries = $country_model->all();
                                             $term = mb_strtolower($term);
                                             foreach ($countries as &$cntr) {
                                                 if (mb_strpos(mb_strtolower($cntr['name']), $term) === 0) {
                                                     $iso3letters[] = $cntr['iso3letter'];
                                                 }
                                             }
                                             unset($cntr);
                                             if ($iso3letters) {
                                                 $al2 = $this->addLeftJoin('wa_country', ":table.iso3letter = {$al1}.value");
                                                 $whr .= " OR {$al2}.iso3letter IN ('" . implode("','", $iso3letters) . "')";
                                             }
                                         }
                                     }
                                 }
                                 $this->addWhere($whr);
                             } else {
                                 if ($f === 'address:region') {
                                     if (strpos($term, ":") !== false) {
                                         // country_code : region_code - search by country code AND region code AND only in wa_region
                                         $term = explode(":", $term);
                                         $country_iso3 = $model->escape($term[0]);
                                         $code = $model->escape($term[1]);
                                         $al1 = $this->addJoin('wa_contact_data', $on);
                                         $whr = array();
                                         if ($ext !== null) {
                                             $whr[] = "{$al1}.ext = '" . $model->escape($ext) . "'";
                                         }
                                         $al2 = $this->addJoin('wa_contact_data', ":table.contact_id = c.id AND :table.field = 'address:country'");
                                         $al3 = $this->addJoin('wa_region', ":table.code = {$al1}.value AND :table.country_iso3 = {$al2}.value");
                                         $whr[] = "{$al3}.country_iso3 = '{$country_iso3}'";
                                         $whr[] = "{$al3}.code = '{$code}'";
                                         $whr = implode(" AND ", $whr);
                                     } else {
                                         $al1 = $this->addJoin('wa_contact_data', $on);
                                         $whr = "{$al1}.value" . $this->getExpression($op, $term);
                                         if ($ext !== null) {
                                             $whr .= " AND {$al1}.ext = '" . $model->escape($ext) . "'";
                                             $whr = "({$whr})";
                                         }
                                         if ($op === "*=") {
                                             // if search by like, search by wa_region.name but taking into account country
                                             $al2 = $this->addJoin('wa_contact_data', ":table.contact_id = c.id AND :table.field = 'address:country'");
                                             $al3 = $this->addLeftJoin('wa_region', ":table.code = {$al1}.value AND :table.country_iso3 = {$al2}.value");
                                             $whr .= " OR {$al3}.name " . $this->getExpression($op, $term);
                                         }
                                     }
                                     $this->addWhere($whr);
                                 } else {
                                     $on .= ' AND :table.value ' . $this->getExpression($op, $term);
                                     if ($ext !== null) {
                                         $on .= " AND :table.ext = '" . $model->escape($ext) . "'";
                                     }
                                     $this->addJoin('wa_contact_data', $on);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($title) {
         $title = implode(', ', $title);
         // Strip slashes from search title.
         $bs = '\\\\';
         $title = preg_replace("~{$bs}(_|%|&|{$bs})~", '\\1', $title);
     }
     if ($auto_title && $title) {
         $this->addTitle($title, ' ');
     }
 }
 protected function getCountryList()
 {
     $cm = new waCountryModel();
     return $cm->all();
 }