Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Admin_periode extends Controller
- {
- private $opd;
- private $periode;
- private $tahun;
- public function __construct()
- {
- $this->opd = $this->model('Opd_model');
- $this->periode = $this->model('Periode_model');
- $this->tahun = $this->model('Tahun_model');
- $this->access('admin');
- }
- public function index()
- {
- $data ["title"] ="Manajemen Periode";
- $this->view("admin/periode", $data);
- }
- public function listJadwal()
- {
- $data['list'] = json_decode($this->periode->list(), true)['data'];
- echo json_encode($data['list']);
- }
- public function list(...$params)
- {
- switch ($params[0]) {
- case "jadwal":
- $periode = $this->periode->list();
- echo json_encode($periode);
- break;
- case "opd":
- $getOpd = json_decode($this->opd->getAll(1), true)['data'];
- $opdAll = [];
- for ($i = 1; $i <= $getOpd['jumlahPage']; $i++) {
- $getOpd[$i] = json_decode($this->opd->getAll($i), true)['data'];
- $opdAll = array_merge($opdAll, $getOpd[$i]['dataOpd']);
- }
- $periode = json_decode($this->periode->getSingle($params[1]), true)['data'];
- $opdPeriode = [];
- foreach ($periode[0]['opdList'] as $opdPeriod) {
- $opdPeriode[] = array_where($opdAll, 'opdId', $opdPeriod['opd'], 'namaOpd');
- }
- $data = [
- "nama" => $periode[0]['namaPeriode'],
- "tahun" => $periode[0]['tahunPeriode'],
- "opd" => $opdPeriode,
- "jumlah" => count($periode[0]['opdList'])
- ];
- echo json_encode($data);
- break;
- case "selectList":
- $getTahun = json_decode($this->tahun->list(), true)['data'];
- $data['tahun'] = [$getTahun['tahunSatu'], $getTahun['tahunDua'], $getTahun['tahunTiga'], $getTahun['tahunEmpat'], $getTahun['tahunLima']];
- $getOpd = json_decode($this->opd->getAll(1), true)['data'];
- $opdAll = [];
- for ($i = 1; $i <= $getOpd['jumlahPage']; $i++) {
- $getOpd[$i] = json_decode($this->opd->getAll($i), true)['data'];
- $opdAll[] = $getOpd[$i]['dataOpd'];
- }
- $data['opd'] = [];
- foreach ($opdAll[0] as $opdl) {
- $data['opd'][] = [
- "id" => $opdl['opdId'],
- "nama" => $opdl['namaOpd'],
- ];
- }
- echo json_encode($data);
- break;
- case "edit":
- $response = $this->periode->getSingle($params[1]);
- echo $response;
- break;
- }
- }
- public function create()
- {
- if (isset($_POST['opd'])) {
- $semua = false;
- foreach ($_POST['opd'] as $cariSemua) {
- if ($cariSemua['opdId'] === "semua") {
- $semua = true;
- break;
- }
- }
- if ($semua) {
- $getOpd = json_decode($this->opd->getAll(1), true)['data'];
- $opdAll = [];
- for ($i = 1; $i <= $getOpd['jumlahPage']; $i++) {
- $getOpd[$i] = json_decode($this->opd->getAll($i), true)['data'];
- $opdAll = array_merge($opdAll, $getOpd[$i]['dataOpd']);
- }
- $opd = $opdAll;
- $opd = array_map(function ($item){
- return ["opdId" => (int) $item['opdId']];
- }, $opd);
- } else {
- $opd = $_POST['opd'];
- $opd = array_map(function ($item) {
- return ["opdId" => (int) $item["opdId"]];
- }, $opd);
- }
- } else {
- $opd = [];
- }
- $data = [
- "tahunPeriode" => (int) $_POST['tahun'],
- "namaPeriode" => $_POST['nama'],
- "waktuMulai" => tanggal($_POST['mulai'], "Y-m-d"),
- "waktuAkhir" => tanggal($_POST['akhir'], "Y-m-d"),
- "opd" => $opd,
- ];
- $request = json_encode($data);
- $response = $this->periode->create($request);
- echo $response;
- }
- public function update()
- {
- if (isset($_POST['opd'])) {
- $semua = false;
- foreach ($_POST['opd'] as $cariSemua) {
- if ($cariSemua['opdId'] === "semua") {
- $semua = true;
- break;
- }
- }
- if ($semua) {
- $getAllOpd = json_decode($this->opd->getAll(), true)['data'];
- $opd = [];
- foreach ($getAllOpd as $itemOpd) {
- $opd[] = [];
- }
- } else {
- $opd = $_POST['opd'];
- $opd = array_map(function ($item) {
- return ["opdId" => (int) $item["opdId"]];
- }, $opd);
- }
- } else {
- $opd = [];
- }
- $data = [
- "idPeriode" => (int) $_POST['id'],
- "dataUpdate" => [
- "waktuMulai" => tanggal($_POST['mulai'], "Y-m-d"),
- "waktuAkhir" => tanggal($_POST['akhir'], "Y-m-d"),
- "namaPeriode" => $_POST['nama'],
- "tahunPeriode" => (int) $_POST['tahun'],
- ],
- "opd" => $opd,
- "status" => (int) $_POST["status"],
- "semua" => $semua,
- ];
- $request = json_encode($data);
- $response = $this->periode->update($request);
- echo $response;
- }
- public function delete($id)
- {
- $response = $this->periode->delete($id);
- echo $response;
- }
- public function testing()
- {
- echo $this->periode->list();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement