PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Group two CodePlayer/softwareManagement/src/Soft.php

https://gitlab.com/omarfaruk/Web_App_Develop_CRUD
PHP | 249 lines | 218 code | 10 blank | 21 comment | 27 complexity | ea7e8b53304e5e8d248f8d2e20426d54 MD5 | raw file
  1. <?php
  2. namespace App;
  3. use PDO;
  4. class Soft
  5. {
  6. public $lab = '';
  7. public $title = '';
  8. public $version = '';
  9. public $type = '';
  10. public $id = '';
  11. public $data='';
  12. public function __construct()
  13. {
  14. session_start();
  15. try{
  16. $this->conn = new PDO("mysql:host=localhost;dbname=lab", "root", "");
  17. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  18. }catch (PDOException $e){
  19. echo $e->getMessage();
  20. die();
  21. }
  22. }
  23. public function prepare($data = ''){
  24. if (!empty($data['lab'])) {
  25. $this->lab = $data['lab'];
  26. }else{
  27. $_SESSION['labEmpty'] = '<h6 style="color: red">Lab number Required!</h6>';
  28. }
  29. if (!empty($data['title'])) {
  30. $this->title = $data['title'];
  31. }else{
  32. $_SESSION['titleEmpty'] = '<h6 style="color: red">Software Name Required!</h6>';
  33. }
  34. if (!empty($data['version'])) {
  35. $this->version = $data['version'];
  36. }else{
  37. $_SESSION['verEmpty'] = '<h6 style="color: red">Software Version Required!</h6>';
  38. }
  39. if (!empty($data['type'])) {
  40. $this->type = $data['type'];
  41. }else{
  42. $_SESSION['typeEmpty'] = '<h6 style="color: red">Software type Required!</h6>';
  43. }
  44. if (!empty($data['id'])) {
  45. $this->id = $data['id'];
  46. }
  47. $_SESSION['formData'] = $_POST;
  48. return $this;
  49. }
  50. public function store(){
  51. if (!empty($this->lab) && !empty($this->title) && !empty($this->version) && !empty($this->type)){
  52. try {
  53. $query = "INSERT INTO installed_softwares (id, labinfo_id, software_title, version, software_type, created, updated, deleted)
  54. VALUES (:id, :lid, :title, :ver, :soft_type, :created, :updated, :deleted )";
  55. $stmt = $this->conn->prepare($query);
  56. $stmt->execute(array(
  57. ":id"=> '',
  58. ":lid"=> $this->lab,
  59. ":title"=>$this->title,
  60. ":ver"=>$this->version,
  61. ":soft_type" => $this->type,
  62. ":created" => date('y-m-d h-m-s'),
  63. ":updated" => '',
  64. ":deleted" => ''
  65. ));
  66. $_SESSION['msg'] = ' <h5 style="color:green;">Software Successfully Added!</h5> ';
  67. unset($_SESSION['formData']);
  68. header('location: add.php');
  69. } catch (\PDOException $e) {
  70. echo 'Error:' . $e->getMessage();
  71. }
  72. }else{
  73. header('location: add.php');
  74. }
  75. }
  76. //show all data
  77. public function show(){
  78. $query = "SELECT * FROM `installed_softwares` WHERE is_delete = 0";
  79. $stmt = $this->conn->prepare($query);
  80. $stmt->execute();
  81. while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)){
  82. $this->data[] = $rows; //one array/row should be in a multidimensional array
  83. }
  84. return $this->data;
  85. }
  86. //single view
  87. public function view(){
  88. $query = "SELECT * FROM `installed_softwares` WHERE id=". "'" . $this->id. "'"; //i have problem here
  89. $stmt = $this->conn->prepare($query);
  90. $stmt->execute();
  91. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  92. return $row;
  93. }
  94. //update single data
  95. public function update(){
  96. try {
  97. $query = "UPDATE installed_softwares SET labinfo_id = :lid, software_title = :title, version = :ver, software_type = :soft_type,
  98. created = :created, updated = :updated, deleted = :deleted WHERE id =" . $this->id;
  99. $stmt = $this->conn->prepare($query);
  100. $stmt->execute(array(
  101. ':lid' => $this->lab,
  102. ':title' => $this->title,
  103. ':ver' => $this->version,
  104. ':soft_type' => $this->type,
  105. ':created' => '',
  106. ':updated' => date('y-m-d h-m-s'),
  107. ':deleted' => ''
  108. ));
  109. // $_SESSION['msg'] = 'Data Successfully updated';
  110. $_SESSION['msg'] = ' <h5 style="color:green;">Software Successfully Updated!</h5> ';
  111. header('location: show.php');
  112. } catch(PDOException $e) {
  113. echo 'Error: ' . $e->getMessage();
  114. }
  115. }
  116. //delete Single data
  117. public function delete(){
  118. try {
  119. $stmt = $this->conn->prepare('DELETE FROM installed_softwares WHERE id = :id');
  120. $stmt->bindParam(':id', $this->id); // this time, we'll use the bindParam method
  121. $stmt->execute();
  122. $_SESSION['msg'] = 'Data Successfully deleted';
  123. header('location: show.php');
  124. } catch(PDOException $e) {
  125. echo 'Error: ' . $e->getMessage();
  126. }
  127. }
  128. //Soft Delete
  129. public function trash(){
  130. try {
  131. $query = "SELECT * FROM installed_softwares WHERE id=". $this->id;
  132. $STH = $this->conn->prepare($query);
  133. $STH->execute();
  134. $user = $STH->fetch(PDO::FETCH_ASSOC);
  135. // print_r($user);
  136. // die();
  137. if ($user['is_delete'] == 0){
  138. $qr = "UPDATE installed_softwares SET is_delete = 1 WHERE id=" . $this->id;
  139. $STH = $this->conn->prepare($qr);
  140. if ($STH->execute()){
  141. $_SESSION['msg'] = ' <h5 style="color:red;">Software Successfully Deleted!</h5> ';
  142. // $_SESSION['msg'] = 'Software successfully deleted';
  143. header('location:show.php');
  144. }
  145. }
  146. } catch(PDOException $e) {
  147. echo 'Error: ' . $e->getMessage();
  148. }
  149. }
  150. //Deleted items
  151. public function trashlist(){
  152. $query = "SELECT * FROM `installed_softwares` WHERE is_delete = 1";
  153. $stmt = $this->conn->prepare($query);
  154. $stmt->execute();
  155. while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)){
  156. $this->data[] = $rows; //one array/row should be in a multidimensional array
  157. }
  158. return $this->data;
  159. }
  160. //Lab search
  161. public function searchlab($search = "")
  162. {
  163. if (!empty($search['lab_search'])) {
  164. $keyword = $search['lab_search'];
  165. $keyword = preg_replace("#[^0-9a-z]#i", "", $keyword);
  166. $sql = "SELECT * FROM `installed_softwares` WHERE `labinfo_id` LIKE :keyword;";
  167. $stmt = $this->conn->prepare($sql);
  168. $stmt->bindValue(':keyword', '%' . $keyword . '%');
  169. $stmt->execute();
  170. if ($stmt->rowCount() > 0) {
  171. while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
  172. // echo"<pre>".print_r($result,true)."</pre>";
  173. $this->data[] = $result;
  174. // header('location:../index.php');
  175. }
  176. } else {
  177. $_SESSION['msg'] = 'There was nothing to show. Please search with right keyword.';
  178. }
  179. } else {
  180. $_SESSION['msg'] = 'Please insert something.';
  181. }
  182. return $this->data;
  183. }
  184. //Software search
  185. public function searchSoft($search = "")
  186. {
  187. if (!empty($search['soft_search'])) {
  188. $keyword = $search['soft_search'];
  189. $keyword = preg_replace("#[^0-9a-z]#i", "", $keyword);
  190. $sql = "SELECT * FROM `installed_softwares` WHERE `software_title` LIKE :keyword;";
  191. $stmt = $this->conn->prepare($sql);
  192. $stmt->bindValue(':keyword', '%' . $keyword . '%');
  193. $stmt->execute();
  194. if ($stmt->rowCount() > 0) {
  195. while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
  196. // echo"<pre>".print_r($result,true)."</pre>";
  197. $this->data[] = $result;
  198. }
  199. } else {
  200. $_SESSION['msg'] = 'There was nothing to show. Please search with right keyword.';
  201. }
  202. } /*else {
  203. $_SESSION['msg'] = 'Please insert something.';
  204. }*/
  205. return $this->data;
  206. }
  207. // Restore
  208. public function restore(){
  209. try {
  210. $query = "SELECT * FROM installed_softwares WHERE id=". $this->id;
  211. $STH = $this->conn->prepare($query);
  212. $STH->execute();
  213. $user = $STH->fetch(PDO::FETCH_ASSOC);
  214. // print_r($user);
  215. // die();
  216. if ($user['is_delete'] == 1){
  217. $qr = "UPDATE installed_softwares SET is_delete = 0 WHERE id=" . $this->id;
  218. $STH = $this->conn->prepare($qr);
  219. if ($STH->execute()){
  220. $_SESSION['msg'] = 'Software successfully deleted';
  221. header('location:trash_list.php');
  222. }
  223. }
  224. } catch(PDOException $e) {
  225. echo 'Error: ' . $e->getMessage();
  226. }
  227. }
  228. //show all data from Labinfo Table
  229. public function index()
  230. {
  231. $query = "SELECT * FROM `labinfo`";
  232. $stmt = $this->conn->prepare($query);
  233. $stmt->execute();
  234. while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
  235. $this->data[] = $rows; //one array/row should be in a multidimensional array
  236. }
  237. return $this->data;
  238. }
  239. }