PageRenderTime 244ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/BITM/SEIP143467/BookTitle/BookTitle.php

https://gitlab.com/Princy008/Princy_143467_B37_Atomic_project
PHP | 395 lines | 300 code | 89 blank | 6 comment | 18 complexity | b55ea5eea08726a5dbe149a52726d856 MD5 | raw file
  1. <?php
  2. namespace App\BITM\SEIP143467\BookTitle;
  3. use App\BITM\SEIP143467\Message\Message;
  4. use App\BITM\SEIP143467\Utility\Utility;
  5. use App\BITM\SEIP143467\Model\Database as DB;
  6. use PDO;
  7. class BookTitle extends DB{
  8. public $id="";
  9. public $book_title="";
  10. public $author_name="";
  11. public function __construct(){
  12. parent::__construct();
  13. }
  14. public function index($fetchMode='ASSOC'){
  15. $fetchMode = strtoupper($fetchMode);
  16. $DBH=$this->connection;
  17. $sth=$DBH->prepare("select * from book_title WHERE is_deleted='0'");
  18. $sth->execute();
  19. if(substr_count($fetchMode,'OBJ') > 0)
  20. $sth->setFetchMode(PDO::FETCH_OBJ);
  21. else
  22. $sth->setFetchMode(PDO::FETCH_ASSOC);
  23. $all_books=$sth->fetchAll();
  24. return $all_books;
  25. }
  26. public function view($id){
  27. $DBH=$this->connection;
  28. $sth=$DBH->prepare("select * from book_title Where id=$id");
  29. $sth->execute();
  30. $sth->setFetchMode(PDO::FETCH_ASSOC);
  31. $selected_book=$sth->fetch();
  32. return $selected_book;
  33. }
  34. public function setData($data=null){
  35. if(array_key_exists('id',$data)){
  36. $this->id=$data['id'];
  37. }
  38. if(array_key_exists('book_title',$data)){
  39. $this->book_title=$data['book_title'];
  40. }
  41. if(array_key_exists('author_name',$data)){
  42. $this->author_name=$data['author_name'];
  43. }
  44. }
  45. public function store(){
  46. $dbh=$this->connection;
  47. $values=array($this->book_title,$this->author_name);
  48. $query="insert into book_title(book_title,author_name) VALUES (?,?)";
  49. $sth=$dbh->prepare($query);
  50. $sth->execute($values);
  51. if($sth)
  52. { Message::message("<div id='msg'><h3 align='center'>[ BookTitle: $this->book_title ] ,
  53. [ AuthorName: $this->author_name ] <br> Data Has Been Inserted Successfully!</h3></div>");
  54. }
  55. else
  56. Message::message("<div id='msg'></div><h3 align='center'> <br> Data Hasn't Been Inserted !</h3></div>");
  57. Utility::redirect('index.php');
  58. }
  59. public function update(){
  60. $dbh=$this->connection;
  61. $values=array($this->book_title,$this->author_name);
  62. //var_dump($values);
  63. $query='UPDATE book_title SET book_title = ? , author_name = ? where id ='.$this->id;
  64. $sth=$dbh->prepare($query);
  65. $sth->execute($values);
  66. if($sth)
  67. { Message::message("<div id='msg'><h3 align='center'>[ BookTitle: $this->book_title ] ,
  68. [ AuthorName: $this->author_name ] <br> Data Has Been Inserted Successfully!</h3></div>");
  69. }
  70. else
  71. Message::message("<div id='msg'></div><h3 align='center'> <br> Data Hasn't Been Inserted !</h3></div>");
  72. Utility::redirect('index.php');
  73. }
  74. public function delete($id){
  75. $DBH=$this->connection;
  76. $sth=$DBH->prepare("delete from book_title Where id=$id");
  77. $sth->execute();
  78. if($sth)
  79. { Message::message("<div id='msg'><h3 align='center'>
  80. <br> Data Has deleted Successfully!</h3></div>");
  81. }
  82. else
  83. Message::message("<div id='msg'></div><h3 align='center'> <br> Data Hasn't Been Inserted !</h3></div>");
  84. Utility::redirect('index.php');
  85. }
  86. public function trash($id){
  87. $dbh=$this->connection;
  88. //var_dump($values);
  89. $query='UPDATE book_title SET is_deleted = "1" where id ='.$id;
  90. $sth=$dbh->prepare($query);
  91. $sth->execute();
  92. if($sth)
  93. { Message::message("<div id='msg'><h3 align='center'>
  94. <br> Data Has Been trashed Successfully!</h3></div>");
  95. }
  96. else
  97. Message::message("<div id='msg'></div><h3 align='center'> <br> Data Hasn't Been trashed !</h3></div>");
  98. Utility::redirect('index.php');
  99. }
  100. public function trashed($fetchMode='ASSOC'){
  101. $fetchMode = strtoupper($fetchMode);
  102. $DBH=$this->connection;
  103. $sth=$DBH->prepare("select * from book_title WHERE is_deleted='1'");
  104. $sth->execute();
  105. if(substr_count($fetchMode,'OBJ') > 0)
  106. $sth->setFetchMode(PDO::FETCH_OBJ);
  107. else
  108. $sth->setFetchMode(PDO::FETCH_ASSOC);
  109. $all_books=$sth->fetchAll();
  110. return $all_books;
  111. }
  112. public function recover($id){
  113. $dbh=$this->connection;
  114. //var_dump($values);
  115. $query='UPDATE book_title SET is_deleted = "0" where id ='.$id;
  116. $sth=$dbh->prepare($query);
  117. $sth->execute();
  118. if($sth)
  119. { Message::message("<div id='msg'><h3 align='center'>
  120. <br> Data Has Been recovered Successfully!</h3></div>");
  121. }
  122. else
  123. Message::message("<div id='msg'></div><h3 align='center'> <br> Data Hasn't Been recovered !</h3></div>");
  124. Utility::redirect('trashed.php');
  125. }
  126. public function deleteMultiple($IDs = Array())
  127. {
  128. if ((is_array($IDs)) && (count($IDs > 0))) {
  129. $dbh=$this->connection;
  130. $n=count($IDs);
  131. // Utility::dd($IDs);
  132. for($i=0;$i<$n;$i++)
  133. {
  134. $query="delete from book_title Where id=$IDs[$i]";
  135. $sth=$dbh->prepare($query);
  136. $sth->execute();
  137. }
  138. if($sth)
  139. { Message::message("<div id='msg'><h3 align='center'>
  140. <br> Selected Data has been deleted Successfully!</h3></div>");
  141. }
  142. else
  143. Message::message("<div id='msg'></div><h3 align='center'> <br> Data Hasn't Been recovered !</h3></div>");
  144. Utility::redirect('index.php');
  145. }
  146. }
  147. public function recoverMultiple($IDs = Array())
  148. {
  149. if ((is_array($IDs)) && (count($IDs > 0))) {
  150. $dbh=$this->connection;
  151. $n=count($IDs);
  152. // Utility::dd($IDs);
  153. for($i=0;$i<$n;$i++)
  154. {
  155. $query='UPDATE book_title SET is_deleted = "0" where id ='.$IDs["$i"];
  156. $sth=$dbh->prepare($query);
  157. $sth->execute();
  158. }
  159. if($sth)
  160. { Message::message("<div id='msg'><h3 align='center'>
  161. <br> Selected Data has been deleted Successfully!</h3></div>");
  162. }
  163. else
  164. Message::message("<div id='msg'></div><h3 align='center'> <br> Data Hasn't Been recovered !</h3></div>");
  165. Utility::redirect('index.php');
  166. }
  167. }
  168. public function trashMultiple($IDs = Array())
  169. {
  170. if ((is_array($IDs)) && (count($IDs > 0))) {
  171. $dbh=$this->connection;
  172. $n=count($IDs);
  173. // Utility::dd($IDs);
  174. for($i=0;$i<$n;$i++)
  175. {
  176. $query='UPDATE book_title SET is_deleted = "1" where id ='.$IDs["$i"];
  177. $sth=$dbh->prepare($query);
  178. $sth->execute();
  179. }
  180. if($sth)
  181. { Message::message("<div id='msg'><h3 align='center'>
  182. <br> Selected Data has been Trashed Successfully!</h3></div>");
  183. }
  184. else
  185. Message::message("<div id='msg'></div><h3 align='center'> <br> Data Hasn't Been trashed !</h3></div>");
  186. Utility::redirect('index.php');
  187. }
  188. }
  189. public function indexPaginator($page=0,$itemsPerPage=3){
  190. $start = (($page-1) * $itemsPerPage);
  191. $DBH=$this->connection;
  192. $sth=$DBH->prepare("select * from book_title WHERE is_deleted='0' LIMIT $start,$itemsPerPage");
  193. $sth->execute();
  194. $sth->setFetchMode(PDO::FETCH_ASSOC);
  195. $arrSomeData = $sth->fetchAll();
  196. return $arrSomeData;
  197. }// end of indexPaginator();
  198. public function trashPaginator($page=0,$itemsPerPage=3){
  199. $start = (($page-1) * $itemsPerPage);
  200. $DBH=$this->connection;
  201. $sth=$DBH->prepare("select * from book_title WHERE is_deleted='1' LIMIT $start,$itemsPerPage");
  202. $sth->execute();
  203. $sth->setFetchMode(PDO::FETCH_ASSOC);
  204. $arrSomeData = $sth->fetchAll();
  205. return $arrSomeData;
  206. }// end of indexPaginator();
  207. public function getAllKeywords()
  208. {
  209. $_allKeywords = array();
  210. $WordsArr = array();
  211. $sql = "SELECT * FROM book_title WHERE is_deleted='0'";
  212. $DBH=$this->connection;
  213. $STH = $DBH->query($sql);
  214. $STH->setFetchMode(PDO::FETCH_ASSOC);
  215. // for each search field block start
  216. $allData= $STH->fetchAll();
  217. foreach ($allData as $oneData) {
  218. $_allKeywords[] = trim($oneData['book_title']);
  219. }
  220. $STH = $DBH->query($sql);
  221. $STH->setFetchMode(PDO::FETCH_ASSOC);
  222. $allData= $STH->fetchAll();
  223. foreach ($allData as $oneData) {
  224. $eachString= strip_tags($oneData['book_title']);
  225. $eachString=trim( $eachString);
  226. $eachString= preg_replace( "/\r|\n/", " ", $eachString);
  227. $eachString= str_replace("&nbsp;","", $eachString);
  228. $WordsArr = explode(" ", $eachString);
  229. foreach ($WordsArr as $eachWord){
  230. $_allKeywords[] = trim($eachWord);
  231. }
  232. }
  233. // for each search field block end
  234. // for each search field block start
  235. $STH = $DBH->query($sql);
  236. $STH->setFetchMode(PDO::FETCH_ASSOC);
  237. $allData= $STH->fetchAll();
  238. foreach ($allData as $oneData) {
  239. $_allKeywords[] = trim($oneData['author_name']);
  240. }
  241. $STH = $DBH->query($sql);
  242. $STH->setFetchMode(PDO::FETCH_ASSOC);
  243. $allData= $STH->fetchAll();
  244. foreach ($allData as $oneData) {
  245. $eachString= strip_tags($oneData['author_name']);
  246. $eachString=trim( $eachString);
  247. $eachString= preg_replace( "/\r|\n/", " ", $eachString);
  248. $eachString= str_replace("&nbsp;","", $eachString);
  249. $WordsArr = explode(" ", $eachString);
  250. foreach ($WordsArr as $eachWord){
  251. $_allKeywords[] = trim($eachWord);
  252. }
  253. }
  254. // for each search field block end
  255. return array_unique($_allKeywords);
  256. }// get all keywords
  257. public function search($requestArray){
  258. $sql = "";
  259. $DBH=$this->connection;
  260. if( isset($requestArray['byTitle']) && isset($requestArray['byAuthor']) ) $sql = "SELECT * FROM `book_title` WHERE `is_deleted` ='0' AND (`book_title` LIKE '%".$requestArray['search']."%' OR `author_name` LIKE '%".$requestArray['search']."%')";
  261. if(isset($requestArray['byTitle']) && !isset($requestArray['byAuthor']) ) $sql = "SELECT * FROM `book_title` WHERE `is_deleted` ='0' AND `book_title` LIKE '%".$requestArray['search']."%'";
  262. if(!isset($requestArray['byTitle']) && isset($requestArray['byAuthor']) ) $sql = "SELECT * FROM `book_title` WHERE `is_deleted` ='0' AND `author_name` LIKE '%".$requestArray['search']."%'";
  263. $STH = $DBH->query($sql);
  264. $STH->setFetchMode(PDO::FETCH_ASSOC);
  265. $allData = $STH->fetchAll();
  266. return $allData;
  267. }// end of search()
  268. }