yf_manage_shop_price_update admin module

Fileplugins/shop/admin_modules/manage_shop/yf_manage_shop_price_update.class.php
Useadmin URL ?object=manage_shop_price_update

Public properties

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

PropertyDefaultDescription
$is_init null
$is_post null

Public methods 4

public function products_price_update()
    public function products_price_update()
    {
        $data = $this->_data();
        $form = $this->_form($data);
        return  $form;
    }

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

_data

public function _data()
    public function _data()
    {
        $_class_price = $this->_class_price;
        // init sql
        $sql_table = db('shop_products');
        $sql_table_t = $sql_table . '_tmp';
        // prepare filter
        list($_where, $_order) = _class('table2_filter', 'classes/table2/')->_filter_sql_prepare($this->_filter, $this->_filter_params);
        // compile sql
        $sql_filter = $this->_class_admin_products->_sql([
            'fields' => 'DISTINCT p.*',
            'where' => 1 . $_where,
            'order' => $_order,
        ]);
        $sql_count = $this->_class_admin_products->_sql([
            'fields' => 'COUNT(p.id)',
            'where' => 1 . $_where,
            'order' => $_order,
        ]);
        $count = db()->get_one($sql_count);
        // build temp data
        // prepare percent
        $_percent = $_class_price->_number_float($_POST['percent'] ?? null);
        $percent = 1 + $_percent / 100;
        $percent = $_class_price->_number_mysql($percent, 4);
        // prepare add
        $_add = $_class_price->_number_float($_POST['add'] ?? null);
        $add = $_class_price->_number_mysql($_add);
        // prepare to field
        $_fields = $this->_fields_update;
        $to_field = ($_POST['to_field'] ?? null);
        // to field error
        if ( ! empty($to_field) && ! isset($_fields[$to_field])) {
            return  js_redirect('./', true, 'error to field');
        }
        $apply = ($_POST['apply'] ?? null);
        $confirm = ($_POST['confirm'] ?? null);
        $is_update = isset($_fields[$to_field]) && isset($apply) && isset($confirm) ? true : false;
        $to_field = $to_field ?: $_fields['price_raw'];
        $sql_price_update = "$to_field = ( IF( price_raw > 0, price_raw, price ) * $percent + $add )";
        $css_field[$to_field] = 'text-success';
        $limit = 5;
        // preview
        $sql_fields = implode(', ', $this->_fields_show);
        db_query("CREATE TEMPORARY TABLE $sql_table_t LIKE $sql_table");
        db_query("INSERT INTO $sql_table_t $sql_filter LIMIT $limit");
        db_query("UPDATE $sql_table_t SET $sql_price_update LIMIT $limit");
        $result = db_get_all("SELECT $sql_fields FROM $sql_table_t as p $_order LIMIT $limit");
        $result_t = table($result, ['no_total' => true])
            ->text('name')
            ->text('price_raw', ['class' => $css_field['price_raw']])
            ->text('price', ['class' => $css_field['price']])
            ->text('old_price', ['class' => $css_field['old_price']]);
        $result_t = _class('html')->panel(['title' => 'Preview', 'body' => $result_t]);
        $result = [$count, $result_t];
        // apply
        if ($is_update) {
            db_query("UPDATE $sql_table as p"
                . ' LEFT JOIN ' . db('shop_product_to_region') . ' AS pr ON pr.product_id = p.id'
                . " SET $sql_price_update WHERE 1 $_where");
            $info = '';
            if ( ! empty($this->_filter)) {
                foreach ($this->_filter as $key => $value) {
                    if (is_array($value)) {
                        $value = implode(', ', $value);
                    }
                    if (strlen($value) < 1) {
                        continue;
                    }
                    $info .= "$key: $value; ";
                }
                $info = " ( $info )";
            }
            common()->admin_wall_add(["shop price update: percent = $_percent%; add = $_add" . $_class_price->CURRENCY . $info]);
        }
        return  $result;
    }

_form

public function _form($data)
    public function _form($data)
    {
        $_class_price = $this->_class_price;
        list($total, $preview) = $data;
        $replace = [
            '_all' => $total,
            'percent' => $_class_price->_number_format($_POST['percent'] ?? null),
            'add' => $_class_price->_number_format($_POST['add'] ?? null),
        ];
        $to_field = $this->_fields_update;
        // create form
        $link_back = './?object=manage_shop&action=products';
        $_form = form($replace)
            ->row_start(['desc' => 'Total selected'])
                ->info('_all')
                ->link('Back', $link_back, ['title' => 'Back to product filter', 'icon' => 'fa fa-arrow-circle-left'])
            ->row_end()
            ->number('percent', 'Price +/-, %')
            ->number('add', 'Price +/-, ' . $_class_price->CURRENCY)
            ->select_box('to_field', $to_field, [
                'selected' => ($_POST['to_field'] ?? null),
                'translate' => true,
                'desc' => 'Apply to field',
                'tip' => 'the price is taken from the "' . t('price_raw') . '" field and applied to this field',
            ])
            ->submit('preview', 'Preview')
            ->row_start(['desc' => ''])
                ->submit('apply', 'Apply')
                ->check_box('confirm', false, ['desc' => 'confirmation', 'no_label' => true])
            ->row_end();
        return  $_form . $preview;
    }

_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 = main()->is_post();
        $this->is_init = (int) main()->_get('init');
        // get filter
        $_object = main()->_get('object');
        $_action = main()->_get('action');
        $_action_parent = main()->_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;
    }