PageRenderTime 54ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/php/editannotations.php

https://bitbucket.org/silverasm/wordseer
PHP | 149 lines | 125 code | 8 blank | 16 comment | 21 complexity | 7462d619c1491d01e8b595fdbeab2f1e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /* Copyright 2012 Aditi Muralidharan. See the file "LICENSE" for the full license governing this code. */
  3. /******************************************************************
  4. editannotations.php
  5. Called by deleteTag, deleteNote, editNote, deleteNote, and
  6. deleteAnnotation in annotate.js in service of view.js
  7. in service of view.php
  8. Contains utilities for deleting and editing annotations
  9. *******************************************************************/
  10. include_once '../../config.php';
  11. include_once 'dbsetup.php';
  12. include_once 'util.php';
  13. if($_GET['event']=='delete-tag'){
  14. deleteTag($_GET['tag'], $_GET['highlight']);
  15. }
  16. else if($_GET['event'] == 'delete-note'){
  17. deleteNote($_GET['note'], $_GET['highlight']);
  18. }
  19. else if($_GET['event'] == 'edit-note'){
  20. editNote($_GET['note'], mysql_real_escape_string($_GET['text']));
  21. }
  22. else if($_GET['event'] == 'add-note'){
  23. addNote(mysql_real_escape_string($_GET['text']), $_GET['highlight'], $_GET['user']);
  24. }
  25. else if($_GET['event'] == 'add-tags'){
  26. addTags(mysql_real_escape_string($_GET['tags']), $_GET['highlight'], $_GET['user']);
  27. }
  28. else if($_GET['event'] == 'delete-annotation'){
  29. deleteAnnotation($_GET['highlight']);
  30. }
  31. /** delete a tag **/
  32. function deleteTag($tagID, $highlightID){
  33. $query = "DELETE from highlight_xref_tag WHERE tag_id =".$tagID." AND highlight_id = ".$highlightID.";";
  34. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  35. <br/> Query: " . $query . "
  36. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  37. echo json_encode(array('error'=>'no-error'));
  38. }
  39. /** delete a note **/
  40. function deleteNote($noteID, $highlightID){
  41. //delete cross reference
  42. $query = "DELETE from highlight_xref_note WHERE note_id =".$noteID." AND highlight_id = ".$highlightID.";";
  43. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  44. <br/> Query: " . $query . "
  45. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  46. if($result){
  47. // delete note
  48. $query = "DELETE from note where id = ".$noteID.";";
  49. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  50. <br/> Query: " . $query . "
  51. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  52. echo json_encode(array('error'=>'no-error'));
  53. }
  54. }
  55. /** edit a note **/
  56. function editNote($noteID, $text){
  57. $query = "UPDATE note SET text = '".$text."' where id = ".$noteID.";";
  58. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  59. <br/> Query: " . $query . "
  60. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  61. echo json_encode(array('error'=>'no-error'));
  62. }
  63. /** add a note **/
  64. function addNote($text, $highlightID, $user){
  65. $query = "INSERT into note (text, user) VALUES('".$text."', '".$user."');";
  66. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  67. <br/> Query: " . $query . "
  68. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  69. if($result){
  70. $query = "SELECT id from note where text = '".$text."' AND user = '".$user."';";
  71. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  72. <br/> Query: " . $query . "
  73. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  74. $note = mysql_fetch_array($result);
  75. $noteID = $note['id'];
  76. $query = "SELECT narrative_id from highlight where id = ".$highlightID.";";
  77. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  78. <br/> Query: " . $query . "
  79. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  80. $narrative = mysql_fetch_array($result);
  81. $narrativeID = $narrative['narrative_id'];
  82. $query = "INSERT into highlight_xref_note (highlight_id, note_id, narrative) VALUES(".$highlightID.", ".$noteID.", ".$narrativeID.");";
  83. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  84. <br/> Query: " . $query . "
  85. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  86. echo json_encode(array('error'=>'no-error'));
  87. }
  88. }
  89. /** add tags **/
  90. function addTags($tags, $highlightID, $user){
  91. foreach(split(",", $tags) as $tag){
  92. $query = "INSERT into tag (name) VALUES('".$tag."');";
  93. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  94. <br/> Query: " . $query . "
  95. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  96. if($result){
  97. $query = "SELECT id from tag where name = '".$tag."';";
  98. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  99. <br/> Query: " . $query . "
  100. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  101. $t = mysql_fetch_array($result);
  102. $tagID = $t['id'];
  103. $query = "SELECT narrative_id from highlight where id = ".$highlightID.";";
  104. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  105. <br/> Query: " . $query . "
  106. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  107. $narrative = mysql_fetch_array($result);
  108. $narrativeID = $narrative['narrative_id'];
  109. $query = "INSERT into highlight_xref_tag (highlight_id, tag_id, narrative, user) VALUES(".$highlightID.", ".$tagID.", ".$narrativeID.", '".$user."');";
  110. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  111. <br/> Query: " . $query . "
  112. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  113. }
  114. }
  115. echo json_encode(array('error'=>'no-error'));
  116. }
  117. /** delete an annotation and all associated content **/
  118. function deleteAnnotation($highlightID, $user){
  119. $query = "SELECT * from highlight_xref_note WHERE highlight_id = ".$highlightID.";";
  120. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  121. <br/> Query: " . $query . "
  122. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  123. if(mysql_num_rows($result) > 0){
  124. echo json_encode(array('error'=>'There are attached notes!'));
  125. }else{
  126. $query = "SELECT * from highlight_xref_tag WHERE highlight_id = ".$highlightID.";";
  127. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  128. <br/> Query: " . $query . "
  129. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  130. if(mysql_num_rows($result) > 0){
  131. echo json_encode(array('error'=>'There are attached tags!'));
  132. }else{
  133. $query = "DELETE from highlight WHERE id = ".$highlightID.";";
  134. $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.
  135. <br/> Query: " . $query . "
  136. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  137. echo json_encode(array('error'=>'no-error'));
  138. }
  139. }
  140. }
  141. ?>