Merge branch 'wip-filter-stuff'

master
Andrew Dolgov 2 years ago
commit fb471652c0
  1. 15
      classes/labels.php
  2. 499
      classes/pref/filters.php
  3. 4
      classes/rpc.php
  4. 16
      include/controls.php
  5. 21
      js/App.js
  6. 791
      js/CommonFilters.js
  7. 22
      js/common.js
  8. 1
      plugins/auto_assign_labels/init.php
  9. 20
      themes/compact.css
  10. 20
      themes/compact_night.css
  11. 20
      themes/light.css
  12. 8
      themes/light/tt-rss.less
  13. 20
      themes/night.css
  14. 20
      themes/night_blue.css

@ -37,7 +37,18 @@ class Labels
}
}
static function get_all_labels($owner_uid) {
static function get_as_hash($owner_uid) {
$rv = [];
$labels = Labels::get_all($owner_uid);
foreach ($labels as $i => $label) {
$rv[$label["id"]] = $labels[$i];
}
return $rv;
}
static function get_all($owner_uid) {
$rv = array();
$pdo = Db::pdo();
@ -46,7 +57,7 @@ class Labels
WHERE owner_uid = ? ORDER BY caption");
$sth->execute([$owner_uid]);
while ($line = $sth->fetch()) {
while ($line = $sth->fetch(PDO::FETCH_ASSOC)) {
array_push($rv, $line);
}

@ -319,162 +319,90 @@ class Pref_Filters extends Handler_Protected {
$sth->execute([$filter_id, $_SESSION['uid']]);
if (empty($filter_id) || $row = $sth->fetch()) {
$rv = [
"id" => $filter_id,
"enabled" => $row["enabled"] ?? true,
"match_any_rule" => $row["match_any_rule"] ?? false,
"inverse" => $row["inverse"] ?? false,
"title" => $row["title"] ?? "",
"rules" => [],
"actions" => [],
"filter_types" => [],
"action_types" => [],
"plugin_actions" => [],
"labels" => Labels::get_all($_SESSION["uid"])
];
$res = $this->pdo->query("SELECT id,description
FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
while ($line = $res->fetch()) {
$rv["filter_types"][$line["id"]] = __($line["description"]);
}
$enabled = $row["enabled"] ?? true;
$match_any_rule = $row["match_any_rule"] ?? false;
$inverse = $row["inverse"] ?? false;
$title = htmlspecialchars($row["title"] ?? "");
print "<form onsubmit='return false'>";
print \Controls\hidden_tag("op", "pref-filters");
$res = $this->pdo->query("SELECT id,description FROM ttrss_filter_actions
ORDER BY name");
if ($filter_id) {
print \Controls\hidden_tag("id", "$filter_id");
print \Controls\hidden_tag("method", "editSave");
} else {
print \Controls\hidden_tag("method", "add");
while ($line = $res->fetch()) {
$rv["action_types"][$line["id"]] = __($line["description"]);
}
print \Controls\hidden_tag("csrf_token", $_SESSION['csrf_token']);
print "<header>".__("Caption")."</header>
<section>
<input required='true' dojoType='dijit.form.ValidationTextBox' style='width : 20em;' name=\"title\" value=\"$title\">
</section>
<header class='horizontal'>".__("Match")."</header>
<section>
<div dojoType='fox.Toolbar'>
<div dojoType='fox.form.DropDownButton'>
<span>" . __('Select')."</span>
<div dojoType='dijit.Menu' style='display: none;'>
<!-- can't use App.dialogOf() here because DropDownButton is not a child of the Dialog -->
<div onclick='dijit.byId(\"filterEditDlg\").selectRules(true)'
dojoType='dijit.MenuItem'>".__('All')."</div>
<div onclick='dijit.byId(\"filterEditDlg\").selectRules(false)'
dojoType='dijit.MenuItem'>".__('None')."</div>
</div>
</div>
<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).addRule()'>".
__('Add')."</button>
<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).deleteRule()'>".
__('Delete')."</button>
</div>";
$filter_actions = PluginHost::getInstance()->get_filter_actions();
print "<ul id='filterDlg_Matches'>";
foreach ($filter_actions as $fclass => $factions) {
foreach ($factions as $faction) {
$rv["plugin_actions"][$fclass . ":" . $faction["action"]] =
$fclass . ": " . $faction["description"];
}
}
if ($filter_id) {
$rules_sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_rules
WHERE filter_id = ? ORDER BY reg_exp, id");
$rules_sth->execute([$filter_id]);
$rules_sth->execute([$filter_id]);
while ($line = $rules_sth->fetch()) {
if ($line["match_on"]) {
$line["feed_id"] = json_decode($line["match_on"], true);
while ($rrow = $rules_sth->fetch(PDO::FETCH_ASSOC)) {
if ($rrow["match_on"]) {
$rrow["feed_id"] = json_decode($rrow["match_on"], true);
} else {
if ($line["cat_filter"]) {
$feed_id = "CAT:" . (int)$line["cat_id"];
if ($rrow["cat_filter"]) {
$feed_id = "CAT:" . (int)$rrow["cat_id"];
} else {
$feed_id = (int)$line["feed_id"];
$feed_id = (int)$rrow["feed_id"];
}
$line["feed_id"] = ["" . $feed_id]; // set item type to string for in_array()
$rrow["feed_id"] = ["" . $feed_id]; // set item type to string for in_array()
}
unset($line["cat_filter"]);
unset($line["cat_id"]);
unset($line["filter_id"]);
unset($line["id"]);
if (!$line["inverse"]) unset($line["inverse"]);
unset($line["match_on"]);
unset($rrow["cat_filter"]);
unset($rrow["cat_id"]);
unset($rrow["filter_id"]);
unset($rrow["id"]);
if (!$rrow["inverse"]) unset($rrow["inverse"]);
unset($rrow["match_on"]);
print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='Lists.onRowChecked(this)'>
<span onclick='App.dialogOf(this).editRule(this)'>".$this->_get_rule_name($line)."</span>".
\Controls\hidden_tag("rule[]", (string)json_encode($line))."</li>";
}
}
$rrow["name"] = $this->_get_rule_name($rrow);
print "</ul>
</section>";
print "<header class='horizontal'>".__("Apply actions")."</header>
<section>
<div dojoType='fox.Toolbar'>
<div dojoType='fox.form.DropDownButton'>
<span>".__('Select')."</span>
<div dojoType='dijit.Menu' style='display: none'>
<div onclick='dijit.byId(\"filterEditDlg\").selectActions(true)'
dojoType='dijit.MenuItem'>".__('All')."</div>
<div onclick='dijit.byId(\"filterEditDlg\").selectActions(false)'
dojoType='dijit.MenuItem'>".__('None')."</div>
</div>
</div>
<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).addAction()'>".
__('Add')."</button>
<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).deleteAction()'>".
__('Delete')."</button>
</div>";
print "<ul id='filterDlg_Actions'>";
array_push($rv["rules"], $rrow);
}
if ($filter_id) {
$actions_sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions
WHERE filter_id = ? ORDER BY id");
$actions_sth->execute([$filter_id]);
while ($line = $actions_sth->fetch()) {
$line["action_param_label"] = $line["action_param"];
unset($line["filter_id"]);
unset($line["id"]);
print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='Lists.onRowChecked(this)'>
<span onclick='App.dialogOf(this).editAction(this)'>".$this->_get_action_name($line)."</span>".
\Controls\hidden_tag("action[]", (string)json_encode($line))."</li>";
}
}
print "</ul>";
print "</section>";
while ($arow = $actions_sth->fetch(PDO::FETCH_ASSOC)) {
$arow["action_param_label"] = $arow["action_param"];
print "<header>".__("Options")."</header>
<section>";
unset($arow["filter_id"]);
unset($arow["id"]);
print "<fieldset class='narrow'>
<label class='checkbox'>".\Controls\checkbox_tag('enabled', $enabled)." ".__('Enabled')."</label></fieldset>";
$arow["name"] = $this->_get_action_name($arow);
print "<fieldset class='narrow'>
<label class='checkbox'>".\Controls\checkbox_tag('match_any_rule', $match_any_rule)." ".__('Match any rule')."</label>
</fieldset>";
print "<fieldset class='narrow'><label class='checkbox'>".\Controls\checkbox_tag('inverse', $inverse)." ".__('Inverse matching')."</label>
</fieldset>";
print "</section>
<footer>";
if ($filter_id) {
print "<div style='float : left'>
<button dojoType='dijit.form.Button' class='alt-danger' onclick='App.dialogOf(this).removeFilter()'>".
__('Remove')."</button>
</div>
<button dojoType='dijit.form.Button' class='alt-info' onclick='App.dialogOf(this).test()'>".
__('Test')."</button>
<button dojoType='dijit.form.Button' type='submit' class='alt-primary' onclick='App.dialogOf(this).execute()'>".
__('Save')."</button>
<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).hide()'>".
__('Cancel')."</button>";
} else {
print "<button dojoType='dijit.form.Button' class='alt-info' onclick='App.dialogOf(this).test()'>".
__('Test')."</button>
<button dojoType='dijit.form.Button' type='submit' class='alt-primary' onclick='App.dialogOf(this).execute()'>".
__('Create')."</button>
<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).hide()'>".
__('Cancel')."</button>";
array_push($rv["actions"], $arow);
}
}
print "</footer></form>";
print json_encode($rv);
}
}
@ -592,8 +520,7 @@ class Pref_Filters extends Handler_Protected {
$sth->execute(array_merge($ids, [$_SESSION['uid']]));
}
private function _save_rules_and_actions($filter_id)
{
private function _save_rules_and_actions($filter_id) {
$sth = $this->pdo->prepare("DELETE FROM ttrss_filters2_rules WHERE filter_id = ?");
$sth->execute([$filter_id]);
@ -670,7 +597,7 @@ class Pref_Filters extends Handler_Protected {
}
}
function add() {
function add () {
$enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"] ?? false));
$match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"] ?? false));
$title = clean($_REQUEST["title"]);
@ -764,176 +691,12 @@ class Pref_Filters extends Handler_Protected {
<?php
}
function newrule() {
$rule = json_decode(clean($_REQUEST["rule"]), true);
if ($rule) {
$reg_exp = htmlspecialchars($rule["reg_exp"]);
$filter_type = $rule["filter_type"];
$feed_id = $rule["feed_id"];
$inverse_checked = !empty($rule["inverse"]);
} else {
$reg_exp = "";
$filter_type = 1;
$feed_id = ["0"];
$inverse_checked = false;
}
print "<form name='filter_new_rule_form' id='filter_new_rule_form' onsubmit='return false;'>";
$res = $this->pdo->query("SELECT id,description
FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
$filter_types = array();
while ($line = $res->fetch()) {
$filter_types[$line["id"]] = __($line["description"]);
}
print "<header>".__("Match")."</header>";
print "<section>";
print "<textarea dojoType='fox.form.ValidationTextArea'
required='true' id='filterDlg_regExp'
ValidRegExp='true'
rows='4'
style='font-size : 14px; width : 490px; word-break: break-all'
name='reg_exp'>$reg_exp</textarea>";
print "<div dojoType='dijit.Tooltip' id='filterDlg_regExp_tip' connectId='filterDlg_regExp' position='below'></div>";
print "<fieldset>";
print "<label class='checkbox'>".
\Controls\checkbox_tag("inverse", $inverse_checked) .
__("Inverse regular expression matching")."</label>";
print "</fieldset>";
print "<fieldset>";
print "<label style='display : inline'>". __("on field") . "</label> ";
print \Controls\select_hash("filter_type", $filter_type, $filter_types);
print "<label style='padding-left : 10px; display : inline'>" . __("in") . "</label> ";
print "</fieldset>";
print "<fieldset>";
print "<span id='filterDlg_feeds'>";
$this->feed_multi_select("feed_id",
$feed_id,
'style="width : 500px; height : 300px" dojoType="dijit.form.MultiSelect"');
print "</span>";
print "</fieldset>";
print "</section>";
print "<footer>";
print "<button dojoType='dijit.form.Button' style='float : left' class='alt-info' onclick='window.open(\"https://tt-rss.org/wiki/ContentFilters\")'>
<i class='material-icons'>help</i> ".__("More info...")."</button>";
print "<button dojoType='dijit.form.Button' class='alt-primary' type='submit' onclick='App.dialogOf(this).execute()'>".
($rule ? __("Save rule") : __('Add rule'))."</button> ";
print "<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).hide()'>".
__('Cancel')."</button>";
print "</footer>";
print "</form>";
}
function newaction() {
$action = json_decode(clean($_REQUEST["action"]), true);
if ($action) {
$action_param = $action["action_param"];
$action_id = (int)$action["action_id"];
} else {
$action_param = "";
$action_id = 0;
}
print "<form name='filter_new_action_form' id='filter_new_action_form' onsubmit='return false;'>";
function editrule() {
$feed_ids = explode(",", clean($_REQUEST["ids"]));
print "<header>".__("Perform Action")."</header>";
print "<section>";
print "<select name='action_id' dojoType='fox.form.Select'
onchange='Filters.filterDlgCheckAction(this)'>";
$res = $this->pdo->query("SELECT id,description FROM ttrss_filter_actions
ORDER BY name");
while ($line = $res->fetch()) {
$is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
}
print "</select>";
$param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6 || $action_id == 9) ?
"" : "display : none";
$param_hidden = ($action_id == 4 || $action_id == 6) ?
"" : "display : none";
$label_param_hidden = ($action_id == 7) ? "" : "display : none";
$plugin_param_hidden = ($action_id == 9) ? "" : "display : none";
print "<span id='filterDlg_paramBox' style=\"$param_box_hidden\">";
print " ";
//print " " . __("with parameters:") . " ";
print "<input dojoType='dijit.form.TextBox'
id='filterDlg_actionParam' style=\"$param_hidden\"
name='action_param' value=\"$action_param\">";
print \Controls\select_labels("action_param_label", $action_param,
["style" => $label_param_hidden],
"filterDlg_actionParamLabel");
$filter_actions = PluginHost::getInstance()->get_filter_actions();
$filter_action_hash = array();
foreach ($filter_actions as $fclass => $factions) {
foreach ($factions as $faction) {
$filter_action_hash[$fclass . ":" . $faction["action"]] =
$fclass . ": " . $faction["description"];
}
}
if (count($filter_action_hash) == 0) {
$filter_plugin_disabled = ["disabled" => "1"];
$filter_action_hash["no-data"] = __("No actions available");
} else {
$filter_plugin_disabled = [];
}
print \Controls\select_hash("action_param_plugin", $action_param, $filter_action_hash,
array_merge(["style" => $plugin_param_hidden], $filter_plugin_disabled),
"filterDlg_actionParamPlugin");
print "</span>";
print "&nbsp;"; // tiny layout hack
print "</section>";
print "<footer>";
print "<button dojoType='dijit.form.Button' class='alt-primary' type='submit' onclick='App.dialogOf(this).execute()'>".
($action ? __("Save action") : __('Add action'))."</button> ";
print "<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).hide()'>".
__('Cancel')."</button>";
print "</footer>";
print "</form>";
print json_encode([
"multiselect" => $this->_feed_multi_select("feed_id", $feed_ids, 'style="width : 540px; height : 300px" dojoType="dijit.form.MultiSelect"')
]);
}
private function _get_name($id) {
@ -1067,106 +830,110 @@ class Pref_Filters extends Handler_Protected {
$this->pdo->commit();
}
private function feed_multi_select($id, $default_ids = [],
private function _feed_multi_select($id, $default_ids = [],
$attributes = "", $include_all_feeds = true,
$root_id = null, $nest_level = 0) {
$pdo = Db::pdo();
$pdo = Db::pdo();
// print_r(in_array("CAT:6",$default_ids));
$rv = "";
if (!$root_id) {
print "<select multiple=\true\" id=\"$id\" name=\"$id\" $attributes>";
if ($include_all_feeds) {
$is_selected = (in_array("0", $default_ids)) ? "selected=\"1\"" : "";
print "<option $is_selected value=\"0\">".__('All feeds')."</option>";
}
}
// print_r(in_array("CAT:6",$default_ids));
if (get_pref('ENABLE_FEED_CATS')) {
if (!$root_id) {
$rv .= "<select multiple=\true\" id=\"$id\" name=\"$id\" $attributes>";
if ($include_all_feeds) {
$is_selected = (in_array("0", $default_ids)) ? "selected=\"1\"" : "";
$rv .= "<option $is_selected value=\"0\">".__('All feeds')."</option>";
}
}
if (!$root_id) $root_id = null;
if (get_pref('ENABLE_FEED_CATS')) {
$sth = $pdo->prepare("SELECT id,title,
(SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
c2.parent_cat = ttrss_feed_categories.id) AS num_children
FROM ttrss_feed_categories
WHERE owner_uid = :uid AND
(parent_cat = :root_id OR (:root_id IS NULL AND parent_cat IS NULL)) ORDER BY title");
if (!$root_id) $root_id = null;
$sth->execute([":uid" => $_SESSION['uid'], ":root_id" => $root_id]);
$sth = $pdo->prepare("SELECT id,title,
(SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
c2.parent_cat = ttrss_feed_categories.id) AS num_children
FROM ttrss_feed_categories
WHERE owner_uid = :uid AND
(parent_cat = :root_id OR (:root_id IS NULL AND parent_cat IS NULL)) ORDER BY title");
while ($line = $sth->fetch()) {
$sth->execute([":uid" => $_SESSION['uid'], ":root_id" => $root_id]);
for ($i = 0; $i < $nest_level; $i++)
$line["title"] = "" . $line["title"];
while ($line = $sth->fetch()) {
$is_selected = in_array("CAT:".$line["id"], $default_ids) ? "selected=\"1\"" : "";
for ($i = 0; $i < $nest_level; $i++)
$line["title"] = "" . $line["title"];
printf("<option $is_selected value='CAT:%d'>%s</option>",
$line["id"], htmlspecialchars($line["title"]));
$is_selected = in_array("CAT:".$line["id"], $default_ids) ? "selected=\"1\"" : "";
if ($line["num_children"] > 0)
$this->feed_multi_select($id, $default_ids, $attributes,
$include_all_feeds, $line["id"], $nest_level+1);
$rv .= sprintf("<option $is_selected value='CAT:%d'>%s</option>",
$line["id"], htmlspecialchars($line["title"]));
$f_sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
WHERE cat_id = ? AND owner_uid = ? ORDER BY title");
if ($line["num_children"] > 0)
$rv .= $this->_feed_multi_select($id, $default_ids, $attributes,
$include_all_feeds, $line["id"], $nest_level+1);
$f_sth->execute([$line['id'], $_SESSION['uid']]);
$f_sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
WHERE cat_id = ? AND owner_uid = ? ORDER BY title");
while ($fline = $f_sth->fetch()) {
$is_selected = (in_array($fline["id"], $default_ids)) ? "selected=\"1\"" : "";
$f_sth->execute([$line['id'], $_SESSION['uid']]);
$fline["title"] = "" . $fline["title"];
while ($fline = $f_sth->fetch()) {
$is_selected = (in_array($fline["id"], $default_ids)) ? "selected=\"1\"" : "";
for ($i = 0; $i < $nest_level; $i++)
$fline["title"] = "" . $fline["title"];
printf("<option $is_selected value='%d'>%s</option>",
$fline["id"], htmlspecialchars($fline["title"]));
}
}
for ($i = 0; $i < $nest_level; $i++)
$fline["title"] = "" . $fline["title"];
if (!$root_id) {
$is_selected = in_array("CAT:0", $default_ids) ? "selected=\"1\"" : "";
$rv .= sprintf("<option $is_selected value='%d'>%s</option>",
$fline["id"], htmlspecialchars($fline["title"]));
}
}
printf("<option $is_selected value='CAT:0'>%s</option>",
__("Uncategorized"));
if (!$root_id) {
$is_selected = in_array("CAT:0", $default_ids) ? "selected=\"1\"" : "";
$f_sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
WHERE cat_id IS NULL AND owner_uid = ? ORDER BY title");
$f_sth->execute([$_SESSION['uid']]);
$rv .= sprintf("<option $is_selected value='CAT:0'>%s</option>",
__("Uncategorized"));
while ($fline = $f_sth->fetch()) {
$is_selected = in_array($fline["id"], $default_ids) ? "selected=\"1\"" : "";
$f_sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
WHERE cat_id IS NULL AND owner_uid = ? ORDER BY title");
$f_sth->execute([$_SESSION['uid']]);
$fline["title"] = "" . $fline["title"];
while ($fline = $f_sth->fetch()) {
$is_selected = in_array($fline["id"], $default_ids) ? "selected=\"1\"" : "";
for ($i = 0; $i < $nest_level; $i++)
$fline["title"] = "" . $fline["title"];
printf("<option $is_selected value='%d'>%s</option>",
$fline["id"], htmlspecialchars($fline["title"]));
for ($i = 0; $i < $nest_level; $i++)
$fline["title"] = "" . $fline["title"];
$rv .= sprintf("<option $is_selected value='%d'>%s</option>",
$fline["id"], htmlspecialchars($fline["title"]));
}
}
}
} else {
$sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
WHERE owner_uid = ? ORDER BY title");
$sth->execute([$_SESSION['uid']]);
} else {
$sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
WHERE owner_uid = ? ORDER BY title");
$sth->execute([$_SESSION['uid']]);
while ($line = $sth->fetch()) {
while ($line = $sth->fetch()) {
$is_selected = (in_array($line["id"], $default_ids)) ? "selected=\"1\"" : "";
$is_selected = (in_array($line["id"], $default_ids)) ? "selected=\"1\"" : "";
printf("<option $is_selected value='%d'>%s</option>",
$line["id"], htmlspecialchars($line["title"]));
$rv .= sprintf("<option $is_selected value='%d'>%s</option>",
$line["id"], htmlspecialchars($line["title"]));
}
}
}
if (!$root_id) {
print "</select>";
if (!$root_id) {
$rv .= "</select>";
}
return $rv;
}
}
}

@ -399,7 +399,7 @@ class RPC extends Handler_Protected {
$params["icon_indicator_white"] = $this->image_to_base64("images/indicator_white.gif");
$params["labels"] = Labels::get_all_labels($_SESSION["uid"]);
$params["labels"] = Labels::get_all($_SESSION["uid"]);
return $params;
}
@ -430,7 +430,7 @@ class RPC extends Handler_Protected {
$data["max_feed_id"] = (int) $max_feed_id;
$data["num_feeds"] = (int) $num_feeds;
$data['cdm_expanded'] = get_pref('CDM_EXPANDED');
$data["labels"] = Labels::get_all_labels($_SESSION["uid"]);
$data["labels"] = Labels::get_all($_SESSION["uid"]);
if (LOG_DESTINATION == 'sql' && $_SESSION['access_level'] >= 10) {
if (DB_TYPE == 'pgsql') {

@ -60,21 +60,11 @@
return $rv;
}
function select_labels(string $name, string $value, array $attributes = [], string $id = "") {
$pdo = \Db::pdo();
$sth = $pdo->prepare("SELECT caption FROM ttrss_labels2
WHERE owner_uid = ? ORDER BY caption");
$sth->execute([$_SESSION['uid']]);
$values = [];
while ($row = $sth->fetch()) {
array_push($values, $row["caption"]);
}
/*function select_labels(string $name, string $value, array $attributes = [], string $id = "") {
$values = \Labels::get_as_hash($_SESSION["uid"]);
return select_tag($name, $value, $values, $attributes, $id);
}
}*/
function select_hash(string $name, $value, array $values, array $attributes = [], string $id = "") {
$attributes_str = attributes_to_string($attributes);

@ -358,6 +358,27 @@ const App = {
return p;
}
},
// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
getSelectedText: function() {
let text = "";
if (typeof window.getSelection != "undefined") {
const sel = window.getSelection();
if (sel.rangeCount) {
const container = document.createElement("div");
for (let i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
text = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
text = document.selection.createRange().textText;
}
}
return text.stripTags();
},
displayIfChecked: function(checkbox, elemId) {
if (checkbox.checked) {
Element.show(elemId);

@ -5,358 +5,559 @@
/* global __, App, Article, Lists, fox */
/* global xhr, dojo, dijit, Notify, Feeds */
/* exported Filters */
const Filters = {
filterDlgCheckAction: function(sender) {
const action = sender.value;
const action_param = App.byId("filterDlg_paramBox");
if (!action_param) {
console.log("filterDlgCheckAction: can't find action param box!");
return;
}
// if selected action supports parameters, enable params field
if (action == 4 || action == 6 || action == 7 || action == 9) {
Element.show(action_param);
Element.hide(dijit.byId("filterDlg_actionParam").domNode);
Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
Element.hide(dijit.byId("filterDlg_actionParamPlugin").domNode);
if (action == 7) {
Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
} else if (action == 9) {
Element.show(dijit.byId("filterDlg_actionParamPlugin").domNode);
} else {
Element.show(dijit.byId("filterDlg_actionParam").domNode);
}
} else {
Element.hide(action_param);
}
},
createNewRuleElement: function(parentNode, replaceNode) {
const rule = dojo.formToJson("filter_new_rule_form");
xhr.post("backend.php", {op: "pref-filters", method: "printrulename", rule: rule}, (reply) => {
try {
const li = document.createElement('li');
li.innerHTML = `<input dojoType='dijit.form.CheckBox' type='checkbox' onclick='Lists.onRowChecked(this)'>
<span onclick='App.dialogOf(this).editRule(this)'>${reply}</span>
${App.FormFields.hidden_tag("rule[]", rule)}`;
edit: function(filter_id = null) { // if no id, new filter dialog
dojo.parser.parse(li);
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
} else {
parentNode.appendChild(li);
}
} catch (e) {
App.Error.report(e);
}
});
},
createNewActionElement: function(parentNode, replaceNode) {
const form = document.forms["filter_new_action_form"];
const dialog = new fox.SingleUseDialog({
id: "filterEditDlg",
title: filter_id ? __("Edit Filter") : __("Create Filter"),
ACTION_TAG: 4,
ACTION_SCORE: 6,
ACTION_LABEL: 7,
ACTION_PLUGIN: 9,
PARAM_ACTIONS: [4, 6, 7, 9],
filter_info: {},
test: function() {
const test_dialog = new fox.SingleUseDialog({
title: "Test Filter",
results: 0,
limit: 100,
max_offset: 10000,
getTestResults: function (params, offset) {
params.method = 'testFilterDo';
params.offset = offset;
params.limit = test_dialog.limit;
console.log("getTestResults:" + offset);
xhr.json("backend.php", params, (result) => {
try {
if (result && test_dialog && test_dialog.open) {
test_dialog.results += result.length;
console.log("got results:" + result.length);
App.byId("prefFilterProgressMsg").innerHTML = __("Looking for articles (%d processed, %f found)...")
.replace("%f", test_dialog.results)
.replace("%d", offset);
console.log(offset + " " + test_dialog.max_offset);
for (let i = 0; i < result.length; i++) {
const tmp = dojo.create("table", { innerHTML: result[i]});
App.byId("prefFilterTestResultList").innerHTML += tmp.innerHTML;
}
if (test_dialog.results < 30 && offset < test_dialog.max_offset) {
// get the next batch
window.setTimeout(function () {
test_dialog.getTestResults(params, offset + test_dialog.limit);
}, 0);
} else {
// all done
Element.hide("prefFilterLoadingIndicator");
if (test_dialog.results == 0) {
App.byId("prefFilterTestResultList").innerHTML = `<tr><td align='center'>
${__('No recent articles matching this filter have been found.')}</td></tr>`;
App.byId("prefFilterProgressMsg").innerHTML = "Articles matching this filter:";
} else {
App.byId("prefFilterProgressMsg").innerHTML = __("Found %d articles matching this filter:")
.replace("%d", test_dialog.results);
}
}
} else if (!result) {
console.log("getTestResults: can't parse results object");
Element.hide("prefFilterLoadingIndicator");
Notify.error("Error while trying to get filter test results.");
} else {
console.log("getTestResults: dialog closed, bailing out.");
}
} catch (e) {
App.Error.report(e);
}
});
},
content: `
<div>
<img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'>&nbsp;
<span id='prefFilterProgressMsg'>Looking for articles...</span>
</div>
<ul class='panel panel-scrollable list list-unstyled' id='prefFilterTestResultList'></ul>
<footer class='text-center'>
<button dojoType='dijit.form.Button' type='submit' class='alt-primary'>${__('Close this window')}</button>
</footer>
`
});
if (form.action_id.value == 7) {
form.action_param.value = form.action_param_label.value;
} else if (form.action_id.value == 9) {
form.action_param.value = form.action_param_plugin.value;
}
const tmph = dojo.connect(test_dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
const action = dojo.formToJson(form);
test_dialog.getTestResults(dialog.attr('value'), 0);
});
xhr.post("backend.php", { op: "pref-filters", method: "printactionname", action: action }, (reply) => {
try {
const li = document.createElement('li');
test_dialog.show();
},
insertRule: function(parentNode, replaceNode) {
const rule = dojo.formToJson("filter_new_rule_form");
li.innerHTML = `<input dojoType='dijit.form.CheckBox' type='checkbox' onclick='Lists.onRowChecked(this)'>
<span onclick='App.dialogOf(this).editAction(this)'>${reply}</span>
${App.FormFields.hidden_tag("action[]", action)}`;
xhr.post("backend.php", {op: "pref-filters", method: "printrulename", rule: rule}, (reply) => {
try {
const li = document.createElement('li');
li.addClassName("rule");
dojo.parser.parse(li);
li.innerHTML = `${App.FormFields.checkbox_tag("", false, {onclick: 'Lists.onRowChecked(this)'})}
<span class="name" onclick='App.dialogOf(this).onRuleClicked(this)'>${reply}</span>
<span class="payload" >${App.FormFields.hidden_tag("rule[]", rule)}</span>`;
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
} else {
parentNode.appendChild(li);
}
dojo.parser.parse(li);
} catch (e) {
App.Error.report(e);
}
});
},
addFilterRule: function(replaceNode, ruleStr) {
const dialog = new fox.SingleUseDialog({
id: "filterNewRuleDlg",
title: ruleStr ? __("Edit rule") : __("Add rule"),
execute: function () {
if (this.validate()) {
Filters.createNewRuleElement(App.byId("filterDlg_Matches"), replaceNode);
this.hide();
}
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
} else {
parentNode.appendChild(li);
}
} catch (e) {
App.Error.report(e);
}
});
},
content: __('Loading, please wait...'),
});
const tmph = dojo.connect(dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
xhr.post("backend.php", {op: 'pref-filters', method: 'newrule', rule: ruleStr}, (reply) => {
dialog.attr('content', reply);
});
});
insertAction: function(parentNode, replaceNode) {
const form = document.forms["filter_new_action_form"];
dialog.show();
},
addFilterAction: function(replaceNode, actionStr) {
const dialog = new fox.SingleUseDialog({
title: actionStr ? __("Edit action") : __("Add action"),
execute: function () {
if (this.validate()) {
Filters.createNewActionElement(App.byId("filterDlg_Actions"), replaceNode);
this.hide();
if (form.action_id.value == 7) {
form.action_param.value = form.action_param_label.value;
} else if (form.action_id.value == 9) {
form.action_param.value = form.action_param_plugin.value;
}
}
});
const tmph = dojo.connect(dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
xhr.post("backend.php", {op: 'pref-filters', method: 'newaction', action: actionStr}, (reply) => {
dialog.attr('content', reply);
});
});
const action = dojo.formToJson(form);
dialog.show();
},
test: function(params) {
const dialog = new fox.SingleUseDialog({
title: "Test Filter",
results: 0,
limit: 100,
max_offset: 10000,
getTestResults: function (params, offset) {
params.method = 'testFilterDo';
params.offset = offset;
params.limit = dialog.limit;
console.log("getTestResults:" + offset);
xhr.json("backend.php", params, (result) => {
xhr.post("backend.php", { op: "pref-filters", method: "printactionname", action: action }, (reply) => {
try {
if (result && dialog && dialog.open) {
dialog.results += result.length;
console.log("got results:" + result.length);
App.byId("prefFilterProgressMsg").innerHTML = __("Looking for articles (%d processed, %f found)...")
.replace("%f", dialog.results)
.replace("%d", offset);
const li = document.createElement('li');
li.addClassName("action");
console.log(offset + " " + dialog.max_offset);
li.innerHTML = `${App.FormFields.checkbox_tag("", false, {onclick: 'Lists.onRowChecked(this)'})}
<span class="name" onclick='App.dialogOf(this).onActionClicked(this)'>${reply}</span>
<span class="payload">${App.FormFields.hidden_tag("action[]", action)}</span>`;
for (let i = 0; i < result.length; i++) {
const tmp = dojo.create("table", { innerHTML: result[i]});
dojo.parser.parse(li);
App.byId("prefFilterTestResultList").innerHTML += tmp.innerHTML;
}
if (dialog.results < 30 && offset < dialog.max_offset) {
// get the next batch
window.setTimeout(function () {
dialog.getTestResults(params, offset + dialog.limit);
}, 0);
} else {
// all done
Element.hide("prefFilterLoadingIndicator");
if (dialog.results == 0) {
App.byId("prefFilterTestResultList").innerHTML = `<tr><td align='center'>
${__('No recent articles matching this filter have been found.')}</td></tr>`;
App.byId("prefFilterProgressMsg").innerHTML = "Articles matching this filter:";
} else {
App.byId("prefFilterProgressMsg").innerHTML = __("Found %d articles matching this filter:")
.replace("%d", dialog.results);
}
}
} else if (!result) {
console.log("getTestResults: can't parse results object");
Element.hide("prefFilterLoadingIndicator");
Notify.error("Error while trying to get filter test results.");
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
} else {
console.log("getTestResults: dialog closed, bailing out.");
parentNode.appendChild(li);
}
} catch (e) {
App.Error.report(e);
}
});
},
content: `
<div>
<img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'>&nbsp;
<span id='prefFilterProgressMsg'>Looking for articles...</span>
</div>
<ul class='panel panel-scrollable list list-unstyled' id='prefFilterTestResultList'></ul>
<footer class='text-center'>
<button dojoType='dijit.form.Button' type='submit' class='alt-primary'>${__('Close this window')}</button>
</footer>
`
});
dojo.connect(dialog, "onShow", null, function (/* e */) {
dialog.getTestResults(params, 0);
});
dialog.show();
},
edit: function(id) { // if no id, new filter dialog
let query;
if (!App.isPrefs()) {
query = {
op: "pref-filters", method: "edit",
feed: Feeds.getActive(), is_cat: Feeds.activeIsCat()
};
} else {
query = {op: "pref-filters", method: "edit", id: id};
}
console.log('Filters.edit', query);
xhr.post("backend.php", query, function (reply) {
try {
const dialog = new fox.SingleUseDialog({
id: "filterEditDlg",
title: id ? __("Edit Filter") : __("Create Filter"),
test: function () {
Filters.test(this.attr('value'));
},
selectRules: function (select) {
Lists.select("filterDlg_Matches", select);
},
selectActions: function (select) {
Lists.select("filterDlg_Actions", select);
},
editRule: function (e) {
const li = e.closest('li');
const rule = li.querySelector('input[name="rule[]"]').value
Filters.addFilterRule(li, rule);
editRule: function(replaceNode, ruleStr = null) {
const edit_rule_dialog = new fox.SingleUseDialog({
id: "filterNewRuleDlg",
title: ruleStr ? __("Edit rule") : __("Add rule"),
execute: function () {
if (this.validate()) {
dialog.insertRule(App.byId("filterDlg_Matches"), replaceNode);
this.hide();
}
},
editAction: function (e) {
const li = e.closest('li');
const action = li.querySelector('input[name="action[]"]').value
content: __('Loading, please wait...'),
});
Filters.addFilterAction(li, action);
},
removeFilter: function () {
const msg = __("Remove filter?");
const tmph = dojo.connect(edit_rule_dialog, "onShow", null, function () {
dojo.disconnect(tmph);
if (confirm(msg)) {
this.hide();
let rule;
Notify.progress("Removing filter...");
if (ruleStr) {
rule = JSON.parse(ruleStr);
} else {
rule = {
reg_exp: "",
filter_type: 1,
feed_id: ["0"],
inverse: false,
};
}
const query = {op: "pref-filters", method: "remove", ids: this.attr('value').id};
console.log(rule, dialog.filter_info);
xhr.json("backend.php", {op: "pref-filters", method: "editrule", ids: rule.feed_id.join(",")}, function (editrule) {
edit_rule_dialog.attr('content',
`
<form name="filter_new_rule_form" id="filter_new_rule_form" onsubmit="return false">
<section>
<textarea dojoType="fox.form.ValidationTextArea"
required="true" id="filterDlg_regExp" ValidRegExp="true"
rows="4" style="font-size : 14px; width : 530px; word-break: break-all"
name="reg_exp">${rule.reg_exp}</textarea>
<div dojoType="dijit.Tooltip" id="filterDlg_regExp_tip" connectId="filterDlg_regExp" position="below"></div>
<fieldset>
<label class="checkbox">
${App.FormFields.checkbox_tag("inverse", rule.inverse)}
${__("Inverse regular expression matching")}
</label>
</fieldset>
<fieldset>
<label style="display : inline">${__("on")}</label>
${App.FormFields.select_hash("filter_type", rule.filter_type, dialog.filter_info.filter_types)}
<label style="padding-left : 10px; display : inline">${__("in")}</label>
</fieldset>
<fieldset>
<span id="filterDlg_feeds">
${editrule.multiselect}
</span>
</fieldset>
</section>
<footer>
${App.FormFields.button_tag(App.FormFields.icon("help") + " " + __("More info"), "", {class: 'pull-left alt-info',
onclick: "window.open('https://tt-rss.org/wiki/ContentFilters')"})}
${App.FormFields.submit_tag(__("Save rule"), {onclick: "App.dialogOf(this).execute()"})}
${App.FormFields.cancel_dialog_tag(__("Cancel"))}
</footer>
</form>
`);
});
xhr.post("backend.php", query, () => {
const tree = dijit.byId("filterTree");
});
if (tree) tree.reload();
});
}
},
addAction: function () {
Filters.addFilterAction();
},
addRule: function () {
Filters.addFilterRule();
},
deleteAction: function () {
App.findAll("#filterDlg_Actions li[class*=Selected]").forEach(function (e) {
e.parentNode.removeChild(e)
});
edit_rule_dialog.show();
},
editAction: function(replaceNode, actionStr) {
const edit_action_dialog = new fox.SingleUseDialog({
title: actionStr ? __("Edit action") : __("Add action"),
select_labels: function(name, value, labels, attributes = {}, id = "") {
const values = Object.values(labels).map((label) => label.caption);
return App.FormFields.select_tag(name, value, values, attributes, id);
},
deleteRule: function () {
App.findAll("#filterDlg_Matches li[class*=Selected]").forEach(function (e) {
e.parentNode.removeChild(e)
});
toggleParam: function(sender) {
const action = parseInt(sender.value);
dijit.byId("filterDlg_actionParam").domNode.hide();
dijit.byId("filterDlg_actionParamLabel").domNode.hide();
dijit.byId("filterDlg_actionParamPlugin").domNode.hide();
// if selected action supports parameters, enable params field
if (action == dialog.ACTION_LABEL) {
dijit.byId("filterDlg_actionParamLabel").domNode.show();
} else if (action == dialog.ACTION_PLUGIN) {
dijit.byId("filterDlg_actionParamPlugin").domNode.show();
} else if (dialog.PARAM_ACTIONS.indexOf(action) != -1) {
dijit.byId("filterDlg_actionParam").domNode.show();
}
},
execute: function () {
if (this.validate()) {
Notify.progress("Saving data...", true);
xhr.post("backend.php", this.attr('value'), () => {
dialog.hide();
const tree = dijit.byId("filterTree");
if (tree) tree.reload();
});
dialog.insertAction(App.byId("filterDlg_Actions"), replaceNode);
this.hide();
}
},
content: reply
content: __("Loading, please wait...")
});
if (!App.isPrefs()) {
/* global getSelectionText */
const selectedText = getSelectionText();
const tmph = dojo.connect(edit_action_dialog, "onShow", null, function () {
dojo.disconnect(tmph);
const lh = dojo.connect(dialog, "onShow", function () {
dojo.disconnect(lh);
let action;
if (selectedText != "") {
if (actionStr) {
action = JSON.parse(actionStr);
} else {
action = {
action_id: 2,
action_param: ""
};
}
const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) :
Feeds.getActive();
console.log(action);
edit_action_dialog.attr('content',
`
<form name="filter_new_action_form" id="filter_new_action_form" onsubmit="return false;">
<section>
${App.FormFields.select_hash("action_id", -1,
dialog.filter_info.action_types,
{onchange: "App.dialogOf(this).toggleParam(this)"},
"filterDlg_actionSelect")}
<input dojoType="dijit.form.TextBox"
id="filterDlg_actionParam" style="$param_hidden"
name="action_param" value="${App.escapeHtml(action.action_param)}">
${edit_action_dialog.select_labels("action_param_label", action.action_param,
dialog.filter_info.labels,
{},
"filterDlg_actionParamLabel")}
${App.FormFields.select_hash("action_param_plugin", action.action_param,
dialog.filter_info.plugin_actions,
{},
"filterDlg_actionParamPlugin")}
</section>
<footer>
${App.FormFields.submit_tag(__("Save action"), {onclick: "App.dialogOf(this).execute()"})}
${App.FormFields.cancel_dialog_tag(__("Cancel"))}
</footer>
</form>
`);
dijit.byId("filterDlg_actionSelect").attr('value', action.action_id);
/*xhr.post("backend.php", {op: 'pref-filters', method: 'newaction', action: actionStr}, (reply) => {
edit_action_dialog.attr('content', reply);
setTimeout(() => {
edit_action_dialog.hideOrShowActionParam(dijit.byId("filterDlg_actionSelect").attr('value'));
}, 250);
});*/
});
const rule = {reg_exp: selectedText, feed_id: [feed_id], filter_type: 1};
edit_action_dialog.show();
},
selectRules: function (select) {
Lists.select("filterDlg_Matches", select);
},
selectActions: function (select) {
Lists.select("filterDlg_Actions", select);
},
onRuleClicked: function (elem) {
Filters.addFilterRule(null, dojo.toJson(rule));
const li = elem.closest('li');
const rule = li.querySelector('input[name="rule[]"]').value;
} else {
this.editRule(li, rule);
},
onActionClicked: function (elem) {
const query = {op: "article", method: "getmetadatabyid", id: Article.getActive()};
const li = elem.closest('li');
const action = li.querySelector('input[name="action[]"]').value;
xhr.json("backend.php", query, (reply) => {
let title = false;
this.editAction(li, action);
},
removeFilter: function () {
const msg = __("Remove filter?");
if (reply && reply.title) title = reply.title;
if (confirm(msg)) {
this.hide();
if (title || Feeds.getActive() || Feeds.activeIsCat()) {
Notify.progress("Removing filter...");
console.log(title + " " + Feeds.getActive());
const query = {op: "pref-filters", method: "remove", ids: this.attr('value').id};
const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) :
Feeds.getActive();
xhr.post("backend.php", query, () => {
const tree = dijit.byId("filterTree");
const rule = {reg_exp: title, feed_id: [feed_id], filter_type: 1};
if (tree) tree.reload();
});
}
},
addAction: function () {
this.editAction();
},
addRule: function () {
this.editRule();
},
deleteAction: function () {
App.findAll("#filterDlg_Actions li[class*=Selected]").forEach(function (e) {
e.parentNode.removeChild(e)
});
},
deleteRule: function () {
App.findAll("#filterDlg_Matches li[class*=Selected]").forEach(function (e) {
e.parentNode.removeChild(e)
});
},
execute: function () {
if (this.validate()) {
Filters.addFilterRule(null, dojo.toJson(rule));
}