yf_manage_tips admin module

Fileplugins/content/admin_modules/yf_manage_tips.class.php
Useadmin URL ?object=manage_tips

Public properties

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

PropertyDefaultDescription
$lang_def_country null
$pages_langs null

Constants

table'tips'

Public methods 8

active

public function active()
    public function active()
    {
        return _class('admin_methods')->active(['table' => self::table, 'revisions' => true]);
    }

add

public function add()
    public function add()
    {
        $a = [];
        $a['back_link'] = url('/@object');
        ! $a['locale'] && $a['locale'] = conf('language');
        $_this = $this;
        return form((array) $_POST + (array) $a)
            ->validate([
                '__before__' => 'trim',
                'name' => 'required',
                'text' => 'required',
                'locale' => 'required',
            ])
            ->insert_if_ok(self::table, ['name', 'text', 'active', 'locale'])
            ->on_after_update(function () use ($_this) {
                $id = db()->insert_id();
                module_safe('manage_revisions')->add($_this::table, $id, 'add');
                js_redirect(url('/@object/edit/' . $id));
            })
            ->text('name')
            ->textarea('text', ['id' => 'text', 'cols' => 200, 'rows' => 10, 'ckeditor' => ['config' => _class('admin_methods')->_get_cke_config()]])
            ->locale_box('locale')
            ->active_box()
            ->save_and_back();
    }

clone_item

public function clone_item()
    public function clone_item()
    {
        return _class('admin_methods')->clone_item(['table' => self::table, 'revisions' => true]);
    }

delete

public function delete()
    public function delete()
    {
        return _class('admin_methods')->delete(['table' => self::table, 'revisions' => true]);
    }

edit

public function edit()
    public function edit()
    {
        $a = $this->_get_info();
        if ( ! $a) {
            return _404();
        }
        $a['back_link'] = url('/@object');
        $_this = $this;
        return form((array) $_POST + (array) $a)
            ->validate([
                '__before__' => 'trim',
                'name' => 'required',
                'text' => 'required',
            ])
            ->update_if_ok(self::table, ['name', 'text', 'active', 'locale'])
            ->on_before_update(function () use ($a, $_this) {
                module_safe('manage_revisions')->add([
                    'object_name' => $_this::table,
                    'object_id' => $a['id'],
                    'old' => $a,
                    'new' => $_POST,
                    'action' => 'update',
                ]);
            })
            ->container($this->_get_lang_links($a['locale'], $a['name'], 'edit'))
            ->text('name')
            ->textarea('text', ['id' => 'text', 'cols' => 200, 'rows' => 10, 'ckeditor' => ['config' => _class('admin_methods')->_get_cke_config()]])
            ->active_box()
            ->save_and_back();
    }

show

public function show()
    public function show()
    {
        $data = db()->from(self::table)->order_by('name ASC, locale ASC')->get_all();
        foreach ((array) $data as $k => $v) {
            $data[$k]['text'] = strip_tags($v['text']);
        }
        return table($data, [
                'condensed' => true,
                'pager_records_on_page' => 1000,
                'group_by' => 'name',
            ])
            ->text('name', ['link' => url('/@object/edit/%id')])
            ->lang('locale')
            ->text('text')
            ->btn_edit(['no_ajax' => 1, 'btn_no_text' => 1])
            ->btn_delete(['btn_no_text' => 1])
            ->btn_active()
            ->header_add(['no_ajax' => 1]);
    }

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

_get_info

public function _get_info($id = null, $lang = null)
    public function _get_info($id = null, $lang = null)
    {
        $locale = null;
        $id = isset($id) ? $id : ($_GET['id'] ?? null);
        $lang = isset($lang) ? $lang : ($_GET['page'] ?? null);
        $a = db()->from(self::table)
            ->where('locale', $lang ? strtolower($lang) : '')
            ->where('name', _strtolower(urldecode($id)))
            ->or_where('id', (int) $id)
            ->get();
        if ($a) {
            return $a;
        } elseif ($lang) {
            $all_langs = main()->get_data('locale_langs');
            if ( ! isset($all_langs[$lang])) {
                return false;
            }
            // Try with first lang as fallback
            $a = db()->from(self::table)
                ->where('name', _strtolower(urldecode($id)))
                ->or_where('id', (int) $id)
                ->get();
            // Create missing page
            if ($a && $a['locale'] && $lang !== $a['locale']) {
                $new = $a;
                unset($new['id']);
                $new['active'] = 0;
                $new['locale'] = $lang;
                db()->insert_safe(self::table, $new);
                $new['id'] = db()->insert_id();
                return $new;
            }
            return $a;
        }
        return false;
    }