yf_manage_shop_import_products2 admin module

Fileplugins/shop/admin_modules/manage_shop/yf_manage_shop_import_products2.class.php
Useadmin URL ?object=manage_shop_import_products2

Public properties

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

PropertyDefaultDescription
$cache []
$import_action ['update' => 'update', 'insert' => 'insert']
$import_action_default 'update'
$import_field
[0 => 'do not use (0)', 'id' => 'identifier (id)', 'name' => 'name (name)', 'description' => 'description (description)', 'price' => 'price (price)', 'price_raw' => 'cost price (price_raw)', ... (13 items)]
array (
  0 => 'do not use (0)',
  'id' => 'identifier (id)',
  'name' => 'name (name)',
  'description' => 'description (description)',
  'price' => 'price (price)',
  'price_raw' => 'cost price (price_raw)',
  'articul' => 'SKU (articul)',
  'cat_id' => 'category: identifier (cat_id)',
  'category_name' => 'category: name (category_name)',
  'manufacturer_id' => 'manufacturer: identifier (manufacturer_id)',
  'manufacturer_name' => 'manufacturer: name (manufacturer_name)',
  'supplier_id' => 'supplier: identifier (supplier_id)',
  'supplier_name' => 'supplier: name (supplier_name)',
)
$import_field_autodetect
['id' => '(идентификатор)|(id)', 'name' => '(имя)|(название)|(наименование)|(name)', 'description' => '(описание)|(description)', 'price' => '(цена)|(price)', 'price_raw' => '(себестоимость)|(price_raw)', 'articul' => '(артикул)|(articul)', ... (12 items)]
array (
  'id' => '(идентификатор)|(id)',
  'name' => '(имя)|(название)|(наименование)|(name)',
  'description' => '(описание)|(description)',
  'price' => '(цена)|(price)',
  'price_raw' => '(себестоимость)|(price_raw)',
  'articul' => '(артикул)|(articul)',
  'cat_id' => '(категория:?\\s*идентификатор)|(cat_id)',
  'category_name' => '(категория)|(категория:?\\s*название)|(category_name)',
  'manufacturer_id' => '(производитель:?\\s*идентификатор)|(manufacturer_id)',
  'manufacturer_name' => '(производитель)|(производитель:?\\s*название)|(manufacturer_name)',
  'supplier_id' => '(поставщик:?\\s*идентификатор)|(supplier_id)',
  'supplier_name' => '(поставщик)|(поставщик:?\\s*название)|(supplier_name)',
)
$import_rules
['insert' => ['key' => [...], 'exclude' => [...], 'include' => [...]], 'update' => ['key' => [...]]]
array (
  'insert' => 
  array (
    'key' => 
    array (
      0 => 
      array (
        0 => 'name',
        1 => 'cat_id',
      ),
      1 => 
      array (
        0 => 'name',
        1 => 'category_name',
      ),
    ),
    'exclude' => 
    array (
      0 => 'id',
    ),
    'include' => 
    array (
      0 => 'name',
      1 => 'cat_id',
      2 => 'manufacturer_id',
      3 => 'manufacturer_name',
      4 => 'supplier_id',
      5 => 'supplier_name',
      6 => 'articul',
      7 => 'external_url',
      8 => 'origin_url',
      9 => 'source',
    ),
  ),
  'update' => 
  array (
    'key' => 
    array (
      0 => 
      array (
        0 => 'id',
      ),
      1 => 
      array (
        0 => 'supplier_id',
        1 => 'articul',
      ),
      2 => 
      array (
        0 => 'supplier_name',
        1 => 'articul',
      ),
      3 => 
      array (
        0 => 'name',
      ),
    ),
  ),
)
$is_init null
$is_post null
$set_cat_id null
$set_cat_name 'shop_cats'
$supplier null
$upload_list null
$upload_list__field ['id', 'file_name', 'file_size', 'time', 'status']
$upload_list__file_name null
$upload_path null
$upload_status ['upload' => 'uploaded', 'import' => 'imported']

Public methods 8

import2

public function import2()
    public function import2()
    {
        if ( ! empty($_GET['api'])) {
            return  $this->_call();
        }
        $data = $this->_data();
        $form = $this->_form($data);
        return  $form;
    }

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

_cache_fetch

