PageRenderTime 30ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/BITM/SEIP143229/BookTitle/BookTitle.php

https://gitlab.com/ajit57/ajitdas_143229_b35_session30_search_3rdparty
PHP | 183 lines | 149 code | 29 blank | 5 comment | 12 complexity | 2d2695e4b7d1f09334df5962476626d1 MD5 | raw file
  1. <?php
  2. namespace App\BookTitle;
  3. use App\Message\Message;
  4. use App\Model\Database as DB;
  5. use App\Utility\Utility;
  6. use PDO;
  7. class BookTitle extends DB
  8. {
  9. public $id = "";
  10. public $book_title = "";
  11. public $author_name = "";
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function setData($postVaribaleData = NULL)
  17. {
  18. if (array_key_exists('id', $postVaribaleData)) {
  19. $this->id = $postVaribaleData['id'];
  20. }
  21. if (array_key_exists('book_title', $postVaribaleData)) {
  22. $this->book_title = $postVaribaleData['book_title'];
  23. }
  24. if (array_key_exists('author_name', $postVaribaleData)) {
  25. $this->author_name = $postVaribaleData['author_name'];
  26. }
  27. }//end of setData
  28. public function store()
  29. {
  30. $arrData = array($this->book_title, $this->author_name);
  31. $fsql = "Insert INTO book_title(booktitle, author) VALUES (?,?)";
  32. $result = $this->DBH->prepare($fsql);
  33. $result->execute($arrData);
  34. if($result)
  35. Message::setMessage("Success ! Data has been inserted Successfully :)");
  36. else
  37. Message::setMessage("Failed ! Data has not been inserted Successfully ):");
  38. Utility::redirect('index.php');
  39. }//end of store
  40. public function index($fetchMode='ASSOC'){
  41. $STH = $this->DBH->query("SELECT * from book_title WHERE is_deleted='No'");
  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. $STH = $this->DBH->query('SELECT * from book_title where id='.$this->id);
  52. $fetchMode = strtoupper($fetchMode);
  53. if(substr_count($fetchMode,'OBJ') > 0)
  54. $STH->setFetchMode(PDO::FETCH_OBJ);
  55. else
  56. $STH->setFetchMode(PDO::FETCH_ASSOC);
  57. $arrOneData = $STH->fetch();
  58. return $arrOneData;
  59. }//end of view();
  60. public function update(){
  61. $arrData = array($this->book_title, $this->author_name);
  62. $sql = "UPDATE book_title set booktitle=?, author=? WHERE id=".$this->id;
  63. $STH = $this->DBH->prepare($sql);
  64. $STH->execute($arrData);
  65. Utility::redirect('index.php');
  66. }
  67. public function delete(){
  68. $sql='DELETE FROM book_title WHERE id = '.$this->id;
  69. $STH = $this->DBH->prepare($sql);
  70. $result = $STH->execute();
  71. Utility::redirect('index.php');
  72. }
  73. public function trash($fetchMode ='ASSOC'){
  74. $query = "UPDATE book_title SET is_deleted=NOW() Where id=".$this->id;
  75. $stmt = $this->DBH->prepare($query);
  76. $stmt->execute();
  77. Utility::redirect('index.php');
  78. }
  79. public function indexPaginator($page=0,$itemsPerPage=3){
  80. $start = (($page-1) * $itemsPerPage);
  81. $sql = "SELECT * from book_title WHERE is_deleted = 'No' LIMIT $start,$itemsPerPage";
  82. $STH = $this->DBH->query($sql);
  83. $STH->setFetchMode(PDO::FETCH_OBJ);
  84. $arrSomeData = $STH->fetchAll();
  85. return $arrSomeData;
  86. }
  87. public function trashedPaginator($page=0,$itemsPerPage=3){
  88. $start = (($page-1) * $itemsPerPage);
  89. $sql = "SELECT * from book_title WHERE is_deleted <> 'No' LIMIT $start,$itemsPerPage";
  90. $STH = $this->DBH->query($sql);
  91. $STH->setFetchMode(PDO::FETCH_OBJ);
  92. $arrSomeData = $STH->fetchAll();
  93. return $arrSomeData;
  94. }// end of trashedPaginator();
  95. public function search($requestArray){
  96. $sql = "";
  97. if( isset($requestArray['byTitle']) && isset($requestArray['byAuthor']) ) $sql = "SELECT * FROM `book_title` WHERE `is_deleted` ='No' AND (`booktitle` LIKE '%".$requestArray['search']."%' OR `author` LIKE '%".$requestArray['search']."%')";
  98. if(isset($requestArray['byTitle']) && !isset($requestArray['byAuthor']) ) $sql = "SELECT * FROM `book_title` WHERE `is_deleted` ='No' AND `booktitle` LIKE '%".$requestArray['search']."%'";
  99. if(!isset($requestArray['byTitle']) && isset($requestArray['byAuthor']) ) $sql = "SELECT * FROM `book_title` WHERE `is_deleted` ='No' AND `author` LIKE '%".$requestArray['search']."%'";
  100. $STH = $this->DBH->query($sql);
  101. $STH->setFetchMode(PDO::FETCH_OBJ);
  102. $allData = $STH->fetchAll();
  103. return $allData;
  104. }// end of search()
  105. public function getAllKeywords()
  106. {
  107. $_allKeywords = array();
  108. // $WordsArr = array();
  109. $sql = "SELECT * FROM `book_title` WHERE `is_deleted` ='No'";
  110. $STH = $this->DBH->query($sql);
  111. $STH->setFetchMode(PDO::FETCH_OBJ);
  112. // for each search field block start
  113. $allData= $STH->fetchAll();
  114. foreach ($allData as $oneData) {
  115. $_allKeywords[] = trim($oneData->booktitle);
  116. }
  117. $STH = $this->DBH->query($sql);
  118. $STH->setFetchMode(PDO::FETCH_OBJ);
  119. $allData= $STH->fetchAll();
  120. foreach ($allData as $oneData) {
  121. $eachString= strip_tags($oneData->booktitle);
  122. $eachString=trim( $eachString);
  123. $eachString= preg_replace( "/\r|\n/", " ", $eachString);
  124. $eachString= str_replace("&nbsp;","", $eachString);
  125. $WordsArr = explode(" ", $eachString);
  126. foreach ($WordsArr as $eachWord){
  127. $_allKeywords[] = trim($eachWord);
  128. }
  129. }
  130. // for each search field block end
  131. // for each search field block start
  132. $STH = $this->DBH->query($sql);
  133. $STH->setFetchMode(PDO::FETCH_OBJ);
  134. $allData= $STH->fetchAll();
  135. foreach ($allData as $oneData) {
  136. $_allKeywords[] = trim($oneData->author);
  137. }
  138. $STH = $this->DBH->query($sql);
  139. $STH->setFetchMode(PDO::FETCH_OBJ);
  140. $allData= $STH->fetchAll();
  141. foreach ($allData as $oneData) {
  142. $eachString= strip_tags($oneData->author);
  143. $eachString=trim( $eachString);
  144. $eachString= preg_replace( "/\r|\n/", " ", $eachString);
  145. $eachString= str_replace("&nbsp;","", $eachString);
  146. $WordsArr = explode(" ", $eachString);
  147. foreach ($WordsArr as $eachWord){
  148. $_allKeywords[] = trim($eachWord);
  149. }
  150. }
  151. // for each search field block end
  152. return array_unique($_allKeywords);
  153. }// get all keywords
  154. }