<?php

/*
 * *************************************************
 * Copyright (C) 2020 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of @controllerClass@
 *
 * @author andhy
 */
class @controllerClass@ extends MY_Controller {

    public function index($pages = 1) {
        if (!$this->check_access(__METHOD__)) {
            redirect($this->restriction);
        }
        //load needed helper
        Ci::helpers(['form', 'pagination']);
        
        // initialize data
        $data = [
            'model' => Ci::model('@modelClass@'),
            'order' => Ci::get('order'),
            'pages' => $pages,
            'per_page' => Ci::get('per_page'),
            'base_url' => base_url('@controllerUrl@/index'),
            'keyword' => Ci::get('keyword')
        ];
        
        // run pagination system
        $pagination = config_pagination($data);
        
        // send all parameter to view
        Ci::view(__METHOD__, $data + $pagination);
    }

    public function create() {
        if (!$this->check_access(__METHOD__)) {
            redirect($this->restriction);
        }
        
        //load main model
        $model = Ci::model('@modelClass@');
        
        //initialize form field
        $model->data([
@fieldCreate@
        ]);
        
        // run create procedure
        if ($model->validate() && $model->save()) {
            $this->session->set_flashdata('success', lang('add_success', ['{name}' => '']));
            redirect('@controllerUrl@/index');
        } else {
            Ci::helper('form');
            if (($error = validation_errors())) {
                $this->session->set_flashdata('danger', $error);
            }
            Ci::view(__METHOD__, ['model' => $model]);
        }
    }

    public function update($id = null) {
        if (!$this->check_access(__METHOD__)) {
            redirect($this->restriction);
        }
        
        //load main model
        $model = Ci::model('@modelClass@');
        
        // find data by id and assign data to model
        if (!$model->fill($model->find($id, true))) {
            $this->session->set_flashdata('danger', lang('not_found'));
            redirect('@controllerUrl@/index'); // if data not found
        }
        
         //initialize form field and replacing assigned data with post data
        $model->data([
@fieldUpdate@
        ]);
        
        // run update procedure
        if ($model->validate() && $model->save()) {
            $this->session->set_flashdata('success', lang('update_success', ['{name}' => '']));
            redirect('@controllerUrl@/index');
        } else {
            Ci::helper('form');
            if (($error = validation_errors())) {
                $this->session->set_flashdata('danger', $error);
            }
            Ci::view(__METHOD__, ['model' => $model]);
        }
    }

    public function delete($id = null) {
        if (!$this->check_access(__METHOD__)) {
            redirect($this->restriction);
        }
        
        //load main model
        $model = Ci::model('@modelClass@');
        
        // find data by id and assign data to model
        if (!$model->fill($model->find($id, true))) {
            $this->session->set_flashdata('danger', lang('not_found'));
            redirect('@controllerUrl@/index'); // if data not found
        }
        
        // deleting record by assigned data
        $model->delete();
        $this->session->set_flashdata('success', lang('delete_success', ['{name}' => '']));
        redirect('@controllerUrl@/index');
    }

}