Advertisement
cdsatrian

pegawai.php

Jan 22nd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.64 KB | None | 0 0
  1. <?php if(!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Pegawai extends CI_Controller {
  3.   private $limit=10;
  4.   function __construct()
  5.   {
  6.     parent::__construct();
  7.     #load library dan helper yang dibutuhkan
  8.    $this->load->library(array('table','form_validation'));
  9.     $this->load->helper(array('form','url'));
  10.     $this->load->model('pegawai_model',TRUE);
  11.   }
  12.  
  13.   function index($offset=0,$order_column='id',$order_type='asc')
  14.   {
  15.     if (empty($offset)) $offset=0;
  16.     if (empty($order_column)) $order_column='id';
  17.     if (empty($order_type)) $order_type='asc';
  18.     //TODO: check for valid column
  19.     //load data pegawai
  20.     $pegawais=$this->pegawai_model->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
  21.     // generate pagination
  22.     $this->load->library('pagination');
  23.     $config['base_url']=site_url('pegawai/index');
  24.     $config['total_rows']=$this->pegawai_model->count_all();
  25.     $config['per_page']=$this->limit;
  26.     $config['uri_segment']=3;
  27.     $this->pagination->initialize($config);
  28.     $data['pagination']=$this->pagination->create_links();
  29.     // generate table data
  30.     $this->load->library('table');
  31.     $this->table->set_empty("&nbsp;");
  32.     $new_order=($order_type=='asc'?'desc':'asc');
  33.     $this->table->set_heading(
  34.       'No',
  35.       anchor('pegawai/index'.$offset.'/nama/'.$new_order,'Nama'),
  36.       anchor('pegawai/index'.$offset.'/alamat/'.$new_orser,'Alamat'),
  37.       anchor('pegawai/index'.$offset.'/jenis_kelamin/'.$new_orser,'Jenis Kelamin'),
  38.       anchor('pegawai/index'.$offset.'/tanggal_lahir/'.$new_orser,'Tanggal Lahir (dd-mm-yyyy)'),
  39.       'Actions'
  40.     );
  41.     $i=0+$offset;
  42.     foreach ($pegawais as $pegawai){
  43.       $this->table->add_row(
  44.         ++$i,
  45.         $pegawai->nama,
  46.         $pegawai->alamat,
  47.         strtoupper($pegawai->jenis_kelamin)=='M'?'Laki-Laki':'Perempuan',
  48.         date('d-m-y',strtotime($pegawai->tanggal_lahir)),
  49.         anchor('pegawai/view/'.$pegawai->id,'view',array('class'=>'view')).' '
  50.         .anchor('pegawai/update/'.$pegawai->id,'update',array('class'=>'update')).' '
  51.         .anchor('pegawai/delete/'.$pegawai->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Apakah Anda yakin ingin menghapus data pegawai?');"))
  52.       );
  53.     }
  54.     $data['table']=$this->table->generate();
  55.     if ($this->uri->segment(3)=='delete_success')
  56.       $data['message']=='Data berhasil dihapus';
  57.     else if ($this->uri->segment(3)=='add_success')
  58.       $data['message']=='Berhasil di tambah';
  59.     else
  60.       $data['message']='';
  61.     //load view
  62.     $this->load->view('pegawaiList',$data);
  63.   }
  64.  
  65.   function add(){
  66.     //set common properties
  67.     $data['title']='Tamba pegawai baru';
  68.     $data['action']=site_url('pegawai/add');
  69.     $data['link_back']=anchor('pegawai/index/','Back to list of pegawais',array('class'=>'back'));
  70.     $this->_set_rules();
  71.     // run validation
  72.     if ($this->form_validation->run()=== FALSE){
  73.       $data['message']='';
  74.       // set common properties
  75.       $data['title']='add new pegawai';
  76.       $data['message']='';
  77.       $data['pegawai']['id']='';
  78.       $data['pegawai']['nama']='';
  79.       $data['pegawai']['alamat']='';
  80.       $data['pegawai']['jenis_kelamin']='';
  81.       $data['pegawai']['tanggal_lahir']='';
  82.       $data['link_back']=anchor('pegawai/index/','Lihat Daftar Pegawai',array('class'=>'back'));
  83.       $this->load->view('pegawaiEdit',$data);
  84.     } else {
  85.       // save data
  86.       $pegawai= array('nama'=>$this->input->post('nama'),
  87.                       'alamat'=>$this->input->post('alamat'),
  88.                       'jenis_kelamin'=>$this->input­>post('jenis_kelamin'),
  89.                       'tanggal_lahir'=>$date('y-m-d',strtotime($this­>input->post('tanggal_lahir'))));
  90.       $id=$this->siswa_model->save($siswa);
  91.       // set form input nama="id"
  92.       $this->validation->id=$id;
  93.       redirect('pegawai/index/add_success');
  94.     }
  95.   }
  96.  
  97.   function view($id){
  98.     // set common properties
  99.     $data['title']='pegawai Details';
  100.     $data['link_back']= anchor('pegawai/index/','Lihat daftar pegawais',array('class'=>'back'));
  101.     // get pegawai details
  102.     $data['pegawai']=$this->pegawai_model->get_by_id($id)->row();
  103.     // loaad view
  104.     $this->load->view('pegawaiView',$data);
  105.   }
  106.  
  107.   function update($id){
  108.     // set common properties
  109.     $data['title']='Update Pegawai';
  110.     $this->load->library('form_validation');
  111.     // set validation properties
  112.     $this->_set_rules();
  113.     $data['action']=('pegawai/update/'.$id);
  114.     // run validation
  115.     if ($this->form_validation->run()=== FALSE){
  116.       $data['message']='';
  117.       $data['pegawai']=$this->pegawai_model->get_by_id($id)>row_array();
  118.       $_POST['jenis_kelamin']=
  119.       strtoupper($data['pegawai']['jenis_kelamin']);
  120.       $data['pegawai']['tanggal_lahir']= date('d-m-y',strtotime($data['pegawai']['tanggal_lahir']));
  121.       // set common properties
  122.       $data['title']='Update pegawai';
  123.       $data['message']='';
  124.     } else {
  125.       // save data
  126.       $id=$this->input->post('id');
  127.       $pegawai= array('nama'=>$this->input->post('nama'),
  128.                       'alamat'=>$this->input->post('alamat'),
  129.                       'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
  130.                       'tanggal_lahir'=>date('y-m-d',strtotime($this->input->post('tanggal_lahir'))));
  131.       $this->pegawai_model->update($id,$pegawai);
  132.       $data['pegwai']=$this->pegawai_model->get_by_id($id)->row_array();
  133.       // set user message
  134.       $data['message']='update pegawai success';
  135.     }
  136.     $data['link_back']=anchor('pegawai/index/','Lihat daftar pegawai', array('class'=>'back'));
  137.     // load view
  138.     $this->load->view('pegawaiEdit',$data);
  139.   }
  140.  
  141.   function delete($id){
  142.     // delete pegawai
  143.     $this->pegawai_model->delete($id);
  144.     // redirect to pegawai list page
  145.     redirect('pegawai/index/delete_success','refresh');
  146.   }
  147.  
  148.   // validation rules
  149.   function _set_rules(){
  150.     $this->form_validation->set_rules('nama','Nama','required|trim');
  151.     $this->form_validation->set_rules('jenis_kelamin','Jenis Kelamin','required');
  152.     $this->form_validation->set_rules('alamat','Alamat','required|callback_valid_date');
  153.     $this->form_validation->set_rules('tanggal_lahir','Tanggal Lahir','required');
  154.   }
  155.  
  156.   // data_validation callback
  157.   function valid_date($str)
  158.   {
  159.     if(!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/',$str))
  160.     {
  161.       $this->form_validation->set_message('valid_date',
  162.       'date format is not valid. yyyy-mm-dd');
  163.       return false;
  164.     }
  165.     else
  166.     {
  167.       return true;
  168.     }
  169.   }
  170. }
  171.  
  172.  
  173.  
  174. /* End of file pegawai.php */
  175. /* Location: ./application/controllers/pegawai.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement