<?php

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

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

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

    public static function add_feature_html()
    {
        wp_enqueue_style('bootstrapMinCss');
        $context = array();
        $context['view'] = plugins_url() . '/tospur/Admin/views';
        $context['result'] = feature::search_feature();
        $context['url'] = home_url();
        $context['admin_url'] = admin_url();
        Timber::render('feature.html', $context);
    }
}

?>