diff --git a/2016/event-edit.php b/2016/event-edit.php index 9e1a752..3ca1172 100644 --- a/2016/event-edit.php +++ b/2016/event-edit.php @@ -1,613 +1,624 @@ . require 'load.php'; // inherit the Conference or specify one $conference_uid = CURRENT_CONFERENCE_UID; if( isset( $_REQUEST['conference'] ) ) { $conference_uid = $_REQUEST['conference']; } // check if the Conference exists $conference = FullConference::factoryFromUID( $conference_uid ) ->queryRow(); if( !$conference ) { die_with_404(); } // retrieve the Event (if existing) $event = null; if( isset( $_GET['uid'] ) ) { - $event = FullEvent::queryByConferenceAndUID( - $conference->getConferenceID(), - $_GET['uid'] - ); + $event = ( new QueryEvent() ) + ->whereConference( $conference ) + ->joinConference() + ->joinChapter() + ->whereEventUID( $_GET['uid'] ) + ->queryRow(); if( !$event ) { die_with_404(); } if( !$event->isEventEditable() ) { error_die("Can't edit event"); } } else { // check if there are permissions to add event if( !has_permission( 'add-event' ) ) { die( 'missing permissions' ); } } +// known columns internationalized in multiple languages +$i18n_columns = [ + Event::ABSTRACT => __( "Abstract" ), + Event::DESCRIPTION => __( "Descrizione" ), + Event::NOTE => __( "Note" ), +]; + $warning = null; if( $_POST ) { if( is_action( 'save-event' ) ) { $conference_ID = $conference->getConferenceID(); $data = []; $data[] = new DBCol( Event::TITLE, $_POST['title'], 's' ); $data[] = new DBCol( Event::UID, $_POST['uid'], 's' ); $data[] = new DBCol( Event::LANGUAGE, $_POST['language'], 's' ); $data[] = new DBCol( Event::SUBTITLE, $_POST['subtitle'], 's' ); - $data[] = new DBCol( Event::ABSTRACT, $_POST['abstract'], 'snull' ); - $data[] = new DBCol( Event::DESCRIPTION, $_POST['description'], 'snull' ); $data[] = new DBCol( Event::START, $_POST['start'], 's' ); $data[] = new DBCol( Event::END, $_POST['end'], 's' ); $data[] = new DBCol( Event::IMAGE, $_POST['image'], 'snull' ); $data[] = new DBCol( Chapter::ID, $_POST['chapter'], 'd' ); $data[] = new DBCol( Room::ID, $_POST['room'], 'd' ); $data[] = new DBCol( Track::ID, $_POST['track'], 'd' ); $data[] = new DBCol( Conference::ID, $conference_ID, 'd' ); + // for each language save the fields + foreach( all_languages() as $lang ) { + foreach( $i18n_columns as $i18n_column => $label ) { + // generic column name in this language + $field = $i18n_column . '_' . $lang->getISO(); + + // sent column value + $value = $_POST[ $field ] ?? null; + + // prepare to be saved + $data[] = new DBCol( $field, $value, 'snull' ); + } + } + + // convert empty strings to NULL, if possible foreach( $data as $row ) { $row->promoteNULL(); } if( $event ) { // update the existing Event - Event::factory() - ->whereInt( Event::ID, $event->getEventID() ) + ( new QueryEvent() ) + ->whereEvent( $event ) ->update( $data ); } else { // insert a new Event Event::factory() ->insertRow( $data ); } $id = $event ? $event->getEventID() : last_inserted_ID(); // get the updated Event $event = FullEvent::factory() ->whereInt( Event::ID, $id ) ->queryRow(); // POST-redirect-GET http_redirect( $event->getFullEventEditURL(), 303 ); } /** * Change the Image */ if( $event && is_action( 'change-image' ) ) { // prepare the image uploader $image = new FileUploader( 'image', [ 'category' => 'image', 'override-filename' => "event-" . $event->getEventUID(), ] ); // prepare the image pathnames $img_url = $event->getConferenceUID() . _ . 'images'; $img_path = ABSPATH . __ . $event->getConferenceUID() . __ . 'images'; // really upload that shitty image somewhere if( $image->fileChoosed() ) { $ok = $image->uploadTo( $img_path, $status, $filename, $ext ); if( $ok ) { // now update ( new QueryEvent() ) ->whereEvent( $event ) ->update( [ 'event_img' => $img_url . "/$filename.$ext", ] ); // POST-redirect-GET http_redirect( $event->getFullEventEditURL(), 303 ); } else { $warning = $image->getErrorMessage(); } } } /* * Add the user */ if( $event && is_action( 'add-user' ) && isset( $_POST['user'] ) ) { // Add user $user = User::factoryFromUID( $_POST['user'] ) ->select( User::ID ) ->queryRow(); if( $user ) { EventUser::delete( $event->getEventID(), $user->getUserID() ); insert_row('event_user', [ new DBCol( Event::ID, $event->getEventID(), 'd' ), new DBCol( User::ID, $user->getUserID(), 'd' ), ] ); } } /** * Update an user order */ if( $event && is_action( 'update-user' ) && isset( $_POST['user'] ) ) { $user = User::factoryFromUID( $_POST['user'] ) ->select( User::ID ) ->queryRow(); if( $user ) { if ( !empty( $_POST['delete'] ) ) { // Delete user EventUser::delete( $event->getEventID(), $user->getUserID() ); } elseif( isset( $_POST['order'] ) ) { // change order EventUser::factory() ->whereInt( Event::ID, $event->getEventID() ) ->whereInt( User ::ID, $user->getUserID() ) ->update( [ new DBCol( EventUser::ORDER, $_POST['order'], 'd') ] ); } } } // post -> redirect -> get (no: it hide errors) // http_redirect( $_SERVER[ 'REQUEST_URI' ], 303 ); } if( $event ) { Header::spawn( null, [ 'title' => sprintf( __("Modifica %s: %s"), $event->getChapterName(), $event->getEventTitle() ), ] ); } else { Header::spawn( null, [ 'title' => sprintf( __( "Aggiungi %s" ), __( "Evento" ) ), ] ); } ?>