public function _cache_fetch()
    public function _cache_fetch()
    {
        $cache = &$this->cache;
        // category
        $items = db()->from('sys_category_items')->where('cat_id', '=', $this->set_cat_id)->order_by('name')->all();
        if ( ! empty($items)) {
            foreach ($items as $id => $item) {
                $name = mb_strtolower($item['name'], 'UTF-8');
                $cache['category']['id'][$id] = &$items[$id];
                $cache['category']['name'][$name][$id] = &$items[$id];
            }
        }
        // supplier
        $items = db()->from('shop_suppliers')->all();
        if ( ! empty($items)) {
            foreach ($items as $id => $item) {
                $name = mb_strtolower($item['name'], 'UTF-8');
                $cache['supplier']['id'][$id] = &$items[$id];
                $cache['supplier']['name'][$name][$id] = &$items[$id];
            }
        }
        // supplier
        $items = db()->from('shop_manufacturers')->all();
        if ( ! empty($items)) {
            foreach ($items as $id => $item) {
                $name = mb_strtolower($item['name'], 'UTF-8');
                $cache['manufacturer']['id'][$id] = &$items[$id];
                $cache['manufacturer']['name'][$name][$id] = &$items[$id];
            }
        }
    }

_data

public function _data()
    public function _data()
    {
        $sub_action_count = null;
        $_sub_action = [
            '0' => '- not selected -',
            'load' => 'load',
            'import' => 'import',
            'delete' => 'delete',
        ];
        $sub_action = ($_POST['sub_action'] ?? null);
        $is_sub_action = $sub_action !== '0' && isset($_sub_action[$sub_action]);
        // -----
        $_supplier = _class('manage_shop')->_suppliers_for_select;
        $supplier = (int) ($_POST['supplier'] ?? null);
        $is_supplier = $supplier != 0 && isset($_supplier[$supplier]);
        // prepare ng-app
        $_ng_controller = 'ctrl.import2';
        $_api_url_upload = url_admin('//manage_shop/import2/&api=upload');
        $_api_url_upload_list = url_admin('//manage_shop/import2/&api=upload_list');
        $_api_url_upload_item = url_admin('//manage_shop/import2/&api=upload_item');
        // -----
        $_ng_data = $this->_data_ng(true);
        // -----
        // update
        $apply = ($_POST['apply'] ?? null);
        $confirm = ($_POST['confirm'] ?? null);
        $is_action = $this->is_post && $is_sub_action && $is_supplier && isset($apply) ? true : false;
        $is_update = $is_action && isset($confirm) ? true : false;
        $no_confirm = $is_action && ! isset($confirm) ? true : false;
        if ($is_update) {
            $sub_action_count = null;
            if ($sub_action == 'add') {
            } elseif ($sub_action == 'delete') {
            }
            if ($sub_action_count) {
                common()->message_success('Operation completed successfully.');
                _class('_shop_region', 'modules/shop/')->_cache_refresh();
            } else {
                common()->message_warning('This operation has already been performed.');
            }
            $sub_action = null;
            $region = null;
        }
        if ($no_confirm) {
            common()->message_warning('Confirmation required.');
        }
        // -----
        $result = [
            '_api_url_upload' => $_api_url_upload,
            '_api_url_upload_list' => $_api_url_upload_list,
            '_api_url_upload_item' => $_api_url_upload_item,
            '_ng_controller' => $_ng_controller,
            '_ng_data' => $_ng_data,
            '_sub_action' => $_sub_action,
            '_supplier' => $_supplier,
            'sub_action' => $sub_action,
            'supplier' => $supplier,
        ];
        return  $result;
    }

_data_ng

public function _data_ng($json = false)
    public function _data_ng($json = false)
    {
        $cache = &$this->cache;
        $_url = [
            'product_view' => url_user(['object' => 'shop',        'action' => 'product',      'id' => 0]),
            'product_edit' => url_admin(['object' => 'manage_shop', 'action' => 'product_edit', 'id' => 0]),
        ];
        $_upload_list = $this->upload_list;
        $_upload_status = $this->upload_status;
        $_import_field = $this->import_field;
        $_import_field_autodetect = $this->import_field_autodetect;
        $_import_action = $this->import_action;
        $_import_action_array = [];
        foreach ($_import_action as $key => $value) {
            $_import_action_array[] = [
                'key' => $key,
                'value' => $value,
            ];
        }
        // supplier
        $_supplier = $cache['supplier']['id'];
        $_supplier_array = [];
        $_supplier_array[] = [
            'id' => null,
            'title' => 'supplier',
        ];
        foreach ($_supplier as $id => $item) {
            $title = $item['name'];
            $_supplier_array[] = [
                'id' => $id,
                'title' => $title . ' (' . $id . ')',
            ];
        }
        // category
        $_category = $cache['category']['id'];
        $_category_array = [];
        $_category_array[] = [
            'id' => null,
            'title' => 'category',
        ];
        foreach ($_category as $id => $item) {
            $title = $item['name'];
            $_category_array[] = [
                'id' => $id,
                'title' => $title . ' (' . $id . ')',
            ];
        }
        $result = [
            '_url' => $_url,
            '_upload_status' => $_upload_status,
            '_upload_list' => $_upload_list,
            '_import_field' => $_import_field,
            '_import_field_autodetect' => $_import_field_autodetect,
            '_import_action' => $_import_action,
            '_import_action_array' => $_import_action_array,
            '_supplier' => $_supplier,
            '_supplier_array' => $_supplier_array,
            '_category' => $_category,
            '_category_array' => $_category_array,
        ];
        if ($json) {
            $result = json_encode($result, JSON_NUMERIC_CHECK);
        }
        return  $result;
    }

