]> git.wh0rd.org - tt-rss.git/blame - mobile/functions.php
cdmClicked: use correct function to parse returned JSON
[tt-rss.git] / mobile / functions.php
CommitLineData
e9e95dae 1<?php
3dd46f19 2 define('TTRSS_SESSION_NAME', 'ttrss_m_sid');
0d3adafe 3
f0a0c1ff
AD
4 /* TODO replace with interface to db-prefs */
5
6 function mobile_pref_toggled($link, $id) {
e9105eb5
AD
7 if (get_pref($link, "_MOBILE_$id"))
8 return "true";
9 else
10 return "";
f0a0c1ff
AD
11 }
12
13 function mobile_get_pref($link, $id) {
e9105eb5
AD
14 //return $_SESSION["mobile-prefs"][$id];
15 return get_pref($link, "_MOBILE_$id");
f0a0c1ff
AD
16 }
17
18 function mobile_set_pref($link, $id, $value) {
e9105eb5
AD
19 //$_SESSION["mobile-prefs"][$id] = $value;
20 return set_pref($link, "_MOBILE_$id", $value);
f0a0c1ff
AD
21 }
22
3518718b
AD
23 function mobile_feed_has_icon($id) {
24 $filename = "../".ICONS_DIR."/$id.ico";
42096f52 25
3518718b
AD
26 return file_exists($filename) && filesize($filename) > 0;
27 }
42096f52 28
95004daf 29 function render_flat_feed_list($link, $offset) {
b1bd222c 30 $owner_uid = $_SESSION["uid"];
af88c48a 31 $limit = 0;
95004daf
AD
32
33 if (!$offset) $offset = 0;
b1bd222c 34
f0a0c1ff
AD
35 if (mobile_get_pref($link, "SORT_FEEDS_UNREAD")) {
36 $order_by = "unread DESC, title";
37 } else {
38 $order_by = "title";
39 }
40
af88c48a
AD
41 if ($limit > 0) {
42 $limit_qpart = "LIMIT $limit OFFSET $offset";
43 } else {
44 $limit_qpart = "";
45 }
46
b1bd222c
AD
47 $result = db_query($link, "SELECT id,
48 title,
49 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
50 WHERE feed_id = ttrss_feeds.id AND unread = true
51 AND ttrss_user_entries.ref_id = ttrss_entries.id
af88c48a 52 AND owner_uid = '$owner_uid') AS unread
b1bd222c
AD
53 FROM ttrss_feeds
54 WHERE
6e63a7c3 55 ttrss_feeds.owner_uid = '$owner_uid'
af88c48a 56 ORDER BY $order_by $limit_qpart");
b1bd222c 57
95004daf 58 if (!$offset) print '<ul id="home" title="'.__('Home').'" selected="true"
9ab798a5 59 myBackLabel="'.__('Logout').'" myBackHref="logout.php" myBackTarget="_self">';
b1bd222c 60
af88c48a 61
b1bd222c 62 // print "<li><a href='#cat-actions'>".__('Actions...')."</a></li>";
95004daf
AD
63
64 $num_feeds = 0;
65
b1bd222c
AD
66 while ($line = db_fetch_assoc($result)) {
67 $id = $line["id"];
68 $unread = $line["unread"];
69
70 // $unread = rand(0, 100);
71
72 if ($unread > 0) {
73 $line["title"] = $line["title"] . " ($unread)";
74 $class = '';
75 } else {
76 $class = 'oldItem';
77 }
78
79 if (mobile_feed_has_icon($id)) {
80 $icon_url = "../".ICONS_URL."/$id.ico";
81 } else {
82 $icon_url = "../images/blank_icon.gif";
83 }
f0a0c1ff
AD
84
85 if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
86 print "<li class='$class'><a href='feed.php?id=$id'>" .
87 "<img class='tinyIcon' src='$icon_url'/>".
88 $line["title"] . "</a></li>";
89 }
af88c48a
AD
90
91 ++$num_feeds;
b1bd222c 92 }
b1bd222c 93
af88c48a 94/* $next_offset = $offset + $num_feeds;
95004daf
AD
95
96 print "<li><a href=\"home.php?skip=$next_offset\"
af88c48a 97 target=\"_replace\">Show more feeds...</a></li>"; */
b1bd222c 98
95004daf 99 if (!$offset) print "</ul>";
b1bd222c
AD
100
101 }
102
3518718b
AD
103 function render_category($link, $cat_id) {
104 $owner_uid = $_SESSION["uid"];
42096f52 105
e2b7a855 106 if ($cat_id >= 0) {
85233b8e 107
e2b7a855
AD
108 if ($cat_id != 0) {
109 $cat_query = "cat_id = '$cat_id'";
3518718b 110 } else {
e2b7a855 111 $cat_query = "cat_id IS NULL";
0d3adafe 112 }
f0a0c1ff
AD
113
114 if (mobile_get_pref($link, "SORT_FEEDS_UNREAD")) {
115 $order_by = "unread DESC, title";
116 } else {
117 $order_by = "title";
118 }
119
e2b7a855
AD
120 $result = db_query($link, "SELECT id,
121 title,
122 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
123 WHERE feed_id = ttrss_feeds.id AND unread = true
124 AND ttrss_user_entries.ref_id = ttrss_entries.id
125 AND owner_uid = '$owner_uid') as unread
126 FROM ttrss_feeds
127 WHERE
e2b7a855 128 ttrss_feeds.owner_uid = '$owner_uid' AND
e2b7a855 129 $cat_query
f0a0c1ff 130 ORDER BY $order_by");
e2b7a855
AD
131
132 $title = getCategoryTitle($link, $cat_id);
133
9ab798a5 134 print "<ul id='cat-$cat_id' title='$title' myBackLabel='".__("Home")."'
bf974b02 135 myBackHref='home.php'>";
e2b7a855
AD
136
137 // print "<li><a href='#cat-actions'>".__('Actions...')."</a></li>";
138
139 while ($line = db_fetch_assoc($result)) {
140 $id = $line["id"];
141 $unread = $line["unread"];
3e94601e 142
e2b7a855
AD
143 // $unread = rand(0, 100);
144
145 if ($unread > 0) {
146 $line["title"] = $line["title"] . " ($unread)";
147 $class = '';
148 } else {
149 $class = 'oldItem';
150 }
151
152 if (mobile_feed_has_icon($id)) {
153 $icon_url = "../".ICONS_URL."/$id.ico";
154 } else {
155 $icon_url = "../images/blank_icon.gif";
156 }
f0a0c1ff
AD
157
158 if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
159 print "<li class='$class'><a href='feed.php?id=$id&cat=$cat_id'>" .
160 "<img class='tinyIcon' src='$icon_url'/>".
161 $line["title"] . "</a></li>";
162 }
0d3adafe 163 }
e2b7a855
AD
164
165 print "</ul>";
166 } else if ($cat_id == -1) {
167
17fd15be
AD
168 $title = __('Special');
169
9ab798a5
AD
170 print "<ul id='cat--1' title='$title' myBackLabel='".__("Home")."'
171 myBackHref='home.php'>";
e2b7a855 172
87e3d2dd 173 foreach (array(-4, -3, -1, -2, 0) as $id) {
e2b7a855
AD
174 $title = getFeedTitle($link, $id);
175 $unread = getFeedUnread($link, $id, false);
af88c48a 176 $icon = getFeedIcon($id);
e2b7a855
AD
177
178 if ($unread > 0) {
179 $title = $title . " ($unread)";
180 $class = '';
181 } else {
182 $class = 'oldItem';
eead4d26 183 }
8e3f7217 184
f0a0c1ff
AD
185 if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
186 print "<li class='$class'>
af88c48a
AD
187 <a href='feed.php?id=$id&cat=-1'>
188 <img class='tinyIcon' src='../$icon'/>$title</a></li>";
f0a0c1ff 189 }
e2b7a855 190 }
8e3f7217 191
17fd15be
AD
192 print "</ul>";
193 } else if ($cat_id == -2) {
194
195 $title = __('Labels');
196
9ab798a5
AD
197 print "<ul id='cat--2' title='$title' myBackLabel='".__("Home")."'
198 myBackHref='home.php'>";
17fd15be
AD
199
200 $result = db_query($link, "SELECT id, caption FROM ttrss_labels2
201 WHERE owner_uid = '$owner_uid'");
202
203 $label_data = array();
204
205 while ($line = db_fetch_assoc($result)) {
206
207 $id = -$line["id"] - 11;
208
209 $unread = getFeedUnread($link, $id);
210 $title = $line["caption"];
211
212 if ($unread > 0) {
213 $title = $title . " ($unread)";
214 $class = '';
215 } else {
216 $class = 'oldItem';
217 }
218
f0a0c1ff
AD
219 if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
220 print "<li class='$class'>
221 <a href='feed.php?id=$id&cat=-2'>$title</a></li>";
222 }
17fd15be 223 }
e2b7a855
AD
224 print "</ul>";
225 }
0d3adafe
AD
226 }
227
3518718b
AD
228 function render_categories_list($link) {
229 $owner_uid = $_SESSION["uid"];
3585a3c5 230
9ab798a5
AD
231 print '<ul id="home" title="'.__('Home').'" selected="true"
232 myBackLabel="'.__('Logout').'" myBackHref="logout.php" myBackTarget="_self">';
eead4d26 233
3585a3c5
AD
234
235// print "<li><a href='#searchForm'>Search...</a></li>";
236
3518718b
AD
237 foreach (array(-1, -2) as $id) {
238 $title = getCategoryTitle($link, $id);
239 $unread = getFeedUnread($link, $id, true);
240 if ($unread > 0) {
241 $title = $title . " ($unread)";
242 $class = '';
eead4d26 243 } else {
3518718b 244 $class = 'oldItem';
c878bc01 245 }
2f468537 246
3518718b 247 print "<li class='$class'><a href='cat.php?id=$id'>$title</a></li>";
2f468537
AD
248 }
249
3518718b
AD
250 $result = db_query($link, "SELECT
251 ttrss_feed_categories.id,
252 ttrss_feed_categories.title,
253 COUNT(ttrss_feeds.id) AS num_feeds
254 FROM ttrss_feed_categories, ttrss_feeds
255 WHERE ttrss_feed_categories.owner_uid = $owner_uid
256 AND ttrss_feed_categories.id = cat_id
3518718b
AD
257 GROUP BY ttrss_feed_categories.id,
258 ttrss_feed_categories.title
259 ORDER BY ttrss_feed_categories.title");
2f468537 260
3518718b 261 while ($line = db_fetch_assoc($result)) {
fc46ab83 262
3518718b 263 if ($line["num_feeds"] > 0) {
2f468537 264
3518718b 265 $unread = getFeedUnread($link, $line["id"], true);
2f468537 266 $id = $line["id"];
510ac75f 267
3518718b
AD
268 if ($unread > 0) {
269 $line["title"] = $line["title"] . " ($unread)";
270 $class = '';
510ac75f 271 } else {
3518718b 272 $class = 'oldItem';
510ac75f 273 }
2f468537 274
f0a0c1ff
AD
275 if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
276 print "<li class='$class'><a href='cat.php?id=$id'>" .
277 $line["title"] . "</a></li>";
278 }
2f468537 279 }
2f468537
AD
280 }
281
2f468537 282
3518718b
AD
283 $result = db_query($link, "SELECT COUNT(*) AS nf FROM ttrss_feeds WHERE
284 cat_id IS NULL and owner_uid = '$owner_uid'");
42096f52 285
3518718b 286 $num_feeds = db_fetch_result($result, 0, "nf");
42096f52 287
3518718b
AD
288 if ($num_feeds > 0) {
289 $unread = getFeedUnread($link, 0, true);
290 $title = "Uncategorized";
42096f52 291
3518718b
AD
292 if ($unread > 0) {
293 $title = "$title ($unread)";
294 $class = '';
42096f52 295 } else {
3518718b 296 $class = 'oldItem';
42096f52
AD
297 }
298
f0a0c1ff
AD
299 if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
300 print "<li class='$class'><a href='cat.php?id=0'>$title</a></li>";
301 }
3518718b 302 }
42096f52 303
3518718b
AD
304 print "</ul>";
305 }
24ac6776 306
3585a3c5 307 function render_headlines_list($link, $feed_id, $cat_id, $offset, $search) {
24ac6776 308
3518718b 309 $feed_id = $feed_id;
78d7a965 310 $limit = 15;
3518718b 311 $filter = '';
3e092346 312 $is_cat = false;
3518718b 313 $view_mode = 'adaptive';
d9aad400 314
3585a3c5
AD
315 if ($search) {
316 $search_mode = 'this_feed';
317 $match_on = 'both';
318 } else {
319 $search_mode = '';
320 $match_on = '';
321 }
78d7a965 322
3518718b 323 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
78d7a965 324 $view_mode, $is_cat, $search, $search_mode, $match_on, false, $offset);
541890fb 325
3518718b
AD
326 $result = $qfh_ret[0];
327 $feed_title = $qfh_ret[1];
42096f52 328
78d7a965 329 if (!$offset) {
3585a3c5 330
cb216e9d 331 print "<form id=\"searchForm-$feed_id-$cat_id\" class=\"dialog\" method=\"POST\"
3585a3c5
AD
332 action=\"feed.php\">
333
334 <input type=\"hidden\" name=\"id\" value=\"$feed_id\">
335 <input type=\"hidden\" name=\"cat\" value=\"$cat_id\">
336
337 <fieldset>
cb216e9d 338 <h1>Search</h1>
3585a3c5
AD
339 <a class=\"button leftButton\" type=\"cancel\">Cancel</a>
340 <a class=\"button blueButton\" type=\"submit\">Search</a>
341
342 <label>Search:</label>
343 <input id=\"search\" type=\"text\" name=\"search\"/>
344 </fieldset>
345 </form>";
346
78d7a965
AD
347 if ($cat_id) {
348 $cat_title = getCategoryTitle($link, $cat_id);
3e092346 349
78d7a965
AD
350 print "<ul id=\"feed-$feed_id\" title=\"$feed_title\" selected=\"true\"
351 myBackLabel='$cat_title' myBackHref='cat.php?id=$cat_id'>";
352 } else {
353 print "<ul id=\"feed-$feed_id\" title=\"$feed_title\" selected=\"true\"
354 myBackLabel='".__("Home")."' myBackHref='home.php'>";
355 }
3585a3c5 356
cb216e9d 357 print "<li><a href='#searchForm-$feed_id-$cat_id'>Search...</a></li>";
b1bd222c 358 }
24ac6776 359
78d7a965
AD
360 $num_headlines = 0;
361
3518718b
AD
362 while ($line = db_fetch_assoc($result)) {
363 $id = $line["id"];
3e94601e 364 $real_feed_id = $line["feed_id"];
f70f7e28 365
3518718b
AD
366 if (sql_bool_to_bool($line["unread"])) {
367 $class = '';
f70f7e28 368 } else {
3518718b 369 $class = 'oldItem';
f70f7e28
AD
370 }
371
3e94601e
AD
372 if (mobile_feed_has_icon($real_feed_id)) {
373 $icon_url = "../".ICONS_URL."/$real_feed_id.ico";
374 } else {
375 $icon_url = "../images/blank_icon.gif";
376 }
377
378 print "<li class='$class'><a href='article.php?id=$id&feed=$feed_id&cat=$cat_id'>
379 <img class='tinyIcon' src='$icon_url'>";
3518718b
AD
380 print $line["title"];
381 print "</a></li>";
4a596be6 382
78d7a965
AD
383 ++$num_headlines;
384
42096f52
AD
385 }
386
3585a3c5
AD
387 if ($num_headlines == 0 && $search) {
388 $articles_url = "feed.php?id=$feed_id&cat=$cat_id&skip=$next_offset";
389
390 print "<li><a href=\"$articles_url\">" . __("Nothing found (click to reload feed).") . "</a></li>";
391
392 }
393
78d7a965
AD
394// print "<a target='_replace' href='feed.php?id=$feed_id&cat=$cat_id&skip=0'>Next $limit articles...</a>";
395
396 $next_offset = $offset + $num_headlines;
397 $num_unread = getFeedUnread($link, $feed_id, $is_cat);
398
399 /* FIXME needs normal implementation */
400
3585a3c5
AD
401 if ($num_headlines > 0 && ($num_unread == 0 || $num_unread > $next_offset)) {
402
403 $articles_url = "feed.php?id=$feed_id&cat=$cat_id&skip=$next_offset".
404 "&search=$search";
405
406 print "<li><a href=\"$articles_url\"
78d7a965
AD
407 target=\"_replace\">Get more articles...</a></li>";
408 }
409
410 if (!$offset) print "</ul>";
3518718b 411
42096f52
AD
412 }
413
3e092346 414 function render_article($link, $id, $feed_id, $cat_id) {
0809065e 415
3518718b
AD
416 $query = "SELECT title,link,content,feed_id,comments,int_id,
417 marked,unread,published,
418 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
419 author
420 FROM ttrss_entries,ttrss_user_entries
421 WHERE id = '$id' AND ref_id = id AND owner_uid = " .
422 $_SESSION["uid"] ;
0809065e 423
3518718b 424 $result = db_query($link, $query);
0809065e 425
3e092346 426 if (db_num_rows($result) != 0) {
0809065e 427
3e092346 428 $line = db_fetch_assoc($result);
0809065e 429
3e092346
AD
430 $tmp_result = db_query($link, "UPDATE ttrss_user_entries
431 SET unread = false,last_read = NOW()
432 WHERE ref_id = '$id'
433 AND owner_uid = " . $_SESSION["uid"]);
0809065e 434
324944f3
AD
435 $updated_fmt = make_local_datetime($link, $line['updated'], false);
436
3e092346
AD
437 $title = $line["title"];
438 $article_link = $line["link"];
439
440 $feed_title = getFeedTitle($link, $feed_id, false);
441
442 print "<div class=\"panel\" id=\"article-$id\" title=\"$title\"
443 selected=\"true\"
444 myBackLabel='$feed_title' myBackHref='feed.php?id=$feed_id&cat=$cat_id'>";
445
17cd1097 446 print "<h2><a target='_blank' href='$article_link'>$title</a></h2>";
3e092346
AD
447
448 print "<fieldset>";
449
bf974b02 450/* print "<div class=\"row\">";
3e092346 451 print "<label id='title'><a target='_blank' href='$article_link'>$title</a></label>";
bf974b02 452 print "</div>"; */
3e092346
AD
453
454 $is_starred = (sql_bool_to_bool($line["marked"])) ? "true" : "false";
455 $is_published = (sql_bool_to_bool($line["published"])) ? "true" : "false";
456
3e092346
AD
457 print "<div class=\"row\">";
458 print "<label id='updated'>Updated:</label>";
459 print "<input enabled='false' name='updated' disabled value='$updated_fmt'/>";
460 print "</div>";
461
462 print "</fieldset>";
3bac78a0
AD
463
464 $content = sanitize_rss($link, $line["content"]);
465 $content = preg_replace("/href=/i", "target=\"_blank\" href=", $content);
466
f0a0c1ff
AD
467 if (!mobile_get_pref($link, "SHOW_IMAGES")) {
468 $content = preg_replace('/<img[^>]+>/is', '', $content);
469 }
470
706fe949 471 print "<p>$content</p>";
6101b0e1 472
a6d56d81
AD
473 print "<div class='nav'>
474 <label>Navigation</label>
475 <div class='button left' onclick='goPrev($id, $feed_id, this)'>Prev</div>
476 <div class='button right' onclick='goNext($id, $feed_id, this)'>Next</div>
477 </div>";
478
6101b0e1
AD
479 print "<fieldset>";
480
481 print "<div class=\"row\">
482 <label>Starred</label>
483 <div class=\"toggle\" onclick=\"toggleMarked($id, this)\" toggled=\"$is_starred\"><span class=\"thumb\"></span><span class=\"toggleOn\">ON</span><span class=\"toggleOff\">OFF</span></div>
484 </div>";
3e092346 485
6101b0e1
AD
486 print "<div class=\"row\">
487 <label>Published</label>
488 <div class=\"toggle\" onclick=\"togglePublished($id, this)\" toggled=\"$is_published\"><span class=\"thumb\"></span><span class=\"toggleOn\">ON</span><span class=\"toggleOff\">OFF</span></div>
489 </div>";
490
491 print "</fieldset>";
492
3e092346 493 print "</div>";
f70f7e28 494
3e092346 495 }
4a596be6 496 }
0d3adafe 497?>