yf_locale_editor_user_vars admin module

Fileplugins/locale/admin_modules/locale_editor/yf_locale_editor_user_vars.class.php
Useadmin URL ?object=locale_editor_user_vars

Public properties

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

PropertyDefaultDescription
$VARS_IGNORE_CASE null
$_parent null

Public methods 5

public function user_var_delete()

Delete user var.

    public function user_var_delete()
    {
        $_GET['id'] = (int) ($_GET['id'] ?? 0);
        if ($_GET['id']) {
            db()->query('DELETE FROM ' . db('locale_user_tr') . ' WHERE id=' . (int) ($_GET['id']));
        }
        // Return user back
        if ( ! empty($_POST['ajax_mode'])) {
            main()->NO_GRAPHICS = true;
            echo $_GET['id'] ?? null;
        } else {
            return js_redirect('./?object=' . ($_GET['object'] ?? null) . '&action=user_vars' . _add_get());
        }
    }

user_var_edit

public function user_var_edit()

Edit user var.

    public function user_var_edit()
    {
        $_GET['id'] = (int) ($_GET['id'] ?? 0);
        $A = db()->query_fetch('SELECT * FROM ' . db('locale_user_tr') . ' WHERE id=' . (int) ($_GET['id']));
        if ( ! $A) {
            return _e('No id');
        }
        $error = '';
        if (main()->is_post()) {
            $new_name = (string) ($_POST['name'] ?? '');
            if ( ! strlen(trim($new_name))) {
                $error = _e('Empty var name');
            } elseif (db()->query_fetch('SELECT id FROM ' . db('locale_user_tr')
                . ' WHERE user_id=' . (int) $A['user_id'] . ' AND locale="' . _es($A['locale']) . '" AND name="' . _es($new_name) . '" AND id!=' . (int) $A['id'])
            ) {
                $error = _e('Such var already exists for this user and locale');
            } else {
                db()->update_safe('locale_user_tr', [
                    'name' => $new_name,
                    'translation' => (string) ($_POST['translation'] ?? ''),
                    'last_update' => time(),
                ], 'id=' . (int) ($_GET['id']));
                return js_redirect('./?object=' . $_GET['object'] . '&action=user_vars');
            }
        }
        $DATA = my_array_merge($A, $_POST);

        $replace = [
            'form_action' => './?object=' . $_GET['object'] . '&action=' . $_GET['action'] . ( ! empty($_GET['id']) ? '&id=' . (int) $_GET['id'] : ''),
            'back_url' => process_url('./?object=' . $_GET['object'] . '&action=user_vars'),
            'error' => $error,
            'csrf_token' => _class('csrf_guard')->session_token(),
            'for_edit' => 1,
            'id' => (int) $DATA['id'],
            'user_id' => (int) $DATA['user_id'],
            'name' => _htmlchars($DATA['name']),
            'translation' => _htmlchars($DATA['translation']),
            'locale' => _htmlchars($DATA['locale']),
            'site_id' => (int) $DATA['site_id'],
        ];
        return tpl()->parse(($_GET['object'] ?? null) . '/user_vars_edit', $replace);
    }

user_var_push

public function user_var_push($FORCE_ID = false)

Push user var into main traslation table.

    public function user_var_push($FORCE_ID = false)
    {
        $_GET['id'] = (int) ($FORCE_ID ? $FORCE_ID : ($_GET['id'] ?? null));
        $A = db()->query_fetch('SELECT * FROM ' . db('locale_user_tr') . ' WHERE id=' . (int) ($_GET['id'] ?? null));
        if ( ! $A) {
            return _e('No id');
        }
        // Same normalization as the "exists globally" check of the list
        $VAR_NAME = _strtolower(str_replace(' ', '_', $A['name']));
        if ( ! strlen($VAR_NAME)) {
            return _e('Empty var name');
        }
        $CUR_LOCALE = $A['locale'];
        if ( ! $CUR_LOCALE) {
            return _e('Empty var locale');
        }
        $EDITED_VALUE = $A['translation'];
        if ( ! strlen($EDITED_VALUE)) {
            return _e('Empty var translation');
        }
        // Get main translation var (if exists)
        $var_info = db()->query_fetch('SELECT * FROM ' . db('locale_vars') . ' WHERE value="' . _es($VAR_NAME) . '"');
        if ( ! $var_info) {
            $var_info = [
                'value' => $VAR_NAME,
                'location' => '',
            ];
            db()->insert_safe('locale_vars', $var_info);
            $var_id = db()->insert_id();
            if ($var_id) {
                $var_info['id'] = $var_id;
            }
        }
        if ( ! $var_info['id']) {
            return _e('No locale var id');
        }
        $sql_data = [
            'var_id' => (int) ($var_info['id']),
            'value' => $EDITED_VALUE,
            'locale' => $CUR_LOCALE,
        ];
        // Get translation for the current locale
        $var_tr = [];
        $Q = db()->query('SELECT * FROM ' . db('locale_translate') . ' WHERE var_id=' . (int) ($var_info['id']));
        while ($A = db()->fetch_assoc($Q)) {
            $var_tr[$A['locale']] = $A['value'];
        }
        if (isset($var_tr[$CUR_LOCALE])) {
            db()->update_safe('locale_translate', $sql_data, 'var_id=' . (int) ($var_info['id']) . ' AND locale="' . _es($CUR_LOCALE) . '"');
        } else {
            db()->insert_safe('locale_translate', $sql_data);
        }
        return $FORCE_ID ? '' : js_redirect('./?object=' . ($_GET['object'] ?? null) . '&action=user_vars');
    }

