Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.61 KB | None | 0 0
  1.  
  2. // ikhwan 2
  3. $app->post('/submit_weekly_report', function(Request $request, Response $response) {
  4.     $parsedparam = $request->getParsedBody();
  5.     $detail = json_decode($parsedparam['description'], true);
  6.     $id_siswa = sanitizeInput($detail['id_siswa']);
  7.     $id_guru  = sanitizeInput($detail['id_guru']);
  8.     $nama_guru = sanitizeInput($detail['nama_guru']);
  9.     $jabatan = sanitizeInput($detail['jabatan']);
  10.     $id_kelas = sanitizeInput($detail['id_kelas']);
  11.     $id_tahun = sanitizeInput($detail['id_tahun']);
  12.     $bulan = sanitizeInput($detail['bulan']);
  13.     $draft = sanitizeInput($detail['draft']); //1 atau 0
  14.     $nama_bulan = date('F', mktime(0, 0, 0, $bulan, 10));
  15.     // $nama_bulan = 'agustus';
  16.     $judul = sanitizeInput($detail['judul']);
  17.     $isi = sanitizeInput($detail['isi']);
  18.  
  19.     $directory = $this->get('settings')['upload_directory_weekly_report'];
  20.     $uploadedFiles = $request->getUploadedFiles();
  21.     $filenames = array(); // gambars
  22.     $filepdfs = array(); // pdfs
  23.     $responseRes = array();
  24.     $tanggal = date('Y-m-d h:i', time());
  25.     $lastID = null;
  26.  
  27.     // insert new weekly_report
  28.     $queryInput = "INSERT INTO raport_weekly(id_siswa, id_guru, nama_guru, jabatan, id_kelas, id_tahun, bulan, nama_bulan, flag, draft, date_submit_by_guru, submit, judul, isi) VALUES (:id_siswa, :id_guru, :nama_guru, :jabatan, :id_kelas, :id_tahun, :bulan, :nama_bulan, '0', :draft, now(), '0', :judul, :isi)";
  29.     $stmt = $this->db->prepare($queryInput);
  30.     $stmt->bindParam(':id_siswa', $id_siswa, PDO::PARAM_STR);
  31.     $stmt->bindParam(':id_guru', $id_guru, PDO::PARAM_STR);
  32.     $stmt->bindParam(':nama_guru', $nama_guru, PDO::PARAM_STR);
  33.     $stmt->bindParam(':jabatan', $jabatan, PDO::PARAM_STR);
  34.     $stmt->bindParam(':id_kelas', $id_kelas, PDO::PARAM_STR);
  35.     $stmt->bindParam(':id_tahun', $id_tahun, PDO::PARAM_STR);
  36.     $stmt->bindParam(':bulan', $bulan, PDO::PARAM_STR);
  37.     $stmt->bindParam(':nama_bulan', $nama_bulan, PDO::PARAM_STR);
  38.     // $stmt->bindParam(':flag', 0, PDO::PARAM_STR);
  39.     $stmt->bindParam(':draft', $draft, PDO::PARAM_STR);
  40.     // $stmt->bindParam(':submit', 0, PDO::PARAM_STR);
  41.     $stmt->bindParam(':judul', $judul, PDO::PARAM_STR);
  42.     $stmt->bindParam(':isi', $isi, PDO::PARAM_STR);
  43.     // $stmt->execute();
  44.     // $lastID = $db->insert_id;
  45.    
  46.     if($stmt->execute()){
  47.         $lastID = $this->db->lastInsertId();
  48.     } else {
  49.         $responseRes = array(
  50.             'status' => 'failed',
  51.             'message' => 'Failed adding weekly report!'
  52.         );
  53.         return $response->withJson($responseRes, 200);
  54.     }
  55.  
  56.     if($lastID != null){
  57.         // input gambars
  58.         foreach ($uploadedFiles['gambars'] as $uploadedFile) {
  59.             if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
  60.                 $filename = moveUploadedFile($directory, $uploadedFile);
  61.                 // $filenames[] = $filename;
  62.                 if($filename){
  63.                     $query = "INSERT INTO raport_weekly_file(id_weekly_report, tipe, source, thumbnail) VALUES (:id, 'Gambar', :source, :thumbnail)";
  64.                     $stmt = $this->db->prepare($query);
  65.                     $stmt->bindParam(':id', $lastID, PDO::PARAM_STR);
  66.                     $stmt->bindParam(':source', $filename, PDO::PARAM_STR);
  67.                     $stmt->bindParam(':thumbnail', $filename, PDO::PARAM_STR);
  68.                     $stmt->execute();
  69.                 }
  70.  
  71.             } else {
  72.                 $responseRes = array(
  73.                     'status' => 'failed',
  74.                     'message' => 'Error while uploading image!'
  75.                 );
  76.             }
  77.         }
  78.  
  79.     // input pdf
  80.         foreach ($uploadedFiles['pdfs'] as $uploadedFile) {
  81.             if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
  82.                 $filename = moveUploadedFile($directory, $uploadedFile);
  83.                 // $filepdfs[] = $filename;
  84.                 if($filename){
  85.                     $query = "INSERT INTO raport_weekly_file(id_weekly_report, tipe, source, thumbnail) VALUES (:id, 'Pdf', :source, :thumbnail)";
  86.                     $stmt = $this->db->prepare($query);
  87.                     $stmt->bindParam(':id', $lastID, PDO::PARAM_STR);
  88.                     $stmt->bindParam(':source', $filename, PDO::PARAM_STR);
  89.                     $stmt->bindParam(':thumbnail', $filename, PDO::PARAM_STR);
  90.                     $stmt->execute();
  91.                 }
  92.             } else {
  93.                 $responseRes = array(
  94.                     'status' => 'failed',
  95.                     'message' => 'Error while uploading pdf!'
  96.                 );
  97.             }
  98.         }
  99.  
  100.  
  101.         // if(count($filenames) > 0 ){
  102.         //  foreach ($filenames as $f) {
  103.         //      $query = "INSERT INTO raport_weekly_file(id_weekly_report, tipe, source, thumbnail) VALUES (:id, :tipe, :source, :thumbnail)";
  104.         //      $stmt = $this->db->prepare($query);
  105.         //      $stmt->bindParam(':id', $lastID, PDO::PARAM_STR);
  106.         //      $stmt->bindParam(':tipe', 'Gambar', PDO::PARAM_STR);
  107.         //      $stmt->bindParam(':source', $f, PDO::PARAM_STR);
  108.         //      $stmt->bindParam(':thumbnail', $f, PDO::PARAM_STR);
  109.         //      $stmt->execute();
  110.         //  }
  111.         // }
  112.  
  113.         // if(count($filepdfs) > 0){
  114.         //  foreach ($filepdfs as $f) {
  115.         //      $query = "INSERT INTO raport_weekly_file(id_weekly_report, tipe, source) VALUES (:id, :tipe, :source)";
  116.         //      $stmt = $this->db->prepare($query);
  117.         //      $stmt->bindParam(':id', $lastID, PDO::PARAM_STR);
  118.         //      $stmt->bindParam(':tipe', 'Pdf', PDO::PARAM_STR);
  119.         //      $stmt->bindParam(':source', $f, PDO::PARAM_STR);
  120.         //      $stmt->execute();
  121.         //  }
  122.         // }
  123.  
  124.         $responseRes = array(
  125.             'status' => 'Success',
  126.             'message' => 'Success creating new weekly report!'
  127.         );
  128.         return $response->withJson($responseRes, 200);
  129.     } else {
  130.         $responseRes = array(
  131.             'status' => 'Error',
  132.             'message' => 'Last id null!'
  133.         );
  134.         return $response->withJson($responseRes, 200);
  135.     }
  136.     $responseRes = array(
  137.         'status' => 'Success',
  138.         'message' => $parsedparam
  139.     );
  140.     return $response->withJson($responseRes, 200);
  141.  
  142. });
  143.  
  144. $app->post('/list_weekly_report_adm', function (Request $request, Response $response) {
  145.     $parsedparam = $request->getParsedBody();
  146.     $id_kelas    = sanitizeInput($parsedparam['id_kelas']);
  147.     $id_tahun    = sanitizeInput($parsedparam['id_tahun']);
  148.     $bulan       = sanitizeInput($parsedparam['bulan']);
  149.     $query  = "SELECT a.id, a.id_siswa, a.nama_guru, a.jabatan, a.id_kelas, a.id_tahun, a.bulan, a.nama_bulan, a.date_submit_by_guru, a.judul, b.source FROM raport_weekly a INNER JOIN raport_weekly_file b ON a.id = b.id_weekly_report WHERE a.id_kelas = $id_kelas AND a.id_tahun = $id_tahun AND a.bulan = $bulan  GROUP BY a.id ORDER BY a.id desc";
  150.     // $weekly = ambilData($this->db, $query);
  151.     return $response->withJson(ambilData($this->db, $query, true), 200);
  152. });
  153.  
  154. $app->post('/detail_weekly_report_adm', function (Request $request, Response $response) {
  155.     $parsedparam = $request->getParsedBody();
  156.     $id          = sanitizeInput($parsedparam['id']);
  157.  
  158.     $query  = "SELECT * FROM raport_weekly WHERE id = $id";
  159.     $weeklyReport = ambilDataSingle($this->db, $query);
  160.  
  161.     // source
  162.     $qSource = "SELECT * FROM raport_weekly_file WHERE id_weekly_report = $id";
  163.     $weeklyReport->assets = ambilDataSingle($this->db, $qSource, true);
  164.    
  165.     return $response->withJson(["status" => "success", "data" => $weeklyReport], 200);
  166.  
  167. });
  168.  
  169. $app->post('/update_weekly_report', function (Request $request, Response $response) {
  170.     $parsedparam = $request->getParsedBody();
  171.     $id = sanitizeInput($parsedparam['id']); // id weekly peroprt
  172.     $id_siswa = sanitizeInput($parsedparam['id_siswa']);
  173.     $id_guru  = sanitizeInput($parsedparam['id_guru']);
  174.     $nama_guru = sanitizeInput($parsedparam['nama_guru']);
  175.     $jabatan = sanitizeInput($parsedparam['jabatan']);
  176.     $id_kelas = sanitizeInput($parsedparam['id_kelas']);
  177.     $id_tahun = sanitizeInput($parsedparam['id_tahun']);
  178.     $bulan = sanitizeInput($parsedparam['bulan']);
  179.     $draft = sanitizeInput($parsedparam['draft']); //1 atau 0
  180.     $nama_bulan = date('F', mktime(0, 0, 0, $bulan, 10));
  181.     $judul = sanitizeInput($parsedparam['judul']);
  182.     $isi = sanitizeInput($parsedparam['isi']);
  183.     $responseRes = array();
  184.     $tanggal = date('Y-m-d h:i', time());
  185.  
  186.     // insert new weekly_report
  187.     $queryInput = "UPDATE raport_weekly SET id_siswa=:id_siswa, id_guru=:id_guru, nama_guru=:nama_guru, jabatan=:jabatan, id_kelas=:id_kelas, id_tahun=:id_tahun, bulan=:bulan, nama_bulan=:nama_bulan, judul=:judul, isi=:isi, draft=:draft WHERE id = :id";
  188.     $stmt = $this->db->prepare($queryInput);
  189.     $stmt->bindParam(':id_siswa', $id_siswa, PDO::PARAM_STR);
  190.     $stmt->bindParam(':id_guru', $id_guru, PDO::PARAM_STR);
  191.     $stmt->bindParam(':nama_guru', $nama_guru, PDO::PARAM_STR);
  192.     $stmt->bindParam(':jabatan', $jabatan, PDO::PARAM_STR);
  193.     $stmt->bindParam(':id_kelas', $id_kelas, PDO::PARAM_STR);
  194.     $stmt->bindParam(':id_tahun', $id_tahun, PDO::PARAM_STR);
  195.     $stmt->bindParam(':bulan', $bulan, PDO::PARAM_STR);
  196.     $stmt->bindParam(':nama_bulan', $nama_bulan, PDO::PARAM_STR);
  197.     $stmt->bindParam(':judul', $judul, PDO::PARAM_STR);
  198.     $stmt->bindParam(':isi', $isi, PDO::PARAM_STR);
  199.     $stmt->bindParam(':id', $id, PDO::PARAM_STR);
  200.     $stmt->bindParam(':draft', $draft, PDO::PARAM_STR);
  201.     if($stmt->execute()){
  202.         $responseRes = array(
  203.             'status' => 'Success',
  204.             'message' => 'Weekly report successfully updated!'
  205.         );
  206.     } else {
  207.         $responseRes = array(
  208.             'status' => 'failed',
  209.             'message' => 'something went wrong while updating weekly report!'
  210.         );
  211.     }
  212.     return $response->withJson($responseRes, 200);
  213. });
  214.  
  215. $app->post('/delete_img_weekly', function (Request $request, Response $response) {
  216.     $parsedparam = $request->getParsedBody();
  217.     $id = sanitizeInput($parsedparam['id']);
  218.     $directory = $this->get('settings')['upload_directory_weekly_report'];
  219.  
  220.     // select img detail
  221.     $query = "SELECT * FROM raport_weekly_file WHERE id = $id";
  222.     $image = ambilDataSingle($this->db, $query);
  223.     if(unlink($directory.'/'.$image->source)){
  224.         $query = "DELETE FROM raport_weekly_file WHERE id = :id";
  225.         $stmt = $this->db->prepare($query);
  226.         $stmt->bindParam(':id', $id, PDO::PARAM_STR);
  227.         $stmt->execute();
  228.         return $response->withJson(["status" => "success", "message" => "File successfully deleted!"], 200);
  229.     } else {
  230.         return $response->withJson(["status" => "failed", "message" => "An error occured!"], 200);
  231.     }
  232. });
  233.  
  234. $app->post('/upload_img_weekly', function (Request $request, Response $response) {
  235.     $parsedparam = $request->getParsedBody();
  236.     $id = sanitizeInput($parsedparam['id']);
  237.     $directory = $this->get('settings')['upload_directory_weekly_report'];
  238.     $uploadedFiles = $request->getUploadedFiles();
  239.  
  240.     $uploadedFile = $uploadedFiles['file'];
  241.     if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
  242.         $filename = moveUploadedFile($directory, $uploadedFile);
  243.         if($filename){ // kalau berhasil upload hapus file yg ada
  244.             // select img detail
  245.             $query = "SELECT * FROM raport_weekly_file WHERE id = $id";
  246.             $image = ambilDataSingle($this->db, $query);
  247.             if(unlink($directory.'/'.$image->source)){
  248.                 $query = "UPDATE raport_weekly_file SET source = :filename, thumbnail = :filename WHERE id = :id";
  249.                 $stmt = $this->db->prepare($query);
  250.                 $stmt->bindParam(':filename', $filename, PDO::PARAM_STR);
  251.                 $stmt->bindParam(':id', $id, PDO::PARAM_STR);
  252.                 if($stmt->execute()){
  253.                     return $response->withJson(["status" => "success", "message" => "File successfully uploaded!"], 200);
  254.                 } else {
  255.                     return $response->withJson(["status" => "failed", "message" => "An error occured!"], 200);
  256.                 }
  257.             } else {
  258.                 return $response->withJson(["status" => "failed", "message" => "Something wrong with directory!"], 200);
  259.             }
  260.         }
  261.     }
  262. });
  263.  
  264. $app->post('/approve_weekly_report', function (Request $request, Response $response) {
  265.     $parsedparam = $request->getParsedBody();
  266.     $id          = sanitizeInput($parsedparam['id']);
  267.  
  268.     $query = "UPDATE raport_weekly SET submit=1, date_submit_by_admin=NOW() WHERE id=:id";
  269.     $stmt = $this->db->prepare($query);
  270.     $stmt->bindParam(':id', $id, PDO::PARAM_STR);
  271.     if($stmt->execute()){
  272.         return $response->withJson(["status" => "success", "message" => "Weekly report successfully approved!"], 200);
  273.     } else {
  274.         return $response->withJson(["status" => "success", "message" => "Weekly report successfully approved!"], 200);
  275.     }
  276. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement