PageRenderTime 33ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/objects/category.php

https://gitlab.com/Raymon/QualityCaps_PHP
PHP | 282 lines | 138 code | 71 blank | 73 comment | 3 complexity | 2d4e091e3669bb9ea87fb7ee5d19cec1 MD5 | raw file
  1. <?php
  2. // 'category' object
  3. class Category{
  4. // database connection and table name
  5. private $conn;
  6. private $table_name = "categories";
  7. // object properties
  8. public $id;
  9. public $name;
  10. public $description;
  11. // constructor
  12. public function __construct($db){
  13. $this->conn = $db;
  14. }
  15. // delete the product
  16. function delete(){
  17. // delete query
  18. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  19. // prepare query statement
  20. $stmt = $this->conn->prepare($query);
  21. // sanitize
  22. $this->id=htmlspecialchars(strip_tags($this->id));
  23. // bind record id
  24. $stmt->bindParam(1, $this->id);
  25. // execute the query
  26. if($result = $stmt->execute()){
  27. return true;
  28. }else{
  29. return false;
  30. }
  31. }
  32. // count all categories based on search term
  33. public function countAll_BySearch($search_term){
  34. // search query
  35. $query = "SELECT id FROM " . $this->table_name . " WHERE name LIKE ?";
  36. // prepare query statement
  37. $stmt = $this->conn->prepare( $query );
  38. // sanitize
  39. $search_term=htmlspecialchars(strip_tags($search_term));
  40. $search_term = "%{$search_term}%";
  41. // bind search term
  42. $stmt->bindParam(1, $search_term);
  43. // execute query
  44. $stmt->execute();
  45. // get row count
  46. $num = $stmt->rowCount();
  47. // return row count
  48. return $num;
  49. }
  50. // search categories
  51. function search($search_term, $from_record_num, $records_per_page){
  52. // search query
  53. $query = "SELECT id, name, description
  54. FROM " . $this->table_name . "
  55. WHERE name LIKE ?
  56. ORDER BY name ASC
  57. LIMIT ?, ?";
  58. // prepare query statement
  59. $stmt = $this->conn->prepare( $query );
  60. // sanitize
  61. $search_term = "%{$search_term}%";
  62. $search_term=htmlspecialchars(strip_tags($search_term));
  63. // bind variables
  64. $stmt->bindParam(1, $search_term);
  65. $stmt->bindParam(2, $from_record_num, PDO::PARAM_INT);
  66. $stmt->bindParam(3, $records_per_page, PDO::PARAM_INT);
  67. // execute query
  68. $stmt->execute();
  69. // return values
  70. return $stmt;
  71. }
  72. // update the category
  73. function update(){
  74. // update query
  75. $query = "UPDATE " . $this->table_name . "
  76. SET name = :name, description = :description
  77. WHERE id = :id";
  78. // prepare query statement
  79. $stmt = $this->conn->prepare($query);
  80. // sanitize
  81. $this->name=htmlspecialchars(strip_tags($this->name));
  82. $this->description=htmlspecialchars(strip_tags($this->description));
  83. $this->id=htmlspecialchars(strip_tags($this->id));
  84. // bind values
  85. $stmt->bindParam(':name', $this->name);
  86. $stmt->bindParam(':description', $this->description);
  87. $stmt->bindParam(':id', $this->id);
  88. // execute the query
  89. if($stmt->execute()){
  90. return true;
  91. }else{
  92. return false;
  93. }
  94. }
  95. // create category
  96. function create(){
  97. // to get time-stamp for 'created' field
  98. $this->getTimestamp();
  99. // insert query
  100. $query = "INSERT INTO " . $this->table_name . "
  101. SET name = ?, description = ?, created = ?";
  102. // prepare query statement
  103. $stmt = $this->conn->prepare($query);
  104. // sanitize
  105. $this->name=htmlspecialchars(strip_tags($this->name));
  106. $this->description=htmlspecialchars(strip_tags($this->description));
  107. // bind values
  108. $stmt->bindParam(1, $this->name);
  109. $stmt->bindParam(2, $this->description);
  110. $stmt->bindParam(3, $this->timestamp);
  111. // execute query
  112. if($stmt->execute()){
  113. return true;
  114. }else{
  115. $stmt->execute();
  116. $errorInfor = $stmt->errorInfo();
  117. $file = fopen('log.txt', 'a') or exit("Unable to open file!");
  118. //Output a line of the file until the end is reached
  119. fwrite($file, $errorInfor[2] . date('d/m/Y == H:i:s') ."\r\n" );
  120. fclose($file);
  121. return false;
  122. }
  123. }
  124. // read category details
  125. function readOne(){
  126. // select single record query
  127. $query = "SELECT name, description
  128. FROM " . $this->table_name . "
  129. WHERE id = ?
  130. LIMIT 0,1";
  131. // prepare query statement
  132. $stmt = $this->conn->prepare( $query );
  133. // sanitize
  134. $this->id=htmlspecialchars(strip_tags($this->id));
  135. // bind selected record id
  136. $stmt->bindParam(1, $this->id);
  137. // execute the query
  138. $stmt->execute();
  139. // get record details
  140. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  141. // assign values to object properties
  142. $this->name = $row['name'];
  143. $this->description = $row['description'];
  144. }
  145. // read all available categories (with limit clause for paging)
  146. function readAll($from_record_num, $records_per_page){
  147. // query select all categories
  148. $query = "SELECT id, name, description
  149. FROM " . $this->table_name . "
  150. ORDER BY name
  151. LIMIT ?, ?";
  152. // prepare query statement
  153. $stmt = $this->conn->prepare( $query );
  154. // bind values
  155. $stmt->bindParam(1, $from_record_num, PDO::PARAM_INT);
  156. $stmt->bindParam(2, $records_per_page, PDO::PARAM_INT);
  157. // execute query
  158. $stmt->execute();
  159. // return values
  160. return $stmt;
  161. }
  162. // read all categories without limit clause, used drop-down list
  163. function readAll_WithoutPaging(){
  164. // select all data
  165. $query = "SELECT id, name, description
  166. FROM " . $this->table_name . "
  167. ORDER BY name";
  168. // prepare query statement
  169. $stmt = $this->conn->prepare( $query );
  170. // execute query
  171. $stmt->execute();
  172. // return values
  173. return $stmt;
  174. }
  175. // used for paging categories
  176. public function countAll(){
  177. // query to count all data
  178. $query = "SELECT count(*) FROM " . $this->table_name;
  179. // prepare query statement
  180. $stmt = $this->conn->prepare( $query );
  181. // execute query
  182. $stmt->execute();
  183. // get row value
  184. $rows = $stmt->fetch(PDO::FETCH_NUM);
  185. // return all data count
  186. return $rows[0];
  187. }
  188. // used to read category name by its ID
  189. function readName(){
  190. // select single record query
  191. $query = "SELECT name FROM " . $this->table_name . " WHERE id = ? limit 0,1";
  192. // prepare query statement
  193. $stmt = $this->conn->prepare( $query );
  194. // sanitize
  195. $this->id=htmlspecialchars(strip_tags($this->id));
  196. // bind selected record id
  197. $stmt->bindParam(1, $this->id);
  198. // execute query
  199. $stmt->execute();
  200. // read row
  201. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  202. // set value to 'name' property
  203. $this->name = $row['name'];
  204. }
  205. // used for the 'created' field
  206. function getTimestamp(){
  207. date_default_timezone_set('Asia/Manila');
  208. $this->timestamp = date('Y-m-d H:i:s');
  209. }
  210. }
  211. ?>