Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class BatchModel {
- var $id;
- var $code;
- function GetBatches() {
- include 'connection.php';
- $sql = "SELECT * FROM `batch`";
- $result = mysqli_query($con, $sql);
- return $this->StoreBatches($result);
- }
- function AddBatch(BatchModel $batch) {
- include 'connection.php';
- $sql = "INSERT INTO `batch` VALUES ('','$batch->code')";
- $result = mysqli_query($con, $sql);
- if(!$result)
- $result = "Failed";
- else {
- $path = 'Images/Candidates/SE'.$batch->code;
- if (!file_exists($path)) {
- mkdir($path, 0777, true);
- }
- $result = "Success";
- }
- return $result;
- }
- function EditBatch(BatchModel $batch) {
- include 'connection.php';
- $selectSql = "SELECT * FROM `batch` WHERE id='$batch->id'";
- $selectResult = mysqli_query($con, $selectSql);
- while ($row = mysqli_fetch_assoc($selectResult)) {
- $sql = "UPDATE `batch` SET `code`='$batch->code' WHERE id=$batch->id";
- $result = mysqli_query($con, $sql);
- if(!$result)
- $result = "Failed";
- else {
- $oldPath = 'Images/Candidates/SE'.$row['code'];
- $newPath = 'Images/Candidates/SE'.$batch->code;
- rename($oldPath, $newPath);
- $result = "Success";
- }
- }
- return $result;
- }
- function DeleteBatch($id) {
- $result = $this->DeleteTribe($id);
- if($result == "Success")
- {
- include 'connection.php';
- $selectSql = "SELECT * FROM `batch` WHERE id='$id'";
- $selectResult = mysqli_query($con, $selectSql);
- while ($row = mysqli_fetch_assoc($selectResult)) {
- $sql = "DELETE FROM `batch` WHERE id=$id";
- $result = mysqli_query($con, $sql);
- if(!$result)
- $result = "Failed";
- else {
- $this->deleteDir('Images/Candidates/SE'.$batch->code);
- $result = "Success";
- }
- }
- return $result;
- }
- else
- return $result;
- }
- function DeleteTribe($id) {
- include 'connection.php';
- // $sql = "DELETE tribe FROM tribe INNER JOIN batchtribe ON tribe.id=batchtribe.tribeid WHERE batchtribe.batchid='$id'";
- $sql = "DELETE tribe FROM tribe INNER JOIN batchtribe WHERE tribe.id=batchtribe.tribeid AND batchtribe.batchid='$id'";
- //$sql = "";
- $result = mysqli_query($con, $sql);
- if(!$result)
- $result = "Failed2";
- else
- $result = "Success";
- return $result;
- }
- function StoreBatches($result) {
- $batches = array();
- $i = 0;
- while ($row = mysqli_fetch_assoc($result))
- {
- $id = $row['id'];
- $code = $row['code'];
- $batches[$i] = array(
- 'id' => $id,
- 'code' => $code
- );
- $i++;
- }
- return $batches;
- }
- function deleteDir($dirPath) {
- if (! is_dir($dirPath)) {
- throw new InvalidArgumentException("$dirPath must be a directory");
- }
- if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
- $dirPath .= '/';
- }
- $files = glob($dirPath . '*', GLOB_MARK);
- foreach ($files as $file) {
- if (is_dir($file)) {
- self::deleteDir($file);
- } else {
- unlink($file);
- }
- }
- rmdir($dirPath);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement