labels.php 8.9 KB

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