user_vars

public function user_vars()

Display list of user-specific vars.

    public function user_vars()
    {
        $filter_sql = $i = null;
        if (isset($_GET['id']) && ! isset($_GET['page'])) {
            $_GET['page'] = ($_GET['id'] ?? null);
            $_GET['id'] = null;
        }
        // Group actions here
        if (main()->is_post()) {
            if (isset($_POST['multi-push'])) {
                foreach ((array) ($_POST['items'] ?? null) as $_id) {
                    $_id = (int) $_id;
                    if ( ! empty($_id)) {
                        $this->user_var_push($_id);
                    }
                }
            }
            if (isset($_POST['multi-delete'])) {
                foreach ((array) $_POST['items'] as $_id) {
                    $_id = (int) $_id;
                    if ( ! empty($_id)) {
                        db()->query('DELETE FROM ' . db('locale_user_tr') . ' WHERE id=' . $_id);
                    }
                }
            }
            return js_redirect('./?object=' . $_GET['object'] . '&action=user_vars' . _add_get());
        }

        $sql = 'SELECT * FROM ' . db('locale_user_tr') . ' ORDER BY user_id DESC, name ASC';

        list($add_sql, $pages, $total) = common()->divide_pages($sql, '', '', 100);

        $data = $items = $users_ids = $users_names = $vars_names = [];
        $Q = db()->query($sql . $add_sql);
        while ($A = db()->fetch_assoc($Q)) {
            $data[$A['id']] = $A;
            if ($A['user_id']) {
                $users_ids[$A['user_id']] = (int) ($A['user_id']);
            }
            if (strlen($A['name'])) {
                $vars_names[$A['name']] = $A['name'];
            }
        }
        if ( ! empty($users_ids)) {
            $Q = db()->query('SELECT * FROM ' . db('user') . ' WHERE id IN(' . implode(',', $users_ids) . ')');
            while ($A = db()->fetch_assoc($Q)) {
                $users_names[$A['id']] = $A['email'];
            }
        }
        // Check if var exists in the global table
        $global_vars = [];
        if ( ! empty($vars_names)) {
            $names_sql = [];
            foreach ($vars_names as $_name) {
                $names_sql[] = '"' . _es(_strtolower(str_replace(' ', '_', $_name))) . '"';
            }
            foreach ((array) db()->query_fetch_all('SELECT * FROM ' . db('locale_vars') . ' WHERE value IN(' . implode(',', $names_sql) . ')') as $A) {
                $global_vars[_strtolower(str_replace(' ', '_', $A['value']))] = $A['id'];
            }
        }

        $color_exists = '#ff5';
        $i = 0;

        foreach ((array) $data as $A) {
            $var_bg_color = '';
            $global_var_exists = isset($global_vars[_strtolower(str_replace(' ', '_', $A['name']))]);
            if ($global_var_exists) {
                $var_bg_color = $color_exists;
            }
            $items[] = [
                'bg_class' => $i++ % 2 ? 'bg1' : 'bg2',
                'id' => (int) ($A['id']),
                'user_id' => (int) ($A['user_id']),
                'user_name' => _prepare_html($users_names[$A['user_id']] ?? ''),
                'user_link' => _profile_link($A['user_id']),
                'name' => _prepare_html(str_replace('_', ' ', $A['name'])),
                'translation' => _prepare_html($A['translation']),
                'locale' => _prepare_html($A['locale']),
                'site_id' => (int) ($A['site_id']),
                'last_update' => _format_date($A['last_update'], 'long'),
                'global_exists' => (int) $global_var_exists,
                'var_bg_color' => $var_bg_color,
                'edit_url' => './?object=' . $_GET['object'] . '&action=user_var_edit&id=' . $A['id'],
                'delete_url' => './?object=' . $_GET['object'] . '&action=user_var_delete&id=' . $A['id'],
                'push_url' => './?object=' . $_GET['object'] . '&action=user_var_push&id=' . $A['id'],
            ];
        }
        $replace = [
            'form_action' => './?object=' . $_GET['object'] . '&action=' . $_GET['action'] . ( ! empty($_GET['id']) ? '&id=' . (int) $_GET['id'] : ''),
            'error' => _e(),
            'items' => $items,
            'pages' => $pages,
            'total' => $total,
            'show_vars_link' => './?object=' . $_GET['object'] . '&action=show_vars',
            'csrf_token' => _class('csrf_guard')->session_token(),
        ];
        return tpl()->parse($_GET['object'] . '/user_vars_main', $replace);
    }

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

_init

public function _init()
    public function _init()
    {
        $this->_parent = module('locale_editor');
    }