]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tag.php
Update some gettext strings and number the arguements so life is easier for translators
[quix0rs-gnu-social.git] / actions / tag.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/actions/showstream.php');
23 define('TAGS_PER_PAGE', 100);
24 define('AGE_FACTOR', 864000.0);
25
26 class TagAction extends StreamAction {
27
28         function handle($args) {
29
30                 parent::handle($args);
31
32                 # Looks like we're good; show the header
33
34                 if (isset($args['tag']) && $args['tag']) {
35                         $tag = $args['tag'];
36                         common_show_header(sprintf(_("Notices tagged with %s"), $tag),
37                                                            array($this, 'show_header'), $tag,
38                                                            array($this, 'show_top'));
39
40                         $this->show_notices($tag);
41                 } else {
42                         common_show_header(_("Tags"),
43                                                            array($this, 'show_header'), '',
44                                                            array($this, 'show_top'));
45                         $this->show_tags();
46                 }
47
48                 common_show_footer();
49         }
50
51         function show_header($tag = false) {
52                 if (false && $tag) {
53                         common_element('link', array('rel' => 'alternate',
54                                                                                  'href' => common_local_url('tagrss', array('tag' => $tag)),
55                                                                                  'type' => 'application/rss+xml',
56                                                                                  'title' => sprintf(_('Feed for tag %s'), $tag)));
57                 }
58         }
59
60         function get_instructions() {
61                 return _('Showing most popular tags from the last week');
62         }
63
64         function show_top($tag = false) {
65                 if (!$tag) {
66                         $instr = $this->get_instructions();
67                         $output = common_markup_to_html($instr);
68                         common_element_start('div', 'instructions');
69                         common_raw($output);
70                         common_element_end('div');
71                 }
72
73                 common_element_start('ul', array('id' => 'nav_views'));
74
75                 common_menu_item(common_local_url('tags'),
76                                                  _('Recent Tags'),
77                                                  _('Recent Tags'),
78                                                  !$tag);
79                 if ($tag) {
80                         common_menu_item(common_local_url('tag', array('tag' => $tag)),
81                                                          '#' . $tag,
82                                                          sprintf(_("Notices tagged with %s"), $tag),
83                                                          true);
84                 }
85                 common_element_end('ul');
86         }
87
88         function show_tags()
89         {
90                 # This should probably be cached rather than recalculated
91                 $tags = DB_DataObject::factory('Notice_tag');
92                 $tags->selectAdd('max(notice_id) as last_notice_id');
93                 $tags->selectAdd(sprintf('sum(exp(-(now() - created)/%f)) as weight', AGE_FACTOR));
94                 $tags->groupBy('tag');
95                 $tags->orderBy('weight DESC');
96
97                 # $tags->whereAdd('created > "' . strftime('%Y-%m-%d %H:%M:%S', strtotime('-1 MONTH')) . '"');
98
99                 $tags->limit(TAGS_PER_PAGE);
100
101                 $cnt = $tags->find();
102
103                 if ($cnt > 0) {
104                         common_element_start('p', 'tagcloud');
105                         
106                         $tw = array();
107                         $sum = 0;
108                         while ($tags->fetch()) {
109                                 $tw[$tags->tag] = $tags->weight;
110                                 $sum += $tags->weight;
111                         }
112                         common_element_end('ul');
113                 }
114
115                 common_pagination($page > 1, $cnt > TAGS_PER_PAGE,
116                                                   $page, 'tags');
117         }
118
119         private static function show_tag($tag) {
120                 common_element_start('li', array('class' => 'notice_single'));
121                 common_element_start('a', array(
122                                                         'class' => 'nickname',
123                                                         'href' => common_local_url('tag', array('tag' => $tag->tag)),
124                                                         'title' => sprintf(_("Notices tagged with %s"), $tag->tag)));
125                 common_text('#' . $tag->tag);
126                 common_element_end('a');
127                 common_text(sprintf(_('%s Notices recently tagged with %s'), $tag->num, $tag->tag));
128
129                 $notice = Notice::staticGet($tag->last_notice_id);
130                 if ($notice) {
131                         $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
132                         common_element_start('p', 'time');
133                         common_text(_('Last message posted: '));
134                         common_element('a', array('class' => 'permalink',
135                                                                           'href' => $noticeurl,
136                                                                           'title' => common_exact_date($notice->created)),
137                                                         common_date_string($notice->created));
138
139                         common_text(_(' by '));
140                         $profile = $notice->getProfile();
141                         common_element('a', array('href' => $profile->profileurl),
142                                                    $profile->nickname);
143                         common_element_end('p');
144                 }
145         }
146
147         function show_tag($tag, $weight, $relative) {
148                 
149                 # XXX: these should probably tune to the size of the site
150                 if ($relative > 0.1) {
151                         $cls =  'largest';
152                 } else if ($relative > 0.05) {
153                         $cls = 'verylarge';
154                 } else if ($relative > 0.02) {
155                         $cls = 'large';
156                 } else if ($relative > 0.01) {
157                         $cls = 'medium';
158                 } else if ($relative > 0.005) {
159                         $cls = 'small';
160                 } else if ($relative > 0.002) {
161                         $cls = 'verysmall';
162                 } else {
163                         $cls = 'smallest';
164                 }
165                 
166                 common_element('a', array('class' => "$cls weight-$weight relative-$relative",
167                                                                   'href' => common_local_url('tag', array('tag' => $tag))),
168                                            $tag);
169                 common_text(' ');
170         }
171         
172         function show_notices($tag) {
173
174                 $tags = DB_DataObject::factory('Notice_tag');
175
176                 $tags->tag = $tag;
177
178                 $tags->orderBy('created DESC');
179
180                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
181
182                 $tags->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
183
184                 $cnt = $tags->find();
185
186                 if ($cnt > 0) {
187                         common_element_start('ul', array('id' => 'notices'));
188                         for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
189                                 if ($tags->fetch()) {
190                                         $notice = new Notice();
191                                         $notice->id = $tags->notice_id;
192                                         $result = $notice->find(true);
193                                         if (!$result) {
194                                                 continue;
195                                         }
196                                         $this->show_notice($notice);
197                                 } else {
198                                         // shouldn't happen!
199                                         break;
200                                 }
201                         }
202                         common_element_end('ul');
203                 }
204
205                 common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
206                                                   $page, 'tag', array('tag' => $tag));
207         }
208 }