labels.php 9.0 KB

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