labels.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. class Pref_Labels extends Handler_Protected {
  3. function csrf_ignore($method) {
  4. $csrf_ignored = array("index", "getlabeltree", "edit");
  5. return array_search($method, $csrf_ignored) !== false;
  6. }
  7. function edit() {
  8. $label_id = clean($_REQUEST['id']);
  9. $sth = $this->pdo->prepare("SELECT * FROM ttrss_labels2 WHERE
  10. id = ? AND owner_uid = ?");
  11. $sth->execute([$label_id, $_SESSION['uid']]);
  12. if ($line = $sth->fetch()) {
  13. print_hidden("id", "$label_id");
  14. print_hidden("op", "pref-labels");
  15. print_hidden("method", "save");
  16. print "<form onsubmit='return false;'>";
  17. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  18. print "<div class=\"dlgSecCont\">";
  19. $fg_color = $line['fg_color'];
  20. $bg_color = $line['bg_color'];
  21. print "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-bottom : 4px; margin-right : 4px'>&alpha;</span>";
  22. print "<input style=\"font-size : 16px\" name=\"caption\"
  23. dojoType=\"dijit.form.ValidationTextBox\"
  24. required=\"true\"
  25. value=\"".htmlspecialchars($line['caption'])."\">";
  26. print "</div>";
  27. print "<div class=\"dlgSec\">" . __("Colors") . "</div>";
  28. print "<div class=\"dlgSecCont\">";
  29. print "<table cellspacing=\"0\">";
  30. print "<tr><td>".__("Foreground:")."</td><td>".__("Background:").
  31. "</td></tr>";
  32. print "<tr><td style='padding-right : 10px'>";
  33. print "<input dojoType=\"dijit.form.TextBox\"
  34. style=\"display : none\" id=\"labelEdit_fgColor\"
  35. name=\"fg_color\" value=\"$fg_color\">";
  36. print "<input dojoType=\"dijit.form.TextBox\"
  37. style=\"display : none\" id=\"labelEdit_bgColor\"
  38. name=\"bg_color\" value=\"$bg_color\">";
  39. print "<div dojoType=\"dijit.ColorPalette\">
  40. <script type=\"dojo/method\" event=\"onChange\" args=\"fg_color\">
  41. dijit.byId(\"labelEdit_fgColor\").attr('value', fg_color);
  42. $('label-editor-indicator').setStyle({color: fg_color});
  43. </script>
  44. </div>";
  45. print "</div>";
  46. print "</td><td>";
  47. print "<div dojoType=\"dijit.ColorPalette\">
  48. <script type=\"dojo/method\" event=\"onChange\" args=\"bg_color\">
  49. dijit.byId(\"labelEdit_bgColor\").attr('value', bg_color);
  50. $('label-editor-indicator').setStyle({backgroundColor: bg_color});
  51. </script>
  52. </div>";
  53. print "</div>";
  54. print "</td></tr></table>";
  55. print "</div>";
  56. # print "</form>";
  57. print "<div class=\"dlgButtons\">";
  58. print "<button dojoType=\"dijit.form.Button\" type=\"submit\" class=\"btn-primary\" onclick=\"dijit.byId('labelEditDlg').execute()\">".
  59. __('Save')."</button>";
  60. print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('labelEditDlg').hide()\">".
  61. __('Cancel')."</button>";
  62. print "</div>";
  63. print "</form>";
  64. }
  65. }
  66. function getlabeltree() {
  67. $root = array();
  68. $root['id'] = 'root';
  69. $root['name'] = __('Labels');
  70. $root['items'] = array();
  71. $sth = $this->pdo->prepare("SELECT *
  72. FROM ttrss_labels2
  73. WHERE owner_uid = ?
  74. ORDER BY caption");
  75. $sth->execute([$_SESSION['uid']]);
  76. while ($line = $sth->fetch()) {
  77. $label = array();
  78. $label['id'] = 'LABEL:' . $line['id'];
  79. $label['bare_id'] = $line['id'];
  80. $label['name'] = $line['caption'];
  81. $label['fg_color'] = $line['fg_color'];
  82. $label['bg_color'] = $line['bg_color'];
  83. $label['type'] = 'label';
  84. $label['checkbox'] = false;
  85. array_push($root['items'], $label);
  86. }
  87. $fl = array();
  88. $fl['identifier'] = 'id';
  89. $fl['label'] = 'name';
  90. $fl['items'] = array($root);
  91. print json_encode($fl);
  92. return;
  93. }
  94. function colorset() {
  95. $kind = clean($_REQUEST["kind"]);
  96. $ids = explode(',', clean($_REQUEST["ids"]));
  97. $color = clean($_REQUEST["color"]);
  98. $fg = clean($_REQUEST["fg"]);
  99. $bg = clean($_REQUEST["bg"]);
  100. foreach ($ids as $id) {
  101. if ($kind == "fg" || $kind == "bg") {
  102. $sth = $this->pdo->prepare("UPDATE ttrss_labels2 SET
  103. ${kind}_color = ? WHERE id = ?
  104. AND owner_uid = ?");
  105. $sth->execute([$color, $id, $_SESSION['uid']]);
  106. } else {
  107. $sth = $this->pdo->prepare("UPDATE ttrss_labels2 SET
  108. fg_color = ?, bg_color = ? WHERE id = ?
  109. AND owner_uid = ?");
  110. $sth->execute([$fg, $bg, $id, $_SESSION['uid']]);
  111. }
  112. $caption = Labels::find_caption($id, $_SESSION["uid"]);
  113. /* Remove cached data */
  114. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
  115. WHERE label_cache LIKE ? AND owner_uid = ?");
  116. $sth->execute(["%$caption%", $_SESSION['uid']]);
  117. }
  118. }
  119. function colorreset() {
  120. $ids = explode(',', clean($_REQUEST["ids"]));
  121. foreach ($ids as $id) {
  122. $sth = $this->pdo->prepare("UPDATE ttrss_labels2 SET
  123. fg_color = '', bg_color = '' WHERE id = ?
  124. AND owner_uid = ?");
  125. $sth->execute([$id, $_SESSION['uid']]);
  126. $caption = Labels::find_caption($id, $_SESSION["uid"]);
  127. /* Remove cached data */
  128. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
  129. WHERE label_cache LIKE ? AND owner_uid = ?");
  130. $sth->execute(["%$caption%", $_SESSION['uid']]);
  131. }
  132. }
  133. function save() {
  134. $id = clean($_REQUEST["id"]);
  135. $caption = trim(clean($_REQUEST["caption"]));
  136. $this->pdo->beginTransaction();
  137. $sth = $this->pdo->prepare("SELECT caption FROM ttrss_labels2
  138. WHERE id = ? AND owner_uid = ?");
  139. $sth->execute([$id, $_SESSION['uid']]);
  140. if ($row = $sth->fetch()) {
  141. $old_caption = $row["caption"];
  142. $sth = $this->pdo->prepare("SELECT id FROM ttrss_labels2
  143. WHERE caption = ? AND owner_uid = ?");
  144. $sth->execute([$caption, $_SESSION['uid']]);
  145. if (!$sth->fetch()) {
  146. if ($caption) {
  147. $sth = $this->pdo->prepare("UPDATE ttrss_labels2 SET
  148. caption = ? WHERE id = ? AND
  149. owner_uid = ?");
  150. $sth->execute([$caption, $id, $_SESSION['uid']]);
  151. /* Update filters that reference label being renamed */
  152. $sth = $this->pdo->prepare("UPDATE ttrss_filters2_actions SET
  153. action_param = ? WHERE action_param = ?
  154. AND action_id = 7
  155. AND filter_id IN (SELECT id FROM ttrss_filters2 WHERE owner_uid = ?)");
  156. $sth->execute([$caption, $old_caption, $_SESSION['uid']]);
  157. print clean($_REQUEST["value"]);
  158. } else {
  159. print $old_caption;
  160. }
  161. } else {
  162. print $old_caption;
  163. }
  164. }
  165. $this->pdo->commit();
  166. }
  167. function remove() {
  168. $ids = explode(",", clean($_REQUEST["ids"]));
  169. foreach ($ids as $id) {
  170. Labels::remove($id, $_SESSION["uid"]);
  171. }
  172. }
  173. function add() {
  174. $caption = clean($_REQUEST["caption"]);
  175. $output = clean($_REQUEST["output"]);
  176. if ($caption) {
  177. if (Labels::create($caption)) {
  178. if (!$output) {
  179. print T_sprintf("Created label <b>%s</b>", htmlspecialchars($caption));
  180. }
  181. }
  182. if ($output == "select") {
  183. header("Content-Type: text/xml");
  184. print "<rpc-reply><payload>";
  185. print_label_select("select_label",
  186. $caption, "");
  187. print "</payload></rpc-reply>";
  188. }
  189. }
  190. return;
  191. }
  192. function index() {
  193. print "<div id=\"pref-label-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
  194. print "<div id=\"pref-label-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
  195. print "<div id=\"pref-label-toolbar\" dojoType=\"dijit.Toolbar\">";
  196. print "<div dojoType=\"dijit.form.DropDownButton\">".
  197. "<span>" . __('Select')."</span>";
  198. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  199. print "<div onclick=\"dijit.byId('labelTree').model.setAllChecked(true)\"
  200. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  201. print "<div onclick=\"dijit.byId('labelTree').model.setAllChecked(false)\"
  202. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  203. print "</div></div>";
  204. print"<button dojoType=\"dijit.form.Button\" onclick=\"return addLabel()\">".
  205. __('Create label')."</button dojoType=\"dijit.form.Button\"> ";
  206. print "<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedLabels()\">".
  207. __('Remove')."</button dojoType=\"dijit.form.Button\"> ";
  208. print "<button dojoType=\"dijit.form.Button\" onclick=\"labelColorReset()\">".
  209. __('Clear colors')."</button dojoType=\"dijit.form.Button\">";
  210. print "</div>"; #toolbar
  211. print "</div>"; #pane
  212. print "<div id=\"pref-label-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
  213. print "<div id=\"labellistLoading\">
  214. <img src='images/indicator_tiny.gif'>".
  215. __("Loading, please wait...")."</div>";
  216. print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"labelStore\"
  217. url=\"backend.php?op=pref-labels&method=getlabeltree\">
  218. </div>
  219. <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"labelModel\" store=\"labelStore\"
  220. query=\"{id:'root'}\" rootId=\"root\"
  221. childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
  222. </div>
  223. <div dojoType=\"fox.PrefLabelTree\" id=\"labelTree\"
  224. model=\"labelModel\" openOnClick=\"true\">
  225. <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
  226. Element.hide(\"labellistLoading\");
  227. </script>
  228. <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
  229. var id = String(item.id);
  230. var bare_id = id.substr(id.indexOf(':')+1);
  231. if (id.match('LABEL:')) {
  232. editLabel(bare_id);
  233. }
  234. </script>
  235. </div>";
  236. print "</div>"; #pane
  237. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
  238. "hook_prefs_tab", "prefLabels");
  239. print "</div>"; #container
  240. }
  241. }