HotaruCMS Extensions is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

HotaruExtensions

/pluginspost_views_widget
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* name: Post Views Widget
* description: Displays lists of posts entries in a widget ordered by views.
* version: 0.2
* folder: post_views_widget
* class: PostViewsWidget
* requires: widgets 0.6
* hooks: install_plugin, header_include
* author: shibuya246
* authorurl: http://shibuya246.com
*
* PHP version 5
*
* LICENSE: Hotaru CMS is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along
* with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
*
* @category  Content Management System
* @package   HotaruCMS
* @author    Nick Ramsay <admin@hotarucms.org>
* @copyright Copyright (c) 2009, Hotaru CMS
* @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link      http://www.hotarucms.org/
*/

class PostViewsWidget
{
    private $limit = 10; // number of items to show in the widget

    /**
    *  Add default settings for Posts Widget plugin on installation
    */
    public function install_plugin($h)
    {
        // Widgets:
        // plugin name, function name, optional arguments
        $h->addWidget('post_views_widget', 'post_views_widget');
    }


    /**
    * Display the top or latest posts in the sidebar
    *
    * @param $type either 'top' or 'new', matching the post_status in the db.
    */
    public function widget_post_views_widget($h)
    {
        $this->postviewsWidgetDefault($h);
    }


    /**
    * Display the default sidebar box
    */
    public function postviewsWidgetDefault($h)
    {
        $posts = $this->getPostViewsWidget($h, false, $this->limit);
        $title = $h->lang['post_views_widget_title'];

        if (!defined('SITEURL')) {
            define('SITEURL', BASEURL);
        }

        if (isset($posts) && !empty($posts)) {

            $output = "<h4 class='widget_head post_views_widget_title'>\n";
            $link = SITEURL . "views/";

            $output .= "<a href='" . $link . "' title='" . $h->lang["post_views_widget_title_anchor_title"] . "'>" . $title . "</a>\n";

            //$output .= "<a href='" . $h->url(array('page'=>'journals', 'rss'=>'true')) . "' title='" . $h->lang["post_views_widget_icon_anchor_title"] . "'>\n";
            //$output .= "<img src='" . SITEURL . "content/themes/" . THEME . "images/rss_16.png' alt='RSS' /></a>\n"; // RSS icon

            $output .= "</h4>\n";

            $output .= "<ul class='widget_body post_views_widget_items'>\n";
            $output .= $this->getPostViewsWidgetItems($h, $posts);
            $output .= "</ul>\n";
        }

        // Display the whole thing:
        if (isset($output) && $output != '') { echo $output; }
    }


    /**
    * Get widget posts
    *
    * @param $type either 'top' or 'new', matching the post_status in the db.
    * return array $posts
    */
    public function getPostViewsWidget($h, $custom = true, $limit = 10)
    {
        $filter['post_archived = %s'] = 'N';
        //$filter['post_status = %s'] = 'new';
        $filter['post_type = %s'] = 'news';

        // get the prepared SQL query
        $posts = $h->db->select($h, array('*'), 'posts', $filter, 'post_views DESC', $limit);
        if ($posts) { return $posts; } else { return false; }
    }


    /**
    * Get post widget items
    *
    * @param array $posts
    * return string $ouput
    */
    public function getPostViewsWidgetItems($h, $posts = array())
    {
        if (!$posts) { return false; }

        $need_cache = false;

        // check for a cached version and use it if no recent update:
        $output = $h->smartCache('html', 'posts', 10, '', 'views');
        if ($output) {
                return $output;
        } else {
                $need_cache = true;
        }

        foreach ($posts as $item) {

            $h->post->url = $item->post_url; // used in Hotaru's url function
            $h->post->category = $item->post_category; // used in Hotaru's url function

            // POST TITLE
            $output .= "<li class='post_views_widget_item'>\n";

            $output .= "<div class='post_views_widget_views'>";
            $output .= $item->post_views;
            $output .= "</div>\n";

            $output .= "<div class='post_views_widget_link'>\n";

            $item_title = stripslashes(html_entity_decode(urldecode($item->post_title), ENT_QUOTES,'UTF-8'));
            $output .= "<a href='" . $h->url(array('page'=>$item->post_id)) . "' title='" . urldecode($item->post_domain) . "'>\n" . $item_title . "\n</a></div>\n";
            $output .= "</li>\n";
        }

        if ($need_cache) {
                $h->smartCache('html', 'posts', 10, $output, 'views'); // make or rewrite the cache file
        }

        return $output;
    }

}
?>
Ajax-loader Loading, please wait...