function avc_get_filter_relationships($filter_values)
{
    $ret = array();
    global $wpdb;
    global $TABLES;
    global $FILTERS;
    // Transform filter_values into an array
    $filters = explode(',', $filter_values);
    // Determine which filter is used, and connect with the underlying database table
    $split = explode(':', $filters[0]);
    // anvelope-latime
    $name = explode('-', $split[0]);
    // anvelope
    $index = array_search($name[0], $TABLES);
    // 0
    $filter_column_names = $FILTERS[$index];
    // ['anvelope-latime', .... 'amvelope-brand']
    $table = $wpdb->prefix . 'filter_' . $name[0];
    // wp_filter_anvelope
    // 0. Initialize the return arrays
    // - each array will contain all elements by default
    // - later these will be intersected when looping through the filters
    foreach ($filter_column_names as $index => $filter_column_name) {
        $column = avc_remove_filter_prefix($filter_column_name);
        $query = $wpdb->get_results("SELECT {$column} FROM " . $table);
        $ret[$index] = array_unique(avc_prettify_single_relation($query));
    }
    // 1. Loop through all filters individually
    // - intersect the return arrays with relationships from the database
    foreach ($filters as $filter) {
        // Get filter name and value
        // returns: [0] => anvelope-latime, [1] => .33
        $split = explode(':', $filter);
        $filter_name = $split[0];
        // anvelope-latime
        if (isset($split[1])) {
            $filter_value = $split[1];
            // .33
            // Do the query
            $column = avc_remove_filter_prefix($filter_name);
            $value = explode('.', $filter_value);
            if (isset($value[1])) {
                foreach ($filter_column_names as $index => $filter_column_name) {
                    $column_name = avc_remove_filter_prefix($filter_column_name);
                    if ($column != $column_name) {
                        $relation = $wpdb->get_results("SELECT " . $column_name . " FROM " . $table . " WHERE " . $column . " ='" . $value[1] . "' ");
                        $ret[$index] = array_unique(array_intersect($ret[$index], avc_prettify_single_relation($relation)));
                    }
                }
            }
        }
    }
    // 2. When there are multiple filters active
    // Build the SQL query's WHERE clause
    $where = '';
    $multiple_filters = 0;
    foreach ($filters as $filter) {
        // Get filter name and value
        // returns: [0] => anvelope-latime, [1] => .33
        $split = explode(':', $filter);
        if (isset($split[1])) {
            $column = avc_remove_filter_prefix($split[0]);
            // Remove . from the value
            // returns .'33' => '33'
            $value = explode('.', $split[1]);
            if (isset($value[1])) {
                $where .= $column . " = '" . $value[1] . "' ";
                $where .= " AND ";
                $multiple_filters++;
            }
        }
    }
    if ($multiple_filters > 1) {
        $where = str_lreplace(" AND ", "", $where);
        // Execute the SQL
        $relations = $wpdb->get_results("SELECT * FROM " . $table . " WHERE " . $where);
        $relations = avc_prettify_relationships($relations);
        // intersect the results from 1.) with these new results
        foreach ($filter_column_names as $index => $filter_column_name) {
            $ret[$index] = array_unique(array_intersect($ret[$index], $relations[$index]));
        }
    }
    return $ret;
}
Esempio n. 2
0
function anvelocom_tables()
{
    global $wpdb;
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    global $TABLES;
    global $FILTERS;
    foreach ($TABLES as $index => $table) {
        $fields = '';
        foreach ($FILTERS[$index] as $filter) {
            $fields .= avc_remove_filter_prefix($filter);
            $fields .= ' varchar(255), ';
        }
        $table_name = $wpdb->prefix . "filter_" . $table;
        $sql = "CREATE TABLE {$table_name} (\n      id mediumint(9) NOT NULL AUTO_INCREMENT,\n      {$fields}\n      UNIQUE KEY id (id)\n    );";
        dbDelta($sql);
    }
}