Beispiel #1
0
/** 
* Returns an array of DateTime objects for each start date of occurrence
* @since 1.0.0
* @deprecated 1.5
* @see eo_get_the_occurrences_of()
*
* @param int $post_id Optional, the event (post) ID, 
* @return array|false Array of DateTime objects of the start date-times of occurences. False if none exist.
*/
function eo_get_the_occurrences($post_id = 0)
{
    //_deprecated_function( __FUNCTION__, '1.5', 'eo_get_the_occurrences_of()' );
    $occurrences = eo_get_the_occurrences_of($post_id);
    if ($occurrences) {
        return wp_list_pluck($occurrences, 'start');
    }
    return false;
}
 /**
  * Event Organiser generates start dates and uses the duration of the first
  * event to calculate the other occurrences. So the end date of a recurring event
  * may vary.
  * @see https://wordpress.org/support/topic/last-day-missing-because-of-the-leap-year-2016?replies=3
  */
 public function testOverVarythingMonths()
 {
     $event = array('start' => new DateTime('2015-02-28 00:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-03 00:00:00', eo_get_blog_timezone()), 'schedule' => 'monthly', 'schedule_meta' => 'BYMONTHDAY=28', 'schedule_last' => new DateTime('2015-04-28 00:00:00', eo_get_blog_timezone()));
     $event_id = eo_insert_event($event);
     $actual = array_values(eo_get_the_occurrences_of($event_id));
     $expected = array(array('start' => new DateTime('2015-02-28 00:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-03 00:00:00', eo_get_blog_timezone())), array('start' => new DateTime('2015-03-28 00:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-31 00:00:00', eo_get_blog_timezone())), array('start' => new DateTime('2015-04-28 00:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-05-01 00:00:00', eo_get_blog_timezone())));
     //$actual and $expected contain the occurrence IDs as keys.
     $this->assertEquals($expected, $actual);
 }
