<?php

class feature
{
    public static function ajax_add_feature()
    {
        $name = $_POST['name'];
        $type = $_POST['type'];
        $array = array();
        $result = feature::search_feature($type, $name);
        if ($result) {
            $array['code'] = 2001;
        } else {
            feature::add_feature($type, $name);
            $array['code'] = 2000;
        }
        wp_send_json($array);
    }

    public static function search_feature($type, $name = null)
    {
        $sql = "select id,name from " . Config::TOSPUR_TAG_TABLE . " where type = " . $type;
        if ($name) {
            $sql .= " and name = '" . $name . "'";
        }
        return DBManager::get_results($sql);
    }

    public static function add_feature($type, $name)
    {
        $result = DBManager::insert(Config::TOSPUR_TAG_TABLE, array(
            'name' => $name,
            'type' => $type
        ));
        return $result;
    }

    public static function add_feature_html()
    {
        $context = array();
        $context['view'] = plugins_url() . '/tospur/Admin/views';
        $context['url'] = home_url();
        $context['request_url'] = $_SERVER['request_url'];
        $type = null;
        switch ($_GET['page']) {
            case 'add_tag':
                $type = 0;
                break;
            case 'add_feature':
                $type = 1;
                break;
        }
        $context['type'] = $type;
        $context['result'] = feature::search_feature($type);
        Timber::render('feature.html', $context);
    }
}

?>