/objects/product.php

https://gitlab.com/Raymon/QualityCaps_PHP · PHP · 494 lines · 285 code · 105 blank · 104 comment · 4 complexity · 89e8136680659c3823d354b1c03be66a MD5 · raw file

  1. <?php
  2. // 'product' object
  3. class Product{
  4. // database connection and table name
  5. private $conn;
  6. private $table_name = "products";
  7. // object properties
  8. public $id;
  9. public $name;
  10. public $price;
  11. public $category_id;
  12. public $category_name;
  13. public $supplier_id;
  14. public $supplier_name;
  15. // constructor
  16. public function __construct($db){
  17. $this->conn = $db;
  18. }
  19. // read products with field sorting
  20. public function readAll_WithSorting($from_record_num, $records_per_page, $field, $order){
  21. $query = "SELECT p.id, p.name, p.price, p.category_id, c.name as category_name, p.supplier_id, s.name as supplier_name,
  22. FROM products p
  23. LEFT JOIN categories c
  24. ON p.category_id=c.id
  25. LEFT JOIN suppliers s
  26. ON p.supplier_id = s.id
  27. ORDER BY {$field} {$order}
  28. LIMIT :from_record_num, :records_per_page";
  29. // prepare query
  30. $stmt = $this->conn->prepare($query);
  31. // bind
  32. $stmt->bindParam(":from_record_num", $from_record_num, PDO::PARAM_INT);
  33. $stmt->bindParam(":records_per_page", $records_per_page, PDO::PARAM_INT);
  34. $stmt->execute();
  35. // return values from database
  36. return $stmt;
  37. }
  38. // create product record
  39. function create(){
  40. // insert product query
  41. $query = "INSERT INTO
  42. " . $this->table_name . "
  43. SET
  44. name = ?, price = ?, category_id = ?,supplier_id = ?";
  45. // prepare query statement
  46. $stmt = $this->conn->prepare($query);
  47. // sanitize
  48. $this->name=htmlspecialchars(strip_tags($this->name));
  49. $this->price=htmlspecialchars(strip_tags($this->price));
  50. $this->category_id=htmlspecialchars(strip_tags($this->category_id));
  51. $this->supplier_id=htmlspecialchars(strip_tags($this->supplier_id));
  52. // bind values
  53. $stmt->bindParam(1, $this->name);
  54. $stmt->bindParam(2, $this->price);
  55. $stmt->bindParam(3, $this->category_id);
  56. $stmt->bindParam(4, $this->supplier_id);
  57. // execute query
  58. if($stmt->execute()){
  59. return true;
  60. }else{
  61. $stmt->execute();
  62. $errorInfor = $stmt->errorInfo();
  63. $file = fopen('log.txt', 'a') or exit("Unable to open file!");
  64. //Output a line of the file until the end is reached
  65. fwrite($file, $errorInfor[2] . date('d/m/Y == H:i:s') ."\r\n" );
  66. fclose($file);
  67. return false;
  68. }
  69. }
  70. // read all products
  71. function readAll($from_record_num, $records_per_page){
  72. // select all products query
  73. $query = "SELECT
  74. c.name as category_name, p.id, p.name, p.price, p.category_id,p.supplier_id,s.name as supplier_name
  75. FROM
  76. " . $this->table_name . " p
  77. LEFT JOIN
  78. categories c
  79. ON
  80. p.category_id = c.id
  81. LEFT JOIN
  82. suppliers s
  83. ON
  84. p.supplier_id = s.id
  85. ORDER BY
  86. p.name DESC
  87. LIMIT
  88. ?, ?";
  89. // prepare query statement
  90. $stmt = $this->conn->prepare( $query );
  91. // bind limit clause variables
  92. $stmt->bindParam(1, $from_record_num, PDO::PARAM_INT);
  93. $stmt->bindParam(2, $records_per_page, PDO::PARAM_INT);
  94. // execute query
  95. $stmt->execute();
  96. // return values
  97. return $stmt;
  98. }
  99. // read all inactive products
  100. function readAll_Inactive($from_record_num, $records_per_page){
  101. // sql query to read all inactive products
  102. $query = "SELECT
  103. c.name as category_name,s.name as supplier_name, p.id, p.name, p.price, p.category_id,p.supplier_id
  104. FROM
  105. " . $this->table_name . " p
  106. LEFT JOIN
  107. categories c
  108. ON
  109. p.category_id = c.id
  110. LEFT JOIN
  111. suppliers s
  112. ON
  113. p.supplier_id = s.id
  114. ORDER BY
  115. p.name ASC
  116. LIMIT
  117. ?, ?";
  118. // prepare query statement
  119. $stmt = $this->conn->prepare( $query );
  120. // bind limit clause variables
  121. $stmt->bindParam(1, $from_record_num, PDO::PARAM_INT);
  122. $stmt->bindParam(2, $records_per_page, PDO::PARAM_INT);
  123. // execute query
  124. $stmt->execute();
  125. // return values
  126. return $stmt;
  127. }
  128. // read products by category id
  129. function readAllByCategory($from_record_num, $records_per_page){
  130. // query to read all products by category
  131. $query = "SELECT
  132. c.name as category_name,s.name as supplier_name, p.id, p.name, p.price, p.category_id,p.supplier_id
  133. FROM
  134. " . $this->table_name . " p
  135. LEFT JOIN
  136. categories c
  137. ON
  138. p.category_id = c.id
  139. LEFT JOIN
  140. suppliers s
  141. ON
  142. p.supplier_id = s.id
  143. WHERE
  144. category_id = ?
  145. ORDER BY
  146. p.name ASC
  147. LIMIT
  148. ?, ?";
  149. // prepare query statement
  150. $stmt = $this->conn->prepare( $query );
  151. // sanitize
  152. $this->category_id=htmlspecialchars(strip_tags($this->category_id));
  153. // bind variable values
  154. $stmt->bindParam(1, $this->category_id);
  155. $stmt->bindParam(2, $from_record_num, PDO::PARAM_INT);
  156. $stmt->bindParam(3, $records_per_page, PDO::PARAM_INT);
  157. // execute query
  158. $stmt->execute();
  159. // return values
  160. return $stmt;
  161. }
  162. // read products by search term
  163. function search($search_term, $from_record_num, $records_per_page){
  164. // select query
  165. $query = "SELECT
  166. c.name as category_name, p.id, p.name, p.price, p.category_id
  167. FROM
  168. " . $this->table_name . " p
  169. LEFT JOIN
  170. categories c
  171. ON p.category_id = c.id
  172. WHERE
  173. p.name LIKE ?
  174. ORDER BY
  175. p.name ASC
  176. LIMIT
  177. ?, ?";
  178. // prepare query statement
  179. $stmt = $this->conn->prepare( $query );
  180. // sanitize
  181. $search_term = "%{$search_term}%";
  182. $search_term=htmlspecialchars(strip_tags($search_term));
  183. // bind variable values
  184. $stmt->bindParam(1, $search_term);
  185. $stmt->bindParam(2, $from_record_num, PDO::PARAM_INT);
  186. $stmt->bindParam(3, $records_per_page, PDO::PARAM_INT);
  187. // execute query
  188. $stmt->execute();
  189. // return values from database
  190. return $stmt;
  191. }
  192. // read all product based on product ids included in the $ids variable
  193. // reference http://stackoverflow.com/a/10722827/827418
  194. public function readByIds($ids){
  195. $ids_arr = str_repeat('?,', count($ids) - 1) . '?';
  196. // query to select products
  197. $query = "SELECT id, name, price FROM products WHERE id IN ({$ids_arr}) ORDER BY name";
  198. // prepare query statement
  199. $stmt = $this->conn->prepare($query);
  200. // execute query
  201. $stmt->execute($ids);
  202. // return values from database
  203. return $stmt;
  204. }
  205. // used for paging products
  206. public function countAll(){
  207. // query to count all product records
  208. $query = "SELECT count(*) FROM " . $this->table_name ;
  209. // prepare query statement
  210. $stmt = $this->conn->prepare( $query );
  211. // execute query
  212. $stmt->execute();
  213. // get row value
  214. $rows = $stmt->fetch(PDO::FETCH_NUM);
  215. // return count
  216. return $rows[0];
  217. }
  218. // used for paging products by category
  219. public function countAll_ByCategory(){
  220. // 'select count' query to count products under a category
  221. $query = "SELECT count(*) FROM " . $this->table_name . " WHERE category_id=? ";
  222. // prepare query statement
  223. $stmt = $this->conn->prepare( $query );
  224. // sanitize
  225. $this->category_id=htmlspecialchars(strip_tags($this->category_id));
  226. // bind category id variable
  227. $stmt->bindParam(1, $this->category_id);
  228. // execute query
  229. $stmt->execute();
  230. // get row value
  231. $rows = $stmt->fetch(PDO::FETCH_NUM);
  232. // return count
  233. return $rows[0];
  234. }
  235. // used for paging inactive product list
  236. public function countAll_Inactive(){
  237. // count all inactive products
  238. $query = "SELECT count(*) FROM " . $this->table_name;
  239. // prepare query statement
  240. $stmt = $this->conn->prepare( $query );
  241. // execute query
  242. $stmt->execute();
  243. // get row value
  244. $rows = $stmt->fetch(PDO::FETCH_NUM);
  245. // return count
  246. return $rows[0];
  247. }
  248. // used for paging products based on search term
  249. public function countAll_BySearch($search_term){
  250. // select query to count all records by search term
  251. $query = "SELECT id FROM " . $this->table_name . " WHERE name LIKE ?";
  252. // prepare query statement
  253. $stmt = $this->conn->prepare( $query );
  254. // sanitize
  255. $search_term = "%{$search_term}%";
  256. $search_term=htmlspecialchars(strip_tags($search_term));
  257. // bind search term
  258. $stmt->bindParam(1, $search_term);
  259. // execute query
  260. $stmt->execute();
  261. // get number of records retrieved
  262. $num = $stmt->rowCount();
  263. // return count
  264. return $num;
  265. }
  266. // check if product is active or not
  267. function isActive(){
  268. // query to select single record
  269. $query = "SELECT
  270. id
  271. FROM
  272. " . $this->table_name . "
  273. WHERE
  274. id=?
  275. LIMIT
  276. 0,1";
  277. // prepare query statement
  278. $stmt = $this->conn->prepare( $query );
  279. // sanitize
  280. $this->id=htmlspecialchars(strip_tags($this->id));
  281. // bind product id value
  282. $stmt->bindParam(1, $this->id);
  283. // execute query
  284. $stmt->execute();
  285. // get number of records retrieved
  286. $num = $stmt->rowCount();
  287. if($num>0){
  288. return true;
  289. }
  290. return false;
  291. }
  292. // used when filling up the update product form
  293. function readOne(){
  294. // query to select single record
  295. $query = "SELECT
  296. c.name as category_name,s.name as supplier_name, p.name, p.price, p.category_id,p.supplier_id
  297. FROM
  298. " . $this->table_name . " p
  299. LEFT JOIN
  300. categories c
  301. ON p.category_id = c.id
  302. LEFT JOIN
  303. suppliers s
  304. ON p.supplier_id = s.id
  305. WHERE
  306. p.id = ?
  307. LIMIT
  308. 0,1";
  309. // prepare query statement
  310. $stmt = $this->conn->prepare( $query );
  311. // sanitize
  312. $this->id=htmlspecialchars(strip_tags($this->id));
  313. // bind product id value
  314. $stmt->bindParam(1, $this->id);
  315. // execute query
  316. $stmt->execute();
  317. // get row values
  318. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  319. // assign retrieved row value to object properties
  320. $this->name = $row['name'];
  321. $this->price = $row['price'];
  322. $this->category_id = $row['category_id'];
  323. $this->category_name = $row['category_name'];
  324. $this->supplier_id = $row['supplier_id'];
  325. $this->supplier_name = $row['supplier_name'];
  326. }
  327. // update the product
  328. function update(){
  329. // product update query
  330. $query = "UPDATE
  331. " . $this->table_name . "
  332. SET
  333. name = :name,
  334. price = :price,
  335. category_id = :category_id,
  336. supplier_id = :supplier_id
  337. WHERE
  338. id = :id";
  339. // prepare query statement
  340. $stmt = $this->conn->prepare($query);
  341. // sanitize
  342. $this->name=htmlspecialchars(strip_tags($this->name));
  343. $this->price=htmlspecialchars(strip_tags($this->price));
  344. $this->category_id=htmlspecialchars(strip_tags($this->category_id));
  345. $this->supplier_id=htmlspecialchars(strip_tags($this->supplier_id));
  346. $this->id=htmlspecialchars(strip_tags($this->id));
  347. // bind variable values
  348. $stmt->bindParam(':name', $this->name);
  349. $stmt->bindParam(':price', $this->price);
  350. $stmt->bindParam(':category_id', $this->category_id);
  351. $stmt->bindParam(':supplier_id', $this->supplier_id);
  352. $stmt->bindParam(':id', $this->id);
  353. // execute the query
  354. if($stmt->execute()){
  355. return true;
  356. }else{
  357. return false;
  358. }
  359. }
  360. // delete the product
  361. function delete(){
  362. // delete product query
  363. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  364. // prepare query
  365. $stmt = $this->conn->prepare($query);
  366. // sanitize
  367. $this->id=htmlspecialchars(strip_tags($this->id));
  368. // bind product id variable
  369. $stmt->bindParam(1, $this->id);
  370. // execute query
  371. if($result=$stmt->execute()){
  372. return true;
  373. }else{
  374. $stmt->execute();
  375. $errorInfor = $stmt->errorInfo();
  376. $file = fopen('log.txt', 'a') or exit("Unable to open file!");
  377. //Output a line of the file until the end is reached
  378. fwrite($file, $errorInfor[2] . date('d/m/Y == H:i:s') ."\r\n" );
  379. fclose($file);
  380. return false;
  381. }
  382. }
  383. // used for the 'created' field when creating a product
  384. function getTimestamp(){
  385. date_default_timezone_set('Asia/Manila');
  386. $this->timestamp = date('Y-m-d H:i:s');
  387. }
  388. }
  389. ?>