diff --git a/includes/class-QueryEvent.php b/includes/class-QueryEvent.php index a9d6df1..5bb4972 100644 --- a/includes/class-QueryEvent.php +++ b/includes/class-QueryEvent.php @@ -1,184 +1,220 @@ . // load dependent traits class_exists( QueryConference::class, true ); class_exists( QueryChapter ::class, true ); class_exists( QueryTrack ::class, true ); class_exists( QueryRoom ::class, true ); /** * Methods for a QueryEvent class */ trait QueryEventTrait { use QueryConferenceTrait; /** * Where the Event is... * * @param object $event Event * @return self */ public function whereEvent( $event ) { return $this->whereEventID( $event->getEventID() ); } /** * Where the Event ID is... * * @param int $id Event ID * @return self */ public function whereEventID( $id ) { return $this->whereInt( $this->EVENT_ID, $id ); } /** * Where the Event UID is this one * * @param string $uid Event UID * @return self */ public function whereEventUID( $uid ) { return $this->whereStr( Event::UID, $uid ); } /** * Starting from an Event search the nearby Events happening in the same moment * * @return self */ public function whereEventMeanwhile( $event ) { return $this->whereInt( 'event.event_ID', $event->getEventID(), '!=' ) ->whereStr( 'event_start', $event->getEventEnd( 'Y-m-d H:i:s' ), '<=' ) ->whereStr( 'event_end', $event->getEventStart( 'Y-m-d H:i:s' ), '>=' ); } + /** + * Select a field saying if the Event has a Sharable + * + * @param string $field Name of this column + * @param callable $callback Callable with a QuerySharable as first argument + * @return self + */ + public function selectEventHasSharable( $field = 'event_has_sharable', $callback = null ) { + + // subquery to check if this Event has a Sharable + $has_sharable = ( new QuerySharable() ) + ->equals( $this->EVENT_ID, Sharable::EVENT_ ); + + // eventually append other constraints + if( $callback ) { + $callback( $has_sharable ); + } + + // build the 'SELECT EXISTS( ... ) AS field' + return $this->selectExists( $has_sharable, $field ); + } + + /** + * Select a field saying if the Event has a video + * + * @param string $field Name of this column + * @return self + */ + public function selectEventHasVideo( $field = 'event_has_video' ) { + return $this->selectEventHasSharable( $field, function( $query_sharable ) { + + // limit only to the videos + $query_sharable->whereSharableType( 'video' ); + } ); + } + /** * Where the Event is editable by me */ public function whereEventIsEditable() { throw new Exception( "to be implemented" ); } } /** * Class able to query a FullEvent. */ class QueryEvent extends Query { use QueryEventTrait; use QueryChapterTrait; use QueryTrackTrait; use QueryRoomTrait; /** * Univoque Event ID column name * * @var */ protected $EVENT_ID = 'event.event_ID'; /** * Univoque Chapter ID column name * * @var */ protected $CHAPTER_ID = 'event.chapter_ID'; /* * Univoque Conference ID column name * * Used from ConferenceTrait#joinConference() * * @var */ protected $CONFERENCE_ID = 'event.conference_ID'; /** * Univoque Track ID column name * * @var */ protected $TRACK_ID = 'event.track_ID'; /** * Univoque Room ID column name * * @var */ protected $ROOM_ID = 'event.room_ID'; /** * Constructor */ public function __construct() { parent::__construct(); $this->from( Event::T ); $this->defaultClass( FullEvent::class ); } /** * Limit to a certain User * * @deprecated * * @param $user User * @return self */ public function whereUser( $user ) { return $this->joinEventUser() ->whereInt( EventUser::USER_, $user->getUserID() ); } /** * Join Events to User IDs * * You can call it multiple time safely. * * @return self */ public function joinEventUser() { if( empty( $this->joinedEventUser ) ) { $this->from( EventUser::T ); $this->equals( EventUser::EVENT_, Event::ID_ ); $this->joinedEventUser = true; } return $this; } /** * Join Events and their Track, Chapter and Room (can be NULL). * * You can call it multiple time safely. * * @return self */ public function joinTrackChapterRoom() { if( empty( $this->joinedTrackChapterRoom ) ) { $this->joinRoom( 'LEFT' ); $this->joinTrack( 'LEFT' ); $this->joinChapter( 'LEFT' ); $this->joinedTrackChapterRoom = true; } return $this; } } diff --git a/includes/class-QuerySharable.php b/includes/class-QuerySharable.php index b608c75..51cef94 100644 --- a/includes/class-QuerySharable.php +++ b/includes/class-QuerySharable.php @@ -1,147 +1,157 @@ . // require dependent traits class_exists( QueryEvent::class, true ); /** * Methods related to a QuerySharable class */ trait QuerySharableTrait { /** * Where the Sharable is... * * @param object $event Sharable * @return self */ public function whereSharable( $event ) { return $this->whereSharableID( $event->getSharableID() ); } /** * Where the Sharable ID is... * * @param int $id Sharable ID * @return self */ public function whereSharableID( $id ) { return $this->whereInt( $this->SHARABLE_ID, $id ); } + /** + * Where the Sharable type is + * + * @param string $type + * @return self + */ + public function whereSharableType( $type ) { + return $this->whereStr( Sharable::TYPE, $type ); + } + /** * Where the Sharable has a parent * * @param boolean $has Set to false to have not a parent * @return self */ public function whereSharableHasParent( $has = true ) { // it has the parent if it's not NULL $verb = $has ? 'IS NOT' : 'IS'; return $this->compare( Sharable::PARENT, $verb, 'NULL' ); } /** * Where the Sharable has not a parent * * @param boolean $has Set to false to have not a parent * @return self */ public function whereSharableIsRoot() { return $this->whereSharableHasParent( false ); } /** * Where the Sharable has a parent and it's this one * * @param Sharable $sharable * @return self */ public function whereSharableParent( $sharable ) { return $this->whereSharableParentID( $sharable->getSharableID() ); } /** * Where the Sharable has a parent and it's this one * * @param int $id Sharable ID * @return self */ public function whereSharableParentID( $id ) { return $this->whereInt( Sharable::PARENT, $id ); } /** * Select a field called 'has_sharable_children' * * @return self */ public function selectSharableHasChildren( $alias = 'sharable_has_children' ) { // check if it exists another Sharable with this row as its parent $temp_subquery_alias = 'sharable_children'; $subquery = ( new QuerySharable( null, $temp_subquery_alias ) ) ->equals( $temp_subquery_alias . DOT . Sharable::PARENT, Sharable::ID_ ) ->getQuery(); return $this->select( "EXISTS( $subquery ) $alias"); } } /** * Utility used to Query a Sharable. */ class QuerySharable extends Query { use QuerySharableTrait; use QueryEventTrait; /** * Univoque Sharable ID column name * * @var */ protected $SHARABLE_ID = 'sharable.sharable_ID'; /** * Univoque Event ID column name * * @var */ protected $EVENT_ID = 'sharable.event_ID'; /** * Constructor * * @param DB $db Database or NULL for the default one * @param string $alias Table alias */ public function __construct( $db = null, $alias = true ) { // initialize Query parent::__construct(); // select default table $this->fromAlias( Sharable::T, $alias ); // select default result class name $this->defaultClass( Sharable::class ); } }