<?php

class InfoSEOs_model extends CI_Model {

    var $insert_id = -1;
    var $top_num = false;
    var $search_url = false;
    var $order_by = false;

    function __construct() {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    function init() {
        $this->top_num = false;
        $this->search_url = false;
        $this->order_by = " ORDER BY  iseo.seo_datetime DESC  ";
    }

    function all() {
        $this->init();
        $this->order_by = " ORDER BY  iseo.seo_url ASC  ";
        return $this->get_list();
    }

    function search($url) {
        $this->init();
        $this->top_num = 16;
        if ($url) {
            $sql_url = '%' . $this->HT->escape_like_str($url) . '%';
            $this->search_url = "AND seo_url like N'$sql_url' ";
        }
        return $this->get_list();
    }

    function get_list() {
        $this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
        $sql .= "	   iseo.seo_id, \n"
                . "       iseo.seo_h1, \n"
                . "       iseo.seo_url, \n"
                . "       iseo.seo_title, \n"
                . "       iseo.seo_keywords, \n"
                . "       iseo.seo_description, \n"
                . "       iseo.seo_summary, \n"
                . "       iseo.seo_sitecode, \n"
                . "       iseo.seo_datetime \n"
                . "FROM   infoSEOs iseo \n"
                . "WHERE  iseo.seo_sitecode = ? ";
        $this->search_url ? $sql.=$this->search_url : false;
        $this->order_by ? $sql.=$this->order_by : false;

        $query = $this->HT->query($sql, array($this->config->item('site_code')));
        //print_r($this->HT->queries);
        return $query->result();
    }

    function get_detail($seo_url_seo_id) {

        $sql = "SELECT TOP 1 iseo.seo_id, \n"
                . "       iseo.seo_h1, \n"
                . "       iseo.seo_url, \n"
                . "       iseo.seo_title, \n"
                . "       iseo.seo_keywords, \n"
                . "       iseo.seo_description, \n"
                . "       iseo.seo_summary, \n"
                . "       iseo.seo_sitecode, \n"
                . "       iseo.seo_datetime \n"
                . "FROM   infoSEOs iseo \n"
                . "WHERE  iseo.seo_sitecode = ? ";
        if (is_numeric($seo_url_seo_id)) {
            $sql.= "AND  iseo.seo_id = ? ";
        } else {
            $sql.= "AND  iseo.seo_url = ? ";
        }
        $query = $this->HT->query($sql, array($this->config->item('site_code'), $seo_url_seo_id));
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return FALSE;
        }
    }

    function url_check($seo_id, $seo_url) {
        $sql = "SELECT TOP 1 seo.seo_id \n"
                . "FROM   infoSEOs seo \n"
                . "WHERE  seo.seo_id <> ? \n"
                . "       AND seo.seo_url = ? \n"
                . "       AND seo.seo_sitecode = ? ";
        $query = $this->HT->query($sql, array($seo_id, $seo_url, $this->config->item('site_code')));
        //print_r($this->HT->queries);
        if ($query->num_rows() > 0) {
            return false;
        } else {
            return true;
        }
    }

    function add($seo_h1, $seo_url, $seo_title, $seo_keywords, $seo_description, $seo_summary) {
        $sql = "INSERT INTO infoSEOs \n"
                . "  ( \n"
                . "    seo_h1, \n"
                . "    seo_url, \n"
                . "    seo_title, \n"
                . "    seo_keywords, \n"
                . "    seo_description, \n"
                . "    seo_summary, \n"
                . "    seo_sitecode, \n"
                . "    seo_datetime \n"
                . "  ) \n"
                . "VALUES \n"
                . "  ( \n"
                . "    N?, \n"
                . "    N?, \n"
                . "    N?, \n"
                . "    N?, \n"
                . "    N?, \n"
                . "    N?, \n"
                . "    ?, \n"
                . "    getdate() \n"
                . "  )";
        $query = $this->HT->query($sql, array($seo_h1, $seo_url, $seo_title, $seo_keywords, $seo_description, $seo_summary, $this->config->item('site_code')));
        $this->insert_id = $this->HT->last_id('infoSEOs');
        return $query;
    }

    function update($seo_id, $seo_h1, $seo_url, $seo_title, $seo_keywords, $seo_description, $seo_summary) {
        $sql = "UPDATE infoSEOs \n"
                . "SET    seo_h1 = N?, \n"
                . "       seo_url = N?, \n"
                . "       seo_title = N?, \n"
                . "       seo_keywords = N?, \n"
                . "       seo_description = N?, \n"
                . "       seo_summary = N?, \n"
                . "       seo_datetime = getdate() \n"
                . "WHERE  seo_sitecode = ? \n"
                . "       AND  seo_id= ?";
        $query = $this->HT->query($sql, array($seo_h1, $seo_url, $seo_title, $seo_keywords, $seo_description, $seo_summary, $this->config->item('site_code'), $seo_id));
        return $query;
    }

    function delete($seo_id) {
        $sql = "DELETE  \n"
                . "FROM   infoSEOs \n"
                . "WHERE  seo_id = ? \n"
                . "       AND seo_sitecode = ?";
        $query = $this->HT->query($sql, array($seo_id, $this->config->item('site_code')));
        return $query;
    }

}
