Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. <?php
  2.  
  3. class BatchModel {
  4. var $id;
  5. var $code;
  6.  
  7. function GetBatches() {
  8. include 'connection.php';
  9.  
  10. $sql = "SELECT * FROM `batch`";
  11. $result = mysqli_query($con, $sql);
  12.  
  13. return $this->StoreBatches($result);
  14. }
  15.  
  16. function AddBatch(BatchModel $batch) {
  17. include 'connection.php';
  18.  
  19. $sql = "INSERT INTO `batch` VALUES ('','$batch->code')";
  20.  
  21. $result = mysqli_query($con, $sql);
  22.  
  23. if(!$result)
  24. $result = "Failed";
  25. else {
  26.  
  27. $path = 'Images/Candidates/SE'.$batch->code;
  28.  
  29. if (!file_exists($path)) {
  30. mkdir($path, 0777, true);
  31. }
  32.  
  33. $result = "Success";
  34. }
  35.  
  36. return $result;
  37.  
  38. }
  39.  
  40. function EditBatch(BatchModel $batch) {
  41. include 'connection.php';
  42.  
  43. $selectSql = "SELECT * FROM `batch` WHERE id='$batch->id'";
  44. $selectResult = mysqli_query($con, $selectSql);
  45.  
  46. while ($row = mysqli_fetch_assoc($selectResult)) {
  47.  
  48. $sql = "UPDATE `batch` SET `code`='$batch->code' WHERE id=$batch->id";
  49.  
  50. $result = mysqli_query($con, $sql);
  51.  
  52. if(!$result)
  53. $result = "Failed";
  54. else {
  55. $oldPath = 'Images/Candidates/SE'.$row['code'];
  56. $newPath = 'Images/Candidates/SE'.$batch->code;
  57.  
  58. rename($oldPath, $newPath);
  59.  
  60. $result = "Success";
  61. }
  62. }
  63.  
  64. return $result;
  65. }
  66.  
  67. function DeleteBatch($id) {
  68. $result = $this->DeleteTribe($id);
  69.  
  70. if($result == "Success")
  71. {
  72. include 'connection.php';
  73.  
  74. $selectSql = "SELECT * FROM `batch` WHERE id='$id'";
  75. $selectResult = mysqli_query($con, $selectSql);
  76.  
  77. while ($row = mysqli_fetch_assoc($selectResult)) {
  78.  
  79. $sql = "DELETE FROM `batch` WHERE id=$id";
  80. $result = mysqli_query($con, $sql);
  81.  
  82. if(!$result)
  83. $result = "Failed";
  84. else {
  85.  
  86. $this->deleteDir('Images/Candidates/SE'.$batch->code);
  87.  
  88. $result = "Success";
  89. }
  90. }
  91.  
  92. return $result;
  93. }
  94. else
  95. return $result;
  96.  
  97. }
  98.  
  99. function DeleteTribe($id) {
  100. include 'connection.php';
  101.  
  102. // $sql = "DELETE tribe FROM tribe INNER JOIN batchtribe ON tribe.id=batchtribe.tribeid WHERE batchtribe.batchid='$id'";
  103. $sql = "DELETE tribe FROM tribe INNER JOIN batchtribe WHERE tribe.id=batchtribe.tribeid AND batchtribe.batchid='$id'";
  104. //$sql = "";
  105. $result = mysqli_query($con, $sql);
  106.  
  107. if(!$result)
  108. $result = "Failed2";
  109. else
  110. $result = "Success";
  111.  
  112. return $result;
  113.  
  114. }
  115.  
  116. function StoreBatches($result) {
  117. $batches = array();
  118.  
  119. $i = 0;
  120. while ($row = mysqli_fetch_assoc($result))
  121. {
  122. $id = $row['id'];
  123. $code = $row['code'];
  124.  
  125. $batches[$i] = array(
  126. 'id' => $id,
  127. 'code' => $code
  128. );
  129.  
  130. $i++;
  131. }
  132.  
  133. return $batches;
  134. }
  135.  
  136. function deleteDir($dirPath) {
  137. if (! is_dir($dirPath)) {
  138. throw new InvalidArgumentException("$dirPath must be a directory");
  139. }
  140. if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
  141. $dirPath .= '/';
  142. }
  143. $files = glob($dirPath . '*', GLOB_MARK);
  144. foreach ($files as $file) {
  145. if (is_dir($file)) {
  146. self::deleteDir($file);
  147. } else {
  148. unlink($file);
  149. }
  150. }
  151. rmdir($dirPath);
  152. }
  153. }
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement