<?php

/*
 * *************************************************
 * Copyright (C) 2021 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 MY_Form_validation
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class MY_Form_validation extends CI_Form_validation {

    public function is_unique($str, $field): bool {

        if (!isset($this->CI->db)) {
            return false;
        } else {
            sscanf($field, '%[^.].%[^.].%[^.].%[^.]', $table, $field1, $field2, $except);
            $this->CI->db->from($table);
            $this->CI->db->where($field1, $str);
            if ($field2 && $except) {
                $this->CI->db->where($field2 . ' !=', $except);
            }
            $num_rows = $this->CI->db->count_all_results();
            return $num_rows === 0;
        }
    }

}
