/src/BITM/SEIP142691/Birthday/Birthday.php

https://gitlab.com/istiyakaminsanto/Atomic_project_IstiyakAmin_142691_B35 · PHP · 283 lines · 166 code · 113 blank · 4 comment · 14 complexity · 248026483eea67b39fea927b77d9c2bc MD5 · raw file

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