Beispiel #3
0
 /**
  * Checks that included/excluded dates are converted to the blog timezone 
  * before being inserted into the database
  * @see https://github.com/stephenharris/Event-Organiser/issues/264
  */
 function testEventIncludesInForeignTimezone()
 {
     //Set the site timezone to America/New_York
     $original_tz = get_option('timezone_string');
     update_option('timezone_string', 'America/New_York');
     wp_cache_delete('eventorganiser_timezone');
     //Next define an event using a foreign (to the site) timezone. E.g. UTC.
     $utc = new DateTimeZone('UTC');
     $event_id = eo_insert_event(array('start' => new DateTime('2015-03-27 15:00:00', $utc), 'end' => new DateTime('2015-03-27 22:00:00', $utc), 'schedule' => 'daily', 'schedule_last' => new DateTime('2015-03-29 15:00:00', $utc), 'include' => array(new DateTime('2015-03-30 15:00:00', $utc)), 'exclude' => array(new DateTime('2015-03-28 15:00:00', $utc))));
     $actual = array_values(eo_get_the_occurrences_of($event_id));
     $expected = array(array('start' => new DateTime('2015-03-27 11:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-27 18:00:00', eo_get_blog_timezone())), array('start' => new DateTime('2015-03-29 11:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-29 18:00:00', eo_get_blog_timezone())), array('start' => new DateTime('2015-03-30 11:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-30 18:00:00', eo_get_blog_timezone())));
     $this->assertEquals($expected, $actual);
     //Reset
     update_option('timezone_string', $original_tz);
     wp_cache_delete('eventorganiser_timezone');
 }
 /**
  * Currently the following case is not allowed:
  * - Changing the start date to a date where an occurrence already exists
  */
 public function testMoveOccurrenceNotAllowed()
 {
     $event = array('start' => new DateTime('2014-08-11 18:48:00', eo_get_blog_timezone()), 'end' => new DateTime('2014-08-11 19:48:00', eo_get_blog_timezone()), 'schedule' => 'weekly', 'until' => new DateTime('2014-09-01 18:48:00', eo_get_blog_timezone()));
     $event_id = $this->factory->event->create($event);
     $occurrences = eo_get_the_occurrences_of($event_id);
     $occurrence_ids = array_keys($occurrences);
     $occurrence_id = $occurrence_ids[2];
     //Check the start/end datetimes are as expected
     $this->assertEquals(array('start' => new DateTime('2014-08-25 18:48:00', eo_get_blog_timezone()), 'end' => new DateTime('2014-08-25 19:48:00', eo_get_blog_timezone())), $occurrences[$occurrence_id]);
     //Try to move to an 'occupied date' (even with different time)
     $new_start = new DateTime('2014-08-18 15:48:00', eo_get_blog_timezone());
     $new_end = new DateTime('2014-08-18 16:48:00', eo_get_blog_timezone());
     $response = eventorganiser_move_occurrence($event_id, $occurrence_id, $new_start, $new_end);
     $this->assertInstanceOf('WP_Error', $response);
     $this->assertEquals('events-cannot-share-date', $response->get_error_code());
 }
 /**
  * Update an EO event when a CiviEvent is updated
  *
  * @since 0.1
  *
  * @param string $op the type of database operation
  * @param string $objectName the type of object
  * @param integer $objectId the ID of the object
  * @param object $objectRef the object
  * @return void
  */
 public function event_updated($op, $objectName, $objectId, $objectRef)
 {
     // target our operation
     if ($op != 'edit') {
         return;
     }
     // target our object type
     if ($objectName != 'Event') {
         return;
     }
     // kick out if not event object
     if (!$objectRef instanceof CRM_Event_DAO_Event) {
         return;
     }
     /*
     error_log( print_r( array(
     	'method' => __METHOD__,
     	'op' => $op,
     	'objectName' => $objectName,
     	'objectId' => $objectId,
     	'objectRef' => $objectRef,
     ), true ) );
     */
     // check for event_type_id, which is a mandatory field
     if (!empty($objectRef->id) and empty($objectRef->event_type_id)) {
         // this probably means that a Location has been added to the event
         if (!empty($objectRef->loc_block_id)) {
             /*
             error_log( print_r( array(
             	'method' => __METHOD__,
             	'updated_event' => $updated_event,
             ), true ) );
             */
             // get full event data
             $updated_event = $this->get_event_by_id($objectRef->id);
             // update the EO event
             $event_id = $this->plugin->eo->update_event($updated_event);
         } else {
             // what's going on ehre?
             return;
             trigger_error(print_r(array('method' => __METHOD__, 'op' => $op, 'objectName' => $objectName, 'objectId' => $objectId, 'objectRef' => $objectRef), true), E_USER_ERROR);
             die;
         }
     } else {
         // update a single EO event - or create if it doesn't exist
         $event_id = $this->plugin->eo->update_event((array) $objectRef);
     }
     // kick out if not event object
     if (is_wp_error($event_id)) {
         // log error
         error_log(print_r(array('method' => __METHOD__, 'error' => $event_id->get_error_message()), true));
         // kick out
         return;
     }
     // get occurrences
     $occurrences = eo_get_the_occurrences_of($event_id);
     // in this context, a CiviEvent can only have an EO event with a
     // single occurrence associated with it, so use first item
     $keys = array_keys($occurrences);
     $occurrence_id = array_shift($keys);
     // store correspondences
     $this->plugin->db->store_event_correspondences($event_id, array($occurrence_id => $objectRef->id));
 }
 /**
  * Sync CiviEvents to EO events. This will NOT create sequences
  *
  * @since 0.1
  *
  * @return void
  */
 public function sync_events_to_eo()
 {
     // this can be a lengthy process
     $this->make_sync_uninterruptible();
     // get all Civi Events
     $all_civi_events = $this->plugin->civi->get_all_civi_events();
     // sync Civi to EO
     if (count($all_civi_events['values']) > 0) {
         // loop
         foreach ($all_civi_events['values'] as $civi_event) {
             // update a single EO event - or create if it doesn't exist
             $event_id = $this->plugin->eo->update_event($civi_event);
             // get occurrences
             $occurrences = eo_get_the_occurrences_of($event_id);
             // in this context, a CiviEvent can only have an EO event with a
             // single occurrence associated with it, so use first item
             $keys = array_keys($occurrences);
             $occurrence_id = array_pop($keys);
             // store correspondences
             $this->store_event_correspondences($event_id, array($occurrence_id => $civi_event['id']));
         }
     }
 }
