File manager - Edit - /home/wwwroot/camplus.hk/master.camplus.hk/public_html/application/modules/subject/controllers/Subject.php
Back
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Subject extends MY_Controller { function __construct() { parent::__construct(); $this->load->model('Subject_model'); $this->load->model('Academic_year/Academic_year_model'); } function listing() { if($this->session->userdata("user_id")) { if(!$this->acl->hasPermission('MANAGE_SUBJECT')) show_error($this->lang->line('no_permission_error')); $data = $this->data; $data['header'] = TRUE; $data['sidebar'] = TRUE; $data['_view'] = "subject"; $data['quickbar'] = TRUE; $data['footer'] = TRUE; $data['top_menu'] = "common_top"; $data['sub_menu'] = "subject"; $data['pagetitle'] = $this->lang->line('subject'); $data['subjects'] = $this->Subject_model->getSubjects(); $data['venues'] = $this->Subject_model->getVenues(); $ac_years = $this->Academic_year_model->getAcademicYears(); $ay_id = $this->Academic_year_model->getCurrentAcademicYear()['id']; $data['ac_years'] = $ac_years; $data['ay_id']=$ay_id; $this->load->view('basetemplate', $data); } else { redirect("login"); } } function add() { if($this->session->userdata("user_id")) { if(!$this->acl->hasPermission('MANAGE_SUBJECT')) show_error($this->lang->line('no_permission_error')); $this->form_validation->set_rules('name_chi', 'Chinese Name', 'required'); $this->form_validation->set_rules('name_eng', 'English Name', 'required'); $this->form_validation->set_rules('shortname_chi', 'Chinese Short Name', 'required'); $this->form_validation->set_rules('shortname_eng', 'English Short Name', 'required'); if ($this->form_validation->run()==false){ $this->session->set_flashdata('error', $this->lang->line('action_performed_error')); } else { if($this->Subject_model->addSubject($this->input->post())) $this->session->set_flashdata('success', $this->lang->line('action_performed_success')); else $this->session->set_flashdata('error', $this->lang->line('action_performed_error')); } redirect($_SERVER['HTTP_REFERER']); } else { redirect("login"); } } function edit($id) { if($this->session->userdata("user_id")) { if(!$this->acl->hasPermission('MANAGE_SUBJECT')) show_error($this->lang->line('no_permission_error')); $this->form_validation->set_rules('name_chi', 'Chinese Name', 'required'); $this->form_validation->set_rules('name_eng', 'English Name', 'required'); $this->form_validation->set_rules('shortname_chi', 'Chinese Short Name', 'required'); $this->form_validation->set_rules('shortname_eng', 'English Short Name', 'required'); $data = $this->Subject_model->getSubject($id); if(empty($data)) show_error('No record found'); if ($this->form_validation->run()==false){ $this->session->set_flashdata('error', $this->lang->line('action_performed_error')); } else { if($this->Subject_model->editSubject($this->input->post(),$id)) $this->session->set_flashdata('success', $this->lang->line('action_performed_success')); else $this->session->set_flashdata('error', $this->lang->line('action_performed_error')); } redirect($_SERVER['HTTP_REFERER']); } else { redirect("login"); } } function delete($id=0){ if($this->session->userdata("user_id")) { if(!$this->acl->hasPermission('MANAGE_SUBJECT')) show_error($this->lang->line('no_permission_error')); $data = $this->Subject_model->getSubject($id); if(empty($data)) show_error('No record found'); if($this->Subject_model->deleteSubject($id)) $this->session->set_flashdata('success', $this->lang->line('action_performed_success')); else $this->session->set_flashdata('error', $this->lang->line('action_performed_error')); redirect($_SERVER['HTTP_REFERER']); } else { redirect("login"); } } function import() { if($this->session->userdata("user_id")) { if(!$this->acl->hasPermission('MANAGE_SUBJECT')) show_error($this->lang->line('no_permission_error')); $config['upload_path'] = './files/import'; $config['allowed_types'] = 'csv|xlsx|xls'; $config['max_size'] = 10000; $config['max_width'] = 10024; $config['max_height'] = 10008; $this->load->library('upload', $config); if ( ! $this->upload->do_upload('subjects')) { $error = array('error' => $this->upload->display_errors()); print_r( $error);die; $this->session->set_flashdata('error', $this->lang->line('action_performed_error')); redirect($_SERVER['HTTP_REFERER']); } else { $data = $this->upload->data(); $file = $data['full_path']; //load the excel library $this->load->library('excel'); //read file from path $objPHPExcel = PHPExcel_IOFactory::load($file); // new code //Table used to display the contents of the file //echo '<center><table style="width:50%;" border=1>'; // Get worksheet dimensions $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); // Loop through each row of the worksheet in turn $foundIds = array(); $exceldata=[]; for ($row = 1; $row <= $highestRow; $row++) { // Read a row of data into an array $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); $exceldata[]=$rowData[0]; // echo "<tr>"; //echoing every cell in the selected row for simplicity. You can save the data in database too. // foreach($rowData[0] as $k=>$v) // echo "<td>".$v."</td>"; // echo "</tr>"; } //echo '</table></center>'; if(!empty($exceldata)) { $this->Subject_model->importsubject($exceldata); $this->session->set_flashdata('success', $this->lang->line('action_performed_success')); } else { $this->session->set_flashdata('error', $this->lang->line('action_performed_error')); } redirect($_SERVER['HTTP_REFERER']); } }else { redirect("login"); } } function preview() { $config['upload_path'] = './files/tmp_upload'; $config['allowed_types'] = 'csv|xlsx|xls'; $config['max_size'] = 10000; $config['max_width'] = 10024; $config['max_height'] = 10008; $this->load->library('upload', $config); if ( ! $this->upload->do_upload('subjects')) { echo '<h3 style="color:red">Please select only these file types eg. csv,xls,xlsx etc. </h3>'; echo '<input id="duplicate" name="duplicate" type="hidden" value="1" />'; die; } else { $data = $this->upload->data(); $file = $data['full_path']; //load the excel library $this->load->library('excel'); //read file from path $objPHPExcel = PHPExcel_IOFactory::load($file); //Get worksheet dimensions $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); //Loop through each row of the worksheet in turn $foundIds = array(); $exceldata=[]; for($row = 1; $row < $highestRow; $row++) { // Read a row of data into an array $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); $exceldata[]=$rowData[0]; } $orders=$exceldata; $foundIds = array(); // check array duplicates eng_shortname, chi_shortname foreach($orders as $index => $order ) { $dup=0; if(isset($foundIds[$order['2']] ) ) { $dup=1; $orders[$foundIds[$order['2']]]['is_dupe'] = true; } else if(isset($foundIds[$order['3']] ) ) { $dup=1; $orders[$foundIds[$order['3']]]['is_dupe'] = true; } else { $orders[$index]['is_dupe'] = false; } if($dup == 1) $orders[$index]['is_dupe'] = true; $foundIds[$order['2']] = $index; $foundIds[$order['3']] = $index; } // print_r($orders); $duplicate=0; $required=0; $venue=""; $eng_shortname=""; $chi_shortname=""; //print_r($orders); echo '<table class="table table-striped table-bordered table-hover" id="data_table" data-always-visible="1">'; foreach ( $orders as $order ) { if($order[0]!="") { // check venue // $venue_data=$this->Subject_model->getVenue($order[4]); // if(empty($venue_data)) // { if('venue'!=$order[4]) // $venue .=$order[4] . ' '; // } $uniquedataEng=$this->Subject_model->checkUniqueEngShort($order['2']); // check unique eng_shortname $uniquedataChi=$this->Subject_model->checkUniqueChiShort($order['3']); // check unique chi_shortname if(isset($uniquedataEng)) { $eng_shortname .=$uniquedataEng['shortname_eng'] .' '; } if(isset($uniquedataChi)) { $chi_shortname .=$uniquedataChi['shortname_chi'] .' '; } // required fileds check if(empty($order[0]) || empty($order[1]) || empty($order[2]) || empty($order[3]) ) { $required++; } echo '<tr '; if($order['is_dupe']==1 ) { $duplicate++; echo " style='color:red'"; } echo '>'; $count = count($order); foreach ( $order as $value ) { if (--$count <= 0) { break; } echo '<td>'. $value .'</td>'; } echo '</tr>'; } } echo '</table>'; if($required > 0) echo '<h5 style="color:red">Requireds fields are not empty eng_name, chi_name, eng_shortname, chi_shortname. </h5>'; if(!empty($eng_shortname) || !empty($chi_shortname)) echo '<h5 style="color:red">This is available in DB: '.$eng_shortname. ' '. $chi_shortname .'</h5>'; // if(!empty(trim($venue))) // echo '<h5 style="color:red">This venue is not avialable in DB: '.$venue .'</h5>' ; if($duplicate > 0) echo '<h5 style="color:red">Imported values must be unique</h5>'; if($required == 0 && $duplicate== 0 && $venue=="" && $eng_shortname=="" && $chi_shortname=="") echo '<input id="duplicate" name="duplicate" type="hidden" value="0" />'; else echo '<input id="duplicate" name="duplicate" type="hidden" value="1" />'; // echo '<input name="subjectsarray" type="hidden" value='.htmlspecialchars( serialize( $orders ) ).' />'; } } function downloadFormat() { if($this->session->userdata("user_id")) { $filename="subject_csv_format.xls"; //load our new PHPExcel library $this->load->library('excel'); //activate worksheet number 1 $this->excel->setActiveSheetIndex(0); //name the worksheet $this->excel->getActiveSheet()->setTitle('Subjects list'); $subjects = $this->Subject_model->getSubjects_download(); $outarray = array(); for($i=0;$i<count($subjects);$i++) { $newarray=$subjects[$i]; // unset($newarray['id']); if($i==0) { $headerarr=array("name_eng"=>"English Name","name_chi"=>"Chinese Name","shortname_eng"=>"English Short Name","shortname_chi"=>"Chinese Short Name","venue"=>"Venue","status"=>"Status"); $outarray[]=$headerarr; $outarray[]=$newarray; } else { // unset($newarray['id']); $outarray[]=$newarray; } } // read data to active sheet $this->excel->getActiveSheet()->fromArray($outarray); /* header('Content-Type: application/vnd.ms-excel'); //mime type header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name header('Cache-Control: max-age=0'); //no cache */ header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=".$filename); $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); $objWriter->save('php://output'); } else { redirect("login"); } } function get($id=0) { if($this->session->userdata("user_id")) { $data = $this->Subject_model->getSubject($id); echo json_encode($data); } else { redirect("login"); } } function getGrade($id, $ay_id) { if($this->session->userdata("user_id")) { $result = $this->db->get_where('tb_subject_grade',array('subject_id'=>$id, 'ay_id'=>$ay_id))->row_array(); if(!empty($result)) echo json_encode(['data'=>$result['grade']]); else echo json_encode(['data'=>'']); } else { redirect("login"); } } function saveGrade($id,$ay_id) { if($this->session->userdata("user_id")) { $exist = $this->db->get_where('tb_subject_grade',['subject_id'=>$id,'ay_id'=>$ay_id])->row_array(); if($exist) { $result = $this->db->update('tb_subject_grade',array('grade'=>json_encode($this->input->post('data'))),array('subject_id'=>$id,'ay_id'=>$ay_id)); } else { $result = $this->db->insert('tb_subject_grade',array('grade'=>json_encode($this->input->post('data')),'subject_id'=>$id,'ay_id'=>$ay_id)); } echo json_encode($result); } else { redirect("login"); } } }
| ver. 1.4 |
Github
|
.
| PHP 7.2.34 | Generation time: 0.28 |
proxy
|
phpinfo
|
Settings