rep__get_search_locations( $args = [] )

This function renders the Sync Log Files HTML markup to display sync log file content.

Namespace

REP_Malcolm

Return

array

An array containing the list of all search locations.

Source

File: \includes\rep-helper-functions.php

function rep__get_search_locations( $args = [] ){

    global $wpdb, $pagenow, $post;

    $post_type = REP__PREFIX.'_agency';

    $args = wp_parse_args(
        $args,
        array(
            'orderby' => array(
                'suburb' => 'asc',
                'state' => 'asc',
                'postcode' => 'asc'
            ),
            'groupby' => ["suburb", "state", "postcode"],
            'selects' => ["suburb", "state", "postcode"],
            'include_postcodes_only' => false,
            'include_states_only' => false
        )
    );

    extract( $args );

    $post_table = $wpdb->prefix.'rep_search_location';

    $wheres = ['%d']; 

    $param = [1];

    $orders = []; 

    if( $orderby ){
        if( is_array( $orderby ) ){
            foreach( $orderby as $key => $order ){
                $orders[] = "{$key} {$order}";
            }    
        } else {
            $orders[] = "{$orderby} {$order}";
        }
    }

    if(is_array($selects)) $selects = implode(", ",$selects);
    if($selects) $selects = "SELECT DISTINCT {$selects}";
    
    if(is_array($wheres)) $wheres = implode(" AND ", $wheres);
    if($wheres) $wheres = "WHERE {$wheres}";

    if(is_array($groupby)) $groupby = implode(", ",$groupby);
    if($groupby) $groupby = "GROUP BY {$groupby}";

    if(is_array($orders)) $orders = implode(", ", $orders);
    
    $sql = "{$selects} FROM {$post_table} {$wheres} {$groupby} ORDER BY {$orders}";

    $sql = $wpdb->prepare( $sql , $param);
    $data = $wpdb->get_results($sql , 'ARRAY_A'); 

    if( $include_states_only ){
        $states = rep__get_search_locations(array(
            'selects' => ['"" AS suburb', "state", '"" AS postcode'],
            'include_states_only' => false,
            'include_postcodes_only' => false,
        ));

        $data = array_merge( $data, $states );
    }

    if( $include_postcodes_only ){
        $post_codes = rep__get_search_locations(array(
            'selects' => ['"" AS suburb', '"" AS state', "postcode"],
            'include_states_only' => false,
            'include_postcodes_only' => false
        ));

        $data = array_merge( $data, $post_codes );
    }
    
    return $data;
}