vendredi 4 mars 2016

Trying to export specific fields from an events calendar with php to csv

I'm trying to add some extra fields to this export plugin for tribe events calendar in wordpress. The fields in question are the event URL, Venue, City, and state fields to be exported to the csv.

I can only get results from the eventURL and get N/A returned for the Venue, City, and State. Any help please. My meta_keys are right so not sure what to do.

As a note my php skills are lacking.

Thanks.

/**
 * Builds the CSV data array used to generate a CSV file.
 *
 * @param $start_date:  The beginning of the date range
 * @param $end_date:  The end of the date range
 * @return $csv_output:  The CSV data array
 */
function build_output($start_date, $end_date)
{
    $csv_output = array();

    $events = ee_get_all_events();

    foreach($events as $event) {
        // check date range
        $date_time_info = is_within_range($start_date, $end_date, $event->ID);

        if($date_time_info) {
            $csv_row = array();
            //  get categories
            $csv_row[] = ee_get_event_categories($event->ID);
            //  get date-time
            $csv_row[] = $date_time_info['start_month'];
            $csv_row[] = $date_time_info['start_day'];
            $csv_row[] = $date_time_info['end_day'];
            //  get title
            $csv_row[] = ee_scrub_text($event->post_title);
            //  get start/end times
            $csv_row[] = $date_time_info['start_time'];
            $csv_row[] = $date_time_info['end_time'];

            //  get content
            $csv_row[] = ee_scrub_text($event->post_content);
            //  get URL
            $csv_row[] = getEventURL($event->ID);
            // get venue information
            $csv_row[] = getVenue($event->ID);
            $csv_row[] = getCity($event->ID);
            $csv_row[] = getState($event->ID);
            $csv_output[] = $csv_row;
        }
    }

    $csv_output = sort_results($csv_output);
    $row_headers = array('CATEGORY', 'START MONTH', 'START DAY', 'END DAY', 'TITLE', 'START TIME', 'END TIME', 'DESCRIPTION', 'WEBSITE', 'VENUE', 'CITY', 'STATE');
    array_unshift($csv_output, $row_headers);

    return $csv_output;
}

/**
 * Returns an array of all active tribe events
 */
function ee_get_all_events()
{
    global $wpdb;

    $query = "
        SELECT *
        FROM $wpdb->posts
        WHERE $wpdb->posts.post_type = 'tribe_events'
        AND $wpdb->posts.post_status = 'publish'
    ";

    return $wpdb->get_results($query, OBJECT);
}

/**
 * Parses the event's start date-time and end date-time stamps.  Returns
 * the parsed array if the specified event id falls between the specified
 * dates.  Otherwise returns false.
 */
function is_within_range($range_start, $range_end, $id)
{
    $range_start_date = new DateTime($range_start . ' 00:00:00');
    $range_end_date = new DateTime($range_end . ' 23:59:59');

    global $wpdb;

    $query = "
        SELECT $wpdb->postmeta.meta_value
        FROM $wpdb->postmeta
        WHERE $wpdb->postmeta.meta_key = '_EventStartDate'
        AND $wpdb->postmeta.post_id = $id
    ";

    $start_result = $wpdb->get_results($query, OBJECT);
    $event_start_date = new DateTime($start_result[0]->meta_value);

    // Make sure this event is in range.  If not, return false.
    if($event_start_date < $range_start_date || $event_start_date > $range_end_date)
        return false;

    $query = "
        SELECT $wpdb->postmeta.meta_value
        FROM $wpdb->postmeta
        WHERE $wpdb->postmeta.meta_key = '_EventEndDate'
        AND $wpdb->postmeta.post_id = $id
    ";

    $end_result = $wpdb->get_results($query, OBJECT);
    $event_end_date = new DateTime($end_result[0]->meta_value);

    $parsed_dates = array(
        'start_month'   =>  date_format($event_start_date, 'F'),
        'start_day'     =>  date_format($event_start_date, 'd'),
        'end_day'       =>  date_format($event_end_date, 'd'),
        'start_time'    =>  date_format($event_start_date, 'H:i:s'),
        'end_time'      =>  date_format($event_end_date, 'H:i:s')
    );

    return $parsed_dates;
}

/**
 * Returns a string concatenation of all categories assigned
 * to a specified event.
 *
 * @param $id: The database ID of the event
 * @return $categories: The categories string
 */
function ee_get_event_categories($id)
{
    global $wpdb;
    $categories = "";

    $query = "
        SELECT *
        FROM $wpdb->term_relationships
        WHERE $wpdb->term_relationships.object_id = $id
    ";

    $terms = $wpdb->get_results($query, OBJECT);

    foreach($terms as $term)
    {
        $term_id = get_term_id($term->term_taxonomy_id);
        $term_type = get_term_type($term->term_taxonomy_id);

        if($term_type == "tribe_events_cat") {
            $name = ee_get_term_name($term_id);
            $name = ee_scrub_text($name);
            $categories .= (strlen($categories) > 0 ? " | " . $name : $name);
        }
    }

    return $categories;
}

