⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to make callback_field and callback_after_insert function working?



Rachma

Rachma
  • profile picture
  • Member

Posted 22 December 2014 - 13:43 PM

I need to join 2 tables in a form, while the child have multiple value. I've trying using callback field, it's working. But I can't catch the records even I use callback_after_insert. Please help me. I'm so confused..

Can anyone help me solve this case?

 

This is my code:

public function tenaga_kerja()
    {
        $c = new grocery_crud();
        $c->set_table('tenaga_kerja');
        $c->where('id_perusahaan', $this->session->userdata('id_perusahaan'));
        $this->load->config('grocery_crud');
        $this->config->set_item('grocery_crud_file_upload_allow_file_types', 'pdf');
        $c->fields('nama_lengkap', 'status', 'jabatan', 'jenjang_pendidikan', 'jurusan_pendidikan', 'file_ijazah', 'sertifikasi');
        $c->set_crud_url_path(base_url(strtolower(__CLASS__ . "/" . __FUNCTION__)), base_url(strtolower(__CLASS__ . "/data_tenaga_kerja")));

        //field type
        $c->field_type('id_perusahaan', 'hidden', $this->session->userdata('id_perusahaan'));
        $c->field_type('status', 'enum', array('Permanen', 'Non Permanen'));
        $c->set_field_upload('file_ijazah', 'assets/uploads/file_ijazah_tenaga_ahli');
        $c->field_type('id_permohonan', 'hidden', $this->session->userdata('id_permohonan'));

        // set relation
        $c->set_relation('jenjang_pendidikan', 'ref_jenjang_pendidikan', 'jenjang_pendidikan', null, 'id_jenjang_pendidikan');

	$c->callback_field('sertifikasi', array($this, 'callback_sertifikasi'));
		
	$c->callback_after_insert(array($this,'callback_before_insert_or_update'));
		

        $output = $c->render();
        $this->logs();

        if ($c->getState() != 'list') {
            $this->data_tenaga_kerja($output);
        } else {
            return $output;
        }

    }


    public function callback_before_insert_or_update($post_array, $primary_key){
	
		//echo '<script> alert('.json_encode($post_array['judul_pelatihan'][1]).'); </script>';
		$juduls = $post_array['judul_pelatihan'];
		$nomors = $post_array['nomor_sertifikat'];
		
			$count = count($juduls);
			$data = array();
			for($i=0; $i<$count; $i++) {
				$data[$i] = array(
					'id_tenaga_kerja' => $primary_key,
					'id_perusahaan' => $this->session->userdata('id_perusahaan'),
					'judul_pelatihan' => $juduls[$i],
					'nomor_sertifikat' => $nomors[$i],
					'file_sertifikat' => NULL
					);
			}		
				$this->db->insert_batch('sertifikasi_tenaga_kerja', $data);				
				return TRUE;
			
	}
	
    public function callback_sertifikasi(){
		$output = '<script> $(document).ready(function (){ var counter = 2;	var limit = 11;	';
		$output .= '$("#btn_add").click(function(){';
		$output .= 'if (counter == limit){ alert("Anda terlalu banyak menambahkan 10 data!");	} ';
		$output .= 'else {';
		$output .= 'var clone1 = $("#cloneObject1").clone(); ';
		$output .= 'clone1.attr("id","cloneObject" +counter); clone1.empty(); ';
		$output .= 'clone1.append("<td id='td"+counter+"_1'></td><td id='td"+counter+"_2'></td><td id='td"+counter+"_3'></td><td id='td"+counter+"_4'></td>"); ';
		$output .= 'clone1.appendTo("#cloneMother"); ';
		$output .= 'var clone2 = $("#judul_pelatihan1").clone().val(""); clone2.attr("id","judul_pelatihan" +counter); ';
		$output .= 'clone2.appendTo("#td"+counter+"_1"); ';
		$output .= 'var clone3 = $("#nomor_sertifikat1").clone().val(""); clone3.attr("id","nomor_sertifikat" + counter); ';
		$output .= 'clone3.appendTo("#td"+counter+"_2"); ';
		$output .= 'var clone4 = $("#file_sertifikat1").clone().val(""); clone4.attr("id","file_sertifikat" + counter); ';
		$output .= 'clone4.appendTo("#td"+counter+"_3"); ';
		$output .= 'var clone5 = $("#btn_add").clone(); clone5.attr({id: "btn_del"+counter, name:"btn_del", class:"input-del"}); ';
		$output .= 'clone5.appendTo("#td"+counter+"_4"); counter++; ';
		$output .= '} }); ';
		$output .= '';
		$output .= '}); </script>';
		$output .= '<div class="div-sertifikasi"><table class="tabel-sertifikasi"><thead><tr>';
		$output .= '<th class="title-sertifikat">Judul Pelatihan</th>';
		$output .= '<th class="title-sertifikat">Nomor Sertifikat</th>';
		$output .= '<th class="title-sertifikat">File Sertifikat</th>';
		$output .= '<th></th>';
		$output .= '</tr></thead>';
		$output .= '<tbody id="cloneMother"><tr id="cloneObject1">';
		$output .= '<td id="td1_1"><input class="input-sertifikat" type="text" name="judul_pelatihan[]" placeholder="Judul.." id="judul_pelatihan1" required /></td>';
		$output .= '<td id="td1_2"><input class="input-sertifikat" type="text" name="nomor_sertifikat[]" placeholder="Nomor.." id="nomor_sertifikat1" /></td>';
		$output .= '<td id="td1_3"><input class="input-sertifikat" type="file" name="file_sertifikat[]" placeholder="File" id="file_sertifikat1" /></td>';
		$output .= '<td id="td1_4"><input class="input-add" type="button" name="btn_add" id="btn_add" /></td>';
		$output .= '</tr></tbody>';
		$output .= '</table></div>';

		return $output;
    }

