]> git.wh0rd.org - tt-rss.git/blobdiff - modules/pref-labels.php
allow adding labels on the fly
[tt-rss.git] / modules / pref-labels.php
index c07d6132f0554e0a45b5f99aa350fb8d22bc8f29..d9cacecc366b52e39e63a184f910b1e4c37e4605 100644 (file)
 <?php
-       // We need to accept raw SQL data in label queries, so not everything is escaped
-       // here, this is by design. If you don't like it, disable labels
-       // altogether with GLOBAL_ENABLE_LABELS = false
-
        function module_pref_labels($link) {
-               if (!GLOBAL_ENABLE_LABELS) { 
-
-                       print "<p>Sorry, labels have been administratively disabled for this installation. Please contact instance owner or edit configuration file to enable this functionality.</p>";
-                       return; 
-               }
-
-               $subop = $_GET["subop"];
-
-               if ($subop == "edit") {
-
-                       $label_id = db_escape_string($_GET["id"]);
 
-                       $result = db_query($link, "SELECT sql_exp,description   FROM ttrss_labels WHERE 
-                               owner_uid = ".$_SESSION["uid"]." AND id = '$label_id' ORDER by description");
+               $subop = $_REQUEST["subop"];
 
-                       $line = db_fetch_assoc($result);
+               if ($subop == "color-set") {
+                       $kind = db_escape_string($_REQUEST["kind"]);
+                       $ids = split(',', db_escape_string($_REQUEST["ids"]));
+                       $color = db_escape_string($_REQUEST["color"]);
+                       $fg = db_escape_string($_REQUEST["fg"]);
+                       $bg = db_escape_string($_REQUEST["bg"]);
 
-                       $sql_exp = htmlspecialchars(db_unescape_string($line["sql_exp"]));
-                       $description = htmlspecialchars(db_unescape_string($line["description"]));
+                       foreach ($ids as $id) {
 
-                       print "<div id=\"infoBoxTitle\">Label editor</div>";
-                       print "<div class=\"infoBoxContents\">";
+                               if ($kind == "fg" || $kind == "bg") {
+                                       db_query($link, "UPDATE ttrss_labels2 SET
+                                               ${kind}_color = '$color' WHERE id = '$id'
+                                               AND owner_uid = " . $_SESSION["uid"]);                  
+                               } else {
+                                       db_query($link, "UPDATE ttrss_labels2 SET
+                                               fg_color = '$fg', bg_color = '$bg' WHERE id = '$id'
+                                               AND owner_uid = " . $_SESSION["uid"]);                  
+                               }
+                       }
 
-                       print "<form id=\"label_edit_form\" onsubmit='return false'>";
+                       return;
+               }
 
-                       print "<input type=\"hidden\" name=\"op\" value=\"pref-labels\">";
-                       print "<input type=\"hidden\" name=\"id\" value=\"$label_id\">";
-                       print "<input type=\"hidden\" name=\"subop\" value=\"editSave\">"; 
+               if ($subop == "color-reset") {
+                       $ids = split(',', db_escape_string($_REQUEST["ids"]));
 
-                       print "<table width='100%'>";
+                       foreach ($ids as $id) {
+                               db_query($link, "UPDATE ttrss_labels2 SET
+                                       fg_color = '', bg_color = '' WHERE id = '$id'
+                                       AND owner_uid = " . $_SESSION["uid"]);                  
+                       }
 
-                       print "<tr><td>Caption:</td>
-                               <td><input onkeypress=\"return filterCR(event, labelEditSave)\"
-                                       onkeyup=\"toggleSubmitNotEmpty(this, 'infobox_submit')\"
-                                       onchange=\"toggleSubmitNotEmpty(this, 'infobox_submit')\"
-                                        name=\"description\" class=\"iedit\" value=\"$description\">";
+               }
 
-                       print "</td></tr>";
+               if ($subop == "save") {
 
-                       print "<tr><td colspan=\"2\">
-                               <p>SQL Expression:</p>";
+                       $id = db_escape_string($_REQUEST["id"]);
+                       $caption = db_escape_string(trim($_REQUEST["value"]));
 
-                       print "<textarea onkeyup=\"toggleSubmitNotEmpty(this, 'infobox_submit')\"
-                                        rows=\"4\" name=\"sql_exp\" class=\"iedit\">$sql_exp</textarea>";
+                       db_query($link, "BEGIN");
 
-                       print "</td></tr></table>";
+                       $result = db_query($link, "SELECT caption FROM ttrss_labels2
+                               WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
 
-                       print "</form>";
+                       if (db_num_rows($result) != 0) {
+                               $old_caption = db_fetch_result($result, 0, "caption");
 
-                       print "<div style=\"display : none\" id=\"label_test_result\"></div>";
+                               $result = db_query($link, "SELECT id FROM ttrss_labels2
+                                       WHERE caption = '$caption' AND owner_uid = ". $_SESSION["uid"]);
 
-                       print "<div align='right'>";
+                               if (db_num_rows($result) == 0) {
+                                       if ($caption) {
+                                               $result = db_query($link, "UPDATE ttrss_labels2 SET
+                                                       caption = '$caption' WHERE id = '$id' AND
+                                                       owner_uid = " . $_SESSION["uid"]);
 
-                       $is_disabled = (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE) ? "disabled" : "";
+                                               /* Update filters that reference label being renamed */
 
-                       print "<input $is_disabled type=\"submit\" onclick=\"return labelTest()\" value=\"Test\">
-                               ";
+                                               $old_caption = db_escape_string($old_caption);
 
-                       print "<input type=\"submit\" 
-                               id=\"infobox_submit\"
-                               class=\"button\" onclick=\"return labelEditSave()\" 
-                               value=\"Save\"> ";
+                                               db_query($link, "UPDATE ttrss_filters SET
+                                                       action_param = '$caption' WHERE action_param = '$old_caption'
+                                                       AND action_id = 7
+                                                       AND owner_uid = " . $_SESSION["uid"]);
 
-                       print "<input class=\"button\"
-                               type=\"submit\" onclick=\"return labelEditCancel()\" 
-                               value=\"Cancel\">";
+                                               print $_REQUEST["value"];
+                                       } else {
+                                               print $old_caption;
+                                       }
+                               } else {
+                                       print $old_caption;
+                               }
+                       }
 
-                       print "</div>";
+                       db_query($link, "COMMIT");
 
                        return;
                }
 
-               if ($subop == "test") {
+               if ($subop == "remove") {
 
-                       $expr = db_unescape_string(trim($_GET["expr"]));
-                       $descr = db_unescape_string(trim($_GET["descr"]));
+                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
 
-                       print "<div>";
+                       foreach ($ids as $id) {
+                               label_remove($link, $id, $_SESSION["uid"]);
+                       }
 
-                       error_reporting(0);
+               }
 
+               if ($subop == "add") {
+                       $caption = db_escape_string($_REQUEST["caption"]);
+                       $output = db_escape_string($_REQUEST["output"]);
 
-                       $result = db_query($link, 
-                               "SELECT count(ttrss_entries.id) AS num_matches
-                                       FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
-                                       WHERE ($expr) AND 
-                                               ttrss_user_entries.ref_id = ttrss_entries.id AND
-                                               ttrss_user_entries.feed_id = ttrss_feeds.id AND
-                                               ttrss_user_entries.owner_uid = " . $_SESSION["uid"], false);
+                       if ($caption) {
 
-                       error_reporting (DEFAULT_ERROR_LEVEL);
+                               if (label_create($link, $caption)) {
+                                       if (!$output) {
+                                               print T_sprintf("Created label <b>%s</b>", htmlspecialchars($caption));
+                                       }
+                               }
 
-                       if (!$result) {
-                               print "<p>" . db_last_error($link) . "</p>";
-                               print "</div>";
-                               return;
-                       }
+                               if ($output == "select") {
+                                       header("Content-Type: text/xml");
 
-                       $num_matches = db_fetch_result($result, 0, "num_matches");;
-                       
-                       if ($num_matches > 0) { 
+                                       print "<rpc-reply><payload><![CDATA[";
 
-                               if ($num_matches > 10) {
-                                       $showing_msg = ", showing first 10";
-                               }
+                                       print_label_select($link, "select_label", 
+                                               $caption, "");
 
-                               print "<p>Query returned <b>$num_matches</b> matches$showing_msg:</p>";
-
-                               $result = db_query($link, 
-                                       "SELECT ttrss_entries.title, 
-                                               (SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
-                                       FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
-                                                       WHERE ($expr) AND 
-                                                       ttrss_user_entries.ref_id = ttrss_entries.id
-                                                       AND ttrss_user_entries.feed_id = ttrss_feeds.id
-                                                       AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"] . " 
-                                                       ORDER BY date_entered DESC LIMIT 10", false);
-
-                               print "<ul class=\"labelTestResults\">";
-
-                               $row_class = "even";
-                               
-                               while ($line = db_fetch_assoc($result)) {
-                                       $row_class = toggleEvenOdd($row_class);
-                                       
-                                       print "<li class=\"$row_class\">".$line["title"].
-                                               " <span class=\"insensitive\">(".$line["feed_title"].")</span></li>";
+                                       print "]]></payload></rpc-reply>";
                                }
-                               print "</ul>";
-
-                       } else {
-                               print "<p>Query didn't return any matches.</p>";
                        }
 
-                       print "</div>";
-
                        return;
                }
 
-               if ($subop == "editSave") {
+               set_pref($link, "_PREFS_ACTIVE_TAB", "labelConfig");
 
-                       $sql_exp = trim($_GET["sql_exp"]);
-                       $descr = db_escape_string(trim($_GET["description"]));
-                       $label_id = db_escape_string($_GET["id"]);
-                       
-                       $result = db_query($link, "UPDATE ttrss_labels SET 
-                               sql_exp = '$sql_exp', 
-                               description = '$descr'
-                               WHERE id = '$label_id'");
-
-                       if (db_affected_rows($link, $result) != 0) {
-                               print_notice(T_sprintf("Saved label <b>%s</b>", htmlspecialchars($descr)));
-                       }
+               $sort = db_escape_string($_REQUEST["sort"]);
 
+               if (!$sort || $sort == "undefined") {
+                       $sort = "caption";
                }
 
-               if ($subop == "remove") {
+               $label_search = db_escape_string($_REQUEST["search"]);
 
-                       if (!WEB_DEMO_MODE) {
+               if (array_key_exists("search", $_REQUEST)) {
+                       $_SESSION["prefs_label_search"] = $label_search;
+               } else {
+                       $label_search = $_SESSION["prefs_label_search"];
+               }
 
-                               $ids = split(",", db_escape_string($_GET["ids"]));
+               print "<div style='float : right'>
+                       <input id=\"label_search\" size=\"20\" type=\"search\"
+                               onfocus=\"javascript:disableHotkeys();\" 
+                               onblur=\"javascript:enableHotkeys();\"
+                               onchange=\"javascript:updateLabelList()\" value=\"$label_search\">
+                       <button onclick=\"javascript:updateLabelList()\">".__('Search')."</button>
+                       </div>";
 
-                               foreach ($ids as $id) {
-                                       db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
-                                       
-                               }
-                       }
-               }
+               print "<div class=\"prefGenericAddBox\">";
 
-               if ($subop == "add") {
+               print"<button onclick=\"return addLabel()\">".
+                       __('Create label')."</button> ";
 
-                       // no escaping is done here on purpose
-                       $sql_exp = trim($_GET["sql_exp"]);
-                       $description = db_escape_string($_GET["description"]);
+               print "<button onclick=\"javascript:removeSelectedLabels()\">".
+                       __('Remove')."</button> ";
 
-                       if (!$sql_exp || !$description) return;
+               print "<button onclick=\"labelColorReset()\">".
+                       __('Clear colors')."</button>";
 
-                       $result = db_query($link,
-                               "INSERT INTO ttrss_labels (sql_exp,description,owner_uid) 
-                               VALUES ('$sql_exp', '$description', '".$_SESSION["uid"]."')");
 
-                       if (db_affected_rows($link, $result) != 0) {
-                               print T_sprintf("Created label <b>%s</b>", htmlspecialchars($description));
-                       }
+               print "</div>";
 
-                       return;
-               }
+               if ($label_search) {
 
-               $sort = db_escape_string($_GET["sort"]);
+                       $label_search = split(" ", $label_search);
+                       $tokens = array();
 
-               if (!$sort || $sort == "undefined") {
-                       $sort = "description";
-               }
+                       foreach ($label_search as $token) {
 
-               print "<a class='helpLinkPic' href=\"javascript:displayHelpInfobox(1)\">
-                       <img src='images/sign_quest.png'></a>";
+                               $token = trim($token);
+                               array_push($tokens, "(UPPER(caption) LIKE UPPER('%$token%'))");
 
-               print "<div class=\"prefGenericAddBox\">";
+                       }
 
-               print"<input type=\"submit\" class=\"button\" 
-                       id=\"label_create_btn\"
-                       onclick=\"return displayDlg('quickAddLabel', false)\" 
-                       value=\"".__('Create label')."\"></div>";
+                       $label_search_query = "(" . join($tokens, " AND ") . ") AND ";
+                       
+               } else {
+                       $label_search_query = "";
+               }
 
                $result = db_query($link, "SELECT 
-                               id,sql_exp,description
+                               *
                        FROM 
-                               ttrss_labels 
+                               ttrss_labels2
                        WHERE 
+                               $label_search_query
                                owner_uid = ".$_SESSION["uid"]."
                        ORDER BY $sort");
 
                                class=\"prefLabelList\" id=\"prefLabelList\">";
 
                        print "<tr><td class=\"selectPrompt\" colspan=\"8\">
-                               Select: 
-                                       <a href=\"javascript:selectPrefRows('label', true)\">All</a>,
-                                       <a href=\"javascript:selectPrefRows('label', false)\">None</a>
+                               ".__('Select:')." 
+                                       <a href=\"javascript:selectPrefRows('label', true)\">".__('All')."</a>,
+                                       <a href=\"javascript:selectPrefRows('label', false)\">".__('None')."</a>
                                </td</tr>";
 
-                       print "<tr class=\"title\">
+/*                     print "<tr class=\"title\">
                                                <td width=\"5%\">&nbsp;</td>
-                                               <td width=\"30%\"><a href=\"javascript:updateLabelList('description')\">".__('Caption')."</a></td>
-                                               <td width=\"50%\"><a href=\"javascript:updateLabelList('sql_exp')\">".__('SQL Expression')."</a>
+                                               <td width=\"95%\"><a href=\"javascript:updateLabelList('caption')\">".__('Caption')."</a></td>
                                                </td>
-                                               </tr>";
+                                               </tr>"; */
                        
                        $lnum = 0;
                        
                                $class = ($lnum % 2) ? "even" : "odd";
        
                                $label_id = $line["id"];
-                               $edit_label_id = $_GET["id"];
-       
-                               if ($subop == "edit" && $label_id != $edit_label_id) {
-                                       $class .= "Grayed";
-                                       $this_row_id = "";
-                               } else {
-                                       $this_row_id = "id=\"LILRR-$label_id\"";
-                               }
-       
+                               $this_row_id = "id=\"LILRR-$label_id\"";
+
                                print "<tr class=\"$class\" $this_row_id>";
        
-                               $line["sql_exp"] = htmlspecialchars(db_unescape_string($line["sql_exp"]));
-                               $line["description"] = htmlspecialchars(
-                                               db_unescape_string($line["description"]));
-       
-                               if (!$line["description"]) $line["description"] = "[No caption]";
-       
-                               print "<td align='center'><input onclick='toggleSelectPrefRow(this, \"label\");' 
+                               $line["caption"] = htmlspecialchars($line["caption"]);
+
+                               $fg_color = $line["fg_color"];
+                               $bg_color = $line["bg_color"];
+
+                               if (!$fg_color) $fg_color = "";
+                               if (!$bg_color) $bg_color = "";
+
+                               print "<td width='5%' align='center'><input 
+                                       onclick='toggleSelectPrefRow(this, \"label\");' 
                                        type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
        
-                               print "<td><a href=\"javascript:editLabel($label_id);\">" . 
-                                       $line["description"] . "</td>";                 
+                               $id = $line['id'];
+
+                               print "<td>";
 
-                               print "<td><a href=\"javascript:editLabel($label_id);\">" . 
-                                       $line["sql_exp"] . "</td>";             
+                               print "<div class='labelColorIndicator' id='LICID-$id' 
+                                       style='color : $fg_color; background-color : $bg_color'
+                                       title='".__('Click to change color')."'
+                                       onclick=\"colorPicker('$id', '$fg_color', '$bg_color')\">&alpha;";
+                               print_color_picker($id);
+                               print "</div>";
+
+
+                               print "<span class='prefsLabelEntry' 
+                                       id=\"LILT-".$line["id"]."\">" . $line["caption"] . 
+                                       "</span>";
+
+                               print "</td>";
 
                                print "</tr>";
        
                                ++$lnum;
                        }
-       
-                       if ($lnum == 0) {
-                               print "<tr><td colspan=\"4\" align=\"center\">".__('No labels defined.')."</td></tr>";
-                       }
-       
+
                        print "</table>";
        
-                       print "<p id=\"labelOpToolbar\">";
-       
-                       print "<input type=\"submit\" class=\"button\" disabled=\"true\"
-                                       onclick=\"javascript:editSelectedLabel()\" value=\"".__('Edit')."\">
-                               <input type=\"submit\" class=\"button\" disabled=\"true\"
-                               onclick=\"javascript:removeSelectedLabels()\" value=\"".__('Remove')."\">";
 
                } else {
-                       print "<p>".__('No labels defined.')."</p>";
+                       print "<p>";
+                       if (!$label_search) {
+                               print_warning(__('No labels defined.'));
+                       } else {
+                               print_warning(__('No matching labels found.'));
+                       }
+                       print "</p>";
+
+               }
+       }
+
+       function print_color_picker($id) {
+
+               print "<div id=\"colorPicker-$id\" 
+                       onmouseover=\"colorPickerActive(true)\"
+                       onmouseout=\"colorPickerActive(false)\"
+                       class=\"colorPicker\" style='display : none'>";
+
+               $color_picker_pairs = array(
+                       array('#ff0000', '#ffffff'),
+                       array('#009000', '#ffffff'),
+                       array('#0000ff', '#ffffff'),    
+                       array('#ff00ff', '#ffffff'),                            
+                       array('#009090', '#ffffff'),
+
+                       array('#ffffff', '#ff0000'),
+                       array('#000000', '#00ff00'),
+                       array('#ffffff', '#0000ff'),
+                       array('#ffffff', '#ff00ff'),
+                       array('#000000', '#00ffff'),
+
+                       array('#7b07e1', '#ffffff'),
+                       array('#0091b4', '#ffffff'),
+                       array('#00aa71', '#ffffff'),
+                       array('#7d9e01', '#ffffff'),
+                       array('#e14a00', '#ffffff'),
+
+                       array('#ffffff', '#7b07e1'),
+                       array('#ffffff', '#00b5e1'),
+                       array('#ffffff', '#00e196'),
+                       array('#ffffff', '#b3e100'),
+                       array('#ffffff', '#e14a00'),
+
+                       array('#000000', '#ffffff'),
+                       array('#ffffff', '#000000'),
+                       array('#ffffff', '#909000'),
+                       array('#063064', '#fff7d5'),
+                       array('#ffffff', '#4E4E90'),
+               );
+
+               foreach ($color_picker_pairs as $c) { 
+                       $fg_color = $c[0];
+                       $bg_color = $c[1];
+
+                       print "<div class='colorPickerEntry' 
+                               style='color : $fg_color; background-color : $bg_color;'
+                               onclick=\"colorPickerDo('$id', '$fg_color', '$bg_color')\">&alpha;</div>";
+
                }
+
+               print "<br clear='both'>";
+
+               print "<br/><b>".__('custom color:')."</b>";
+               print "<div class=\"ccPrompt\" onclick=\"labelColorAsk('$id', 'fg')\">".__("foreground")."</div>";
+               print "<div class=\"ccPrompt\" onclick=\"labelColorAsk('$id', 'bg')\">".__("background")."</div>";
+
+               print "</div>";
        }
+
 ?>