<?php

class AuditlogController extends Controller
{
	public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('login', 'error', 'logout', 'print'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('Index', 'getlist', 'create', 'save','Print'),
                'expression' => '$user->isMember()',
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function filters() {
        return array('accessControl'); // perform access control for CRUD operations
    }
    public function actionIndex()
	{
		$this->render('index');
	}

	public function actionGetlist() {
        $model = new AuditLog;
        $draw = $_POST['draw'];
        $offset = $_POST['start'];
        $limit = $_POST['length'];
        $columns = array(
            // datatable column index  => database column name
            0 => 't.id',
            1 => 'functionality',
            2 => 'year',
            3 => 'month',
            4 => 'changes',
            5 => 'product_det.gmc_code',
            6 => 'old_value',
            7 => 'new_value',
        );
        $filterColumn = $columns[$_POST['order'][0]['column']];
        $filterOrder = $_POST['order'][0]['dir'];
        $orderBy = $filterColumn . " " . $filterOrder;
        $condition = " 1=1 ";
        if (isset($_POST['search']['value']) && trim($_POST['search']['value']) != "") {
            $searchString = trim($_POST['search']['value']);
            $condition .= " AND ( functionality LIKE '%" . $searchString . "%'  ) OR   ( t.year LIKE '%" . $searchString . "%' )  OR (t.month  LIKE '%" . $searchString . "%' ) OR   (t.changes  LIKE '%" . $searchString . "%' )
                            OR (product_det.gmc_code LIKE '%" . $searchString . "%')";        
        }

        $count = $model->with('product_det')->count(array('condition' => $condition));
        $model = $model->with('product_det')->findAll(array('condition' => $condition, 'limit' => $limit, 'offset' => $offset, 'order' => $orderBy));
        $recordsTotal = $count;
        $recordsFiltered = $count;
        $data['draw'] = $draw;
        $data['recordsTotal'] = $recordsTotal;
        $data['recordsFiltered'] = $recordsFiltered;
        $rowData = array();
        foreach ($model as $i => $row) {
            $div = '';
          //  $title = $row['phone'] . " - " . $row["name"];
           // $div = '<button class="btn btn-primary btn-xs" title ="Edit" onclick="loadPopupModal(\'auditlog/create&id=' . $row['id'] . '\',\'' . $title . '\',\'sm\')"><i class="fa fa-tasks"></i></button>';
            //$div = '<button class="btn btn-primary btn-xs" title ="Edit" onclick="loadPopupModal(\'customer/createview&id=' . $row['id'] . '\',\'' . $title . '\',\'sm\')"><i class="fa fa-tasks"></i></button>';
            //$div .= '&nbsp; <button class="btn btn-danger btn-xs" title ="Delete" onclick="del(' . $row['id'] . ')"><i class="fa fa-trash"></i></button>';
            $rowData[] = array(($i + 1), $row["functionality"], $row["year"],$arr_months[(int)$row['month']], $row["changes"], $row['product_det']['gmc_code'], '<div class="btn-wrap text-center">' . $div . '</div>');
        }

        $data['data'] = $rowData;
        echo json_encode($data);
    }
}