Advertisement
martinms

Admin_periode.php

May 15th, 2024
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.08 KB | None | 0 0
  1. <?php
  2.  
  3. class Admin_periode extends Controller
  4. {
  5.  
  6.     private $opd;
  7.     private $periode;
  8.     private $tahun;
  9.     public function __construct()
  10.     {
  11.         $this->opd = $this->model('Opd_model');
  12.         $this->periode = $this->model('Periode_model');
  13.         $this->tahun = $this->model('Tahun_model');
  14.         $this->access('admin');
  15.     }
  16.  
  17.     public function index()
  18.     {
  19.         $data ["title"] ="Manajemen Periode";
  20.         $this->view("admin/periode", $data);
  21.     }
  22.     public function listJadwal()
  23.     {
  24.         $data['list'] = json_decode($this->periode->list(), true)['data'];
  25.         echo json_encode($data['list']);
  26.     }
  27.  
  28.     public function list(...$params)
  29.     {
  30.         switch ($params[0]) {
  31.             case "jadwal":
  32.                 $periode = $this->periode->list();
  33.                 echo json_encode($periode);
  34.                 break;
  35.             case "opd":
  36.                 $getOpd = json_decode($this->opd->getAll(1), true)['data'];
  37.                 $opdAll = [];
  38.                 for ($i = 1; $i <= $getOpd['jumlahPage']; $i++) {
  39.                     $getOpd[$i] = json_decode($this->opd->getAll($i), true)['data'];
  40.                     $opdAll = array_merge($opdAll, $getOpd[$i]['dataOpd']);
  41.                 }
  42.                 $periode = json_decode($this->periode->getSingle($params[1]), true)['data'];
  43.                 $opdPeriode = [];
  44.                 foreach ($periode[0]['opdList'] as $opdPeriod) {
  45.                     $opdPeriode[] = array_where($opdAll, 'opdId', $opdPeriod['opd'], 'namaOpd');
  46.                 }
  47.                 $data = [
  48.                     "nama" => $periode[0]['namaPeriode'],
  49.                     "tahun" => $periode[0]['tahunPeriode'],
  50.                     "opd" => $opdPeriode,
  51.                     "jumlah" => count($periode[0]['opdList'])
  52.                 ];
  53.                 echo json_encode($data);
  54.                 break;
  55.             case "selectList":
  56.                 $getTahun = json_decode($this->tahun->list(), true)['data'];
  57.                 $data['tahun'] = [$getTahun['tahunSatu'], $getTahun['tahunDua'], $getTahun['tahunTiga'], $getTahun['tahunEmpat'], $getTahun['tahunLima']];
  58.                 $getOpd = json_decode($this->opd->getAll(1), true)['data'];
  59.                 $opdAll = [];
  60.                 for ($i = 1; $i <= $getOpd['jumlahPage']; $i++) {
  61.                     $getOpd[$i] = json_decode($this->opd->getAll($i), true)['data'];
  62.                     $opdAll[] = $getOpd[$i]['dataOpd'];
  63.                 }
  64.                 $data['opd'] = [];
  65.                 foreach ($opdAll[0] as $opdl) {
  66.                     $data['opd'][] = [
  67.                         "id" => $opdl['opdId'],
  68.                         "nama" => $opdl['namaOpd'],
  69.                     ];
  70.                 }
  71.                 echo json_encode($data);
  72.                 break;
  73.             case "edit":
  74.                 $response = $this->periode->getSingle($params[1]);
  75.                 echo $response;
  76.                 break;
  77.  
  78.         }
  79.     }
  80.  
  81.     public function create()
  82.     {
  83.         if (isset($_POST['opd'])) {
  84.             $semua = false;
  85.              foreach ($_POST['opd'] as $cariSemua) {
  86.                 if ($cariSemua['opdId'] === "semua") {
  87.                     $semua = true;
  88.                     break;
  89.                 }
  90.             }
  91.             if ($semua) {
  92.                 $getOpd = json_decode($this->opd->getAll(1), true)['data'];
  93.                 $opdAll = [];
  94.                 for ($i = 1; $i <= $getOpd['jumlahPage']; $i++) {
  95.                     $getOpd[$i] = json_decode($this->opd->getAll($i), true)['data'];
  96.                     $opdAll = array_merge($opdAll, $getOpd[$i]['dataOpd']);
  97.                 }
  98.                 $opd = $opdAll;
  99.                 $opd = array_map(function ($item){
  100.                     return ["opdId" => (int) $item['opdId']];
  101.                 }, $opd);
  102.             } else {
  103.                 $opd = $_POST['opd'];
  104.                 $opd = array_map(function ($item) {
  105.                     return ["opdId" => (int) $item["opdId"]];
  106.                 }, $opd);
  107.             }
  108.         } else {
  109.             $opd = [];
  110.         }
  111.         $data = [
  112.             "tahunPeriode" => (int) $_POST['tahun'],
  113.             "namaPeriode" => $_POST['nama'],
  114.             "waktuMulai" => tanggal($_POST['mulai'], "Y-m-d"),
  115.             "waktuAkhir" => tanggal($_POST['akhir'], "Y-m-d"),
  116.             "opd" => $opd,
  117.         ];
  118.         $request = json_encode($data);
  119.         $response = $this->periode->create($request);
  120.         echo $response;
  121.     }
  122.  
  123.     public function update()
  124.     {
  125.  
  126.         if (isset($_POST['opd'])) {
  127.             $semua = false;
  128.             foreach ($_POST['opd'] as $cariSemua) {
  129.                 if ($cariSemua['opdId'] === "semua") {
  130.                     $semua = true;
  131.                     break;
  132.                 }
  133.             }
  134.             if ($semua) {
  135.                 $getAllOpd = json_decode($this->opd->getAll(), true)['data'];
  136.                 $opd = [];
  137.                 foreach ($getAllOpd as $itemOpd) {
  138.                     $opd[] = [];
  139.                 }
  140.             } else {
  141.                 $opd = $_POST['opd'];
  142.                 $opd = array_map(function ($item) {
  143.                     return ["opdId" => (int) $item["opdId"]];
  144.                 }, $opd);
  145.             }
  146.         } else {
  147.             $opd = [];
  148.         }
  149.         $data = [
  150.             "idPeriode" => (int) $_POST['id'],
  151.             "dataUpdate" => [
  152.                 "waktuMulai" => tanggal($_POST['mulai'], "Y-m-d"),
  153.                 "waktuAkhir" => tanggal($_POST['akhir'], "Y-m-d"),
  154.                 "namaPeriode" => $_POST['nama'],
  155.                 "tahunPeriode" => (int) $_POST['tahun'],
  156.             ],
  157.             "opd" => $opd,
  158.             "status" => (int) $_POST["status"],
  159.             "semua" => $semua,
  160.         ];
  161.         $request = json_encode($data);
  162.         $response = $this->periode->update($request);
  163.         echo $response;
  164.     }
  165.  
  166.     public function delete($id)
  167.     {
  168.         $response = $this->periode->delete($id);
  169.         echo $response;
  170.     }
  171.  
  172.     public function testing()
  173.     {
  174.         echo $this->periode->list();
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement