Advertisement
virbo

Yii2 DataController for Datatables

Feb 20th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.11 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use Yii;
  6. use yii\web\Controller;
  7. use yii\filters\VerbFilter;
  8. use yii\web\NotFoundHttpException;
  9. use app\models\Soapmodel;
  10. use app\models\Setting;
  11. use yii\helpers\Json;
  12. use app\models\TmpMhs;
  13. use app\models\Prodi;
  14. use app\models\TmpKelas;
  15.  
  16. /**
  17.  * Data Controller
  18.  * @copyright   : Yusuf Ayuba
  19.  * @licensi     : Comersial
  20.  * @year        : 2016
  21.  * @since       : 1.0.0
  22.  */
  23. class DataController extends Controller
  24. {
  25.     protected $wsdl;
  26.     protected $token;
  27.     protected $session;
  28.     protected $xx;
  29.  
  30.     const ERROR_SYNC = 0;
  31.     //const STATUS_ACTIVE = 1;
  32.  
  33.     public function behaviors()
  34.     {
  35.         return [
  36.             'verbs' => [
  37.                 'class' => VerbFilter::className(),
  38.                 'actions' => [
  39.                     //'sync-mhs' => ['post'],
  40.                 ],
  41.             ],
  42.         ];
  43.     }
  44.  
  45.     public function init()
  46.     {
  47.         $this->session = Yii::$app->session;
  48.         $this->wsdl = new Soapmodel();
  49.         $setting = Setting::find()->one();
  50.         //$this->token = $this->wsdl->token($setting->ws_user,$setting->ws_password);
  51.         $this->token = Yii::$app->session['siap']['ws_token'];
  52.     }
  53.  
  54.     /*
  55.         Fungsi Data untuk menampilkan data mahasiswa secara ajax ke Datatable
  56.     */
  57.     public function actionMhs()
  58.     {
  59.         $temp_cols = ['id_pd','nipd','nm_pd','tgl_lahir',
  60.                         'nm_lemb','nm_agama','mulai_smt','nm_stat_mhs'];
  61.  
  62.         $i=0;
  63.         foreach ($temp_cols as $item) {
  64.             $column[$i] = $item;
  65.             $i++;
  66.         }
  67.  
  68.         $search = Yii::$app->request->post('search');
  69.         $sSearch = trim($search['value']);
  70.  
  71.         $iStart = Yii::$app->request->post('start');
  72.         $iLength = Yii::$app->request->post('length');
  73.         $iOrder = Yii::$app->request->post('order');
  74.  
  75.         $limit = $iLength?$iLength : Yii::$app->params['limit'];
  76.         $offset = $iStart?$iStart : Yii::$app->params['offset'];
  77.         $order = $iOrder?$column[$iOrder[0]['column']].' '.$iOrder[0]['dir']:'mulai_smt DESC';
  78.  
  79.         $temp_total = $this->wsdl->count_all(
  80.                 $this->token,
  81.                 Yii::$app->params['mahasiswa_pt'],
  82.                 Yii::$app->params['filter']
  83.             );
  84.         $totalData = $temp_total['result'];
  85.         $totalFiltered = $totalData;
  86.  
  87.         $temp_rec = $this->wsdl->list_mhs(
  88.                 $this->token,
  89.                 Yii::$app->params['filter'],
  90.                 $order,
  91.                 $limit,
  92.                 $offset
  93.             );
  94.         //var_dump($temp_rec);
  95.         if (!empty($sSearch)) {
  96.             $filter = "((LOWER(nm_pd) LIKE '%".strtolower($sSearch)."%') OR
  97.                                (LOWER(jk) LIKE '%".strtolower($sSearch)."%') OR
  98.                                (LOWER(nm_lemb) LIKE '%".strtolower($sSearch)."%') OR
  99.                                (LOWER(nm_agama) LIKE '%".strtolower($sSearch)."%') OR
  100.                                (nipd LIKE '%".$sSearch."%') OR
  101.                                (mulai_smt LIKE '%".$sSearch."%') OR
  102.                                (LOWER(ket_keluar) LIKE '%".strtolower($sSearch)."%')
  103.                            )";
  104.             $temp_rec = $this->wsdl->list_mhs(
  105.                     $this->token,
  106.                     $filter,
  107.                     $order,
  108.                     $limit,
  109.                     $offset
  110.                 );
  111.             //$__total = $this->wsdl->count_all(
  112.             //        $this->token,
  113.             //        Yii::$app->params['mahasiswa_pt'],
  114.             //        $filter
  115.             //    );
  116.             $__total = count($temp_rec['result']);
  117.             //$totalFiltered = $__total['result'];
  118.             $totalFiltered = $__total;
  119.         }
  120.  
  121.         $temp_error_code = $temp_rec['error_code'];
  122.         $temp_error_desc = $temp_rec['error_desc'];
  123.         if (($temp_error_code==0) && ($temp_error_desc=='')) {
  124.             $temp_data = [];
  125.             $i=0;
  126.             foreach ($temp_rec['result'] as $key) {
  127.                 $temp_data[] = [
  128.                     //++$i+$this->offset,
  129.                     ++$iStart+Yii::$app->params['offset'],
  130.                     $key['nipd'],
  131.                     $key['nm_pd'],
  132.                     $key['jk'],
  133.                     date('d-m-Y',strtotime($key['tgl_lahir'])),
  134.                     $key['nm_lemb'],
  135.                     $key['nm_agama'],
  136.                     substr($key['mulai_smt'], 0,4),
  137.                     $key['nm_stat_mhs']
  138.                 ];
  139.             }
  140.             $temp_output = [
  141.                 'draw' => intval(Yii::$app->request->get('draw')),
  142.                 'recordsTotal' => intval( $totalData ),
  143.                 'recordsFiltered' => intval( $totalFiltered ),
  144.                 'data' => $temp_data
  145.             ];
  146.             echo Json::encode($temp_output);
  147.         }
  148.     }
  149.  
  150.     /*
  151.         Fungsi Data untuk menampilkan data Kelas Perkuliahan secara ajax ke Datatable
  152.     */
  153.     public function actionKls()
  154.     {
  155.         $temp_cols = ['id_kls','nm_lemb','id_smt',
  156.                     'kode_mk','nm_mk','nm_kls',
  157.                     'sks_mk','jmlmhs','dosen'];
  158.  
  159.         $i=0;
  160.         foreach ($temp_cols as $item) {
  161.             $column[$i] = $item;
  162.             $i++;
  163.         }
  164.  
  165.         $search = Yii::$app->request->post('search');
  166.         $sSearch = trim($search['value']);
  167.        
  168.  
  169.         $iStart = Yii::$app->request->post('start');
  170.         $iLength = Yii::$app->request->post('length');
  171.         $iOrder = Yii::$app->request->post('order');
  172.  
  173.         $limit = $iLength?$iLength:Yii::$app->params['limit'];
  174.         $offset = $iStart?$iStart:Yii::$app->params['offset'];
  175.         $order = $iOrder?$column[$iOrder[0]['column']].' '.$iOrder[0]['dir']:'nm_smt DESC';
  176.  
  177.         $temp_total = $this->wsdl->count_all(
  178.                 $this->token,
  179.                 Yii::$app->params['kelas'],
  180.                 Yii::$app->params['filter']
  181.             );
  182.         $totalData = $temp_total['result'];
  183.         $totalFiltered = $totalData;
  184.  
  185.         $temp_rec = $this->wsdl->list_kelas(
  186.                 $this->token,
  187.                 Yii::$app->params['filter'],
  188.                 $order,
  189.                 $limit,
  190.                 $offset
  191.             );
  192.         if (!empty($sSearch)) {
  193.             /*
  194.                 Filter di field sks_mk = ambigu
  195.             */
  196.             $filter = "((LOWER(nm_kls) LIKE '%".strtolower($sSearch)."%') OR
  197.                                (LOWER(kode_mk) LIKE '%".strtolower($sSearch)."%') OR
  198.                                (LOWER(nm_mk) LIKE '%".strtolower($sSearch)."%') OR
  199.                                (nm_smt LIKE '%".$sSearch."%') OR
  200.                                (LOWER(nm_sdm) LIKE '%".strtolower($sSearch)."%')
  201.                            )";
  202.             $temp_rec = $this->wsdl->list_kelas(
  203.                     $this->token,
  204.                     $filter,
  205.                     $order,
  206.                     $limit,
  207.                     $offset
  208.                 );
  209.             //$__total = $this->wsdl->count_all(
  210.             //        $this->token,
  211.             //        Yii::$app->params['mahasiswa_pt'],
  212.             //        $filter
  213.             //    );
  214.             $__total = count($temp_rec['result']);
  215.             //$totalFiltered = $__total['result'];
  216.             $totalFiltered = $__total;
  217.         }
  218.  
  219.         $temp_error_code = $temp_rec['error_code'];
  220.         $temp_error_desc = $temp_rec['error_desc'];
  221.         if (($temp_error_code==0) && ($temp_error_desc=='')) {
  222.             $temp_data = [];
  223.             $i=0;
  224.             foreach ($temp_rec['result'] as $key) {
  225.                 $temp_data[] = [
  226.                     //++$i+$this->offset,
  227.                     ++$iStart+Yii::$app->params['offset'],
  228.                     $key['nm_lemb'],
  229.                     $key['nm_smt'],
  230.                     $key['kode_mk'],
  231.                     $key['nm_mk'],
  232.                     $key['nm_kls'],
  233.                     $key['sks_mk'],
  234.                     $key['jmlmhs'],
  235.                     $key['dosen']
  236.                 ];
  237.             }
  238.             $temp_output = [
  239.                 'draw' => intval(Yii::$app->request->get('draw')),
  240.                 'recordsTotal' => intval( $totalData ),
  241.                 'recordsFiltered' => intval( $totalFiltered ),
  242.                 'data' => $temp_data
  243.             ];
  244.             echo Json::encode($temp_output);
  245.         }
  246.     }
  247.  
  248.     /*
  249.         Fungsi Data untuk menampilkan data Aktifitas Kuliah Mahasiswa secara ajax ke Datatable
  250.     */
  251.     public function actionAkm()
  252.     {
  253.         $temp_cols = ['id_pd','nipd','nm_pd','nm_lemb','nm_smt',
  254.                         'mulai_smt','ips','ipk','sks_smt',
  255.                         'sks_total','nm_stat_mhs'];
  256.  
  257.         $i=0;
  258.         foreach ($temp_cols as $item) {
  259.             $column[$i] = $item;
  260.             $i++;
  261.         }
  262.  
  263.         $search = Yii::$app->request->post('search');
  264.         $sSearch = trim($search['value']);
  265.  
  266.         $iStart = Yii::$app->request->post('start');
  267.         $iLength = Yii::$app->request->post('length');
  268.         $iOrder = Yii::$app->request->post('order');
  269.  
  270.         $limit = $iLength?$iLength : Yii::$app->params['limit'];
  271.         $offset = $iStart?$iStart : Yii::$app->params['offset'];
  272.         $order = $iOrder?$column[$iOrder[0]['column']].' '.$iOrder[0]['dir']:'id_smt DESC';
  273.         //echo $order;
  274.  
  275.         $temp_total = $this->wsdl->count_all(
  276.                 $this->token,
  277.                 Yii::$app->params['mahasiswa_pt'],
  278.                 Yii::$app->params['filter']
  279.             );
  280.         $totalData = $temp_total['result'];
  281.         $totalFiltered = $totalData;
  282.  
  283.         $temp_rec = $this->wsdl->list_kuliah(
  284.                 $this->token,
  285.                 Yii::$app->params['filter'],
  286.                 $order,
  287.                 $limit,
  288.                 $offset
  289.             );
  290.         //var_dump($temp_rec['result']);
  291.         if (!empty($sSearch)) {
  292.             $filter = "((LOWER(nm_pd) LIKE '%".strtolower($sSearch)."%') OR
  293.                                (LOWER(nm_lemb) LIKE '%".strtolower($sSearch)."%') OR
  294.                                (LOWER(nm_smt) LIKE '%".strtolower($sSearch)."%') OR
  295.                                (nipd LIKE '%".$sSearch."%') OR
  296.                                (mulai_smt LIKE '%".$sSearch."%') OR
  297.                                (LOWER(nm_stat_mhs) LIKE '%".strtolower($sSearch)."%')
  298.                            )";
  299.             $temp_rec = $this->wsdl->list_kuliah(
  300.                     $this->token,
  301.                     $filter,
  302.                     $order,
  303.                     $limit,
  304.                     $offset
  305.                 );
  306.             $__total = count($temp_rec['result']);
  307.             //$totalFiltered = $__total['result'];
  308.             $totalFiltered = $__total;
  309.         }
  310.  
  311.         $temp_error_code = $temp_rec['error_code'];
  312.         $temp_error_desc = $temp_rec['error_desc'];
  313.         if (($temp_error_code==0) && ($temp_error_desc=='')) {
  314.             $temp_data = [];
  315.             $i=0;
  316.             foreach ($temp_rec['result'] as $key) {
  317.                 $temp_data[] = [
  318.                     //++$i+$this->offset,
  319.                     ++$iStart+Yii::$app->params['offset'],
  320.                     $key['nipd'],
  321.                     $key['nm_pd'],
  322.                     $key['nm_lemb'],
  323.                     $key['nm_smt'],
  324.                     substr($key['mulai_smt'], 0,4),
  325.                     round($key['ips'],2),
  326.                     round($key['ipk'],2),
  327.                     $key['sks_smt'],
  328.                     $key['sks_total'],
  329.                     $key['nm_stat_mhs']
  330.                 ];
  331.             }
  332.             $temp_output = [
  333.                 'draw' => intval(Yii::$app->request->get('draw')),
  334.                 'recordsTotal' => intval( $totalData ),
  335.                 'recordsFiltered' => intval( $totalFiltered ),
  336.                 'data' => $temp_data
  337.             ];
  338.             echo Json::encode($temp_output);
  339.         }
  340.     }
  341.  
  342.     /*
  343.         Fungsi Data untuk menampilkan data Mahasiswa Lulus/DO secara ajax ke Datatable
  344.     */
  345.     public function actionLulus()
  346.     {
  347.         $temp_cols = ['id_pd','nipd','nm_pd','nm_lemb',
  348.                         'mulai_smt','ket_keluar','tgl_keluar','ket'];
  349.  
  350.         $i=0;
  351.         foreach ($temp_cols as $item) {
  352.             $column[$i] = $item;
  353.             $i++;
  354.         }
  355.  
  356.         $search = Yii::$app->request->post('search');
  357.         $sSearch = trim($search['value']);
  358.  
  359.         $iStart = Yii::$app->request->post('start');
  360.         $iLength = Yii::$app->request->post('length');
  361.         $iOrder = Yii::$app->request->post('order');
  362.  
  363.         $limit = $iLength?$iLength : Yii::$app->params['limit'];
  364.         $offset = $iStart?$iStart : Yii::$app->params['offset'];
  365.         $order = $iOrder?$column[$iOrder[0]['column']].' '.$iOrder[0]['dir']:'id_smt DESC';
  366.  
  367.         $temp_total = $this->wsdl->count_all(
  368.                 $this->token,
  369.                 Yii::$app->params['mahasiswa_pt'],
  370.                 Yii::$app->params['filter']
  371.             );
  372.         $totalData = $temp_total['result'];
  373.         $totalFiltered = $totalData;
  374.  
  375.         $temp_rec = $this->wsdl->list_lulus(
  376.                 $this->token,
  377.                 Yii::$app->params['filter'],
  378.                 $order,
  379.                 $limit,
  380.                 $offset
  381.             );
  382.         //var_dump($temp_rec['result']);
  383.         if (!empty($sSearch)) {
  384.             $filter = "((LOWER(nm_pd) LIKE '%".strtolower($sSearch)."%') OR
  385.                                (LOWER(s.nm_lemb) LIKE '%".strtolower($sSearch)."%') OR
  386.                                (nipd LIKE '%".$sSearch."%') OR
  387.                                (mulai_smt LIKE '%".$sSearch."%') OR
  388.                                (LOWER(ket_keluar) LIKE '%".strtolower($sSearch)."%')
  389.                            )";
  390.             $temp_rec = $this->wsdl->list_lulus(
  391.                     $this->token,
  392.                     $filter,
  393.                     $order,
  394.                     $limit,
  395.                     $offset
  396.                 );
  397.             $__total = count($temp_rec['result']);
  398.             //$totalFiltered = $__total['result'];
  399.             $totalFiltered = $__total;
  400.         }
  401.  
  402.         $temp_error_code = $temp_rec['error_code'];
  403.         $temp_error_desc = $temp_rec['error_desc'];
  404.         if (($temp_error_code==0) && ($temp_error_desc=='')) {
  405.             $temp_data = [];
  406.             $i=0;
  407.             foreach ($temp_rec['result'] as $key) {
  408.                 $temp_data[] = [
  409.                     ++$iStart+Yii::$app->params['offset'],
  410.                     $key['nipd'],
  411.                     $key['nm_pd'],
  412.                     $key['nm_lemb'],
  413.                     substr($key['mulai_smt'], 0,4),
  414.                     $key['ket_keluar'],
  415.                     date('d-m-Y',strtotime($key['tgl_keluar'])),
  416.                     $key['ket']
  417.                 ];
  418.             }
  419.             $temp_output = [
  420.                 'draw' => intval(Yii::$app->request->get('draw')),
  421.                 'recordsTotal' => intval( $totalData ),
  422.                 'recordsFiltered' => intval( $totalFiltered ),
  423.                 'data' => $temp_data
  424.             ];
  425.             echo Json::encode($temp_output);
  426.         }
  427.     }
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement