]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tag.php
Publish MicroIDs for email and mpp on profile and notice pages.
[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', 20);
24
25 class TagAction extends StreamAction {
26
27         function handle($args) {
28
29                 parent::handle($args);
30
31                 # Looks like we're good; show the header
32
33                 if (isset($args['tag']) && $args['tag']) {
34                         $tag = $args['tag'];
35                         common_show_header(sprintf(_("Notices tagged with %s"), $tag),
36                                                            array($this, 'show_header'), $tag,
37                                                            array($this, 'show_top'));
38
39                         $this->show_notices($tag);
40                 } else {
41                         common_show_header(_("Tags"),
42                                                            array($this, 'show_header'), '',
43                                                            array($this, 'show_top'));
44                         $this->show_tags();
45                 }
46
47                 common_show_footer();
48         }
49
50         function show_header($tag = false) {
51                 if (false && $tag) {
52                         common_element('link', array('rel' => 'alternate',
53                                                                                  'href' => common_local_url('tagrss', array('tag' => $tag)),
54                                                                                  'type' => 'application/rss+xml',
55                                                                                  'title' => sprintf(_('Feed for tag %s'), $tag)));
56                 }
57         }
58
59         function get_instructions() {
60                 return _('Showing most popular tags from the last week');
61         }
62
63         function show_top($tag = false) {
64                 if (!$tag) {
65                         $instr = $this->get_instructions();
66                         $output = common_markup_to_html($instr);
67                         common_element_start('div', 'instructions');
68                         common_raw($output);
69                         common_element_end('div');
70                 }
71
72                 common_element_start('ul', array('id' => 'nav_views'));
73
74                 common_menu_item(common_local_url('tags'),
75                                                  _('Recent Tags'),
76                                                  _('Recent Tags'),
77                                                  !$tag);
78                 if ($tag) {
79                         common_menu_item(common_local_url('tag', array('tag' => $tag)),
80                                                          '#' . $tag,
81                                                          sprintf(_("Notices tagged with %s"), $tag),
82                                                          true);
83                 }
84                 common_element_end('ul');
85         }
86
87         function show_tags()
88         {
89                 $tags = DB_DataObject::factory('Notice_tag');
90                 $tags->selectAdd('count(1) as num');
91                 $tags->selectAdd('max(notice_id) as last_notice_id');
92                 $tags->groupBy('tag');
93                 $tags->orderBy('num DESC, last_notice_id DESC');
94                 $tags->whereAdd('created > "' . strftime('%Y-%m-%d %H:%M:%S', strtotime('-1 WEEK')) . '"');
95
96                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
97
98                 $tags->limit((($page-1)*TAGS_PER_PAGE), TAGS_PER_PAGE + 1);
99
100                 $cnt = $tags->find();
101
102                 if ($cnt > 0) {
103                         common_element_start('ul', array('id' => 'notices'));
104                         for ($i = 0; $i < min($cnt, TAGS_PER_PAGE); $i++) {
105                                 if ($tags->fetch()) {
106                                         TagAction::show_tag($tags);
107                                 } else {
108                                         // shouldn't happen!
109                                         break;
110                                 }
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                 common_element_end('li');
146         }
147
148
149         function show_notices($tag) {
150
151                 $tags = DB_DataObject::factory('Notice_tag');
152
153                 $tags->tag = $tag;
154
155                 $tags->orderBy('created DESC');
156
157                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
158
159                 $tags->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
160
161                 $cnt = $tags->find();
162
163                 if ($cnt > 0) {
164                         common_element_start('ul', array('id' => 'notices'));
165                         for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
166                                 if ($tags->fetch()) {
167                                         $notice = new Notice();
168                                         $notice->id = $tags->notice_id;
169                                         $result = $notice->find(true);
170                                         if (!$result) {
171                                                 continue;
172                                         }
173                                         $this->show_notice($notice);
174                                 } else {
175                                         // shouldn't happen!
176                                         break;
177                                 }
178                         }
179                         common_element_end('ul');
180                 }
181
182                 common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
183                                                   $page, 'tag', array('tag' => $tag));
184         }
185 }