<?php

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

/**
 * MY_DB_mysqli_driver
 *
 */
class MY_DB_mysqli_driver extends CI_DB_mysqli_driver {

    function add_filters_order_and_pagination($limit_offset_order_and_filters) {
        if (isset($limit_offset_order_and_filters['filters'])) {
            foreach ($limit_offset_order_and_filters['filters'] as $filter) {
                if ($filter['type'] !== 'ignore') {
                    if ($filter['value'] === 'null') {
                        $this->where($filter['key'] . ' is NULL', NULL, FALSE);
                    } else {
                        if ($filter['value'] === 'notnull') {
                            $this->where($filter['key'] . ' is NOT NULL', NULL, FALSE);
                        } else {
                            switch ($filter['type']) {
                                case 'like':
                                    $this->like($filter['key'], $filter['value']);
                                    break;
                                case 'group-like':
                                    $this->group_start();
                                    foreach ($filter['values'] as $value) {
                                        $this->like($filter['key'], $value);
                                    }
                                    $this->group_end();
                                    break;
                                case 'gt':
                                    $this->where($filter['key'] . ' >', $filter['value']);
                                    break;
                                case 'lt':
                                    $this->where($filter['key'] . ' <', $filter['value']);
                                    break;
                                case 'egt':
                                    $this->where($filter['key'] . ' >=', $filter['value']);
                                    break;
                                case 'elt':
                                    $this->where($filter['key'] . ' <=', $filter['value']);
                                    break;
                                case 'in':
                                    $this->where_in($filter['key'], explode(',', $filter['value']));
                                    break;
                                case 'date':
                                    $this->where('date(' . $filter['key'] . ')', $filter['value']);
                                    break;
                                case 'date-egt':
                                    $this->where('date(' . $filter['key'] . ') >=', $filter['value']);
                                    break;
                                case 'date-elt':
                                    $this->where('date(' . $filter['key'] . ') <=', $filter['value']);
                                    break;
                                case 'date-gt':
                                    $this->where('date(' . $filter['key'] . ') >', $filter['value']);
                                    break;
                                case 'date-lt':
                                    $this->where('date(' . $filter['key'] . ') <', $filter['value']);
                                    break;
                                case 'age':
                                    $this->where('TIMESTAMPDIFF(YEAR, DATE(' . $filter['key'] . '), CURRENT_DATE) =', $filter['value']);
                                    break;
                                case 'year-month':
                                    $aux = explode('-', $filter['value']);
                                    if (count($aux) === 2) {
                                        $this->where('YEAR(' . $filter['key'] . ')', $aux[0]);
                                        $this->where('MONTH(' . $filter['key'] . ')', $aux[1]);
                                    }
                                    break;
//                                case 'notnull':
//                                    $this->where($filter['key'] . ' is NOT NULL', NULL, FALSE);
//                                    break;
                                default:
                                    if ($filter['key'] !== NULL) {
                                        $this->where($filter['key'], $filter['value']);
                                    } else {
                                        $this->where($filter['value']);
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
        }
        if (isset($limit_offset_order_and_filters['orders'])) {
            foreach ($limit_offset_order_and_filters['orders'] as $order) {
                $this->order_by($order['key'], $order['value']);
            }
        }
        if (isset($limit_offset_order_and_filters['limit'])) {
            $this->limit($limit_offset_order_and_filters['limit'], $limit_offset_order_and_filters['offset']);
        }
    }

    function add_filters($filters) {
        foreach ($filters['filters'] as $filter) {
            if ($filter['type'] !== 'ignore') {
                if ($filter['value'] === 'null') {
                    $this->where($filter['key'] . ' is NULL', NULL, FALSE);
                } else {
                    if ($filter['value'] === 'notnull') {
                        $this->where($filter['key'] . ' is NOT NULL', NULL, FALSE);
                    } else {
                        switch ($filter['type']) {
                            case 'like':
                                $this->like($filter['key'], $filter['value']);
                                break;
                            case 'group-like':
                                $this->group_start();
                                foreach ($filter['values'] as $value) {
                                    $this->like($filter['key'], $value);
                                }
                                $this->group_end();
                                break;
                            case 'gt':
                                $this->where($filter['key'] . ' >', $filter['value']);
                                break;
                            case 'lt':
                                $this->where($filter['key'] . ' <', $filter['value']);
                                break;
                            case 'egt':
                                $this->where($filter['key'] . ' >=', $filter['value']);
                                break;
                            case 'elt':
                                $this->where($filter['key'] . ' <=', $filter['value']);
                                break;
                            case 'in':
                                $this->where_in($filter['key'], explode(',', $filter['value']));
                                break;
                            case 'date':
                                $this->where('date(' . $filter['key'] . ')', $filter['value']);
                                break;
                            case 'date-egt':
                                $this->where('date(' . $filter['key'] . ') >=', $filter['value']);
                                break;
                            case 'date-elt':
                                $this->where('date(' . $filter['key'] . ') <=', $filter['value']);
                                break;
                            case 'date-gt':
                                $this->where('date(' . $filter['key'] . ') >', $filter['value']);
                                break;
                            case 'date-lt':
                                $this->where('date(' . $filter['key'] . ') <', $filter['value']);
                                break;
                            case 'age':
                                $this->where('TIMESTAMPDIFF(YEAR, ' . $filter['key'] . ', CURRENT_DATE) =', $filter['value']);
                                break;
                            case 'year-month':
                                $aux = explode('-', $filter['value']);
                                if (count($aux) === 2) {
                                    $this->where('YEAR(' . $filter['key'] . ')', $aux[0]);
                                    $this->where('MONTH(' . $filter['key'] . ')', $aux[1]);
                                }
                                break;
//                            case 'notnull':
//                                $this->where($filter['key'] . ' is NOT NULL', NULL, FALSE);
//                                break;
                            default:
                                if ($filter['key'] !== NULL) {
                                    $this->where($filter['key'], $filter['value']);
                                } else {
                                    $this->where($filter['value']);
                                }
                                break;
                        }
                    }
                }
            }
        }
    }

}
