root/trunk/gregarius/tags.php

Revision 1722, 6.6 kB (checked in by mbonetti, 1 year ago)

a couple fixes in tag-related rendering

  • Property svn:eol-style set to native
  • Property svn:eolstyle set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 ###############################################################################
3 # Gregarius - A PHP based RSS aggregator.
4 # Copyright (C) 2003 - 2006 Marco Bonetti
5 #
6 ###############################################################################
7 # This program is free software and open source software; you can redistribute
8 # it and/or modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of the License,
10 # or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE.     See the GNU General Public License for
15 # more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
20 # http://www.gnu.org/licenses/gpl.html
21 #
22 ###############################################################################
23 # E-mail:       mbonetti at gmail dot com
24 # Web page:       http://gregarius.net/
25 #
26 ###############################################################################
27
28 require_once ('init.php');
29
30 function relatedTags($tags) {
31     /* related tags */
32     $twhere = "";
33     foreach ($tags as $tag) {
34         $tag = rss_real_escape_string($tag);
35         $twhere .= "t.tag='$tag' or ";
36     }
37     $twhere .= "1=0";
38
39     $sql = "select fid,tid,m.tdate from ".getTable('metatag') ." m "
40           ."inner join " . getTable('tag') . " t on t.id = m.tid  where m.ttype = 'item'"
41         ." and ($twhere)";
42
43     //echo $sql;
44     $res = rss_query($sql);
45     $fids = array ();
46     $ctid = -1;
47     while (list ($fid, $tid) = rss_fetch_row($res)) {
48         $fids[] = $fid;
49         $tids[] = $tid;
50     }
51     $fids = array_unique($fids);
52     $tids = array_unique($tids);
53
54     $rtags = array ();
55     if (count($fids)) {
56         $sql = "select t.tag, count(*) as cnt from ".getTable('metatag')
57         ." m left join ".getTable('item')." i on (m.fid=i.id) "
58         ." inner join " . getTable('tag')." t on (t.id = m.tid) "
59         ." where m.fid in (".implode(",", $fids).")"
60         ." and t.id not in (".implode(",", $tids).")";
61
62         if (hidePrivate()) {
63             $sql .= " and not(i.unread & ".RSS_MODE_PRIVATE_STATE.") ";
64         }
65
66         $sql .= " group by t.tag order by cnt desc";
67
68         //echo $sql;
69         $res = rss_query($sql);
70         while ((list ($rtag, $cnt) = rss_fetch_row($res))) {
71             $rtags[$rtag] = $cnt;
72         }
73     }
74     return $rtags;
75 }
76
77 if (array_key_exists('tag', $_GET)) {
78     // while this one displays a list of items for the requested tag(s)
79     $tag = strip_tags($_GET['tag']);
80     $twhere = "";
81     $tarr = explode(" ", $tag);
82     $hrTag = implode(" ".__('and')." ", $tarr);
83     $urlTag = implode("+", $tarr);
84
85     foreach ($tarr as $ttkn) {
86         $twhere .= " t.tag='".trim(rss_real_escape_string($ttkn))."' or";
87     }
88     $twhere .= " 1=0";
89
90     $sql = "select fid, count(*) as cnt from " . getTable('metatag')." m "
91     ."inner join " . getTable('tag')." t on t.id = m.tid "
92     ." where ($twhere) "
93     ." and m.ttype = 'item'"
94     ." group by fid order by 2 desc";
95
96     $res = rss_query($sql);
97     $ids = array ();
98     while ((list ($id, $cnt) = rss_fetch_row($res)) && $cnt >= count($tarr)) {
99         $ids[] = $id;
100     }
101
102     $gotsome = count($ids) > 0;
103     $taggedItems = new PaginatedItemList();
104     if ($gotsome) {
105
106         $sqlWhere = " i.id in (".implode(",", $ids).") ";
107         // include deprecated feeds while showing tags.
108         $taggedItems->populate($sqlWhere, "", 0, -1, ITEM_SORT_HINT_MIXED, true);
109
110         $rtags = relatedTags($tarr);
111         $related = array ();
112         foreach ($rtags as $rtag => $cnt) {
113             $relLbl = "<a href=\"".getPath().""
114             . (getConfig('rss.output.usemodrewrite') ? "tag/$rtag" : "tags.php?tag=$rtag").""."\">$rtag</a>";
115
116             $relPlus = array_key_exists($rtag, $taggedItems->allTags);
117             if ($relPlus) {
118                 $relLbl .= sprintf(
119                     '&nbsp;[<a title="%d %s %s \'%s %s %s"\' href="%s+%s">+</a>]',
120                     $cnt,
121                     $cnt > 1 ? __('items') : __('item'),
122                     $cnt > 1 || $cnt == 0 ? __('tagged') : __('tagged'),
123                     htmlspecialchars($hrTag,ENT_QUOTES),
124                     __('and'),
125                     htmlspecialchars($rtag,ENT_QUOTES),
126                     getPath(getConfig('rss.output.usemodrewrite') ? "tag/$rtag" : "tags.php?tag=$rtag"),
127                     htmlspecialchars($urlTag,ENT_QUOTES)
128                     );
129                 
130                 "&nbsp;[<a "."title=\"$cnt "
131                 . ($cnt > 1 ? __('items') : __('item'))." ". ($cnt > 1 || $cnt == 0 ? __('tagged') : __('tagged'))." '"
132                 .htmlspecialchars($hrTag,ENT_QUOTES)." ".__('and')." ".htmlspecialchars($rtag,ENT_QUOTES)."'\" "
133                 ."href=\"".getPath().""
134                     . (getConfig('rss.output.usemodrewrite') ? "tag/$rtag" : "tags.php?tag=$rtag").""
135                     ."+".$urlTag."\">+</a>]";
136             }
137             $idx = ($relPlus ? $taggedItems->allTags[$rtag] : 0);
138             $related["$idx"."_"."$rtag"] = $relLbl."";
139         }
140         krsort($related);
141     }
142
143     // done! Render some stuff
144     if (array_key_exists('rss', $_REQUEST)) {
145         rss_require('cls/rdf.php');
146         // RSS view
147         $title = _TITLE_." - ".__('Tags')." - ".$hrTag;
148         $baselink = guessTransportProto().$_SERVER['HTTP_HOST'].getPath()
149         . (getConfig('rss.output.usemodrewrite') ? "tag/" : "tags.php?tag=");
150
151         if ($gotsome) {
152             $rdf = new RDFItemList($taggedItems);
153         } else {
154             $rdf = new RDFItemList(null);
155         }
156         $rdf->baselink = $baselink;
157         $rdf->resource = $urlTag;
158         $rdf->render($title);
159         exit ();
160     } else {
161         // HTML view
162         //rss_header("Tags " . TITLE_SEP . " " . $hrTag);
163         $GLOBALS['rss']->header = new Header("Tags ".TITLE_SEP." ".$hrTag);
164         $GLOBALS['rss']->feedList = new FeedList(false);
165
166         //echo "\n\n<div id=\"items\" class=\"frame\">\n";
167
168         if ($gotsome) {
169
170             $title = $taggedItems->itemCount." ". ($taggedItems->itemCount > 1 ? __('items') : __('item'))." "
171             . ($taggedItems->itemCount > 1 || $taggedItems->itemCount == 0 ? __('tagged') : __('tagged'))
172             .""." \"".$hrTag."\"";
173
174             if (count($related)) {
175                 $taggedItems->beforeList = "\n<p>".__('Related tags: ')."\n".implode(", \n", $related)."\n</p>\n";
176             }
177
178             $taggedItems->setTitle($title);
179             $taggedItems->setRenderOptions(IL_NO_COLLAPSE|IL_TITLE_NO_ESCAPE);
180             $GLOBALS['rss']->appendContentObject($taggedItems);
181
182             $GLOBALS['rss']->renderWithTemplate('index.php', 'items');
183
184         } else {
185             $GLOBALS['rss']->renderWithTemplate('index.php', 'items');
186
187 //            echo "<p style=\"height: 10em; text-align:center\">";
188 //            printf(__('Oops! No items tagged &laquo;%s&raquo; were found.'), $hrTag);
189 //            echo "</p>";
190         }
191         //echo "</div>\n";
192         //rss_footer();
193     }
194
195 } elseif (array_key_exists('alltags', $_GET)) {
196     rss_require('cls/alltags.php');
197
198     $GLOBALS['rss']->header = new Header("Tags ".TITLE_SEP." ".__('All Tags'));
199     $GLOBALS['rss']->feedList = new FeedList(false);
200     $allTags = new Tags();
201     $allTags->setRenderOptions(IL_TITLE_NO_ESCAPE);
202     $GLOBALS['rss']->appendContentObject($allTags);
203     $GLOBALS['rss']->renderWithTemplate('index.php', 'items');
204 }
205 ?>
206
Note: See TracBrowser for help on using the browser.