<?php
final class DModelCatalogSearchTerms extends Model {

    public function addSearchTerm($data) {
        $this->db->query("INSERT INTO `" . DB_PREFIX . "search_terms` (term, score) values ('" . $this->db->escape($data['term']) . "', 1) ON DUPLICATE KEY UPDATE score=score+1");
    }

    public function deleteSearchTerm($search_term_id) {
        $this->db->query("DELETE FROM `" . DB_PREFIX . "search_terms` WHERE search_term_id = '" . (int)$search_term_id . "'");
    }

    public function getTotalSearchTerms(){
        $sql = "SELECT count(*) as total FROM `".DB_PREFIX."search_terms`";

        $query = $this->db->query($sql);

        return $query->row['total'];
    }

    public function getSearchTermsMostWanted($data=array()){
        $sql = "SELECT * FROM `".DB_PREFIX."search_terms` ORDER BY score DESC, search_terms_id DESC";

        if (isset($data['limit'])){
            $sql .= " limit " . (isset($data['start']) ? $data['start'].", ":"") . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

}
