yf_news user module

Fileplugins/content/modules/yf_news.class.php
Usemodule('news'), URL ?object=news

Public properties

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

PropertyDefaultDescription
$_comments_params
array
['action_name' => 'full', 'object_name' => 'news', 'return_action' => 'full_news', 'action_for_add' => 'add_comment', 'action_for_mark_spam' => 'comments_mark_spam', 'tpl_name' => 'add_comment_for_news'] @conf_skip Params for the comments
$_current null

Constants

table'news'

Public methods 14

add_comment

public function add_comment($params = [])
    public function add_comment($params = [])
    {
        main()->NO_GRAPHICS = 1;

        $id = ($_GET['id'] ?? null);
        $a = $this->_get_info();
        if ( ! $a || ! $a['active']) {
            return _404('Not found');
        }
        $url = url('/@object/' . (($a['url'] ?? null) ?: $a['id']));
        return module('comments')->add((array) $this->_comments_params + [
            'add_form_action' => url('/@object/add_comment/' . $a['id']),
            'return_path' => $url,
            'object_id' => $a['id'],
        ]);
    }
public function comments_mark_spam()
    public function comments_mark_spam()
    {
        main()->NO_GRAPHICS = 1;
        $_GET['ajax_mode'] = 1;
        $params = [
            'object_name' => 'news',
            'action_name' => 'full_news',
        ];
        return module('comments')->mark_spam($params);
    }

show

public function show()
    public function show()
    {
        if ($_GET['id'] ?? null) {
            return $this->_view();
        }
        $num_comments = db()->select('object_id', 'COUNT(*)')->from('comments')->where('object_name', 'news')->group_by('object_id')->get_2d();

        $sql = db()->from(self::table)->where('active', 1)->where('locale', conf('language'))->order_by('add_date', 'desc')->sql();
        list($add_sql, $pages, $total) = common()->divide_pages($sql);
        $items = null;
        foreach ((array) db()->get_all($sql . $add_sql) as $a) {
            $items[$a['id']] = [
                'title' => $a['title'],
                'head_text' => $this->_parse_text_tags($a['head_text']),
                'full_text' => $this->_parse_text_tags($a['full_text']),
                'add_date' => _format_date($a['add_date'], 'long'),
                'full_link' => url('/@object/show/' . (($a['url'] ?? null) ?: $a['id'])),
                'num_comments' => (int) $num_comments[$a['id']],
            ];
        }
        return tpl()->parse('news/main', [
            'items' => $items,
            'pages' => $pages,
            'total' => (int) $total,
        ]);
    }

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

__call

public function __call($name, $args)

Catch missing method call.

    public function __call($name, $args)
    {
        return main()->extend_call($this, $name, $args);
    }
public function _comment_is_allowed($params = [])
    public function _comment_is_allowed($params = [])
    {
        $is_allowed = ($_GET['action'] ?? null) == 'view' ? true : false;
        return $is_allowed;
    }

_get_info

public function _get_info($id = null)
    public function _get_info($id = null)
    {
        $id = $id ?: ($_GET['id'] ?? null);
        $sql = db()->from(self::table)->where('locale', conf('language'));
        if (is_numeric($id)) {
            $sql->whereid($id);
        } else {
            $sql->where('url', $id);
        }
        return $sql->get();
    }

_hook_meta

public function _hook_meta($meta = [])

Meta tags injection.

    public function _hook_meta($meta = [])
    {
        $a = $this->_current;
        if ($a) {
            $desc = _truncate($a['meta_desc'], 250);
            $url = url('/@object/show/' . (($a['url'] ?? null) ?: $a['id']));
            $meta = [
                'keywords' => $a['meta_keywords'],
                'news_keywords' => $a['meta_keywords'],
                'description' => $desc,
                'og:description' => $desc,
                'og:title' => $a['title'],
                'og:type' => 'article',
                'og:url' => $url,
                'og:site_name' => SITE_ADVERT_NAME,
                'canonical' => $url,
                'article:published_time' => date('Y-m-d', $a['add_date']),
            ] + (array) $meta;
        }
        return $meta;
    }

_hook_nav

public function _hook_nav($params = [])

Hook for navigation bar.

    public function _hook_nav($params = [])
    {
        if (in_array(($_GET['action'] ?? null), ['show', 'view']) && ! ($_GET['id'] ?? null) || ! $this->_current) {
            // Default nav bar will be shown
            return false;
        }
        $nav = &$params['nav_bar_obj'];
        return [
            $nav->_nav_item('News', url('/@object')),
            $nav->_nav_item($this->_current['title']),
        ];
    }

_hook_sitemap

public function _hook_sitemap($sitemap = false)

