Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if(!defined('BASEPATH')) exit('No direct script access allowed');
- class Pegawai extends CI_Controller {
- private $limit=10;
- function __construct()
- {
- parent::__construct();
- #load library dan helper yang dibutuhkan
- $this->load->library(array('table','form_validation'));
- $this->load->helper(array('form','url'));
- $this->load->model('pegawai_model',TRUE);
- }
- function index($offset=0,$order_column='id',$order_type='asc')
- {
- if (empty($offset)) $offset=0;
- if (empty($order_column)) $order_column='id';
- if (empty($order_type)) $order_type='asc';
- //TODO: check for valid column
- //load data pegawai
- $pegawais=$this->pegawai_model->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
- // generate pagination
- $this->load->library('pagination');
- $config['base_url']=site_url('pegawai/index');
- $config['total_rows']=$this->pegawai_model->count_all();
- $config['per_page']=$this->limit;
- $config['uri_segment']=3;
- $this->pagination->initialize($config);
- $data['pagination']=$this->pagination->create_links();
- // generate table data
- $this->load->library('table');
- $this->table->set_empty(" ");
- $new_order=($order_type=='asc'?'desc':'asc');
- $this->table->set_heading(
- 'No',
- anchor('pegawai/index'.$offset.'/nama/'.$new_order,'Nama'),
- anchor('pegawai/index'.$offset.'/alamat/'.$new_orser,'Alamat'),
- anchor('pegawai/index'.$offset.'/jenis_kelamin/'.$new_orser,'Jenis Kelamin'),
- anchor('pegawai/index'.$offset.'/tanggal_lahir/'.$new_orser,'Tanggal Lahir (dd-mm-yyyy)'),
- 'Actions'
- );
- $i=0+$offset;
- foreach ($pegawais as $pegawai){
- $this->table->add_row(
- ++$i,
- $pegawai->nama,
- $pegawai->alamat,
- strtoupper($pegawai->jenis_kelamin)=='M'?'Laki-Laki':'Perempuan',
- date('d-m-y',strtotime($pegawai->tanggal_lahir)),
- anchor('pegawai/view/'.$pegawai->id,'view',array('class'=>'view')).' '
- .anchor('pegawai/update/'.$pegawai->id,'update',array('class'=>'update')).' '
- .anchor('pegawai/delete/'.$pegawai->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Apakah Anda yakin ingin menghapus data pegawai?');"))
- );
- }
- $data['table']=$this->table->generate();
- if ($this->uri->segment(3)=='delete_success')
- $data['message']=='Data berhasil dihapus';
- else if ($this->uri->segment(3)=='add_success')
- $data['message']=='Berhasil di tambah';
- else
- $data['message']='';
- //load view
- $this->load->view('pegawaiList',$data);
- }
- function add(){
- //set common properties
- $data['title']='Tamba pegawai baru';
- $data['action']=site_url('pegawai/add');
- $data['link_back']=anchor('pegawai/index/','Back to list of pegawais',array('class'=>'back'));
- $this->_set_rules();
- // run validation
- if ($this->form_validation->run()=== FALSE){
- $data['message']='';
- // set common properties
- $data['title']='add new pegawai';
- $data['message']='';
- $data['pegawai']['id']='';
- $data['pegawai']['nama']='';
- $data['pegawai']['alamat']='';
- $data['pegawai']['jenis_kelamin']='';
- $data['pegawai']['tanggal_lahir']='';
- $data['link_back']=anchor('pegawai/index/','Lihat Daftar Pegawai',array('class'=>'back'));
- $this->load->view('pegawaiEdit',$data);
- } else {
- // save data
- $pegawai= array('nama'=>$this->input->post('nama'),
- 'alamat'=>$this->input->post('alamat'),
- 'jenis_kelamin'=>$this->input>post('jenis_kelamin'),
- 'tanggal_lahir'=>$date('y-m-d',strtotime($this>input->post('tanggal_lahir'))));
- $id=$this->siswa_model->save($siswa);
- // set form input nama="id"
- $this->validation->id=$id;
- redirect('pegawai/index/add_success');
- }
- }
- function view($id){
- // set common properties
- $data['title']='pegawai Details';
- $data['link_back']= anchor('pegawai/index/','Lihat daftar pegawais',array('class'=>'back'));
- // get pegawai details
- $data['pegawai']=$this->pegawai_model->get_by_id($id)->row();
- // loaad view
- $this->load->view('pegawaiView',$data);
- }
- function update($id){
- // set common properties
- $data['title']='Update Pegawai';
- $this->load->library('form_validation');
- // set validation properties
- $this->_set_rules();
- $data['action']=('pegawai/update/'.$id);
- // run validation
- if ($this->form_validation->run()=== FALSE){
- $data['message']='';
- $data['pegawai']=$this->pegawai_model->get_by_id($id)>row_array();
- $_POST['jenis_kelamin']=
- strtoupper($data['pegawai']['jenis_kelamin']);
- $data['pegawai']['tanggal_lahir']= date('d-m-y',strtotime($data['pegawai']['tanggal_lahir']));
- // set common properties
- $data['title']='Update pegawai';
- $data['message']='';
- } else {
- // save data
- $id=$this->input->post('id');
- $pegawai= array('nama'=>$this->input->post('nama'),
- 'alamat'=>$this->input->post('alamat'),
- 'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
- 'tanggal_lahir'=>date('y-m-d',strtotime($this->input->post('tanggal_lahir'))));
- $this->pegawai_model->update($id,$pegawai);
- $data['pegwai']=$this->pegawai_model->get_by_id($id)->row_array();
- // set user message
- $data['message']='update pegawai success';
- }
- $data['link_back']=anchor('pegawai/index/','Lihat daftar pegawai', array('class'=>'back'));
- // load view
- $this->load->view('pegawaiEdit',$data);
- }
- function delete($id){
- // delete pegawai
- $this->pegawai_model->delete($id);
- // redirect to pegawai list page
- redirect('pegawai/index/delete_success','refresh');
- }
- // validation rules
- function _set_rules(){
- $this->form_validation->set_rules('nama','Nama','required|trim');
- $this->form_validation->set_rules('jenis_kelamin','Jenis Kelamin','required');
- $this->form_validation->set_rules('alamat','Alamat','required|callback_valid_date');
- $this->form_validation->set_rules('tanggal_lahir','Tanggal Lahir','required');
- }
- // data_validation callback
- function valid_date($str)
- {
- if(!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/',$str))
- {
- $this->form_validation->set_message('valid_date',
- 'date format is not valid. yyyy-mm-dd');
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- /* End of file pegawai.php */
- /* Location: ./application/controllers/pegawai.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement