labels.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 = $this->dbh->escape_string($_REQUEST['id']);
  9. $result = $this->dbh->query("SELECT * FROM ttrss_labels2 WHERE
  10. id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
  11. $line = $this->dbh->fetch_assoc($result);
  12. print_hidden("id", "$label_id");
  13. print_hidden("op", "pref-labels");
  14. print_hidden("method", "save");
  15. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  16. print "<div class=\"dlgSecCont\">";
  17. $fg_color = $line['fg_color'];
  18. $bg_color = $line['bg_color'];
  19. print "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-bottom : 4px; margin-right : 4px'>&alpha;</span>";
  20. print "<input style=\"font-size : 16px\" name=\"caption\"
  21. dojoType=\"dijit.form.ValidationTextBox\"
  22. required=\"true\"
  23. 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. $('label-editor-indicator').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. $('label-editor-indicator').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\" 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. return;
  62. }
  63. function getlabeltree() {
  64. $root = array();
  65. $root['id'] = 'root';
  66. $root['name'] = __('Labels');
  67. $root['items'] = array();
  68. $result = $this->dbh->query("SELECT *
  69. FROM ttrss_labels2
  70. WHERE owner_uid = ".$_SESSION["uid"]."
  71. ORDER BY caption");
  72. while ($line = $this->dbh->fetch_assoc($result)) {
  73. $label = array();
  74. $label['id'] = 'LABEL:' . $line['id'];
  75. $label['bare_id'] = $line['id'];
  76. $label['name'] = $line['caption'];
  77. $label['fg_color'] = $line['fg_color'];
  78. $label['bg_color'] = $line['bg_color'];
  79. $label['type'] = 'label';
  80. $label['checkbox'] = false;
  81. array_push($root['items'], $label);
  82. }
  83. $fl = array();
  84. $fl['identifier'] = 'id';
  85. $fl['label'] = 'name';
  86. $fl['items'] = array($root);
  87. print json_encode($fl);
  88. return;
  89. }
  90. function colorset() {
  91. $kind = $this->dbh->escape_string($_REQUEST["kind"]);
  92. $ids = explode(',', $this->dbh->escape_string($_REQUEST["ids"]));
  93. $color = $this->dbh->escape_string($_REQUEST["color"]);
  94. $fg = $this->dbh->escape_string($_REQUEST["fg"]);
  95. $bg = $this->dbh->escape_string($_REQUEST["bg"]);
  96. foreach ($ids as $id) {
  97. if ($kind == "fg" || $kind == "bg") {
  98. $this->dbh->query("UPDATE ttrss_labels2 SET
  99. ${kind}_color = '$color' WHERE id = '$id'
  100. AND owner_uid = " . $_SESSION["uid"]);
  101. } else {
  102. $this->dbh->query("UPDATE ttrss_labels2 SET
  103. fg_color = '$fg', bg_color = '$bg' WHERE id = '$id'
  104. AND owner_uid = " . $_SESSION["uid"]);
  105. }
  106. $caption = $this->dbh->escape_string(label_find_caption($id, $_SESSION["uid"]));
  107. /* Remove cached data */
  108. $this->dbh->query("UPDATE ttrss_user_entries SET label_cache = ''
  109. WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $_SESSION["uid"]);
  110. }
  111. return;
  112. }
  113. function colorreset() {
  114. $ids = explode(',', $this->dbh->escape_string($_REQUEST["ids"]));
  115. foreach ($ids as $id) {
  116. $this->dbh->query("UPDATE ttrss_labels2 SET
  117. fg_color = '', bg_color = '' WHERE id = '$id'
  118. AND owner_uid = " . $_SESSION["uid"]);
  119. $caption = $this->dbh->escape_string(label_find_caption($id, $_SESSION["uid"]));
  120. /* Remove cached data */
  121. $this->dbh->query("UPDATE ttrss_user_entries SET label_cache = ''
  122. WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $_SESSION["uid"]);
  123. }
  124. }
  125. function save() {
  126. $id = $this->dbh->escape_string($_REQUEST["id"]);
  127. $caption = $this->dbh->escape_string(trim($_REQUEST["caption"]));
  128. $this->dbh->query("BEGIN");
  129. $result = $this->dbh->query("SELECT caption FROM ttrss_labels2
  130. WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
  131. if ($this->dbh->num_rows($result) != 0) {
  132. $old_caption = $this->dbh->fetch_result($result, 0, "caption");
  133. $result = $this->dbh->query("SELECT id FROM ttrss_labels2
  134. WHERE caption = '$caption' AND owner_uid = ". $_SESSION["uid"]);
  135. if ($this->dbh->num_rows($result) == 0) {
  136. if ($caption) {
  137. $result = $this->dbh->query("UPDATE ttrss_labels2 SET
  138. caption = '$caption' WHERE id = '$id' AND
  139. owner_uid = " . $_SESSION["uid"]);
  140. /* Update filters that reference label being renamed */
  141. $old_caption = $this->dbh->escape_string($old_caption);
  142. $this->dbh->query("UPDATE ttrss_filters2_actions SET
  143. action_param = '$caption' WHERE action_param = '$old_caption'
  144. AND action_id = 7
  145. AND filter_id IN (SELECT id FROM ttrss_filters2 WHERE owner_uid = ".$_SESSION["uid"].")");
  146. print $_REQUEST["value"];
  147. } else {
  148. print $old_caption;
  149. }
  150. } else {
  151. print $old_caption;
  152. }
  153. }
  154. $this->dbh->query("COMMIT");
  155. return;
  156. }
  157. function remove() {
  158. $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
  159. foreach ($ids as $id) {
  160. label_remove($id, $_SESSION["uid"]);
  161. }
  162. }
  163. function add() {
  164. $caption = $this->dbh->escape_string($_REQUEST["caption"]);
  165. $output = $this->dbh->escape_string($_REQUEST["output"]);
  166. if ($caption) {
  167. if (label_create($caption)) {
  168. if (!$output) {
  169. print T_sprintf("Created label <b>%s</b>", htmlspecialchars($caption));
  170. }
  171. }
  172. if ($output == "select") {
  173. header("Content-Type: text/xml");
  174. print "<rpc-reply><payload>";
  175. print_label_select("select_label",
  176. $caption, "");
  177. print "</payload></rpc-reply>";
  178. }
  179. }
  180. return;
  181. }
  182. function index() {
  183. print "<div id=\"pref-label-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
  184. print "<div id=\"pref-label-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
  185. print "<div id=\"pref-label-toolbar\" dojoType=\"dijit.Toolbar\">";
  186. print "<div dojoType=\"dijit.form.DropDownButton\">".
  187. "<span>" . __('Select')."</span>";
  188. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  189. print "<div onclick=\"dijit.byId('labelTree').model.setAllChecked(true)\"
  190. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  191. print "<div onclick=\"dijit.byId('labelTree').model.setAllChecked(false)\"
  192. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  193. print "</div></div>";
  194. print"<button dojoType=\"dijit.form.Button\" onclick=\"return addLabel()\">".
  195. __('Create label')."</button dojoType=\"dijit.form.Button\"> ";
  196. print "<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedLabels()\">".
  197. __('Remove')."</button dojoType=\"dijit.form.Button\"> ";
  198. print "<button dojoType=\"dijit.form.Button\" onclick=\"labelColorReset()\">".
  199. __('Clear colors')."</button dojoType=\"dijit.form.Button\">";
  200. print "</div>"; #toolbar
  201. print "</div>"; #pane
  202. print "<div id=\"pref-label-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
  203. print "<div id=\"labellistLoading\">
  204. <img src='images/indicator_tiny.gif'>".
  205. __("Loading, please wait...")."</div>";
  206. print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"labelStore\"
  207. url=\"backend.php?op=pref-labels&method=getlabeltree\">
  208. </div>
  209. <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"labelModel\" store=\"labelStore\"
  210. query=\"{id:'root'}\" rootId=\"root\"
  211. childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
  212. </div>
  213. <div dojoType=\"fox.PrefLabelTree\" id=\"labelTree\"
  214. model=\"labelModel\" openOnClick=\"true\">
  215. <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
  216. Element.hide(\"labellistLoading\");
  217. </script>
  218. <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
  219. var id = String(item.id);
  220. var bare_id = id.substr(id.indexOf(':')+1);
  221. if (id.match('LABEL:')) {
  222. editLabel(bare_id);
  223. }
  224. </script>
  225. </div>";
  226. print "</div>"; #pane
  227. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
  228. "hook_prefs_tab", "prefLabels");
  229. print "</div>"; #container
  230. }
  231. }