yf_manage_shop_novaposhta_ua admin module

todo: map, geocode src: http://novaposhta.ua/map/index/ua city: Avdiivka (654) map page: /map/citymap?id=654 map iframe: http://maps.novaposhta.ua/index.php?...

Fileplugins/shop/admin_modules/manage_shop/yf_manage_shop_novaposhta_ua.class.php
Useadmin URL ?object=manage_shop_novaposhta_ua

Public properties

A site sets them in its config with $PROJECT_CONF['manage_shop_novaposhta_ua']['NAME'] = ...;

PropertyDefaultDescription
$is_init null
$is_post null

Public methods 7

novaposhta_ua

public function novaposhta_ua()
    public function novaposhta_ua()
    {
        $result = '';
        if ($this->is_post) {
            $is_import = ($_POST['import'] ?? null) && ($_POST['confirm'] ?? null);
            if ($is_import) {
                $result .= $this->novaposhta_ua__import();
            }
        }
        $form = $this->_form($result);
        return  $form;
    }
public function novaposhta_ua__import()
    public function novaposhta_ua__import()
    {
        // get data
        // *** remote
        $url = 'http://novaposhta.ua/shop/office/getJsonWarehouseList/';
        $content = common()->get_remote_page($url);
        $data = json_decode($content, true);
        if (empty($data['response'])) {
            return  'No data found at URL: ' . $url;
        }
        $count = 0;
        $sql_data = [];
        foreach ($data['response'] as $i => $item) {
            $count++;
            $city_raw = ($item['cityRu'] ?? null) ?: '';
            $tel_raw = ($item['phone'] ?? null) ?: '';
            $address_raw = ($item['addressRu'] ?? null) ?: '';
            $city = $this->_cleanup_city($city_raw);
            $tel = $this->_cleanup_tel($tel_raw);
            list($address, $branch_no, $info) = $this->_cleanup_address($address_raw);
            $address = trim($address);
            $branch_no = (int) $branch_no;
            $info = trim($info);
            // add city
            $location = empty($city) ? '' : $city . ', ';
            // add branch
            $location .= 'Отделение' . ($branch_no > 0 ? ' №' . $branch_no : '');
            // add ':'
            $location .= empty($address) && empty($info) ? '' : ': ';
            // add address
            $location .= empty($address) ? '' : $address;
            // add info
            $location .= empty($info) ? '' : " ($info)";
            $sql_data[] = _es([
                'city_raw' => $city_raw,
                'address_raw' => $address_raw,
                'tel_raw' => $tel_raw,
                'city' => $city,
                'branch_no' => $branch_no,
                'address' => $address,
                'info' => $info,
                'location' => $location,
                'tel' => $tel,
                'options' => json_encode($item, JSON_NUMERIC_CHECK),
            ]);
        }
        $table_name = db('shop_novaposhta_ua');
        $count_in_db = (int) db()->get_one("SELECT COUNT(*) FROM $table_name");
        $sql_result = db()->insert_on_duplicate_key_update($table_name, $sql_data);
        $table_result = [
            ['title' => 'Processed: ', 'count' => $count],
            ['title' => 'Added: ', 'count' => $count - $count_in_db],
        ];
        $result = table($table_result, ['no_total' => true])
        ->text('title', 'Operation')
        ->text('count', 'Quantity');
        return  $result;
    }

Methods starting with _ hooks and helpers; not routable as page actions

public function _cleanup_address($data)
    public function _cleanup_address($data)
    {
        $address = null;
        $branch_no = 1;
        $info = null;
        if (empty($data)) {
            return  [$address, $branch_no, $info];
        }
        // info
        if (preg_match_all('#([^\(\)]*)\(([^\)]+)\)([^\(\)]*)#', $data, $match)) {
            $info = implode('; ', array_reverse($match[2]));
            $data = implode('', $match[1]) . implode('', $match[3]);
        }
        // branch_no
        if (preg_match('#^[^\d]+(\d*)\s*:(.*)$#', $data, $match)
            || preg_match('#^[^\d]+N\s*(\d*)\,*(.*)$#', $data, $match)
            || preg_match('#^[^\d]+№\s*(\d*)\s*\,*(.*)$#', $data, $match)) {
            $branch_no = (int) $match[1];
            $branch_no = $branch_no ?: 1;
            $data = $match[2];
        }
        // cleanup
        $data = preg_replace(['#Відділення#'], '', $data);
        $filter = [
            '#\s*Відділення\s*#' => '',
            '#Отделение,\s*#' => '',
            '#Отделение[^\,]\,\s*#' => '',
            '#^\s*:\s*#' => '',
        ];
        // add info
        if (preg_match('#^\s*[:,]\s*([^:]+)\s*[:]\s*(.+)$#', $data, $match)) {
            $info = $match[1] . (empty($info) ? '' : '; ' . $info);
            $data = $match[2];
        }
        $data = preg_replace(array_keys($filter), array_values($filter), $data);
        $address = trim($data);
        $result = [$address, $branch_no, $info];
        return  $result;
    }

_cleanup_city

public function _cleanup_city($data)
    public function _cleanup_city($data)
    {
        if (empty($data)) {
            return  $data;
        }
        $result = preg_replace(['#([^\s])\(#'], ['\1 ('], $data);
        return  $result;
    }

_cleanup_tel

public function _cleanup_tel($data)
    public function _cleanup_tel($data)
    {
        if (empty($data)) {
            return  $data;
        }
        $filter = [
            '#(\d+)\-(\d+\-\d+\-\d+)#' => '(\1) \2',
            '#\)(\d)#' => ') \1',
            '#\)\-#' => ') ',
            '#^(\d+\))#' => '(\1',
            '#[^\d]+$#' => '',
        ];
        $result = preg_replace(array_keys($filter), array_values($filter), $data);
        return  $result;
    }

_form

public function _form($data)
    public function _form($data)
    {
        $data_desc = $data ? 'Result' : '';
        $replace = [];
        $_form = form($replace)
            ->row_start(['desc' => ''])
                ->submit('import', 'Import branch list')
                ->check_box('confirm', false, ['desc' => 'confirmation', 'no_label' => true])
            ->row_end()
            ->container($data, $data_desc);
        return  $_form;
    }

_init

public function _init()
    public function _init()
    {
        $this->_class_price = _class('_shop_price', 'modules/shop/');
        $this->is_post = main()->is_post();
        $this->is_init = (int) main()->_get('init');
    }