PageRenderTime 66ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/logic.php

https://github.com/nst/Quickies
PHP | 275 lines | 198 code | 74 blank | 3 comment | 27 complexity | cbab8712fc34136b0a032c998b22bc8b MD5 | raw file
  1. <?php
  2. require_once('markdown.php');
  3. class DBObject {
  4. }
  5. class Category extends DBObject {
  6. var $id;
  7. var $name;
  8. public static $table_name = 'q_category';
  9. public function delete() {
  10. $query = "DELETE FROM ".Category::$table_name." WHERE id = ".$this->id." LIMIT 1";
  11. $success = null;
  12. $result = mysqli_query($success, $query) or die("mysql_error in delete: <b>".$query."</b> ". mysql_error());
  13. return 1;
  14. }
  15. public static function Create($name_param=null) {
  16. if($name_param == null) return;
  17. $name_param_safe = mysql_real_escape_string(stripslashes($name_param));
  18. $query = "INSERT INTO ".Category::$table_name." (name) VALUES ('".$name_param_safe."')";
  19. //echo $query;
  20. $result = mysqli_query($success, $query) or die("mysql_error in insert: ". mysql_error());
  21. return 1;
  22. }
  23. public function update($req_name) {
  24. $safe_req_name = mysql_real_escape_string(stripslashes($req_name));
  25. $query = "UPDATE ".Category::$table_name." SET name = '".$safe_req_name."' WHERE id = ".$this->id." LIMIT 1";
  26. if($req_name == null) {
  27. die($query);
  28. }
  29. $result = mysqli_query($success, $query) or die("mysql_error in update: <b>".$query."</b> ". mysql_error());
  30. }
  31. public static function AllObjects($where_clause=null) {
  32. $query = "SELECT C.id, C.name FROM ".Category::$table_name." AS C";
  33. if($where_clause) {
  34. $query .= " WHERE (".$where_clause.")";
  35. }
  36. $query .= " ORDER BY C.name";
  37. $con = $GLOBALS["con"];
  38. $result = mysqli_query($con, $query) or die("Error in query: ". mysql_error());
  39. $a = array();
  40. while (list($cat_id, $cat_name) = mysqli_fetch_row($result)) {
  41. $c = new Category();
  42. $c->id = $cat_id;
  43. $c->name = $cat_name;
  44. array_push($a, $c);
  45. }
  46. return $a;
  47. }
  48. public static function CategoriesWithId($id) {
  49. $id_int = (int)$id;
  50. $where_clause = "C.id = ".$id_int;
  51. return Category::AllObjects($where_clause);
  52. }
  53. public static function CategoryWithId($id) {
  54. $cs = Category::CategoriesWithId($id);
  55. return $cs[0];
  56. }
  57. }
  58. class Note extends DBObject {
  59. var $id;
  60. var $title;
  61. var $text;
  62. var $timestamp;
  63. var $category_id;
  64. var $category_name;
  65. public static $table_name = 'q_note';
  66. public function delete() {
  67. $query = "DELETE FROM ".Note::$table_name." WHERE id = ".$this->id." LIMIT 1";
  68. $result = mysqli_query($success, $query) or die("mysql_error in delete: <b>".$query."</b> ". mysql_error());
  69. return 1;
  70. }
  71. public function update($req_cat_id, $req_note_title, $req_note_text) {
  72. $con = $GLOBALS["con"];
  73. $safe_cat_id = mysqli_real_escape_string($con, (int)$req_cat_id);
  74. $safe_note_title = mysqli_real_escape_string($con, stripslashes($req_note_title));
  75. $safe_note_text = mysqli_real_escape_string($con, stripslashes($req_note_text));
  76. $query = "UPDATE ".Note::$table_name." SET title = '".$safe_note_title."', text = '".$safe_note_text."', category_id = '".$safe_cat_id."' WHERE id = ".$this->id." LIMIT 1";
  77. if($req_cat_id == 0 || $req_note_title == null || $req_note_text == null) {
  78. die($query);
  79. }
  80. $result = mysqli_query($con, $query) or die("mysql_error in update: <b>".$query."</b> ". mysql_error());
  81. }
  82. public static function AllObjectsCount() {
  83. $query = "SELECT COUNT(*) FROM ".Note::$table_name;
  84. $con = $GLOBALS["con"];
  85. $result = mysqli_query($con, $query) or die("Error in query: ". mysql_error());
  86. while (list($count) = mysqli_fetch_row($result)) {
  87. return $count;
  88. }
  89. return 0;
  90. }
  91. public static function Create($req_cat_id, $req_title, $req_text) {
  92. if($req_cat_id == null || $req_title == null || $req_text == null) return;
  93. $con = $GLOBALS["con"];
  94. $safe_cat_id = mysqli_real_escape_string($con, (int)$req_cat_id);
  95. $safe_title = mysqli_real_escape_string($con, stripslashes($req_title));
  96. $safe_text = mysqli_real_escape_string($con, stripslashes($req_text));
  97. $query = "INSERT INTO ".Note::$table_name." (category_id, title, text) VALUES ('".$safe_cat_id."', '".$safe_title."', '".$safe_text."')";
  98. $result = mysqli_query($con, $query) or die("mysql_error in insert: ". mysql_error());
  99. return 1;
  100. }
  101. public static function AllObjects($where_clause=null, $order_clause=null, $limit=0) {
  102. $query = "SELECT N.id, N.title, N.text, N.timestamp, N.category_id, C.id, C.name FROM ".Note::$table_name." AS N, ".Category::$table_name." AS C WHERE C.id = N.category_id";
  103. if($where_clause) {
  104. $query .= " AND (".$where_clause.")";
  105. }
  106. $query .= " ORDER BY ".$order_clause." C.name, N.title";
  107. if($limit != 0) {
  108. $query .= " LIMIT ".(int)$limit;
  109. }
  110. $con = $GLOBALS["con"];
  111. $result = mysqli_query($con, $query) or die("Error in query: ". mysql_error());
  112. $a = array();
  113. while (list($note_id, $note_title, $note_text, $note_timestamp, $note_category_id, $category_id, $category_name) = mysqli_fetch_row($result)) {
  114. $n = new Note();
  115. $n->id = $note_id;
  116. $n->title = $note_title;
  117. $n->text = $note_text;
  118. $n->timestamp = $note_timestamp;
  119. $n->category_id = $note_category_id;
  120. $n->category_name = $category_name;
  121. array_push($a, $n);
  122. }
  123. return $a;
  124. }
  125. public static function LatestNotes() {
  126. return Note::AllObjects(null, "N.timestamp DESC,", 15);
  127. }
  128. public static function AllNotesIncludingOnesWithoutCategory() {
  129. $a1 = Note::NotesWithMissingCategory();
  130. $a2 = Note::AllObjects();
  131. foreach($a2 as $n) {
  132. array_push($a1, $n);
  133. }
  134. return $a1;
  135. }
  136. public static function NotesWithMissingCategory() {
  137. $query = "SELECT N.id, N.title, N.text, N.timestamp, N.category_id FROM ".Note::$table_name." AS N WHERE category_id NOT IN (SELECT id FROM ".Category::$table_name.");";
  138. $con = $GLOBALS["con"];
  139. $result = mysqli_query($con, $query) or die("Error in query: ". mysql_error());
  140. $a = array();
  141. while (list($note_id, $note_title, $note_text, $note_timestamp, $note_category_id, $category_id) = mysqli_fetch_row($result)) {
  142. $n = new Note();
  143. $n->id = $note_id;
  144. $n->title = $note_title;
  145. $n->text = $note_text;
  146. $n->timestamp = $note_timestamp;
  147. $n->category_id = 0;
  148. $n->category_name = "** no category **";
  149. array_push($a, $n);
  150. }
  151. return $a;
  152. }
  153. public static function NotesWithId($id) {
  154. $id_int = (int)$id;
  155. $where_clause = "N.id = ".$id_int;
  156. return Note::AllObjects($where_clause);
  157. }
  158. public static function NotesWithCatId($id) {
  159. $id_int = (int)$id;
  160. $where_clause = "C.id = ".$id_int;
  161. return Note::AllObjects($where_clause);
  162. }
  163. public static function NoteWithId($id) {
  164. $notes = Note::NotesWithId($id);
  165. $n = $notes[0];
  166. if($n) return $n;
  167. return Note::NoteWithIdNoCategory($id);
  168. }
  169. public static function NotesWithIdNoCategory($id) {
  170. $id_int = (int)$id;
  171. $query = "SELECT id, title, text, timestamp, category_id FROM `".Note::$table_name."` WHERE id = ".$id_int.";";
  172. //echo $query;
  173. $result = mysql_query($query) or die("Error in query: ". mysql_error());
  174. $a = array();
  175. while (list($note_id, $note_title, $note_text, $note_timestamp, $note_category_id) = mysqli_fetch_row($result)) {
  176. $n = new Note();
  177. $n->id = $note_id;
  178. $n->title = $note_title;
  179. $n->text = $note_text;
  180. $n->timestamp = $note_timestamp;
  181. $n->category_id = $note_category_id;
  182. array_push($a, $n);
  183. }
  184. return $a;
  185. }
  186. public static function NoteWithIdNoCategory($id) {
  187. $a = Note::NotesWithIdNoCategory($id);
  188. if(count($a) > 0) {
  189. return $a[0];
  190. }
  191. return null;
  192. }
  193. public function NotesWithSearchString($s) {
  194. $con = $GLOBALS["con"];
  195. $ss = mysqli_real_escape_string($con, $s);
  196. //echo $s;
  197. $where_clause = "N.title LIKE '%$ss%' OR N.text LIKE '%$ss%'";
  198. return Note::AllObjects($where_clause);
  199. }
  200. }
  201. ?>