diff --git a/includes/class-Event.php b/includes/class-Event.php index bbc1f18..46758b0 100644 --- a/includes/class-Event.php +++ b/includes/class-Event.php @@ -1,409 +1,411 @@ . trait EventTrait { /** * Get event ID * * @return int */ public function getEventID() { return $this->nonnull( Event::ID ); } /** * Get event UID * * @return string */ public function getEventUID() { return $this->get( Event::UID ); } /** * Get localized event title * * @return string */ public function getEventTitle() { return __( $this->get( Event::TITLE ) ); } /** * Check if the Event has a subtitle * * @return boolean */ public function hasEventSubtitle() { $title = $this->get( Event::SUBTITLE ); return !empty( $title ); } /** * Get localized event subtitle * * @return string */ public function getEventSubtitle() { return __( $this->get( Event::SUBTITLE ) ); } /** * Get an human event start date * * @return string */ public function getEventHumanStart() { return HumanTime::diff( $this->getEventStart() ); } /** * Get an human event end date * * @return string */ public function getEventHumanEnd() { return HumanTime::diff( $this->getEventEnd() ); } /** * Get event start date * * @param string If present, format of the date * @return DateTime|string */ public function getEventStart( $format = null ) { $date = $this->get( Event::START ); if( $format ) { return $date->format( $format ); } return $date; } /** * When event end date * * @param string If present, format of the date * @return DateTime|string */ public function getEventEnd( $format = null ) { $date = $this->get( Event::END ); if( $format ) { return $date->format( $format ); } return $date; } /** * Get the event duration * * @return DateInterval */ public function getEventDuration( $format ) { $start = $this->get( Event::START ); $end = $this->get( Event::END ); return $start->diff( $end )->format( $format ); } /** * Get the Event duration in a human-readable format * * @return string */ public function getHumanEventDuration( $args = [] ) { - $complete = true; + if( !isset( $args['adverb'] ) ) { + $args['adverb'] = false; + } return HumanTime::diff( $this->get( Event::START ), $this->get( Event::END ), $args ); } /** * It has an Event image? * * @return bool */ public function hasEventImage() { return $this->has( Event::IMAGE ); } /** * Get the path to the Event image * * @param boolean $absolute Try to force an absolute URL * @return string */ public function getEventImage( $absolute = false ) { return site_page( $this->get( Event::IMAGE ), $absolute ); } /** * It has event description? * * @return bool */ public function hasEventDescription() { return null !== $this->get( Event::DESCRIPTION ); } /** * It has an event abstract? * * @return bool */ public function hasEventAbstract() { return null !== $this->get( Event::ABSTRACT ); } /** * It has an event note? * * @return bool */ function hasEventNote() { return null !== $this->get( Event::NOTE ); } /** * Get the event description * * @return string */ public function getEventDescription() { return $this->get( Event::DESCRIPTION ); } /** * Get the event abstract * * @return string */ public function getEventAbstract() { return $this->get( Event::ABSTRACT ); } /** * Get the event note * * @return string */ public function getEventNote() { return $this->get( Event::NOTE ); } function getEventDescriptionHTML( $args = [] ) { return Markdown::parse( __( $this->getEventDescription() ), $args ); } function getEventAbstractHTML( $args = [] ) { return Markdown::parse( __( $this->getEventAbstract() ), $args ); } function getEventNoteHTML( $args = [] ) { return Markdown::parse( __( $this->getEventNote() ), $args ); } /** * Factory Users by this event * * @return Query */ public function factoryUserByEvent() { return User::factoryByEvent( $this->getEventID() ); } /** * Factory Sharables by this event * * @return Query */ public function factorySharebleByEvent() { return Sharable::factoryByEvent( $this->getEventID() ); } /** * You can edit this event? * * @return bool */ public function isEventEditable() { return has_permission('edit-events'); } /** * Insert subscription if not exists */ function addSubscription($email) { $exists = Subscription::getStandardQuery( $email, $this->getEventID() )->getRow('Subscription'); $exists || Subscription::insert( $email, $this->getEventID() ); return $exists; } /** * Get URL to trop-iCal API for this event * * @param boolean $absolute Set to true for an absolute URL * @return string */ public function getEventCalURL( $absolute = false ) { $event = urlencode( $this->getEventUID() ); $conf = urlencode( $this->getConferenceUID() ); return site_page( "api/tropical.php?conference=$conf&event=$event", $absolute ); } /** * Are event subscriptions available? * * @return bool */ public function areEventSubscriptionsAvailable() { return $this->get( 'event_subscriptions' ) && ! $this->isEventPassed(); } /** * Is event passed? * * @return bool */ public function isEventPassed() { $now = new DateTime('now'); return $now->diff( $this->get('event_end') )->invert === 1; } /** * Normalize an Event object */ protected function normalizeEvent() { $this->integers( Event::ID ); $this->datetimes( Event::START, Event::END ); $this->booleans('event_subscriptions'); } } /** * An Event can be a talk or a lesson etc. */ class Event extends Queried { use EventTrait; /** * Database table name */ const T = 'event'; /** * ID column name */ const ID = 'event_ID'; /** * UID column name */ const UID = 'event_uid'; /** * Title column name */ const TITLE = 'event_title'; /** * Subtitle column name */ const SUBTITLE = 'event_subtitle'; /** * Image column name */ const IMAGE = 'event_img'; /** * Start column name */ const START = 'event_start'; /** * End column name */ const END = 'event_end'; /** * Description column name */ const DESCRIPTION = 'event_description'; /** * Abstract column name */ const ABSTRACT = 'event_abstract'; /** * Note column name */ const NOTE = 'event_note'; /** * Language column name */ const LANGUAGE = 'event_language'; /** * Complete ID column name */ const ID_ = self::T . DOT . self::ID; /** * Complete conference ID column name */ const CONFERENCE_ = self::T . DOT . Conference::ID; /** * Complete room ID column name */ const ROOM_ = self::T . DOT . Room::ID; /** * Complete track ID column name */ const TRACK_ = self::T . DOT . Track::ID; /** * Complete chapter ID column name */ const CHAPTER_ = self::T . DOT . Chapter::ID; /** * Maximum UID length * * @override */ const MAXLEN_UID = 100; /** * Constructor */ public function __construct() { $this->normalizeEvent(); } } diff --git a/includes/class-HumanTime.php b/includes/class-HumanTime.php index d18370e..6c09a04 100644 --- a/includes/class-HumanTime.php +++ b/includes/class-HumanTime.php @@ -1,208 +1,214 @@ . /** * Handle time difference in a human-readable format */ class HumanTime { /** * Get the time difference between two dates (or just one date and now) * * @param DateTime $from * @param DateTime $to * @param array $args * @return string */ public static function diff( $from, $to = null, & $args = [] ) { - if( !isset( $args['complete'] ) ) { - $args['complete'] = false; - } - + // enable long version as default if( !isset( $args['long'] ) ) { $args['long'] = true; } + // enable '%s ago'/'in %s' adverbs as default + if( !isset( $args['adverb'] ) ) { + $args['adverb'] = true; + } + if( $to === null ) { $to = new DateTime('now'); } $diff = $from->diff( $to ); $human = self::humanize( $diff->y, $diff->m, $diff->d, $diff->h, $diff->i, $diff->s, $diff->days, $diff->invert, $from, $args ); - if( $args['complete'] ) { + if( $args['complete'] || !$args['adverb'] ) { return $human; } if( $diff->invert ) { return sprintf( __("fra %s"), $human ); } return sprintf( __("%s fa"), $human ); } private static function humanize( $y, $m, $d, $h, $i, $s, $days, $invert, $from, & $args ) { - $long = & $args['long']; - $complete = & $args['complete']; + $long = & $args['long']; + + // as default it's not a complete construct (e.g. '3 years' VS 'today' ) + if( !isset( $args['complete'] ) ) { + $args['complete'] = false; + } // Top-down: From far away to recently // We are human if( $d > 200 ) { $y++; } if( $y > 1 ) { $phrase = $long ? __("%d anni") : __( "%d y" ); return sprintf( $phrase, $y ); } if( $y === 1 ) { return $long ? __( "un anno" ) : __( "1 y" ); } // This year if( $m > 1 ) { $phrase = $long ? __( "%d mesi" ) : __( "%d m" ); return sprintf( $phrase, $m ); } if( $m === 1 ) { return $long ? __( "un mese" ) : __( "1 m" ); } // This month // We are human $d_fake = $d; if( $h > 18 ) { $d_fake++; } if( $d_fake > 13 && !$long ) { return sprintf( __("%d settimane"), $d_fake / 7 ); } if( $d_fake > 6 && !$long ) { return __("una settimana"); } if( $d_fake > 1 ) { $phrase = $long ? __( "%d giorni" ) : __( "%d d" ); return sprintf( $phrase, $d_fake ); } if( $d_fake === 1 ) { $complete = true; return $invert ? __("domani") : _("ieri"); } // Today // We are human $h_fake = $h; if( $i > 40 ) { $h_fake++; } if( $h_fake > 3 ) { $complete = true; return __("oggi"); } if( $h > 1 ) { if( $m > 0 ) { return sprintf( __("%d ore e %d minuti"), $h, $i ); } return sprintf( __("%d ore"), $h ); } // In this hour // We are human $i_fake = $i + $h * 60; if( $s > 50 ) { $i_fake++; } if( $i_fake > 52 ) { return $long ? __( "un'ora" ) : __( "1 h" ); } if( $i_fake > 37 ) { return __("tre quarti d'ora"); } if( $i_fake > 22 ) { return $long ? __( "mezzora" ) : __( "½ h" ); } if( $i_fake > 1 ) { return sprintf( __("%d minuti"), $i_fake ); } if( $i_fake === 1 ) { return sprintf( __("un minuto") ); } if( $s > 10 ) { return sprintf( __("%d secondi"), $s ); } $complete = true; return __("proprio ora"); } }