getConferenceURL(), - $conference->getConferenceTitle() . icon( 'home', 'left' ) + esc_html( $conference->getConferenceTitle() ) . icon( 'home', 'left' ) ) ?>

getEventURL(), // text __( "Vedi" ) . icon( 'account_box', 'left' ) ) ?>

get( Event::TITLE ) ); } ?> />
get( Event::UID ) ); } ?> />
get( Event::SUBTITLE ) ); } ?> />
get( Event::LANGUAGE ) ); } ?> />
getEventStart()->format( 'Y-m-d H:i:s' ) ); } elseif( isset( $_GET['start'] ) ) { echo value( $_GET['start'] ); } ?> />
getEventEnd()->format( 'Y-m-d H:i:s' ) ); } elseif( isset( $_GET['end'] ) ) { echo value( $_GET['end'] ); } ?> />
-
-
-
- - - -

-
getEventAbstractHTML() ?>
- -
-
- -
-
- - - -

-
getEventDescriptionHTML() ?>
- -
+ $label ): ?> +

+
+ + getISO() ?> + +
+
+ + +
+
+
+ -
+
-
get( Event::IMAGE ) ); } ?>/ > hasEventImage() ): ?> <?= esc_attr( $event->getEventTitle() ) ?>
-
+ +

factoryUserByEvent() ->select( [ User::UID, EventUser::ORDER, ] ) ->defaultClass( EventUser::class ) ->orderBy( EventUser::ORDER ) ->queryGenerator(); ?> valid() ): ?>

$user ): ?>
getUserUID() ) ?> />
getEventUserOrder() ) ?>" />

getUserEditURL(), sprintf( __( "Modifica %s" ), __( "Utente" ) ) ) ?>