_db_exists

public function _db_exists($name, $options = [])
    public function _db_exists($name, $options = [])
    {
        $cache = &$this->cache;
        $_ = $options;
        $key = $_['key'];
        $value = $_['value'];
        if (empty($name)) {
            return  null;
        }
        $cache = &$this->cache[$name];
        $is_cache = is_array($cache[$key][$value]);
        if ($is_cache) {
            $items = $cache[$key][$value];
        } else {
            $_get_options = $options + [
                'name' => $name,
            ];
            if ( ! empty($key) && ! empty($value)) {
                $_get_options['where'][] = [$key => $value];
            }
            $items = $this->_db_get($_get_options);
            // cache
            if ( ! empty($items) && ! empty($key) && ! empty($value)) {
                $value == 'name' && $value = mb_strtolower($value, 'UTF-8');
                $cache[$key][$value] = [];
                foreach ((array) $items as $index => $_item) {
                    if (isset($_item[$key])) {
                        if (isset($_item['id'])) {
                            $id = (int) $_item['id'];
                            $cache['id'][$id] = &$items[$index];
                        } else {
                            $id = $index;
                        }
                        $cache[$key][$value][$id] = &$items[$index];
                    }
                }
            }
        }
        $exists = $items !== null;
        $many = count($items);
        return  [$exists, $many, $items];
    }

_db_get

public function _db_get($options)
    public function _db_get($options)
    {
        $cache = &$this->cache;
        $_ = $options;
        $_name = $_['name'];
        $sql_name = _es($_name);
        if (empty($_name)) {
            return  null;
        }
        $_where = $_['where'];
        $_limit = $_['limit'];
        // prepare where
        $where = [];
        foreach ($_where as $field => $value) {
            list($field, $value) = $this->_field_to_sql($field, $value);
            if ( ! $field) {
                return  null;
            }
            if (isset($value)) {
                $values = (array) $value;
                foreach ($values as $i => $v) {
                    if ( ! is_int($v) && $v !== null) {
                        $values[$i] = "'$v'";
                    }
                }
                $where[] = $field . ' IN( ' . implode(', ', $values) . ' )';
            }
        }
        $sql_where = '';
        if ( ! empty($where)) {
            $sql_where = 'WHERE ' . implode(' AND ', $where);
        }
        // prepare limit
        $sql_limit = '';
        if ($_limit > 0 && (is_string($_limit) || is_int($_limit))) {
            $sql_limit = 'LIMIT ' . $_limit;
        }
        // prepare sql
        $sql = sprintf(
            'SELECT * FROM %s %s %s',
            db($sql_name),
            $sql_where,
            $sql_limit
        );
        $result = db_get_all($sql, '-1');
        return  $result;
    }

_form

public function _form($data)
    public function _form($data)
    {
        // prepare form
        $data = (array) $data;
        $_form_tpl = tpl()->parse('manage_shop/import2__form', $data);
        $_form_ctrl = tpl()->parse('manage_shop/import2__ctrl', $data);
        // create form
        $_form = $_form_ctrl . $_form_tpl;
        return  $_form;
    }

_init

public function _init()
    public function _init()
    {
        $this->_class_price = _class('_shop_price', 'modules/shop/');
        $this->_class_admin_products = _class('manage_shop_products', 'admin_modules/manage_shop/');
        $this->is_post = input()->is_post();
        $this->is_init = (bool) input()->get('init');
        $this->upload_path = PROJECT_PATH . 'uploads/price/';
        $this->upload_list__file_name = $this->upload_path . 'list.csv';
        $this->_load_upload_list();
        // categories
        $this->set_cat_id = _class('cats')->_get_cat_id_by_name($this->set_cat_name);
        // cache fetch
        $this->_cache_fetch();
        // get filter
        $_object = input()->get('object');
        $_action = input()->get('action');
        $_action_parent = input()->get('filter');
        $_session_parent_key = $_object . '__' . $_action_parent;
        $_session_key = $_object . '__' . $_action;
        if ($this->is_init) {
            $_SESSION[$_session_key] = ($_SESSION[$_session_parent_key] ?? null);
        }
        $this->_filter = ($_SESSION[$_session_key] ?? null);
        $this->_filter_params = $this->_class_admin_products->_filter_params;
    }