/**
* Returns the end date of occurrence of event. 
* 
* If used inside the loop, with no id no set, returns end date of
* current event occurrence.
* 
* **Please note:** This function used to accept 3 arguments, it now accepts 4, but with the third deprecated. 
* The third argument specified the occurrence (are 3 for the 3rd occurrence of an event). 
* Instead now use the fourth argument - which specifies the occurrence by ID.
* 
* ### Examples
* Inside the loop, you can output the end date of event (occurrence)
* <code>
*       <php echo eo_get_the_end('jS M YY'); ?>
* </code> 
* Get the end date of the event with id 7 and occurrence ID 3
* <code>
*       <?php $date = eo_get_the_end('jS M YY',7,null, 3); ?>
* </code>
* Print a list of upcoming events with their start and end date
* <code>
*     //Get upcoming events
*     $events = eo_get_events(array(
*          'numberposts'=>5,
*          'events_start_after'=>'today',
*          'showpastevents'=>true,//Will be deprecated, but set it to true to play it safe.
*       ));
*
*
*     if( $events ){
*         echo '<ul>';
*         foreach( $events as $event ){
*           printf("<li><a href='%s' >%s</a> from %s to %s </li>",
*                get_the_permalink($post->ID),
*                get_the_title($post->ID),
*                eo_get_the_start('jS F Y', $post->ID,null,$post->occurrence_id),
*                eo_get_the_end('jS F Y', $post->ID,null,$post->occurrence_id)
*           );
*          }
*         echo '</ul>';
*     }else{
*         echo 'No Upcoming Events';
*     }
* </code>
* 
* @since 1.0.0
* @package event-date-functions
* @param string $format String of format as accepted by PHP date
* @param int $post_id The event (post) ID. Uses current event if empty.
* @param int $deprecated The occurrence number. Deprecated. Use $occurrence_id instead
* @param int $occurrence_id  The occurrence ID
* @return string the end date formated to given format, as accepted by PHP date
*/
function eo_get_the_end($format = 'd-m-Y', $post_id = 0, $deprecated = 0, $occurrence_id = 0)
{
    global $post;
    $event = $post;
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '1.5.6', 'Third argument is depreciated. Please use a fourth argument - occurrence ID. Available from $post->occurrence_id');
        //Backwards compatiblity
        if (!empty($post_id)) {
            $event = eo_get_by_postid($post_id, $deprecated, $occurrence_id);
        }
        if (empty($event)) {
            return false;
        }
        $date = trim($event->EndDate) . ' ' . trim($event->FinishTime);
        if (empty($date) || $date == " ") {
            return false;
        }
        return eo_format_date($date, $format);
    }
    $post_id = (int) (empty($post_id) ? get_the_ID() : $post_id);
    $occurrence_id = (int) (empty($occurrence_id) && isset($event->occurrence_id) ? $event->occurrence_id : $occurrence_id);
    $occurrences = eo_get_the_occurrences_of($post_id);
    if (!$occurrences || !isset($occurrences[$occurrence_id])) {
        return false;
    }
    $end = $occurrences[$occurrence_id]['end'];
    /**
     * Filters the value returned by `eo_get_the_end()`
     *
     * @param string|DateTime $formatted_end The DateTime object or formatted returned value (as determined by $format)
     * @param DateTime $end The end date as a DateTime object
     * @param string $format The format the end date should be returned in
     * @param int $post_id Post ID of the event
     * @param int $occurrence_id  The occurrence ID
     */
    $formatted_date = apply_filters('eventorganiser_get_the_end', eo_format_datetime($end, $format), $end, $format, $post_id, $occurrence_id);
    return $formatted_date;
}
/**
* Returns the end date of occurrence of event. 
* 
* If used inside the loop, with no id no set, returns end date of
* current event occurrence.
* 
* **Please note:** This function used to accept 3 arguments, it now accepts 4, but with the third deprecated. 
* The third argument specified the occurrence (are 3 for the 3rd occurrence of an event). 
* Instead now use the fourth argument - which specifies the occurrence by ID.
* 
* ### Examples
* Inside the loop, you can output the end date of event (occurrence)
* <code>
*       <php echo eo_get_the_end('jS M YY'); ?>
* </code> 
* Get the end date of the event with id 7 and occurrence ID 3
* <code>
*       <?php $date = eo_get_the_end('jS M YY',7,null, 3); ?>
* </code>
* Print a list of upcoming events with their start and end date
* <code>
*     //Get upcoming events
*     $events = eo_get_events(array(
*          'numberposts'=>5,
*          'events_start_after'=>'today',
*          'showpastevents'=>true,//Will be deprecated, but set it to true to play it safe.
*       ));
*
*
*     if( $events ){
*         echo '<ul>';
*         foreach( $events as $event ){
*           printf("<li><a href='%s' >%s</a> from %s to %s </li>",
*                get_the_permalink($post->ID),
*                get_the_title($post->ID),
*                eo_get_the_start('jS F Y', $post->ID,null,$post->occurrence_id),
*                eo_get_the_end('jS F Y', $post->ID,null,$post->occurrence_id)
*           );
*          }
*         echo '</ul>';
*     }else{
*         echo 'No Upcoming Events';
*     }
* </code>
* 
* @since 1.0.0
* @package event-date-functions
* @param string $format String of format as accepted by PHP date
* @param int $post_id The event (post) ID. Uses current event if empty.
* @param int $deprecated The occurrence number. Deprecated. Use $occurrence_id instead
* @param int $occurrence_id  The occurrence ID
* @return string the end date formated to given format, as accepted by PHP date
*/
function eo_get_the_end($format = 'd-m-Y', $post_id = 0, $deprecated = 0, $occurrence_id = 0)
{
    global $post;
    $event = $post;
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '1.5.6', 'Third argument is depreciated. Please use a fourth argument - occurrence ID. Available from $post->occurrence_id');
        //Backwards compatiblity
        if (!empty($post_id)) {
            $event = eo_get_by_postid($post_id, $deprecated, $occurrence_id);
        }
        if (empty($event)) {
            return false;
        }
        $date = trim($event->EndDate) . ' ' . trim($event->FinishTime);
        if (empty($date) || $date == " ") {
            return false;
        }
        return eo_format_date($date, $format);
    }
    $occurrence_id = (int) (empty($occurrence_id) && isset($event->occurrence_id) ? $event->occurrence_id : $occurrence_id);
    $occurrences = eo_get_the_occurrences_of($post_id);
    if (!$occurrences || !isset($occurrences[$occurrence_id])) {
        return false;
    }
    $end = $occurrences[$occurrence_id]['end'];
    return apply_filters('eventorganiser_get_the_end', eo_format_datetime($end, $format), $end, $format, $post_id, $occurrence_id);
}