]> git.wh0rd.org Git - tt-rss.git/commitdiff
allow adding cats from feed editor
authorAndrew Dolgov <fox@fakecake.org>
Sun, 7 Nov 2010 20:30:05 +0000 (23:30 +0300)
committerAndrew Dolgov <fox@fakecake.org>
Sun, 7 Nov 2010 20:30:05 +0000 (23:30 +0300)
functions.js
functions.php
modules/backend-rpc.php
modules/pref-feeds.php

index b9096706a6c3a65249e23b9d22ce837b1a41cbe7..f3ae1965ebf8cb76fc19e18052da4f21d97fe4c6 100644 (file)
@@ -2169,3 +2169,50 @@ function has_local_storage() {
                return false;
        }
 }
+
+function catSelectOnChange(elem) {
+       try {
+               var value = elem[elem.selectedIndex].value;
+               var def = elem.getAttribute('default');
+
+               if (value == "ADD_CAT") {
+
+                       if (def)
+                               dropboxSelect(elem, def);
+                       else
+                               elem.selectedIndex = 0;
+
+                       quickAddCat(elem);
+               }
+
+       } catch (e) {
+               exception_error("catSelectOnChange", e);
+       }
+}
+
+function quickAddCat(select) {
+       try {
+               var cat = prompt(__("Please enter category title:"));
+
+               if (cat) {
+
+                       var query = "?op=rpc&subop=quickAddCat&cat=" + param_escape(cat);
+
+                       new Ajax.Request("backend.php", {
+                               parameters: query,
+                               onComplete: function (transport) {
+                                       var response = transport.responseXML;
+
+                                       var payload = response.getElementsByTagName("payload")[0];
+
+                                       if (payload)
+                                               select.innerHTML = payload.firstChild.nodeValue;
+
+                       } });
+
+               }
+
+       } catch (e) {
+               exception_error("quickAddCat", e);
+       }
+}
index 8ef14d70b73d1fa32042b0c58c3af95db6ad813d..5c63eae043e8e2c88bad0e850afe7d75de6672fc 100644 (file)
        function print_feed_cat_select($link, $id, $default_id = "", 
                $attributes = "", $include_all_cats = true) {
                
-               print "<select id=\"$id\" name=\"$id\" $attributes>";
+               print "<select id=\"$id\" name=\"$id\" default=\"$default_id\" onchange=\"catSelectOnChange(this)\" $attributes>";
 
                if ($include_all_cats) {
                        print "<option value=\"0\">".__('Uncategorized')."</option>";
                                $line["id"], htmlspecialchars($line["title"]));
                }
 
+               print "<option value=\"ADD_CAT\">" .__("Add category...") . "</option>";
+
                print "</select>";
        }
        
                }
        }
 
+       function add_feed_category($link, $feed_cat) {
+               db_query($link, "BEGIN");
+
+               $result = db_query($link,
+                       "SELECT id FROM ttrss_feed_categories
+                       WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
+
+               if (db_num_rows($result) == 0) {
+                       
+                       $result = db_query($link,
+                               "INSERT INTO ttrss_feed_categories (owner_uid,title) 
+                               VALUES ('".$_SESSION["uid"]."', '$feed_cat')");
+
+                       db_query($link, "COMMIT");
+
+                       return true;
+               }
+
+               return false;
+       }                       
+
        function remove_feed_category($link, $id, $owner_uid) {
 
                db_query($link, "DELETE FROM ttrss_feed_categories
index fee93a58643ab746080fd080a2ec63440a5b361b..4a82a888bcd0b2d193c4bce50ec72b5185e3dd28 100644 (file)
                        return;
                }
 
+               if ($subop == "quickAddCat") {
+                       print "<rpc-reply>";    
+
+                       $cat = db_escape_string($_REQUEST["cat"]);
+
+                       add_feed_category($link, $cat);
+
+                       $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
+                               title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
+
+                       if (db_num_rows($result) == 1) {
+                               $id = db_fetch_result($result, 0, "id");
+                       } else {
+                               $id = 0;
+                       }
+
+                       print "<payload><![CDATA[";
+                       print_feed_cat_select($link, "cat_id", $id);
+                       print "]]></payload>";
+
+                       print "</rpc-reply>";
+
+                       return;
+               }
+
                print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
        }
 ?>
index 376980ce3f6894371515f6c449a4e850b52da106..7a436cfd20fa1fc67d12e82bce3f5d9afc6359e8 100644 (file)
 
                        if ($action == "add") {
 
-       
                                $feed_cat = db_escape_string(trim($_REQUEST["cat"]));
 
-                               $result = db_query($link,
-                                       "SELECT id FROM ttrss_feed_categories
-                                       WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
-
-                               if (db_num_rows($result) == 0) {
-                                       
-                                       $result = db_query($link,
-                                               "INSERT INTO ttrss_feed_categories (owner_uid,title) 
-                                               VALUES ('".$_SESSION["uid"]."', '$feed_cat')");
-
-                               } else {
-
-                                       print_warning(T_sprintf("Category <b>$%s</b> already exists in the database.", 
-                                               $feed_cat));
-                               }
+                               if (!add_feed_category($link, $feed_cat))
+                                       print_warning(T_sprintf("Category <b>$%s</b> already exists in the database.", $feed_cat));
 
                        }