<?php

/*
 * Copyright (c) 2019 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 Visitor_model
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */

 class Visitor_model extends CI_Model {

    const TABLE = 'visitor';	
/*id
name
created
modified
createdby
modifiedby */

    public function get($input = array())
    {
        $this->db->select('id, name, created, modified, createdby, modifiedby');
        $this->db->from(self::TABLE);
        if (isset($input['name']) && !empty($input['name'])) {
            $this->db->where('name', $input['name']);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    public function dropdown($input = array())
    {
        $this->db->select('id, name');
        $this->db->from(self::TABLE);
        $query = $this->db->get();
        $res = $query->result_array();
        return array_column($res, 'name', 'id');
    }

    public function detail($input = array())
    {
        $this->db->select('id, name, created, modified, createdbym modifiedby');
        $this->db->from(self::TABLE);
        if (isset($input['id']) && !empty($input['id'])) {
            $this->db->where('id', $input['id']);
        }
        $query = $this->db->get();
        return $query->row_array();
    }

    public function insert($input = array(), $return_id = false)
    {
        $user = $this->session->userdata(CI_NAME .'log');
        $insert = array();
        if (isset($input['name']) && !empty($input['name'])) {
            $insert['name']  = $input['name'];
        }
        $insert['created'] = time();
        $insert['modified'] = time();
        $insert['createdby'] = $user['userid'];
        $insert['modifiedby'] = $user['userid'];
        $res = $this->db->insert(self::TABLE, $insert);
        return ($return_id === true) ? $this->db->insert_id() : $res;
    }
 }