]> git.wh0rd.org - tt-rss.git/blobdiff - classes/pref_feeds.php
fix drag-and-drop categorizing of feeds into nested cats
[tt-rss.git] / classes / pref_feeds.php
index d292319cbf11814ff6c5baf0a616fe663507d708..4056c8a4ebb25e86781ad481efe2e6c2917ab2a6 100644 (file)
@@ -33,6 +33,8 @@ class Pref_Feeds extends Protected_Handler {
        }
 
        private function get_category_items($cat_id) {
+               $show_empty_cats = $_REQUEST['mode'] != 2 &&
+                       get_pref($this->link, '_PREFS_SHOW_EMPTY_CATS');
 
                $items = array();
 
@@ -280,9 +282,16 @@ class Pref_Feeds extends Protected_Handler {
                        (get_pref($this->link, '_PREFS_SHOW_EMPTY_CATS') ? 'false' : 'true'));
        }
 
-       private function process_category_order(&$data_map, $item_id, $parent_id = false) {
-#              print "C: $item_id P: $parent_id\n";
-               $bare_id = substr($item_id, strpos($item_id, ':')+1);
+       private function process_category_order(&$data_map, $item_id, $parent_id = false, $nest_level = 0) {
+               $debug = isset($_REQUEST["debug"]);
+
+               $prefix = "";
+               for ($i = 0; $i < $nest_level; $i++)
+                       $prefix .= "   ";
+
+               if ($debug) _debug("$prefix C: $item_id P: $parent_id");
+
+               $bare_item_id = substr($item_id, strpos($item_id, ':')+1);
 
                if ($item_id != 'root') {
                        if ($parent_id && $parent_id != 'root') {
@@ -293,7 +302,7 @@ class Pref_Feeds extends Protected_Handler {
                        }
 
                        db_query($this->link, "UPDATE ttrss_feed_categories
-                               SET parent_cat = $parent_qpart WHERE id = '$bare_id' AND
+                               SET parent_cat = $parent_qpart WHERE id = '$bare_item_id' AND
                                owner_uid = " . $_SESSION["uid"]);
                }
 
@@ -306,18 +315,23 @@ class Pref_Feeds extends Protected_Handler {
                                $id = $item['_reference'];
                                $bare_id = substr($id, strpos($id, ':')+1);
 
-#                              print "[$order_id] $id/$bare_id\n";
+                               if ($debug) _debug("$prefix [$order_id] $id/$bare_id");
 
                                if ($item['_reference']) {
 
                                        if (strpos($id, "FEED") === 0) {
 
+                                               $cat_id = ($item_id != "root") ?
+                                                       db_escape_string($bare_item_id) : "NULL";
+
                                                db_query($this->link, "UPDATE ttrss_feeds
-                                                               SET order_id = '$order_id' WHERE id = '$bare_id' AND
+                                                       SET order_id = $order_id, cat_id = '$cat_id'
+                                                       WHERE id = '$bare_id' AND
                                                                owner_uid = " . $_SESSION["uid"]);
 
                                        } else if (strpos($id, "CAT:") === 0) {
-                                               $this->process_category_order($data_map, $item['_reference'], $item_id);
+                                               $this->process_category_order($data_map, $item['_reference'], $item_id,
+                                                       $nest_level+1);
 
                                                if ($item_id != 'root') {
                                                        $parent_qpart = db_escape_string($bare_id);
@@ -342,7 +356,10 @@ class Pref_Feeds extends Protected_Handler {
                #file_put_contents("/tmp/saveorder.json", $_POST['payload']);
                #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
 
-               $data['items'] = json_decode($data['items'], true);
+               if (!is_array($data['items']))
+                       $data['items'] = json_decode($data['items'], true);
+
+#              print_r($data['items']);
 
                if (is_array($data) && is_array($data['items'])) {
                        $cat_order_id = 0;
@@ -1288,162 +1305,17 @@ class Pref_Feeds extends Protected_Handler {
                db_query($this->link, "COMMIT");
        }
 
-       function editCats() {
-
-               $action = $_REQUEST["action"];
-
-               if ($action == "save") {
-
-                       $cat_title = db_escape_string(trim($_REQUEST["value"]));
-                       $cat_id = db_escape_string($_REQUEST["cid"]);
-
-                       db_query($this->link, "BEGIN");
-
-                       $result = db_query($this->link, "SELECT title FROM ttrss_feed_categories
-                               WHERE id = '$cat_id' AND owner_uid = ".$_SESSION["uid"]);
-
-                       if (db_num_rows($result) == 1) {
-
-                               $old_title = db_fetch_result($result, 0, "title");
-
-                               if ($cat_title != "") {
-                                       $result = db_query($this->link, "UPDATE ttrss_feed_categories SET
-                                               title = '$cat_title' WHERE id = '$cat_id' AND
-                                               owner_uid = ".$_SESSION["uid"]);
-
-                                       print $cat_title;
-                               } else {
-                                       print $old_title;
-                               }
-                       } else {
-                               print $_REQUEST["value"];
-                       }
-
-                       db_query($this->link, "COMMIT");
-
-                       return;
-
-               }
-
-               if ($action == "add") {
-
-                       $feed_cat = db_escape_string(trim($_REQUEST["cat"]));
-
-                       if (!add_feed_category($this->link, $feed_cat))
-                               print_warning(T_sprintf("Category <b>$%s</b> already exists in the database.", $feed_cat));
-
-               }
-
-               if ($action == "remove") {
-
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
-
-                       foreach ($ids as $id) {
-                               remove_feed_category($this->link, $id, $_SESSION["uid"]);
-                       }
-               }
-
-               print "<div dojoType=\"dijit.Toolbar\">";
-
-               print "<div dojoType=\"dijit.form.DropDownButton\">".
-                               "<span>" . __('Select')."</span>";
-               print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
-               print "<div onclick=\"selectTableRows('prefFeedCatList', 'all')\"
-                       dojoType=\"dijit.MenuItem\">".__('All')."</div>";
-               print "<div onclick=\"selectTableRows('prefFeedCatList', 'none')\"
-                       dojoType=\"dijit.MenuItem\">".__('None')."</div>";
-               print "</div></div>";
-
-               print "<div style='float : right'>";
-
-               print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"newcat\">
-                               <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedCatEditDlg').addCategory()\">".
-                                       __('Create category')."</button></div>";
-
-               print "</div>";
-
-               $result = db_query($this->link, "SELECT c.title, c.id,COUNT(f.id) AS count
-                       FROM ttrss_feed_categories AS c LEFT JOIN ttrss_feeds AS f ON
-                               (f.cat_id = c.id)
-                       WHERE c.owner_uid = ".$_SESSION["uid"]."
-                       GROUP BY c.title, c.id ORDER BY title");
-
-               if (db_num_rows($result) != 0) {
-
-                       print "<div class=\"prefFeedCatHolder\">";
-
-                       print "<table width=\"100%\" class=\"prefFeedCatList\"
-                               cellspacing=\"0\" id=\"prefFeedCatList\">";
-
-                       $lnum = 0;
-
-                       while ($line = db_fetch_assoc($result)) {
-
-                               $class = ($lnum % 2) ? "even" : "odd";
-
-                               $cat_id = $line["id"];
-                               $this_row_id = "id=\"FCATR-$cat_id\"";
-
-                               print "<tr class=\"placeholder\" $this_row_id>";
-
-                               $edit_title = htmlspecialchars($line["title"]);
-
-                               print "<td width='5%' align='center'><input id=\"FCATC-$cat_id\"
-                                       onclick='toggleSelectRow2(this);' dojoType=\"dijit.form.CheckBox\"
-                                       type=\"checkbox\"></td>";
-
-                               print "<td>";
-
-                               if ($line['count'] == 0) print '<em>';
-
-                               print "<span dojoType=\"dijit.InlineEditBox\"
-                                       width=\"300px\" autoSave=\"false\"
-                                       cat-id=\"$cat_id\">" . $edit_title .
-                                       "<script type=\"dojo/method\" event=\"onChange\" args=\"item\">
-                                               var elem = this;
-                                               dojo.xhrPost({
-                                                       url: 'backend.php',
-                                                       content: {op: 'pref-feeds', method: 'editCats',
-                                                               action: 'save',
-                                                               value: this.value,
-                                                               cid: this.srcNodeRef.getAttribute('cat-id')},
-                                                               load: function(response) {
-                                                                       elem.attr('value', response);
-                                                                       updateFeedList();
-                                                       }
-                                               });
-                                       </script>
-                               </span>";
-
-                               if ($line['count'] == 0) print '</em>';
-
-                               print "</td>";
-
-                               print "<td align='right' class='insensitive'>";
-                               echo T_sprintf("%d feeds", $line['count']);
-                               print "</td></tr>";
-
-                               ++$lnum;
-                       }
-
-                       print "</table>";
-                       print "</div>";
-
-               } else {
-                       print "<p>".__('No feed categories defined.')."</p>";
+       function removeCat() {
+               $ids = split(",", db_escape_string($_REQUEST["ids"]));
+               foreach ($ids as $id) {
+                       remove_feed_category($this->link, $id, $_SESSION["uid"]);
                }
+       }
 
-               print "<div class='dlgButtons'>
-                       <div style='float : left'>
-                       <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedCatEditDlg').removeSelected()\">".
-                       __('Remove selected categories')."</button>
-                       </div>";
-
-               print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedCatEditDlg').hide()\">".
-                       __('Close this window')."</button></div>";
-
-               return;
+       function addCat() {
+               $feed_cat = db_escape_string(trim($_REQUEST["cat"]));
 
+               add_feed_category($this->link, $feed_cat);
        }
 
        function index() {
@@ -1528,8 +1400,8 @@ class Pref_Feeds extends Protected_Handler {
                        print "<div dojoType=\"dijit.form.DropDownButton\">".
                                        "<span>" . __('Categories')."</span>";
                        print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
-                       print "<div onclick=\"editFeedCats()\"
-                               dojoType=\"dijit.MenuItem\">".__('Edit categories')."</div>";
+                       print "<div onclick=\"createCategory()\"
+                               dojoType=\"dijit.MenuItem\">".__('Add category')."</div>";
                        print "<div onclick=\"toggleHiddenFeedCats()\"
                                dojoType=\"dijit.MenuItem\">".__('(Un)hide empty categories')."</div>";
                        print "<div onclick=\"resetCatOrder()\"