<?php class Tugas4 extends CI_Controller { private $limit=5;  function __construct() { 	parent::__construct(); 	$this->load->library(array('table','form_validation','session')); 	$this->load->helper(array('form','url','html')); 	$this->load->model('t4_model','',TRUE); 	$this->table->set_empty("&nbsp;"); 	$this->load->library(array('t4_access','t4_template')); }  function tes_template() {	$data['title']='halo'; 	$this->t4_template->display('tes_content',$data); }  function index() {	 	$data['title']='Halaman Utama Tugas 4'; 	 	 	$this->form_validation->set_rules('username','Username','trim|required|strip_tags'); 	$this->form_validation->set_rules('password','Password','trim|required'); 	$this->form_validation->set_rules('token','token','callback_check_login'); 	 	if ($this->form_validation->run() == FALSE ) 	{ 		 	} else { 		redirect($this->input->post('current_url'),'REFRESH'); 	} 	 	 	$data['userdata']=$this->session->userdata('username'); 	$this->t4_template->display('t4_index',$data); }   function logout() { 	$this->t4_access->logout(); 	redirect(base_url().'tugas4','REFRESH'); }  function check_login() { 	$username=$this->input->post('username',TRUE); 	$password=$this->input->post('password',TRUE); 	 	$login = $this->t4_access->login($username, $password); 	if ($login) 	{ 		return TRUE; 	} else { 		$this->form_validation->set_message('check_login','Username atau password anda salah'); 		return FALSE; 	} }  function lihat($jenis='mahasiswa',$offset=0) {	 	$data['userdata']=$this->session->userdata('username'); 	if (!isset($_POST['cari'])) {$cari='%';} else {$cari=$this->input->post('cari');} 	$data['cari']=$cari; 	$data['jenis']=$jenis; 	 	//generate pagination 	$ambildb=$this->t4_model->get_paged_list($this->limit,$offset,$jenis)->result(); 	 	$this->load->library('pagination'); 	$config['base_url']=site_url('tugas4/lihat/'.$jenis.'/'); 	$config['total_rows']=$this->t4_model->ambilmaxdb($jenis); 	$config['per_page']=$this->limit; 	$config['uri_segment']=4; 	$this->pagination->initialize($config); 	$data['pagination']=$this->pagination->create_links();  	//generate table 	$tmpl = array ( 'table_open'  => '<table class="altrowstable" id="alternatecolor" border="22" align="center" style="width:600px;">' ); 	$this->table->set_template($tmpl); 	 	 	if ($jenis=='mahasiswa') { 		$data['title']='Daftar Mahasiswa'; 		$this->table->set_heading('No','NRP','Nama','Tanggal Lahir','Jenis Kelamin','Alamat','Telepon','Action'); 		$i=0+$offset; 		foreach ($ambildb as $row)  		{ 			 if ($data['userdata']=='administrator')  			{  				$this->table->add_row(++$i,$row->nrp,$row->nama,$row->tanggal_lahir,$row->jenis_kelamin,$row->alamat,$row->telepon, 				'<a href="'.base_url().'tugas4/view/'.$jenis.'/'.$row->nrp.'">View</a>  				<a href="'.base_url().'tugas4/update/'.$jenis.'/'.$row->nrp.'">Edit</a>  				'.anchor(base_url().'/tugas4/delete/'.$jenis.'/'.$row->nrp, 				'Delete',array('onclick'=>"return confirm('Apakah anda yakin ingin menghapus data ?')")) );  			} else { 				$this->table->add_row(++$i,$row->nrp,$row->nama,$row->tanggal_lahir,$row->jenis_kelamin,$row->alamat,$row->telepon, 				'<a href="'.base_url().'tugas4/view/'.$jenis.'/'.$row->nrp.'">View</a>' ); 			} 		} 		$data['tambah']='tambah_mahasiswa'; 	} else { 		$data['title']='Daftar Matakuliah'; 		$this->table->set_heading('No','Kode Matakuliah','Nama Kuliah','Total SKS','Action'); 		$i=0+$offset; 		foreach ($ambildb as $row)  		{ 			 if ($data['userdata']=='administrator')  			{ 				$this->table->add_row(++$i,$row->kode_mk,$row->nama_mk,$row->sks, 				'<a href="'.base_url().'tugas4/view/'.$jenis.'/'.$row->kode_mk.'">View</a>  				<a href="'.base_url().'tugas4/update/'.$jenis.'/'.$row->kode_mk.'">Edit</a>  				'.anchor(base_url().'/tugas4/delete/'.$jenis.'/'.$row->kode_mk, 				'Delete',array('onclick'=>"return confirm('Apakah anda yakin ingin menghapus data ?')")) ); 			} else { 				$this->table->add_row(++$i,$row->kode_mk,$row->nama_mk,$row->sks, 				'<a href="'.base_url().'tugas4/view/'.$jenis.'/'.$row->kode_mk.'">View</a>' ); 			} 		} 		$data['tambah']='tambah_matakuliah'; 	} 	 	$data['table']=$this->table->generate(); 	$data['jenis']=$jenis; 	 	if ($this->uri->segment(4)=='delete_success')  			$data['message']='Data Berhasil dihapus'; 		else if ($this->uri->segment(4)=='add_success') 			$data['message']='Data berhasil ditambah'; 		else if ($this->uri->segment(4)=='edit_success') 			$data['message']='Data berhasil diedit'; 		else  			$data['message']=''; 			 	$this->t4_template->display('t4_lihat',$data); 	 }  function tambah_mahasiswa() { 	$data['title']='Tambah Mahasiswa Baru'; 	 	$ar_nama = array(               'name'        => 'nama',               'id'          => 'nama',               'value'       => '',               'maxlength'   => '100',               'size'        => '40',             ); 		$data['nama']=form_input($ar_nama);	 		 	$ar_nrp = array(               'name'        => 'nrp',               'id'          => 'nrp',               'value'       => '',               'maxlength'   => '100',               'size'        => '40',             ); 		$data['nrp']=form_input($ar_nrp); 		 	$ar_alamat = array(               'name'        => 'alamat',               'id'          => 'alamat',               'value'       => '',               'rows'   		=> '4',               'cols'        => '30',             ); 		$data['alamat']=form_textarea($ar_alamat);	 	 	$ar_telepon = array(               'name'        => 'telepon',               'id'          => 'telepon',               'value'       => '',               'maxlength'   => '100',               'size'        => '40',             ); 		$data['telepon']=form_input($ar_telepon); 	 	$ar_email = array(               'name'        => 'email',               'id'          => 'email',               'value'       => '',               'maxlength'   => '100',               'size'        => '40',             ); 		$data['email']=form_input($ar_email); 	 	$ar_sex_m = array( 				'name'=>'jenis_kelamin', 				'id'=>'jenis_kelamin', 				'value'=>'Laki-laki' 			);  		$ar_sex_f = array( 				'name'=>'jenis_kelamin', 				'id'=>'jenis_kelamin', 				'value'=>'Perempuan' 			);   		$data['sex_m'] = form_radio($ar_sex_m); 		$data['sex_f'] = form_radio($ar_sex_f); 		 	$this->_set_rules_mahasiswa(); 	$this->form_validation->set_rules('nrp', 'NRP', 'required|numeric|is_unique[mahasiswa.nrp]'); 	 	if ($this->form_validation->run() == FALSE) 	{ 			$this->t4_template->display('t4_tambah_mahasiswa',$data); 		} 	else 	{	 			$mahasiswa= array( 			'nrp'=>$this->input->post('nrp'), 			'nama'=>$this->input->post('nama'), 			'tanggal_lahir'=>$this->input->post('tanggal_lahir'), 			'alamat'=>$this->input->post('alamat'), 			'telepon'=>$this->input->post('telepon'), 			'email'=>$this->input->post('email'), 			'jenis_kelamin'=>$this->input->post('jenis_kelamin')); 			 			$id=$this->t4_model->savemahasiswa($mahasiswa); 			$this->validation->id = $id; 			 			redirect('tugas4/lihat/mahasiswa/add_success'); 		} 	 }  function tambah_matakuliah() {	 	$data['title']='Tambah Matakuliah Baru'; 	 	$ar_kode_mk = array(               'name'        => 'kode_mk',               'id'          => 'kode_mk',               'value'       => '',               'maxlength'   => '100',               'size'        => '40',             ); 		$data['kode_mk']=form_input($ar_kode_mk);	  	$ar_nama_mk = array(               'name'        => 'nama_mk',               'id'          => 'nama_mk',               'value'       => '',               'maxlength'   => '100',               'size'        => '40',             ); 		$data['nama_mk']=form_input($ar_nama_mk);	  	$ar_sks = array(               'name'        => 'sks',               'id'          => 'sks',               'value'       => '',               'maxlength'   => '100',               'size'        => '40',             ); 		$data['sks']=form_input($ar_sks);	  	$this->_set_rules_matakuliah(); 	$this->form_validation->set_rules('kode_mk', 'Kode Matakuliah', 'required|is_unique[matakuliah.kode_mk]'); 	  	if ($this->form_validation->run() == FALSE) 	{ 			$this->t4_template->display('t4_tambah_matakuliah',$data); 		} 	else 	{	 			$matakuliah= array( 			'kode_mk'=>$this->input->post('kode_mk'), 			'nama_mk'=>$this->input->post('nama_mk'), 			'sks'=>$this->input->post('sks'), 			); 			 			$id=$this->t4_model->savematakuliah($matakuliah); 			$this->validation->id = $id; 			 			redirect('tugas4/lihat/matakuliah/add_success'); 		} 		 }  function view($jenis='mahasiswa',$id) { 	$data['title']='Lihat data'; 	 	$data['ambildb']=$this->t4_model->geteditdata($jenis,$id)->row_array(); 	$data['jenis']=$jenis; 	if ($jenis=='mahasiswa')  	{ 		$data['nrp']=$data['ambildb']['nrp']; 		$data['nama']=$data['ambildb']['nama']; 		$data['tanggal_lahir']=$data['ambildb']['tanggal_lahir']; 		$data['alamat']=$data['ambildb']['alamat']; 		$data['telepon']=$data['ambildb']['telepon']; 		$data['email']=$data['ambildb']['email']; 		$data['jenis_kelamin']=$data['ambildb']['jenis_kelamin']; 		 		$data['idno']=$data['ambildb']['nrp']; 		 	} else { 		$data['kode_mk']=$data['ambildb']['kode_mk']; 		$data['nama_mk']=$data['ambildb']['nama_mk']; 		$data['sks']=$data['ambildb']['sks']; 		 		$data['idno']=$data['ambildb']['kode_mk']; 	} 	 	$this->t4_template->display('t4_view',$data); }  function update($jenis='mahasiswa',$id) { 	$data['title']='Lihat data'; 	 	$data['ambildb']=$this->t4_model->	editdata($jenis,$id)->row_array(); 	$data['jenis']=$jenis; 	 	if ($jenis=='mahasiswa')  	{  		$this->_set_rules_mahasiswa(); 		$this->form_validation->set_rules('nrp', 'NRP', 'required|numeric'); 		 		if ($this->form_validation->run()==FALSE){ 			$data['nrp']=$data['ambildb']['nrp']; 			$data['nama']=$data['ambildb']['nama']; 			$data['tanggal_lahir']=$data['ambildb']['tanggal_lahir']; 			$data['alamat']=$data['ambildb']['alamat']; 			$data['telepon']=$data['ambildb']['telepon']; 			$data['email']=$data['ambildb']['email']; 			$data['jenis_kelamin']=$data['ambildb']['jenis_kelamin']; 			 			$this->t4_template->display('t4_update',$data); 		} else { 		 			$dataupdate= array( 				'nrp'=>$this->input->post('nrp'), 				'nama'=>$this->input->post('nama'), 				'tanggal_lahir'=>$this->input->post('tanggal_lahir'), 				'alamat'=>$this->input->post('alamat'), 				'telepon'=>$this->input->post('telepon'), 				'email'=>$this->input->post('email'), 				'jenis_kelamin'=>$this->input->post('jenis_kelamin')); 			 			$idno = $this->input->post('nrp_awal'); 			$this->t4_model->update($jenis,$idno,$dataupdate); 			redirect(base_url().'tugas4/lihat/'.$jenis.'/edit_success','refresh'); 			 		 		}		 	} else { 		 		$this->_set_rules_matakuliah(); 		$this->form_validation->set_rules('kode_mk', 'Kode Matakuliah', 'required'); 		 		if ($this->form_validation->run()==FALSE){ 			$data['kode_mk']=$data['ambildb']['kode_mk']; 			$data['nama_mk']=$data['ambildb']['nama_mk']; 			$data['sks']=$data['ambildb']['sks']; 			 			$this->t4_template->display('t4_update',$data); 		} else { 			$dataupdate= array( 				'kode_mk'=>$this->input->post('kode_mk'), 				'nama_mk'=>$this->input->post('nama_mk'), 				'sks'=>$this->input->post('sks'), 			); 			 			$idno = $this->input->post('kode_mk_awal'); 			 			$this->t4_model->update($jenis,$idno,$dataupdate); 			redirect(base_url().'tugas4/lihat/'.$jenis.'/edit_success','refresh'); 		} 	} 	 	 }  function delete($jenis,$id) { 	 	$this->t4_model->delete($jenis,$id); 	redirect(base_url().'tugas4/lihat/'.$jenis.'/delete_success','refresh'); }   function _set_rules_mahasiswa()  {	 	$this->load->database('alternate',TRUE); 	 	$active_group='alternate'; 	 	$this->form_validation->set_rules('nama', 'Nama Lengkap', 'required'); 	$this->form_validation->set_rules('tanggal_lahir', 'Tanggal Lahir', 'required'); 	$this->form_validation->set_rules('alamat', 'Alamat', 'required'); 	$this->form_validation->set_rules('telepon', 'Telepon', 'required|numeric'); 	$this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 	$this->form_validation->set_rules('jenis_kelamin', 'Jenis Kelamin', 'required'); }  function _set_rules_matakuliah() { 	$this->load->database('alternate2',TRUE); 	$active_group='alternate2'; 	 	 	$this->form_validation->set_rules('nama_mk', 'Nama Matakuliah', 'required'); 	$this->form_validation->set_rules('sks', 'SKS', 'required|numeric'); }  }