/src/BITM/SEIP145741/Hobbies/Hobbies.php

https://gitlab.com/Shweta_riya/practice · PHP · 239 lines · 150 code · 81 blank · 8 comment · 12 complexity · 23aab415e4312c52846d190d2fc66295 MD5 · raw file

  1. <?php
  2. namespace App\Hobbies;
  3. use App\Message\Message;
  4. use App\Model\database as db;
  5. use App\Utility\Utility;
  6. use PDO;
  7. //require_once("../../../../vendor/autoload.php");
  8. class Hobbies extends db{
  9. public $id;
  10. public $name;
  11. public $hobby;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function setData($data = Null)
  17. {
  18. if (array_key_exists('id', $data)) {
  19. $this->id = $data['id'];
  20. }
  21. if (array_key_exists('name', $data)) {
  22. $this->name= $data['name'];
  23. }
  24. if (array_key_exists('hobby', $data)) {
  25. $this->hobby =implode(",", $data['hobby']);
  26. }
  27. }
  28. public function store(){
  29. $arrData=array($this->name,$this->hobby);
  30. $sql= "Insert INTO hobbies(name,hobby) VALUES (?,?)";
  31. $STH= $this->DBH->prepare($sql);
  32. $result= $STH->execute($arrData);
  33. if($result)
  34. Message::setMessage("Sucess!data has been inserted sucessfully");
  35. else
  36. Message::setMessage("Failure!data has not been inserted sucessfully");
  37. Utility::redirect('create.php');
  38. }// end of store method
  39. public function index($fetchMode='ASSOC'){
  40. $sql = "SELECT * from hobbies where is_deleted = 0 ";
  41. $STH = $this->DBH->query($sql);
  42. $fetchMode = strtoupper($fetchMode);
  43. if(substr_count($fetchMode,'OBJ') > 0)
  44. $STH->setFetchMode(PDO::FETCH_OBJ);
  45. else
  46. $STH->setFetchMode(PDO::FETCH_ASSOC);
  47. $arrAllData = $STH->fetchAll();
  48. return $arrAllData;
  49. }// end of index();
  50. public function view($fetchMode='ASSOC'){
  51. $sql='SELECT * from hobbies WHERE id='.$this->id;
  52. $STH = $this->DBH->query($sql);
  53. //echo $sql;
  54. $fetchMode = strtoupper($fetchMode);
  55. if(substr_count($fetchMode,'OBJ') > 0)
  56. $STH->setFetchMode(PDO::FETCH_OBJ);
  57. else
  58. $STH->setFetchMode(PDO::FETCH_ASSOC);
  59. $arrOneData = $STH->fetch();
  60. return $arrOneData;
  61. }// end of view();
  62. public function update(){
  63. $arrData=array($this->name,$this->hobby);
  64. //UPDATE `atomic_project_35`.`book_title` SET `book_title` = 'nan' WHERE `book_title`.`id` = 4;
  65. $sql="update hobbies SET name=? ,hobby=? WHERE id=".$this->id;
  66. $STH =$this->DBH->prepare($sql);
  67. $STH->execute($arrData);
  68. Utility::redirect('index.php');
  69. }//end of update();
  70. public function delete(){
  71. $sql="Delete from hobbies where id=".$this->id;
  72. $STH=$this->DBH->prepare($sql);
  73. $STH->execute();
  74. Utility::redirect('index.php');
  75. }//end of delete();
  76. public function trash(){
  77. $sql = "Update hobbies SET is_deleted=NOW()where id=".$this->id;
  78. $STH = $this->DBH->prepare($sql);
  79. $STH->execute();
  80. Utility::redirect('index.php');
  81. }// end of trash()
  82. public function indexPaginator($page=1,$itemsPerPage=3){
  83. $start = (($page-1) * $itemsPerPage);
  84. $sql = "SELECT * from hobbies WHERE is_deleted = '0' LIMIT $start,$itemsPerPage";
  85. $STH = $this->DBH->query($sql);
  86. $STH->setFetchMode(PDO::FETCH_OBJ);
  87. $arrSomeData = $STH->fetchAll();
  88. return $arrSomeData;
  89. }// end of index paginator
  90. public function trashedPaginator($page=0,$itemsPerPage=3){
  91. $start = (($page-1) * $itemsPerPage);
  92. $sql = "SELECT * from book_title WHERE is_deleted <> 'No' LIMIT $start,$itemsPerPage";
  93. $STH = $this->DBH->query($sql);
  94. $STH->setFetchMode(PDO::FETCH_OBJ);
  95. $arrSomeData = $STH->fetchAll();
  96. return $arrSomeData;
  97. }// end of trashedPaginator();
  98. public function search($requestArray){
  99. $sql = "";
  100. if( isset($requestArray['byHobby']) && isset($requestArray['byName']) ) $sql = "SELECT * FROM `hobbies` WHERE `is_deleted` ='0' AND (`hobbies` LIKE '%".$requestArray['search']."%' OR `name` LIKE '%".$requestArray['search']."%')";
  101. if(isset($requestArray['byHobby']) && !isset($requestArray['byName']) ) $sql = "SELECT * FROM `hobbies` WHERE `is_deleted` ='0' AND `hobbies` LIKE '%".$requestArray['search']."%'";
  102. if(!isset($requestArray['byHobby']) && isset($requestArray['byName']) ) $sql = "SELECT * FROM `hobbies` WHERE `is_deleted` ='0' AND `name` LIKE '%".$requestArray['search']."%'";
  103. $STH = $this->DBH->query($sql);
  104. $STH->setFetchMode(PDO::FETCH_OBJ);
  105. $allData = $STH->fetchAll();
  106. return $allData;
  107. }// end of search()
  108. public function getAllKeywords()
  109. {
  110. $_allKeywords = array();
  111. $WordsArr = array();
  112. $sql = "SELECT * FROM `hobbies` WHERE `is_deleted` ='0'";
  113. $STH = $this->DBH->query($sql);
  114. $STH->setFetchMode(PDO::FETCH_OBJ);
  115. // for each search field block start
  116. $allData= $STH->fetchAll();
  117. foreach ($allData as $oneData) {
  118. $_allKeywords[] = trim($oneData->hobby);
  119. }
  120. $STH = $this->DBH->query($sql);
  121. $STH->setFetchMode(PDO::FETCH_OBJ);
  122. $allData= $STH->fetchAll();
  123. foreach ($allData as $oneData) {
  124. $eachString= strip_tags($oneData->hobby);
  125. $eachString=trim( $eachString);
  126. $eachString= preg_replace( "/\r|\n/", " ", $eachString);
  127. $eachString= str_replace("&nbsp;","", $eachString);
  128. $WordsArr = explode(" ", $eachString);
  129. foreach ($WordsArr as $eachWord){
  130. $_allKeywords[] = trim($eachWord);
  131. }
  132. }
  133. // for each search field block end
  134. // for each search field block start
  135. $STH = $this->DBH->query($sql);
  136. $STH->setFetchMode(PDO::FETCH_OBJ);
  137. $allData= $STH->fetchAll();
  138. foreach ($allData as $oneData) {
  139. $_allKeywords[] = trim($oneData->name);
  140. }
  141. $STH = $this->DBH->query($sql);
  142. $STH->setFetchMode(PDO::FETCH_OBJ);
  143. $allData= $STH->fetchAll();
  144. foreach ($allData as $oneData) {
  145. $eachString= strip_tags($oneData->name);
  146. $eachString=trim( $eachString);
  147. $eachString= preg_replace( "/\r|\n/", " ", $eachString);
  148. $eachString= str_replace("&nbsp;","", $eachString);
  149. $WordsArr = explode(" ", $eachString);
  150. foreach ($WordsArr as $eachWord){
  151. $_allKeywords[] = trim($eachWord);
  152. }
  153. }
  154. // for each search field block end
  155. return array_unique($_allKeywords);
  156. }// get all keywords
  157. }
  158. //$objHobbies=new Hobbies();