/src/Bitm/SEIP137086/Hobby/Hobby.php

https://gitlab.com/raihan920/atomicproject_137086_crud_public · PHP · 216 lines · 192 code · 22 blank · 2 comment · 22 complexity · e8faa93b0bb0ccb6ecdfc8280d75f6c4 MD5 · raw file

  1. <?php
  2. namespace App\Bitm\SEIP137086\Hobby;
  3. use App\Bitm\SEIP137086\Message\Message;
  4. use App\Bitm\SEIP137086\Utility\Utility;
  5. class Hobby{
  6. public $id="";
  7. public $hobbies="";
  8. public $conn;
  9. public $allHobby=array();
  10. public $deleted_at;
  11. public function __construct()
  12. {
  13. $this->conn=mysqli_connect("localhost","root","","atomicproject137086") or die("Database connection failed");
  14. }
  15. public function prepare($data=""){
  16. if(array_key_exists("hobby",$data)){
  17. $this->hobbies=$data['hobby'];
  18. }
  19. if(array_key_exists("id",$data)){
  20. $this->id=$data['id'];
  21. }
  22. }
  23. public function storeSelected(){
  24. /*Utility::dd($this->hobbies);*/
  25. $commaSeparated = implode(",",$this->hobbies);
  26. $query="INSERT INTO `atomicproject137086`.`hobby` (`hobbies`) VALUE ('".$commaSeparated."')";
  27. $result = mysqli_query($this->conn,$query);
  28. if($result){
  29. Message::message(
  30. "<div class='alert alert-success'>
  31. <strong>Success!</strong> Data has been stored successfully.
  32. </div>"
  33. );
  34. Utility::redirect("index.php");
  35. } else {
  36. echo "Error";
  37. }
  38. }
  39. public function index(){
  40. $query = "SELECT * FROM `atomicproject137086`.`hobby` WHERE `deleted_at` IS NULL";
  41. $result = mysqli_query($this->conn,$query);
  42. while($row = mysqli_fetch_object($result)){
  43. $this->allHobby[] = $row;
  44. }
  45. return $this->allHobby;
  46. }
  47. public function view()
  48. {
  49. $query = "SELECT * FROM `atomicproject137086`.`hobby` WHERE `id`=".$this->id;
  50. $result = mysqli_query($this->conn, $query);
  51. $row = mysqli_fetch_assoc($result);
  52. return $row;
  53. }
  54. public function update()
  55. {
  56. $commaSeparated = implode(",",$this->hobbies);
  57. $query = "UPDATE `atomicproject137086`.`hobby` SET `hobbies` = '".$commaSeparated ."' WHERE `hobby`.`id` =".$this->id;
  58. $result = mysqli_query($this->conn, $query);
  59. if ($result) {
  60. Message::message("
  61. <div class='alert alert-info'>
  62. <strong>Success!</strong> Data has been updated successfully.
  63. </div>");
  64. Utility::redirect("index.php");
  65. } else {
  66. echo "Error";
  67. }
  68. }
  69. public function delete()
  70. {
  71. $query = "DELETE FROM `atomicproject137086`.`hobby` WHERE `hobby`.`id` = ".$this->id;
  72. $result = mysqli_query($this->conn, $query);
  73. if ($result) {
  74. Message::message("
  75. <div class='alert alert-info'>
  76. <strong>Deleted!</strong> Data has been deleted successfully.
  77. </div>");
  78. Utility::redirect("index.php");
  79. } else {
  80. Message::message("
  81. <div class='alert alert-info'>
  82. <strong>Not Deleted!</strong> Data has not been deleted successfully.
  83. </div>");
  84. Utility::redirect("index.php");
  85. }
  86. }
  87. public function trash()
  88. {
  89. $this->deleted_at = time();
  90. $query = "UPDATE `atomicproject137086`.`hobby` SET `deleted_at` =".$this->deleted_at." WHERE `hobby`.`id` = ".$this->id;
  91. $result = mysqli_query($this->conn, $query);
  92. if ($result) {
  93. Message::message("
  94. <div class='alert alert-warning'>
  95. <strong>Trashed!</strong> Data has been trashed successfully.
  96. </div>");
  97. Utility::redirect("index.php");
  98. } else {
  99. Message::message("
  100. <div class='alert alert-warning'>
  101. <strong>Not Trashed!</strong> Data has not been trashed successfully.
  102. </div>");
  103. Utility::redirect("index.php");
  104. }
  105. }
  106. public function trashed()
  107. {
  108. $_allHobby = array();
  109. $query = "SELECT * FROM `atomicproject137086`.`hobby` WHERE `deleted_at` IS NOT NULL";
  110. $result = mysqli_query($this->conn, $query);
  111. while ($row = mysqli_fetch_assoc($result)) {
  112. $_allHobby[] = $row;
  113. }
  114. return $_allHobby;
  115. }
  116. public function recover()
  117. {
  118. $query = "UPDATE `atomicproject137086`.`hobby` SET `deleted_at` = NULL WHERE `hobby`.`id` = " . $this->id;
  119. $result = mysqli_query($this->conn, $query);
  120. if ($result) {
  121. Message::message("
  122. <div class='alert alert-success'>
  123. <strong>Recovered!</strong> Data has been recovered successfully.
  124. </div>");
  125. Utility::redirect("index.php");
  126. } else {
  127. Message::message("
  128. <div class='alert alert-warning'>
  129. <strong>Not Recovered!</strong> Data has not been recovered successfully.
  130. </div>");
  131. Utility::redirect("index.php");
  132. }
  133. }
  134. public function recoverSeleted($IDs = Array())
  135. {
  136. if ((is_array($IDs)) && (count($IDs > 0))) {
  137. $ids = implode(",", $IDs);
  138. $query = "UPDATE `atomicproject137086`.`hobby` SET `deleted_at` = NULL WHERE `hobby`.`id` IN(".$ids.")";
  139. $result = mysqli_query($this->conn, $query);
  140. if ($result) {
  141. Message::message("
  142. <div class='alert alert-success'>
  143. <strong>Recovered!</strong> Selected Data has been recovered successfully.
  144. </div>");
  145. Utility::redirect("index.php");
  146. } else {
  147. Message::message("
  148. <div class='alert alert-warning'>
  149. <strong>Not Recovered!</strong> Selected Data has not been recovered successfully.
  150. </div>");
  151. Utility::redirect("index.php");
  152. }
  153. }
  154. }
  155. public function deleteMultiple($IDs = Array())
  156. {
  157. if ((is_array($IDs)) && (count($IDs > 0))) {
  158. $ids = implode(",", $IDs);
  159. /*Utility::dd($ids);*/
  160. $query = "DELETE FROM `atomicproject137086`.`hobby` WHERE `hobby`.`id` IN(".$ids.")";
  161. $result = mysqli_query($this->conn, $query);
  162. if ($result) {
  163. Message::message("
  164. <div class='alert alert-danger'>
  165. <strong>Deleted!</strong> Selected Data has been deleted successfully.
  166. </div>");
  167. Utility::redirect("index.php");
  168. } else {
  169. Message::message("
  170. <div class='alert alert-warning'>
  171. <strong>Not Deleted!</strong> Selected Data has not been deleted successfully.
  172. </div>");
  173. Utility::redirect("index.php");
  174. }
  175. }
  176. }
  177. public function count()
  178. {
  179. $query = "SELECT COUNT(*) As totalItem From `atomicproject137086`.`hobby`";
  180. $result = mysqli_query($this->conn,$query);
  181. $row = mysqli_fetch_assoc($result);
  182. return $row['totalItem'];
  183. }
  184. public function paginator($pageStartFrom=0,$Limit)
  185. {
  186. $_allHobby = array();
  187. $query = "SELECT * FROM `atomicproject137086`.`hobby` WHERE `deleted_at` IS NULL LIMIT ".$pageStartFrom.",".$Limit;
  188. $result = mysqli_query($this->conn,$query);
  189. while ($row = mysqli_fetch_object($result)){
  190. $_allHobby[]=$row;
  191. }
  192. return $_allHobby;
  193. }
  194. }