• shyfrog

    (@shyfrog)


    I’ve made some adjustments (I’m no coder…so I apologize in advance) to the events module to suit my needs. Unfortunately, the events are out of order. You can view it here: https://www.harlowspub.com/music-events/

    And here is the code I’ve modified:

    <?php
    
    /* Plugin Name: DeMomentSomTres Facebook Event List Shortcode
     * Plugin URI: https://demomentsomtres.com/english/wordpress-plugins/demomentsomtres-facebook-events-list/
     * Description: A simple shortcode to generate an event list from a Facebook Fan Page.
     * Author: marcqueralt
     * Version: 1.3.1
     * Author URI: https://demomentsomtres.com
     *
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation; either version 2 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     * MA 02110-1301, USA.
     *
     * Based on code by Mike Dalisay
     * https://www.codeofaninja.com/2011/07/display-facebook-events-to-your-website.html
     * Based on Jon Smith works https://www.wordsmith-communication.co.uk/ version 0.4
     */
    
    define('DMS3_FBEVENTS_DOMAIN', 'dms3-fb-events');
    load_plugin_textdomain(DMS3_FBEVENTS_DOMAIN, false, basename(dirname(__FILE__)) . '/languages');
    
    function dms3_format($yyyy_mm_ddT) {
        $y = substr($yyyy_mm_ddT, 0, 4);
        $m = substr($yyyy_mm_ddT, 5, 2);
        $d = substr($yyyy_mm_ddT, 8, 2);
        if ('' == $m | '' == $d | '' == $y):
            return '';
        else:
            return $m . '-' . $d . '-' . $y;
        endif;
    }
    
    function dms3_time_format($yyyy_mm_ddThh_mm) {
        $h = substr($yyyy_mm_ddThh_mm, 11, 2);
        $m = substr($yyyy_mm_ddThh_mm, 14, 2);
        if ('' == $h || '' == $m):
            return '';
        else:
            return '9' . ':' . $m;
        endif;
    }
    
    // limit of selected elements
    $dms3_limit = 15;
    // make sure this api file is in your directory, if not get it here https://github.com/facebook/php-sdk/tree/master/src
    if (!class_exists('Facebook')):
        require 'facebook.php';
    endif;
    
    // [fb_event_list appid="" pageid="" appsecret="" locale=""]
    function fb_event_list($atts) {
        global $dms3_limit;
    
        $time_offset = get_option('gmt_offset');
    
        ob_start();
    
        try {
            extract(shortcode_atts(array(
                'appid' => '',
                'pageid' => '',
                'appsecret' => '',
                'fbLocale' => 'Europe/London',
                'limit' => $dms3_limit,
                'order' => 'DESC'
                            ), $atts));
    
            $fqlResult = wp_cache_get('fb_event_list_result', 'fb_event_list');
            if (false == $fqlResult) {
    //          Authenticate
                $facebook = new Facebook(array(
                    'appId' => $appid,
                    'secret' => $appsecret,
                    'cookie' => true, // enable optional cookie support
                ));
    //          query the events
    //          we will select name, pic, start_time, end_time, location, description this time
    //          but there are other data that you can get on the event table
    //          as you've noticed, we have TWO select statement here
    //          since we can't just do "WHERE creator = your_fan_page_id".
    //          only eid is indexable in the event table, sow we have to retrieve
    //          list of events by eids
    //          and this was achieved by selecting all eid from
    //          event_member table where the uid is the id of your fanpage.
    //          *yes, you fanpage automatically becomes an event_member
    //          once it creates an event
                $fql = "SELECT name, pic, start_time, end_time, location, description, eid
                        FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = " . $pageid . " AND start_time > 0 )
                        ORDER BY start_time DESC LIMIT 0," . $limit;
                $param = array(
                    'method' => 'fql.query',
                    'query' => $fql,
                    'callback' => ''
                );
    
                $fqlResult = $facebook->api($param);
    
                wp_cache_set('fb_event_list_result', $fqlResult, 'fb_event_list', 300);
            }
    //      table heading
            echo '<table class="dms3_fb_events">';
            $now = date('c', time() + $time_offset * 60 * 60);
            if ($order != "DESC"):
                usort($fqlResult, create_function('$a,$b', "return strnatcmp(\$b['start'], \$a['start']);"));
            endif;
    //      looping through retrieved data
            foreach ($fqlResult as $keys => $values) {
                $start = $values['start_time'];
                $start_date = dms3_format($start);
                $start_time = dms3_time_format($start);
                $end = $values['end_time'];
                $end_date = dms3_format($end);
                $end_time = dms3_time_format($end);
                $classes = "";
                if ($start < $now)
                    if ($now < $end) /* ongoing */
                        $classes = 'dms3-fb-ongoing-event';
                    else
                        $classes = 'dms3-fb-past-event';
                else
                    $classes = 'dms3-fb-upcoming-event';
                if ($classes != "")
                    $classes = ' class="' . $classes . '" ';
                //printing the data
                echo '<tr' . $classes . '>';
                echo '<td class="dms3_fb_events_content">';
                echo "<div class='dms3_fb_events_title'>" . $start_date . '&nbsp;' . $values['name'] . '&nbsp;' . $start_time . 'pm' . "</div>";
                echo "<img src={$values['pic']} class='dms3_fb_events_image' />";
                echo "<span class='dms3_fb_events_description'>" . $values['description'] . "</span>";
                echo "<span style='float: right;'><a href='https://www.facebook.com/event.php?eid={$values['eid']}' target='_blank'>" . __('view facebook event', DMS3_FBEVENTS_DOMAIN) . '</a></span>';
                echo '</td>';
                echo '</tr>';
            }
            echo "</table>";
        } catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "\n";
        }
    
        $htmlOutput = ob_get_clean();
        return $htmlOutput;
    }
    
    add_shortcode('fb_event_list', 'fb_event_list');
    // end fb_event_list shortcode
    ?>

    https://www.ads-software.com/plugins/demomentsomtres-facebook-events-list/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter shyfrog

    (@shyfrog)

    Alright, this forum is dead. But in case someone sees this, I’ve made some progress.
    Once again, I am not a coder…and I’m flying by the seat of my pants.

    I’ve modified the code some more and now have the months in ASC order, but the days within the months are DESC (??)

    Code below

    define('DMS3_FBEVENTS_DOMAIN', 'dms3-fb-events');
    load_plugin_textdomain(DMS3_FBEVENTS_DOMAIN, false, basename(dirname(__FILE__)) . '/languages');
    
    /*function dms3_format($yyyy_mm_ddT) {
     *   $y = substr($yyyy_mm_ddT, 0, 4);
     *   $m = substr($yyyy_mm_ddT, 5, 2);
     *   $d = substr($yyyy_mm_ddT, 8, 2);
     *   if ('' == $m | '' == $d | '' == $y):
     *       return '';
     *   else:
     *       return $m . '-' . $d . '-' . $y;
     *   endif;
    }*/
    
    function dms3_format($yyyy_mm_ddT) {
    	$t=substr($yyyy_mm_ddT,11,5); // get the time value only
    	$d=substr($yyyy_mm_ddT,0,10); //get date separately from the time
    	$d=date("l, F j, Y", strtotime($d)); //format date output
    	$t12  = date("g:i a", strtotime($t)); //convert from 24 to 12 hour time
    	return $d." - ".$t12;
    }
    
    function dms3_time_format($yyyy_mm_ddThh_mm) {
        $h = substr($yyyy_mm_ddThh_mm, 11, 2);
        $m = substr($yyyy_mm_ddThh_mm, 14, 2);
        if ('' == $h || '' == $m):
            return '';
        else:
            return $h . ':' . $m;
        endif;
    }
    
    // limit of selected elements
    $dms3_limit = 15;
    // make sure this api file is in your directory, if not get it here https://github.com/facebook/php-sdk/tree/master/src
    if (!class_exists('Facebook')):
        require 'facebook.php';
    endif;
    
    // [fb_event_list appid="" pageid="" appsecret="" locale=""]
    function fb_event_list($atts) {
        global $dms3_limit;
    
        $time_offset = get_option('gmt_offset');
    
        ob_start();
    
        try {
            extract(shortcode_atts(array(
                'appid' => '',
                'pageid' => '',
                'appsecret' => '',
                'fbLocale' => 'America/New_York',
                'limit' => $dms3_limit,
                'order' => 'ASC'
                            ), $atts));
    
            $fqlResult = wp_cache_get('fb_event_list_result', 'fb_event_list');
            if (false == $fqlResult) {
    //          Authenticate
                $facebook = new Facebook(array(
                    'appId' => $appid,
                    'secret' => $appsecret,
                    'cookie' => true, // enable optional cookie support
                ));
    //          query the events
    //          we will select name, pic, start_time, end_time, location, description this time
    //          but there are other data that you can get on the event table
    //          as you've noticed, we have TWO select statement here
    //          since we can't just do "WHERE creator = your_fan_page_id".
    //          only eid is indexable in the event table, sow we have to retrieve
    //          list of events by eids
    //          and this was achieved by selecting all eid from
    //          event_member table where the uid is the id of your fanpage.
    //          *yes, you fanpage automatically becomes an event_member
    //          once it creates an event
                $fql = "SELECT name, pic, start_time, end_time, location, description, eid
                        FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = " . $pageid . " AND start_time > 0 )
                        ORDER BY start_time ASC";
                $param = array(
                    'method' => 'fql.query',
                    'query' => $fql,
                    'callback' => ''
                );
    
                $fqlResult = $facebook->api($param);
    
                wp_cache_set('fb_event_list_result', $fqlResult, 'fb_event_list', 300);
            }
    //      table heading
            echo '<table class="dms3_fb_events">';
            $now = date('c', time() + $time_offset * 60 * 60);
                usort($fqlResult, create_function('$a,$b', "return strnatcmp(\$b['start'], \$a['start']);"));
    //      looping through retrieved data
            foreach ($fqlResult as $keys => $values) {
                $start = $values['start_time'];
                $start_date = dms3_format($start);
                $start_time = dms3_time_format($start);
                $end = $values['end_time'];
                $end_date = dms3_format($end);
                $end_time = dms3_time_format($end);
                $classes = "";
                if ($start < $now)
                    if ($now < $end) /* ongoing */
                        $classes = 'dms3-fb-ongoing-event';
                    else
                        $classes = 'dms3-fb-past-event';
                else
                    $classes = 'dms3-fb-upcoming-event';
                if ($classes != "")
                    $classes = ' class="' . $classes . '" ';
                //printing the data
                echo '<tr' . $classes . '>';
                echo '<td class="dms3_fb_events_content">';
                echo "<div class='dms3_fb_events_title'>" . $start_date . "<br />" . $values['name'] . "</div>";
                echo "<img src={$values['pic']} class='dms3_fb_events_image' />";
                echo "<span class='dms3_fb_events_description'>" . $values['description'] . "</span>";
                echo "<span style='float: right;'><a href='https://www.facebook.com/event.php?eid={$values['eid']}' target='_blank'>" . __('view facebook event', DMS3_FBEVENTS_DOMAIN) . '</a></span>';
                echo '</td>';
                echo '</tr>';
            }
            echo "</table>";
        } catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "\n";
        }
    
        $htmlOutput = ob_get_clean();
        return $htmlOutput;
    }
    
    add_shortcode('fb_event_list', 'fb_event_list');
    // end fb_event_list shortcode
    ?>
    Plugin Author Marc Queralt i Bassa

    (@marcqueralt)

    Dear shyfrog,
    This forum is not dead.
    However the time I can focus on free development is limited.
    I’ll try to introduce some changes on the next release of this plugin.
    Your sort order does’nt work as you are sorting by small fields and not based on the whole date.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sorting ASC out of order’ is closed to new replies.