/src/BITM/SEIP137028/City/City.php

https://gitlab.com/armanulislam/FinalAtomicProject · PHP · 223 lines · 164 code · 47 blank · 12 comment · 24 complexity · 6bb6e68505c3a3415987ecf20db287d4 MD5 · raw file

  1. <?php
  2. namespace App\BITM\SEIP137028\City;
  3. use App\BITM\SEIP137028\Message;
  4. use App\BITM\SEIP137028\Utility;
  5. class City
  6. {
  7. public $id;
  8. private $city;
  9. private $name;
  10. private $deletedAt;
  11. private $con;
  12. private $allcityData = array();
  13. private $allTrashedcityData = array();
  14. function __construct()
  15. {
  16. $this->con = mysqli_connect('localhost', 'root', "", 'atomicprojectb22') OR DIE("DATABASE CONNCET FAILED");
  17. }
  18. // Accessing DB for list to show data
  19. function index()
  20. {
  21. $sql = "SELECT * FROM `city` where deleted_at is null";
  22. $result = mysqli_query($this->con, $sql);
  23. // After getting result we need to contain those data in a variable
  24. // saving fetching data in allcityData variable and return it to show the data
  25. // in index.php file
  26. // we must declare allcityData as array to contain all data
  27. while ($row = mysqli_fetch_object($result))
  28. $this->allcityData[] = $row;
  29. return $this->allcityData;
  30. }
  31. public function prepareVariableValue($data)
  32. {
  33. if (array_key_exists('name', $data))
  34. $this->name = $data['name'];
  35. if (array_key_exists('city', $data))
  36. $this->city = $data['city'];
  37. if (array_key_exists('id', $data))
  38. $this->id = $data['id'];
  39. }
  40. function store()
  41. {
  42. $sql = "INSERT INTO `atomicprojectb22`.`city` (`name`, `city`)
  43. VALUES ('".$this->name."', '".$this->city."')";
  44. $result = mysqli_query($this->con, $sql);
  45. if ($result) {
  46. Message::examineMessage("
  47. <div class=\"alert alert-success\">
  48. <strong>Data has been stored successfully!</strong>
  49. </div>");
  50. Utility::reDirectAPageIntoAnotherPage("index.php");
  51. } else {
  52. Message::examineMessage("
  53. <div class=\"alert alert-success\">
  54. <strong>Data has not been stored successfully!</strong>
  55. </div>");
  56. Utility::reDirectAPageIntoAnotherPage("index.php");
  57. }
  58. //echo $result;
  59. // echo $this->city;
  60. //return " I am storing data ";
  61. }
  62. function update()
  63. {
  64. $query = "UPDATE `city` SET `name` = '" . $this->name . "', `city` = '" . $this->city . "'
  65. WHERE `city`.`id` = " . $this->id;
  66. $result = mysqli_query($this->con, $query);
  67. if ($result) {
  68. Message::examineMessage("
  69. <div class=\"alert alert-success\">
  70. <strong>Data has been Updated successfully!</strong>
  71. </div>");
  72. Utility::reDirectAPageIntoAnotherPage("index.php");
  73. } else Message::examineMessage("error");
  74. }
  75. function delete()
  76. {
  77. $query = "DELETE FROM `atomicprojectb22`.`city` WHERE `city`.`id` =" . $this->id;
  78. $result = mysqli_query($this->con, $query);
  79. if ($result) {
  80. Message::examineMessage("
  81. <div class=\"alert alert-success\">
  82. <strong>Data has been deleted successfully!</strong>
  83. </div>");
  84. Utility::reDirectAPageIntoAnotherPage("index.php");
  85. } else Message::examineMessage("error");
  86. }
  87. function view()
  88. {
  89. $query = "SELECT * FROM `city` WHERE `id`=" . $this->id;
  90. $result = mysqli_query($this->con, $query);
  91. $row = mysqli_fetch_object($result);
  92. return $row;
  93. }
  94. function trash()
  95. {
  96. $this->deletedAt = time();
  97. $sql = "UPDATE `city` SET `deleted_at` = '".$this->deletedAt."' WHERE `city`.`id` =". $this->id;
  98. $result = mysqli_query($this->con, $sql);
  99. if ($result) {
  100. Message::examineMessage("
  101. <div class=\"alert alert-success\">
  102. <strong>Data has been trashed successfully!</strong>
  103. </div>");
  104. Utility::reDirectAPageIntoAnotherPage("index.php");
  105. } else Message::examineMessage("error");
  106. }
  107. function trashed()
  108. {
  109. $sql = "SELECT * FROM `city` where deleted_at is not null";
  110. $result = mysqli_query($this->con, $sql);
  111. // After getting result we need to contain those data in a variable
  112. // saving fetching data in allcityData variable and return it to show the data
  113. // in index.php file
  114. // we must declare allcityData as array to contain all data
  115. while ($row = mysqli_fetch_object($result))
  116. $this->allTrashedcityData[] = $row;
  117. return $this->allTrashedcityData;
  118. }
  119. function recover ()
  120. {
  121. $sql = "update city set deleted_at = null where id = ". $this->id;
  122. $result = mysqli_query($this->con, $sql);
  123. if($result) {
  124. Message::examineMessage("
  125. <div class=\"alert alert-success\">
  126. <strong>Data has been recovered successfully!</strong>
  127. </div>");
  128. Utility::reDirectAPageIntoAnotherPage("index.php");
  129. } else Message::examineMessage("error");
  130. }
  131. function recoverSelected($ids = array())
  132. {
  133. if(is_array($ids) && count($ids) > 0) {
  134. $IDs = implode(",", $ids);
  135. $sql = "update city set deleted_at = NULL WHERE id IN (". $IDs .")";
  136. $result = mysqli_query($this->con, $sql);
  137. if($result) {
  138. Message::examineMessage("
  139. <div class=\"alert alert-success\">
  140. <strong>Selected Data has been recovered successfully!</strong>
  141. </div>");
  142. Utility::reDirectAPageIntoAnotherPage("index.php");
  143. } else Message::examineMessage("error");
  144. }
  145. }
  146. function deleteSelected($IDs = array())
  147. {
  148. if(is_array($IDs) && count($IDs) > 0) {
  149. $ids = implode(",", $IDs);
  150. $sql = 'delete from `city` where id IN ('.$ids.')';
  151. $result = mysqli_query($this->con, $sql);
  152. if($result) {
  153. Message::examineMessage("
  154. <div class=\"alert alert-success\">
  155. <strong>Selected Data has been Deleted successfully!</strong>
  156. </div>");
  157. Utility::reDirectAPageIntoAnotherPage("index.php");
  158. } else Message::examineMessage("error");
  159. }
  160. }
  161. public function count(){
  162. $query="SELECT COUNT(*) AS totalItem FROM `city` WHERE deleted_at IS NULL";
  163. $result=mysqli_query($this->con,$query);
  164. $row= mysqli_fetch_assoc($result);
  165. return $row['totalItem'];
  166. }
  167. public function paginator($pageStartFrom=0,$Limit=5){
  168. $_allcity = array();
  169. $query="SELECT * FROM `city` WHERE deleted_at is null LIMIT ".$pageStartFrom.",".$Limit;
  170. $result = mysqli_query($this->con, $query);
  171. while ($row = mysqli_fetch_object($result)) {
  172. $_allcity[] = $row;
  173. }
  174. return $_allcity;
  175. }
  176. }