<?php

class AxcotoFontBirdFontHelper {

    /**
     *  Get an array of font name
     * @global <type> $wpdb
     */
    static function getFonts() {
        global $wpdb;
        static $result = null;
        if ($result) {
            return $result;
        }
        $sql = "SELECT name FROM {$wpdb->prefix}fontbird_fonts ORDER BY id DESC";
        $stmt = $wpdb->prepare($sql);
        $result = $wpdb->get_col($stmt);
        return $result;
    }

    /**
     *  get an array of font name& font id
     * @global <type> $wpdb
     */
    static function loadFonts() {
        global $wpdb;
        static $result = null;
        if ($result) {
            return $result;
        }
        $sql = "SELECT id, name FROM {$wpdb->prefix}fontbird_fonts ORDER By id DESC";
        $stmt = $wpdb->prepare($sql);
        $result = $wpdb->get_results($stmt);
        foreach ($result as &$item) {
            $item->slug = substr($item->name, 0, strlen($item->name) - 4);
        }
        return $result;
    }

    static function loadRules($collectionId) {
        global $wpdb;
        $sqlRules = "SELECT r.id, r.selector, r.collection_id, r.font_id, r.font_size,
                                                f.name
                            FROM {$wpdb->prefix}fontbird_rules as r
                            LEFT JOIN {$wpdb->prefix}fontbird_fonts as f ON f.id=r.font_id
                            WHERE r.collection_id=%d
                            ";
        $stmtRules = $wpdb->prepare($sqlRules, $collectionId);
        $result = $wpdb->get_results($stmtRules);
        if (is_array($result)) {
            foreach ($result as &$item) {
                $item->slug = substr($item->name, 0, strlen($item->name) - 4);
                $item->slug = str_replace('.', '_', $item->slug);
            }
        }
        return $result;
    }

}