]> git.wh0rd.org Git - tt-rss.git/blob - mobile/functions.php
mobile: fix headline ordering
[tt-rss.git] / mobile / functions.php
1 <?php
2         define('TTRSS_SESSION_NAME', 'ttrss_m_sid');
3
4         /* TODO replace with interface to db-prefs */
5
6         function mobile_pref_toggled($link, $id) {
7                 if (get_pref($link, "_MOBILE_$id"))
8                         return "true";
9                 else
10                         return "";
11         }
12
13         function mobile_get_pref($link, $id) {
14                 //return $_SESSION["mobile-prefs"][$id];
15                 return get_pref($link, "_MOBILE_$id");
16         }
17
18         function mobile_set_pref($link, $id, $value) {
19                 //$_SESSION["mobile-prefs"][$id] = $value;
20                 return set_pref($link, "_MOBILE_$id", $value);
21         }
22
23         function mobile_feed_has_icon($id) {
24                 $filename = "../".ICONS_DIR."/$id.ico";
25
26                 return file_exists($filename) && filesize($filename) > 0;
27         }
28
29         function render_flat_feed_list($link, $offset) {
30                 $owner_uid = $_SESSION["uid"];
31                 $limit = 0;
32
33                 if (!$offset) $offset = 0;
34
35                 if (mobile_get_pref($link, "SORT_FEEDS_UNREAD")) {
36                         $order_by = "unread DESC, title";
37                 } else {
38                         $order_by = "title";
39                 }
40
41                 if ($limit > 0) {
42                         $limit_qpart = "LIMIT $limit OFFSET $offset";
43                 } else {
44                         $limit_qpart = "";
45                 }
46
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
52                                         AND owner_uid = '$owner_uid') AS unread
53                         FROM ttrss_feeds
54                         WHERE
55                                 ttrss_feeds.owner_uid = '$owner_uid'
56                         ORDER BY $order_by $limit_qpart");
57
58                 if (!$offset) print '<ul id="home" title="'.__('Home').'" selected="true"
59                         myBackLabel="'.__('Logout').'" myBackHref="logout.php" myBackTarget="_self">';
60
61
62         //              print "<li><a href='#cat-actions'>".__('Actions...')."</a></li>";
63
64                         $num_feeds = 0;
65
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                                 }
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                                 }
90
91                                 ++$num_feeds;
92                         }
93
94 /*                      $next_offset = $offset + $num_feeds;
95
96                         print "<li><a href=\"home.php?skip=$next_offset\"
97         target=\"_replace\">Show more feeds...</a></li>"; */
98
99                         if (!$offset) print "</ul>";
100
101         }
102
103         function render_category($link, $cat_id, $offset) {
104                 $owner_uid = $_SESSION["uid"];
105
106                 if ($cat_id >= 0) {
107
108                         if ($cat_id != 0) {
109                                 $cat_query = "cat_id = '$cat_id'";
110                         } else {
111                                 $cat_query = "cat_id IS NULL";
112                         }
113
114                         if (mobile_get_pref($link, "SORT_FEEDS_UNREAD")) {
115                                 $order_by = "unread DESC, title";
116                         } else {
117                                 $order_by = "title";
118                         }
119
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
128                                 ttrss_feeds.owner_uid = '$owner_uid' AND
129                                 $cat_query
130                         ORDER BY $order_by");
131
132                         $title = getCategoryTitle($link, $cat_id);
133
134                         print "<ul id='cat-$cat_id' title='$title' myBackLabel='".__("Home")."'
135                                 myBackHref='home.php'>";
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"];
142
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                                 }
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                                 }
163                         }
164
165                         print "</ul>";
166                 } else if ($cat_id == -1) {
167
168                         $title = __('Special');
169
170                         print "<ul id='cat--1' title='$title' myBackLabel='".__("Home")."'
171                                 myBackHref='home.php'>";
172
173                         foreach (array(-4, -3, -1, -2, 0) as $id) {
174                                 $title = getFeedTitle($link, $id);
175                                 $unread = getFeedUnread($link, $id, false);
176                                 $icon = getFeedIcon($id);
177
178                                 if ($unread > 0) {
179                                         $title = $title . " ($unread)";
180                                         $class = '';
181                                 } else {
182                                         $class = 'oldItem';
183                                 }
184
185                                 if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
186                                         print "<li class='$class'>
187                                                 <a href='feed.php?id=$id&cat=-1'>
188                                                 <img class='tinyIcon' src='../$icon'/>$title</a></li>";
189                                 }
190                         }
191
192                         print "</ul>";
193                 } else if ($cat_id == -2) {
194
195                         $title = __('Labels');
196
197                         print "<ul id='cat--2' title='$title' myBackLabel='".__("Home")."'
198                                 myBackHref='home.php'>";
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
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                                 }
223                         }
224                         print "</ul>";
225                 }
226         }
227
228         function render_categories_list($link) {
229                 $owner_uid = $_SESSION["uid"];
230
231                 $cat_browse = mobile_get_pref($link, "BROWSE_CATS");
232
233                 print '<ul id="home" title="'.__('Home').'" selected="true"
234                         myBackLabel="'.__('Logout').'" myBackHref="logout.php" myBackTarget="_self">';
235
236 //              print "<li><a href='#searchForm'>Search...</a></li>";
237
238                 foreach (array(-1, -2) as $id) {
239                         $title = getCategoryTitle($link, $id);
240                         $unread = getFeedUnread($link, $id, true);
241                         if ($unread > 0) {
242                                 $title = $title . " ($unread)";
243                                 $class = '';
244                         } else {
245                                 $class = 'oldItem';
246                         }
247
248                         if ($cat_browse)
249                                 print "<li class='$class'><a href='cat.php?id=$id'>$title</a></li>";
250                         else
251                                 print "<li class='$class'><a href='feed.php?id=$id&is_cat=true'>$title</a></li>";
252                 }
253
254                 $result = db_query($link, "SELECT
255                                 ttrss_feed_categories.id,
256                                 ttrss_feed_categories.title,
257                                 COUNT(ttrss_feeds.id) AS num_feeds
258                         FROM ttrss_feed_categories, ttrss_feeds
259                         WHERE ttrss_feed_categories.owner_uid = $owner_uid
260                                 AND ttrss_feed_categories.id = cat_id
261                                 GROUP BY ttrss_feed_categories.id,
262                                         ttrss_feed_categories.title
263                                 ORDER BY ttrss_feed_categories.title");
264
265                 while ($line = db_fetch_assoc($result)) {
266
267                         if ($line["num_feeds"] > 0) {
268
269                                 $unread = getFeedUnread($link, $line["id"], true);
270                                 $id = $line["id"];
271
272                                 if ($unread > 0) {
273                                         $line["title"] = $line["title"] . " ($unread)";
274                                         $class = '';
275                                 } else {
276                                         $class = 'oldItem';
277                                 }
278
279                                 if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
280
281                                         if ($cat_browse)
282                                                 print "<li class='$class'><a href='cat.php?id=$id'>" .
283                                                         $line["title"] . "</a></li>";
284                                         else
285                                                 print "<li class='$class'><a href='feed.php?id=$id&is_cat=true'>".
286                                                         $line["title"] . "</a></li>";
287                                 }
288                         }
289                 }
290
291
292                 $result = db_query($link, "SELECT COUNT(*) AS nf FROM ttrss_feeds WHERE
293                         cat_id IS NULL and owner_uid = '$owner_uid'");
294
295                 $num_feeds = db_fetch_result($result, 0, "nf");
296
297                 if ($num_feeds > 0) {
298                         $unread = getFeedUnread($link, 0, true);
299                         $title = "Uncategorized";
300
301                         if ($unread > 0) {
302                                 $title = "$title ($unread)";
303                                 $class = '';
304                         } else {
305                                 $class = 'oldItem';
306                         }
307
308                         if ($unread > 0 || !mobile_get_pref($link, "HIDE_READ")) {
309                                 if ($cat_browse)
310                                         print "<li class='$class'><a href='cat.php?id=0'>$title</a></li>";
311                                 else
312                                         print "<li class='$class'><a href='feed.php?id=0&is_cat=true'>$title</a></li>";
313
314                         }
315                 }
316
317                 print "</ul>";
318         }
319
320         function render_headlines_list($link, $feed_id, $cat_id, $offset, $search,
321                 $is_cat = false) {
322
323                 $feed_id = $feed_id;
324                 $limit = 15;
325                 $filter = '';
326
327                 if (!mobile_get_pref($link, "HIDE_READ"))
328                         $view_mode = "all_articles";
329                 else
330                         $view_mode = 'adaptive';
331
332                 if ($search) {
333                         $search_mode = 'this_feed';
334                         $match_on = 'both';
335                 } else {
336                         $search_mode = '';
337                         $match_on = '';
338                 }
339
340                 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
341                         $view_mode, $is_cat, $search, $search_mode, $match_on,
342                         "score DESC, date_entered DESC", $offset);
343
344                 $result = $qfh_ret[0];
345                 $feed_title = $qfh_ret[1];
346
347                 if (!$offset) {
348
349                         print "<form id=\"searchForm-$feed_id-$cat_id\" class=\"dialog\" method=\"POST\"
350                                 action=\"feed.php\">
351
352                                 <input type=\"hidden\" name=\"id\" value=\"$feed_id\">
353                                 <input type=\"hidden\" name=\"cat\" value=\"$cat_id\">
354
355                 <fieldset>
356                           <h1>Search</h1>
357                     <a class=\"button leftButton\" type=\"cancel\">Cancel</a>
358                     <a class=\"button blueButton\" type=\"submit\">Search</a>
359
360                     <label>Search:</label>
361                                         <input id=\"search\" type=\"text\" name=\"search\"/>
362                 </fieldset>
363                           </form>";
364
365                         if ($cat_id) {
366                                 $cat_title = getCategoryTitle($link, $cat_id);
367
368                                 print "<ul id=\"feed-$feed_id\" title=\"$feed_title\" selected=\"true\"
369                                         myBackLabel='$cat_title' myBackHref='cat.php?id=$cat_id'>";
370                         } else {
371                                 print "<ul id=\"feed-$feed_id\" title=\"$feed_title\" selected=\"true\"
372                                         myBackLabel='".__("Home")."' myBackHref='home.php'>";
373                         }
374
375                         print "<li><a href='#searchForm-$feed_id-$cat_id'>Search...</a></li>";
376                 }
377
378                 $num_headlines = 0;
379
380                 while ($line = db_fetch_assoc($result)) {
381                         $id = $line["id"];
382                         $real_feed_id = $line["feed_id"];
383
384                         if (sql_bool_to_bool($line["unread"])) {
385                                 $class = '';
386                         } else {
387                                 $class = 'oldItem';
388                         }
389
390                         if (mobile_feed_has_icon($real_feed_id)) {
391                                 $icon_url = "../".ICONS_URL."/$real_feed_id.ico";
392                         } else {
393                                 $icon_url = "../images/blank_icon.gif";
394                         }
395
396                         print "<li class='$class'><a href='article.php?id=$id&feed=$feed_id&cat=$cat_id&is_cat=$is_cat'>
397                                 <img class='tinyIcon' src='$icon_url'>";
398                         print $line["title"];
399                         print "</a></li>";
400
401                         ++$num_headlines;
402
403                 }
404
405                 if ($num_headlines == 0 && $search) {
406                         $articles_url = "feed.php?id=$feed_id&cat=$cat_id&skip=$next_offset";
407
408                         print "<li><a href=\"$articles_url\">" . __("Nothing found (click to reload feed).") . "</a></li>";
409
410                 }
411
412 //              print "<a target='_replace' href='feed.php?id=$feed_id&cat=$cat_id&skip=0'>Next $limit articles...</a>";
413
414                 $next_offset = $offset + $num_headlines;
415                 $num_unread = getFeedUnread($link, $feed_id, $is_cat);
416
417                 /* FIXME needs normal implementation */
418
419                 if ($num_headlines > 0 && ($num_unread == 0 || $num_unread > $next_offset)) {
420
421                         if ($is_cat) {
422                                 $articles_url = "feed.php?id=$feed_id&skip=$next_offset".
423                                         "&search=$search&is_cat=true";
424                         } else {
425                                 $articles_url = "feed.php?id=$feed_id&cat=$cat_id&skip=$next_offset".
426                                         "&search=$search";
427                         }
428
429                         print "<li><a href=\"$articles_url\"
430                                 target=\"_replace\">Get more articles...</a></li>";
431                 }
432
433                 if (!$offset) print "</ul>";
434
435         }
436
437         function render_article($link, $id, $feed_id, $cat_id, $is_cat) {
438
439                 $query = "SELECT title,link,content,feed_id,comments,int_id,
440                         marked,unread,published,
441                         ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
442                         author
443                         FROM ttrss_entries,ttrss_user_entries
444                         WHERE   id = '$id' AND ref_id = id AND owner_uid = " .
445                                 $_SESSION["uid"] ;
446
447                 $result = db_query($link, $query);
448
449                 if (db_num_rows($result) != 0) {
450
451                         $line = db_fetch_assoc($result);
452
453                         $tmp_result = db_query($link, "UPDATE ttrss_user_entries
454                                 SET unread = false,last_read = NOW()
455                                 WHERE ref_id = '$id'
456                                 AND owner_uid = " . $_SESSION["uid"]);
457
458                         $updated_fmt = make_local_datetime($link, $line['updated'], false);
459
460                         $title = $line["title"];
461                         $article_link = $line["link"];
462
463                         if (!$is_cat)
464                                 $feed_title = getFeedTitle($link, $feed_id);
465                         else
466                                 $feed_title = getCategoryTitle($link, $feed_id);
467
468                         print "<div class=\"panel\" id=\"article-$id\" title=\"$title\"
469                                 selected=\"true\"
470                                 myBackLabel='$feed_title' myBackHref='feed.php?id=$feed_id&cat=$cat_id&is_cat=$is_cat'>";
471
472                         if ($line['feed_id'] != $feed_id) {
473                                 $real_feed_title = getFeedTitle($link, $line['feed_id']);
474                                 $real_feed_id = $line['feed_id'];
475                                 $feed_link = "(<a href=\"feed.php?id=$real_feed_id\">$real_feed_title</a>)";
476                         }
477 //                      print "<fieldset>";
478
479                         print "<div style='float : right'>($updated_fmt)</div>";
480
481                         print "<h2><a target='_blank' href='$article_link'>$title</a> $feed_link</h2>";
482
483                         print "<hr>";
484
485 /*                      print "<div class=\"row\">";
486                         print "<label id='title'><a target='_blank' href='$article_link'>$title</a></label>";
487                         print "</div>"; */
488
489                         $is_starred = (sql_bool_to_bool($line["marked"])) ? "true" : "false";
490                         $is_published = (sql_bool_to_bool($line["published"])) ? "true" : "false";
491
492                         //print "<div class=\"row\">";
493                         //print "<label id='updated'>Updated:</label>";
494                         //print "<input enabled='false' name='updated' disabled value='$updated_fmt'/>";
495                         //print "</div>";
496
497 //                      print "</fieldset>";
498
499                         $content = sanitize_rss($link, $line["content"]);
500                         $content = preg_replace("/href=/i", "target=\"_blank\" href=", $content);
501
502                         if (!mobile_get_pref($link, "SHOW_IMAGES")) {
503                                 $content = preg_replace('/<img[^>]+>/is', '', $content);
504                         }
505
506                         print "<p>$content</p>";
507
508             print "<div class='nav'>
509                     <label>Navigation</label>
510                     <div class='button left' onclick='goPrev($id, $feed_id, this)'>Prev</div>
511                     <div class='button right' onclick='goNext($id, $feed_id, this)'>Next</div>
512                   </div>";
513
514                         print "<fieldset>";
515
516                         print "<div class=\"row\">
517                         <label>Starred</label>
518                         <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>
519                     </div>";
520
521                         print "<div class=\"row\">
522                         <label>Published</label>
523                         <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>
524                     </div>";
525
526                         print "<div class=\"row\">
527                         <label>Unread</label>
528                         <div class=\"toggle\" onclick=\"toggleUnread($id, this)\" toggled=\"$is_unread\"><span class=\"thumb\"></span><span class=\"toggleOn\">ON</span><span class=\"toggleOff\">OFF</span></div>
529                     </div>";
530
531
532                         print "</fieldset>";
533
534                         print "</div>";
535
536                 }
537         }
538 ?>