sumberdaya.php 5.79 KB
<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 * @link		http://philsturgeon.co.uk/code/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Sumberdaya extends REST_Controller
{
	function __construct(){
        parent::__construct();
		$this->load->database();
    }
	
	function index_get()
    {
       
		// $id = $this->get('id');
		// if ($this->get('id') == '') {
            // $user = $this->db->get('pmlocal_jenissdy')->result();
        // } else {
            // $this->db->where('id_jenissdy', $id);
            // $user = $this->db->get('pmlocal_jenissdy')->result();
        // }
    	$data = array(
                    'kode_master_sumberdaya'	=> 'A57 *(3 Digit)', // 3digit
                    'nama_master_sumberdaya'	=> 'Beton Readymix Aditive (Khusus) Type 1',
                    'kode_jenis_sumberdaya'		=> 'A5 *(2 Digit)', // 2digit
                    'nama_jenis_sumberdaya'		=> 'READYMIX CONCRETE',
                    'tipe_sumberdaya'			=> 'Material/Alat/Upah/Subkontraktor',
					);
        if($data)
        {
           $this->response($data, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Jenis Sumberdaya could not be found'), 404);
        }
		
    }
   
	function index_post() {
        $data = array(
                    'kode_master_sumberdaya'	=> trim($this->post('kode_master_sumberdaya')), // 3digit
                    'nama_master_sumberdaya'	=> $this->post('nama_master_sumberdaya'),
                    'kode_jenis_sumberdaya'		=> trim($this->post('kode_jenis_sumberdaya')), // 2digit
                    'nama_jenis_sumberdaya'		=> $this->post('nama_jenis_sumberdaya'),
                    'tipe_sumberdaya'			=> trim($this->post('tipe_sumberdaya')),
					);
		if($data['kode_master_sumberdaya'] == '' ){
			$this->response(array('error' => 'Kode Master Sumberdaya could not empty!'), 404);
		}else{
			$this->db->where('kd_sdymaster', $data['kode_master_sumberdaya']);
            $count = $this->db->get('pmlocal_sdymaster')->num_rows();
			if ($count > 0){
				$this->response(array('error' => 'Kode Master Sumberdaya Sudah ada!'), 404);
			}
		}
		
		if($data['nama_master_sumberdaya'] == '' ){
			$this->response(array('error' => 'Nama Master Sumberdaya could not empty!'), 404);
		}
		
		if($data['nama_jenis_sumberdaya'] == '' ){
			$this->response(array('error' => 'Nama Jenis Sumberdaya could not empty!'), 404);
		}
		
		if($data['tipe_sumberdaya'] == '' ){
			$this->response(array('error' => 'Tipe Sumberdaya could not empty!'), 404);
		}else{
            $query = $this->db->query("SELECT * FROM resource_type WHERE LOWER(tpname)='".strtolower($data['tipe_sumberdaya'])."' ");
			if ($query->num_rows() == 0){
				$this->response(array('error' => 'Tipe Sumberdaya Tidak Terdaftar!'), 404);
			}
		}
		
		if($data['kode_jenis_sumberdaya'] == '' ){
			$this->response(array('error' => 'Kode Jenis Sumberdaya could not empty!'), 404);
		}else{
			$this->db->where('kd_jenissdy', $data['kode_jenis_sumberdaya']);
            $count = $this->db->get('pmlocal_jenissdy')->num_rows();
			$datatipe = array(
							'id_jenissdy'		=> $this->db->query('SELECT MAX(id_jenissdy)+1 AS maxid FROM pmlocal_jenissdy')->row()->maxid,
							'kd_jenissdy'		=> trim($this->post('kode_jenis_sumberdaya')), // 2digit
							'nama_jenissdy'		=> $this->post('nama_jenis_sumberdaya'),
						);
			$inserttipe = true;
			if ($count == 0){
				$inserttipe = $this->db->insert('pmlocal_jenissdy', $datatipe);
			}
		}
		
		//tipe sumberdaya
		$tipe = $query->row_array();
		
		if ($inserttipe){
			$data = array(
					'id_sdymaster'		=> $this->db->query('SELECT MAX(id_sdymaster)+1 AS maxid FROM pmlocal_sdymaster')->row()->maxid,
                    'kd_sdymaster'		=> trim($this->post('kode_master_sumberdaya')), // 3digit
                    'nama_sdymaster'	=> $this->post('nama_master_sumberdaya'),
                    'kd_jenissdy'		=> trim($this->post('kode_jenis_sumberdaya')), // 2digit
                    'rtid'				=> $tipe['rtid']
					);
			$insert = $this->db->insert('pmlocal_sdymaster', $data);
			if ($insert) {
				$this->response(array('status' => 'success', 200));
			} else {
				$this->response(array('status' => 'fail', 502));
			}
		}else {
				$this->response(array('status' => 'fail insert jenis sumberdaya', 502));
		}
    }
	
    function user_post()
    {
        //$this->some_model->updateUser( $this->get('id') );
        $message = array('id' => $this->get('id'), 'name' => $this->post('name'), 'email' => $this->post('email'), 'message' => 'ADDED!');
        
        $this->response($message, 200); // 200 being the HTTP response code
    }
    
    function user_delete()
    {
    	//$this->some_model->deletesomething( $this->get('id') );
        $message = array('id' => $this->get('id'), 'message' => 'DELETED!');
        
        $this->response($message, 200); // 200 being the HTTP response code
    }
    
    function sumberdaya_get()
    {
		// $id = $this->get('id');
		// print_r($id);exit;
		$user = $this->db->get('pmlocal_jenissdy')->result();
        
        if($users)
        {
            $data['hasil'] = $this->response($users, 200); // 200 being the HTTP response code
        }

        else
        {
            $data['hasil'] = $this->response(array('error' => 'Couldn\'t find any users!'), 404);
        }
		// return $hasil;
		$this->load->view('content',$data);
    }


	public function send_post()
	{
		var_dump($this->request->body);
	}


	public function send_put()
	{
		var_dump($this->put('foo'));
	}
}