Hook for the site_map.

    public function _hook_sitemap($sitemap = false)
    {
        if ( ! is_object($sitemap)) {
            return false;
        }
        foreach ((array) db()->select('id, url')->from(self::table)->where('active', '1')->where('locale', conf('language'))->order_by('add_date', 'desc')->get_all() as $a) {
            $sitemap->_add('/news/show/' . (($a['url'] ?? null) ?: $a['id']) . ($a['locale'] ? '?lang=' . $a['locale'] : ''));
        }
        return true;
    }

_hook_title

public function _hook_title($title)

Meta page title injection.

    public function _hook_title($title)
    {
        $a = $this->_current;
        if ($a) {
            return $a['title'];
        }
        return $title;
    }

_init

public function _init()
    public function _init()
    {
        asset('animate-css');
        jquery('
			$(\'.news_item_title\').hover(function(){
				$(this).parent().find(\'.news_btn\').addClass(\'active_news_btn\');
			},function(){
				$(this).parent().find(\'.news_btn\').removeClass(\'active_news_btn\');
			});
			$(\'.news_btn\').hover(function(){
				$(this).parent().parent().find(\'.news_item_title\').addClass(\'active_news\');
			},function(){
				$(this).parent().parent().find(\'.news_item_title\').removeClass(\'active_news\');
			});
		');
        css('
			.news .news_item { font-size: 13px; font-weight:normal; margin-bottom: 20px; margin-top: 20px; }
			.news h1 { font-size: 18px; margin-bottom: 20px; }
			.news h3 { font-size: 18px; margin-top: 5px; }
			.news .news_item_content { }
			.news .news_item_content h1, .news .news_item_content h2, .news .news_item_content h3,
			.news .news_item_content h4, .news .news_item_content h5, .news .news_item_content h6 { font-size: 13px; font-weight:normal; }
			.news .news_item_footer { color: #aaa; }
			.news .news_item_date { color: #777; }
			.news .news_item_social { margin-top: 20px; }
			.news .news_item_comments { margin-top: 20px; }
		');
    }
public function _module_action_handler($method, $dirty_args = [])
    public function _module_action_handler($method, $dirty_args = [])
    {
        // For links like this:
        // /news/uvazhaemie-polzovateli-i-posetiteli
        // /news/unique-news-item
        $id = trim(($dirty_args['action'] ?? null) ?: $_GET['action']);
        if ( ! $_GET['id'] && strlen($id) && $id != 'show') {
            $_GET['id'] = $id;
        }
        if ( ! $method || substr($method, 0, 1) === '_' || ! method_exists($this, $method)) {
            $_GET['action'] = $method;
            $method = 'show';
        }
        return $this->$method();
    }
public function _parse_text_tags($text)

News text is admin HTML that may hold template tags on purpose. They ran in the page's second pass; with tpl NO_TAGS_IN_INSERTED on there is none, so the text is parsed here as a template of its own.

    public function _parse_text_tags($text)
    {
        if ( ! tpl()->NO_TAGS_IN_INSERTED || ! is_string($text) || strpos($text, '{') === false) {
            return $text;
        }
        return tpl()->parse_string($text);
    }

_view

public function _view()
    public function _view()
    {
        $a = $this->_get_info();
        if ( ! $a || ! $a['active']) {
            return _404('Not found');
        }
        $this->_current = $a;

        $url = url('/@object/@action/' . (($a['url'] ?? null) ?: $a['id']));
        $comments = module('comments')->_show_comments((array) $this->_comments_params + [
            'add_form_action' => url('/@object/add_comment/' . $a['id']),
            'return_path' => $url,
            'object_id' => $a['id'],
        ]);
        $comments_form = main()->USER_ID ? module('comments')->add((array) $this->_comments_params + [
            'add_form_action' => url('/@object/add_comment/' . $a['id']),
            'return_path' => $url,
            'object_id' => $a['id'],
        ]) : '';
        return tpl()->parse('news/full_news', [
            'title' => $a['title'],
            'head_text' => $this->_parse_text_tags($a['head_text']),
            'full_text' => $this->_parse_text_tags($a['full_text']),
            'add_date' => _format_date($a['add_date'], 'long'),
            'full_link' => $url,
            'comments_url' => url('/@object/add_comment/' . $a['id']),
            'comments_form' => $comments_form,
            'comments_block' => $comments['comments'],
            'num_comments' => (int) ($comments['num_comments']),
            'social' => html()->social_simple_share(['horizontal' => true, 'url' => $url, 'title' => $a['title'] . ' | ' . t('News') . (defined('SITE_ADVERT_NAME') ? ' | ' . SITE_ADVERT_NAME : '')]),
        ]);
    }