. 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 = [] ) { 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 ); } /** * Get the Event description rendered in HTML * * @return string */ public function getEventDescriptionHTML( $args = [] ) { return Markdown::parse( $this->getEventDescription(), $args ); } /** * Get the Event abstract rendered in HTML * * @return string */ public function getEventAbstractHTML( $args = [] ) { return Markdown::parse( $this->getEventAbstract(), $args ); } /** * Get the Event notes rendered in HTML * * @return string */ public 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'; /** * Description 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(); } /** * Generate the appropriate SELECT for the User Abstract * * @return string */ public static function ABSTRACT_L10N() { - return i18n_coalesce( self::ABSTRACT, 'event_abstract_%s' ); + return i18n_coalesce( self::ABSTRACT ); } /** * Generate the appropriate SELECT for the User Description * * @return string */ public static function DESCRIPTION_L10N() { - return i18n_coalesce( self::DESCRIPTION, 'event_description_%s' ); + return i18n_coalesce( self::DESCRIPTION ); } /** * Generate the appropriate SELECT for the User Description * * @return string */ public static function NOTE_L10N() { - return i18n_coalesce( self::NOTE, 'event_description_%s' ); + return i18n_coalesce( self::NOTE ); } /** * All the public Event fields * * @return array */ public static function fields() { return [ self::ID_, self::UID, self::TITLE, self::DESCRIPTION_L10N(), self::ABSTRACT_L10N(), self::NOTE_L10n(), self::SUBTITLE, self::IMAGE, self::START, self::END, self::LANGUAGE, ]; } } diff --git a/includes/class-QueryEvent.php b/includes/class-QueryEvent.php index 7d18096..3c6521f 100644 --- a/includes/class-QueryEvent.php +++ b/includes/class-QueryEvent.php @@ -1,181 +1,182 @@ . // load dependent traits class_exists( QueryConference::class, true ); /** * Class able to query a FullEvent. */ class QueryEvent extends Query { use QueryConferenceTrait; /** * 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 ); } /** * 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 ); } /** * Limit to a certain User * * @param $user User * @return self */ public function whereUser( $user ) { return $this->joinEventUser() ->whereInt( EventUser::USER_, $user->getUserID() ); } /** * Where the Event UID is this one * * @param string $uid Event UID * @return self */ public function whereEventUID( $uid ) { return $this->whereStr( Event::UID, $uid ); } /** * Join a table with the Chapter table * * @return self */ public function joinChapter() { return $this->joinOn( 'INNER', 'chapter', 'chapter.chapter_ID' , $this->CHAPTER_ID ); } /** * Join a table with the Track table * * @return self */ public function joinTrack() { return $this->joinOn( 'INNER', Track::T, Track::ID_, $this->TRACK_ID ); } /** * Join a table with the Room table * * @return self */ public function joinRoom() { return $this->joinOn( 'INNER', Room::T, Room::ID_, $this->ROOM_ID ); } /** * 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->from( Room::T ) ->equals( Room::ID_, Event::ROOM_ ); $this->from( Track::T ) ->equals( Track::ID_, Event::TRACK_ ); $this->from( Chapter::T ) ->equals( Chapter::ID_, Event::CHAPTER_ ); $this->joinedTrackChapterRoom = true; } return $this; } } diff --git a/includes/functions.php b/includes/functions.php index f1ce59f..a7f348e 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,134 +1,138 @@ . function license($code) { return Licenses::instance()->get($code); } function request_uri() { return URL_protocol() . URL_domain() . ROOT . $_SERVER['REQUEST_URI']; } /** * Generate a stupid MySQL COALESCE() clause to select the most appropriate i18n database column * * @param string $name Base virtual name of the column - * @param mixed $i18n Associative array of languages and column names, or a generic column name like 'column_%s' + * @param mixed $i18n Associative array of languages and column names, or a generic column name like 'column_%s', or nothing to be the "$name_%s" * @return string */ -function i18n_coalesce( $name, $i18n ) { +function i18n_coalesce( $name, $i18n = null ) { // current language (e.g. 'en') $lang = latest_language()->getISO(); // default language of the website $default = 'it'; + if( !$i18n ) { + $i18n = $name . '_%s'; + } + // allow a generic string if( is_string( $i18n ) ) { $generic = $i18n; $i18n = []; $i18n[ $lang ] = sprintf( $generic, $lang ); $i18n[ $default ] = sprintf( $generic, $default ); } // base priority column name $base_priority = $i18n[ $default ]; // try to catch the more specific column, then the default if( $lang !== $default && isset( $i18n[ $lang ] ) ) { $more_priority = $i18n[ $lang ]; return "COALESCE( `$more_priority`, `$base_priority` ) AS $name"; } // just pick the default return "`$base_priority` AS $name"; } /** * Eventually redirect the request to this permalink * * It's allowed the 'lang' parameter to be present or not. * * @param string $permalink URL of the incoming request (absolute or relative) * @return string */ function force_permalink( $permalink ) { // incoming URL $from = BASE_URL . $_SERVER['REQUEST_URI']; // force absolute $permalink = site_page( $permalink, true ); // if it's the same request, do nothing if( $from === $permalink ) { return; } // do not send a 301 Moved Permanently if the language was missing (maybe the user was just redirected to his language) $status = isset( $_GET['l'] ) ? 301 // 301 Moved Permanently : 303; // 303 See Other // redirect to the correct location http_redirect( $permalink, $status ); } /** * Force an URL to have the language argument it it requested the page with it * * @return string */ function keep_url_in_language( $url ) { // force an absolute URL $url = site_page( $url, true ); // current language ISO code $lang = latest_language()->getISO(); // force this argument return replace_url_args( $url, [ 'l' => $lang, ] ); } /** * Override parameters in GET query string * * @param string $url The entire URL (no relative URLs they confuse parse_url) * @param array $parameters Associative array, with key = paramter, value = value or null to remove it * @return string the new query part of the URL with "?" already placed in front, or an empty string * @author Ludovico Pavesi * @since 2019-09-30 */ function replace_url_args( $url, $parameters ) { $parsed = parse_url( $url ); if( isset( $parsed['query'] ) ) { parse_str( $parsed['query'], $query ); } else { $query = []; } foreach( $parameters as $k => $v ) { if( $v === null ) { unset( $query[ $k ] ); } else { $query[ $k ] = $v; } } return http_build_get_query( $url, $parameters ); }