function data_tenaga_kerja(){ //multigrid
        $this->config->load('grocery_crud');
        $this->config->set_item('grocery_crud_dialog_forms', true);
        $this->config->set_item('grocery_crud_default_per_page', 10);

        $output1 = $this->tenaga_kerja();
        $output2 = $this->sertifikasi_tenaga_kerja();
        $output3 = $this->jumlah_tenaga_kerja();

        $js_files = $output1->js_files + $output2->js_files + $output3->js_files;
        $css_files = $output1->css_files + $output2->css_files + $output3->css_files;

        $output = '<h2>1. Daftar Tenaga Kerja Ahli sesuai Bidang Usaha yang dimohon /  Quality
		Assurance / Quality Control (QA/QC)</h2><span style="color:red; font-size:12px">(dilampirkan ijazah terakhir, sertifikat kompetensi, dan riwayat pekerjaan untuk setiap tenaga ahli).</span>' . $output1->output . '
		<span style="color:red; font-size:12px">Keterangan: 
		<br/>- Pelatihan yang dicantumkan hanya pelatihan yang berhubungan dengan posisi atau jabatan.  
		<br/>- Untuk warga negara asing, wajib mencantumkan nomor IMTA pada kolom KETERANGAN.
		<br/></span>
		<hr/><h2>2. Sertifikasi Tenaga Kerja</h2>' . $output2->output.'<hr/><h2>3. Jumlah Tenaga Kerja</h2>' . $output3->output;

        $level = $this->session->userdata('level');
        if ($level != NULL) {
            if ($level == 1) {
                $this->load->view('level1/skt_tabel', (object)array(
                    'js_files' => $js_files,
                    'css_files' => $css_files,
                    'output' => $output
                ));
            } else {
                redirect('hal/dashboard');
            }
        } elseif ($level == NULL) {
            redirect('hal/logout');
        }
    }
 
 
[attachment=887:Untitled-1.png][attachment=884:Untitled-2.png]

tomomiha

tomomiha
  • profile picture
  • Member

Posted 09 February 2017 - 17:12 PM

After insert you have data in your database, than just pick it up with query and use it whatever you want.

Session data might not work in callback functions, I think.