/**
 * Returns a the term_type for a specified term taxonomy ID
 *
 * @param $id:  The term taxonomy id to lookup
 * @return $term_id:  The id of this term
 */
function get_term_id($id)
{
    global $wpdb;

    $query = "
        SELECT $wpdb->term_taxonomy.term_id
        FROM $wpdb->term_taxonomy
        WHERE $wpdb->term_taxonomy.term_taxonomy_id = $id
    ";

    $result = $wpdb->get_results($query, OBJECT);

    return $result[0]->term_id;
}

/**
 * Returns a the term_type for a specified term ID
 *
 * @param $id:  The term id to lookup
 * @return $term_type:  The type of this term
 */
function get_term_type($id)
{
    global $wpdb;

    $query = "
        SELECT $wpdb->term_taxonomy.taxonomy
        FROM $wpdb->term_taxonomy
        WHERE $wpdb->term_taxonomy.term_taxonomy_id = $id
    ";

    $result = $wpdb->get_results($query, OBJECT);

    return $result[0]->taxonomy;
}

/**
 * Returns a the term name for a specified term ID
 *
 * @param $id:  The term id to lookup
 * @return $term_name:  The name of this term
 */
function ee_get_term_name($id)
{
    global $wpdb;

    $query = "
        SELECT $wpdb->terms.name
        FROM $wpdb->terms
        WHERE $wpdb->terms.term_id = $id
    ";

    $result = $wpdb->get_results($query, OBJECT);

    return $result[0]->name;
}
/**
 * Returns the URL field for the event
 */
function getEventURL($id)
{
    global $wpdb;

    $query = "
        SELECT $wpdb->postmeta.meta_value
        FROM $wpdb->postmeta
        WHERE $wpdb->postmeta.post_id = $id
        AND $wpdb->postmeta.meta_key = '_EventURL'
    ";

    $result = $wpdb->get_results($query, OBJECT);

    if (!$result)
        return "N/A";
    else
        return $result[0]->meta_value;
}
/**
 * Returns the VENUE field for the event
 */
function getVenue($id)
{
    global $wpdb;

    $query = "
        SELECT $wpdb->postmeta.meta_value
        FROM $wpdb->postmeta
        WHERE $wpdb->postmeta.post_id = $id
        AND $wpdb->postmeta.meta_key = '_VenueVenue'
    ";

    $result = $wpdb->get_results($query, OBJECT);


        if (!$result)
        return "N/A";
    else
        return $result[0]->meta_value;
}

// /**
//  * Returns the CITY field for the event
// */
function getCity($id)
{
    global $wpdb;

    $query = "
        SELECT $wpdb->postmeta.meta_value
        FROM $wpdb->postmeta
        WHERE $wpdb->postmeta.post_id = $id
        AND $wpdb->postmeta.meta_key = '_VenueCity'
    ";

    $result = $wpdb->get_results($query, OBJECT);


        if (!$result)
        return "N/A";
    else
        return $result[0]->meta_value;
}

// /**
//  * Returns the STATE field for the event
//  */
function getState($id)
{
    global $wpdb;

    $query = "
        SELECT $wpdb->postmeta.meta_value
        FROM $wpdb->postmeta
        WHERE $wpdb->postmeta.post_id = $id
        AND $wpdb->postmeta.meta_key = '_VenueState'
    ";

    $result = $wpdb->get_results($query, OBJECT);


        if (!$result)
        return "N/A";
    else
        return $result[0]->meta_value;
}
/**
 * Sends a CSV file to the user with the data for all events
 * that fall within their specified date range
 *
 * @param $data:  the array of data to format as CSV
 */
function outputCSV($data)
{
    $output = fopen("php://output", "w");

    foreach ($data as $row) {
        fputcsv($output, $row); // here you can change delimiter/enclosure
    }
    fclose($output);
}

/**
 * Converts HTML entities in a string to UTF-8 encoding
 *
 * @param $text: The string to convert
 * @return $fixed: The converted string
 */
function ee_scrub_text($text)
{
    $fixed = mb_convert_encoding($text, "HTML-ENTITIES");
    return $fixed;
}

function sort_results($events)
{
    usort($events, "compare_events");
    return $events;
}

function compare_events($a, $b)
{
    $months = array(
        'January'   =>  1,
        'February'  =>  2,
        'March'     =>  3,
        'April'     =>  4,
        'May'       =>  5,
        'June'      =>  6,
        'July'      =>  7,
        'August'    =>  8,
        'September' =>  9,
        'October'   =>  10,
        'November'  =>  11,
        'December'  =>  12
    );

    if ($months[$a[1]] < $months[$b[1]])
        return -1;
    else if($months[$a[1]] > $months[$b[1]])
        return 1;
    else {
        if ($a[2] < $b[2])
            return -1;
        else if ($a[2] > $b[2])
            return 1;
        else
            return 0;
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire