diff --git a/2016/user-edit.php b/2016/user-edit.php index 327e587..1708ccf 100644 --- a/2016/user-edit.php +++ b/2016/user-edit.php @@ -1,395 +1,433 @@ . require 'load.php'; $user = null; if( isset( $_GET['uid'] ) ) { $user = User::factoryFromUID( @ $_GET['uid'] ) ->queryRow(); if( !$user ) { die( "not found" ); } if( !$user->hasPermissionToEditUser() ) { error_die( "Can't edit user" ); } } else { if( !has_permission( 'edit-users' ) ) { error_die( "Can't create user" ); } } // register form submit action if( is_action( 'save-user' ) ) { // avoid spaces if( $_POST['email'] ) { $_POST['email'] = luser_input( $_POST['email'], 32 ); } // generate Gravatar if( $_POST['email'] ) { $_POST['gravatar'] = md5( $_POST['email'] ); } // prepare data sent via POST $data = []; $data[] = new DBCol( User::NAME, $_POST['name'], 's' ); $data[] = new DBCol( User::SURNAME, $_POST['surname'], 's' ); $data[] = new DBCol( User::UID, $_POST['uid'], 's' ); $data[] = new DBCol( User::EMAIL, $_POST['email'], 'snull' ); $data[] = new DBCol( User::WEBSITE, $_POST['site'], 'snull' ); $data[] = new DBCol( User::GRAVATAR, $_POST['gravatar'], 'snull' ); $data[] = new DBCol( User::LOVED_LICENSE, $_POST['lovelicense'], 'snull' ); + // for each language + foreach( all_languages() as $lang ) { + + // generic column name in this language + $field = sprintf( 'user_bio_%s', $lang->getISO() ); + + // sent column value + $value = $_POST[ $field ] ?? null; + + // prepare to be saved + $data[] = new DBCol( $field, $value, 'snull' ); + } + // promote empty strings to null foreach( $data as $row ) { $row->promoteNULL(); } if( $user ) { // update existing user User::factoryByID( $user->getUserID() ) ->update( $data ); } else { // insert a new User User::factory() ->insertRow( $data ); } $id = $user ? $user->getUserID() : last_inserted_ID(); $user = User::factoryByID( $id ) ->queryRow(); // POST -> redirect -> GET http_redirect( $user->getUserEditURL(), 302 ); } if( isset( $_POST['action'], $_POST['skill_uid'], $_POST['skill_score'] ) ) { $skill = Skill::factoryFromUID( $_POST['skill_uid'] ) ->queryRow(); $skill or error_die( sprintf( "Skill '%s' not found", esc_html( $_POST['skill_uid'] ) ) ); if( is_action( 'change-skill' ) ) { if( isset( $_POST['skill_delete'] ) ) { // Delete skill checked query( sprintf( "DELETE FROM {$T('user_skill')} WHERE user_ID = %d AND skill_ID = %d", $user->getUserID(), $skill->getSkillID() ) ); } else { // Update skill elsewhere query_update('user_skill', new DBCol('skill_score', $_POST['skill_score'], 'd' ), sprintf( 'skill_ID = %d', $skill->getSkillID() ) ); } } if( is_action( 'add-skill' ) ) { $skill = Skill::factoryFromUID( $_POST['skill_uid'] ) ->queryRow(); // If exists, delete it query( sprintf( "DELETE FROM {$T('user_skill')} WHERE user_ID = %d AND skill_ID = %d", $user->getUserID(), $skill->getSkillID() ) ); insert_row('user_skill', [ new DBCol('user_ID', $user->getUserID(), 'd'), new DBCol('skill_ID', $skill->getSkillID(), 'd'), new DBCol('skill_score', $_POST['skill_score'], 'd'), ] ); } } // register action to delete the user if( $user && is_action( 'delete-user' ) ) { // delete the user from the database User::factory() ->whereInt( 'user_ID', $user->getUserID() ) ->delete(); // POST -> redirect -> GET http_redirect( $user->getUserEditURL(), 302 ); } Header::spawn('user', [ 'title' => $user ? sprintf( __("Modifica utente %s"), $user->getUserFullname() ) : __( "Aggiungi Utente" ) , 'url' => $user ? $user->getUserURL() : null, ] ); ?>

getUserURL(), __( "Vedi" ) . icon('account_box', 'left') ) ?>

get( User::NAME ) ) : '' ?> />
get( User::SURNAME ) ) : '' ?> />
getUserUID() ) : '' ?> />
getUserEmail() ) : '' ?> />
hasUserGravatar() ? value( $user->getUserGravatarUID() ) : '' ?> />
get( User::WEBSITE ) ) : '' ?> />
hasUserLovelicense() ) { echo $user->getUserLovelicense()->getLink(); } ?>
+
+ +
+ + getISO() ) ?> +
+
+

getHuman() + ) ?> + + getISO() === 'it' ): ?> +

Legacy description translated by the community:

+ + +
+
+
+ +

factoryUserSkills() ->queryGenerator(); ?> valid() ): ?>

getSkillPhrase() ?>

. require 'load.php'; $conference = FullConference::factoryFromUID( CURRENT_CONFERENCE_UID ) ->queryRow(); $conference or die_with_404(); $user = User::factoryByUID( $_GET['uid'] ) ->select( [ User::ID, User::UID, User::GRAVATAR, User::NAME, User::SURNAME, User::IMAGE, User::WEBSITE, User::LOVED_LICENSE, - User::BIO, + User::BIO_L10N(), ] ) ->select( User::allSocialFields() ) ->queryRow(); if( !$user ) { die_with_404(); } if( FORCE_PERMALINK ) { $user->forceUserPermalink(); } Header::spawn( null, [ 'title' => $user->getUserFullname(), 'url' => $user->getUserURL(), 'og' => [ 'image' => $user->hasUserImage() ? $user->getUserImage() : null, 'type' => 'profile', 'profile:first_name' => $user->user_name, 'profile:last_name' => $user->user_surname ] ] ); ?> hasPermissionToEditUser() ): ?>

getUserUID()}", __("Modifica") . icon('edit', 'left') ) ?>

user_site ): ?>

user_site, __("Sito personale") . icon('contact_mail', 'right'), null, 'btn waves-effect purple darken-2', 'target="_blank"' ) ?>

factoryUserSkills() ->queryGenerator(); ?> valid() ): ?>

getSkillCode() ) ?>
hasUserLovelicense() ): ?>
getUserLovelicense() ?>

" . $license->getLink() . "" ) ?>

hasUserBio() ): ?>

getUserBioHTML( ['p' => 'flow-text'] ) ?>

whereConference( $conference ) ->whereUser( $user ) ->joinTrackChapterRoom() ->queryGenerator(); ?> valid() ): ?>
getEventURL(), sprintf( "%s", esc_html( $title = $event->getEventTitle() ) ), sprintf( __("Vedi %s"), $title ) ) ?> getChapterName() ) ?> getTrackName() ) ?> getLocationName() ) ?>
getRoomName() ) ?>
%s (il %s)"), $event->getEventStart("H:i"), $event->getEventStart("d/m/Y") ) ?>
getEventHumanStart() ?>

joinConference() ->joinLocation() ->joinChapter() ->joinEventUser() ->whereConferenceNot( $conference ) ->whereUser( $user ) ->orderBy( Event::START, 'DESC' ) ->queryGenerator(); ?> valid() ): ?>

hasConferenceEventsURL() ) { // print the Event permalink echo HTML::a( $event->getEventURL(), esc_html( $event->getEventTitle() ) ); } else { // just print the Event title echo esc_html( $event->getEventTitle() ); } ?> getConferenceTitle() ) ?> getLocationName() ) ?>
%s (il %s)"), $event->getEventStart("H:i"), $event->getEventStart("d/m/Y") ) ?>
getEventHumanStart() ?>
isUserSocial() ): ?>

has( User::RSS ) and SocialBox::spawn( $user, "RSS", $user->get( User::RSS ), 'home.png' ); $user->has( User::FACEBOOK ) and SocialBox::spawn( $user, "Facebook", $user->getUserFacebruck(), 'facebook_logo.png' ); $user->has( User::GOOGLE_PLUS ) and SocialBox::spawn( $user, "Google+", $user->getUserGuggolpluz(), 'google.png' ); $user->has( User::TWITTER ) and SocialBox::spawn( $user, "Twitter", $user->getUserTuitt(), 'twitter.png' ); $user->has( User::LINKEDIN ) and SocialBox::spawn( $user, "Linkedin", $user->getUserLinkeddon(), 'linkedin.png' ); $user->has( User::GITHUB ) and SocialBox::spawn( $user, "Github", $user->getUserGithubbo(), 'github.png' ); ?>
. require 'load.php'; $conference = FullConference::factoryFromUID( CURRENT_CONFERENCE_UID ) ->queryRow(); if( !$conference ) { die_with_404(); } $user = User::factoryByUID( $_GET['uid'] ) + ->select( [ + User::ID, + User::UID, + User::GRAVATAR, + User::NAME, + User::SURNAME, + User::IMAGE, + User::WEBSITE, + User::LOVED_LICENSE, + User::BIO_L10N(), + ] ) + ->select( User::allSocialFields() ) ->queryRow(); if( !$user ) { die_with_404(); } if( FORCE_PERMALINK ) { $user->forceUserPermalink(); } enqueue_js('jquery'); template( 'header', [ 'conference' => $conference, 'title' => $user->getUserFullname(), 'og' => [ 'image' => $user->getUserImage( true ), 'url' => $user->getUserURL( true ), 'type' => 'profile', 'profile:first_name' => $user->get( 'user_name' ), 'profile:last_name' => $user->get( 'user_surname' ), ] ] ); ?>

getUserFullName() ) ?>

hasPermissionToEditUser() ): ?>

getUserUID()}", __("Modifica") . icon('edit', 'left') ) ?>

has( User::WEBSITE ) ): ?> - <?= esc_html(
 								$user->getUserFullname()
 							) ?> has( User::WEBSITE ) ): ?>
user_site ): ?>

user_site, __("Sito personale") . icon('inbox', 'right'), null, 'btn waves-effect purple darken-2', 'target="_blank"' ) ?>

factoryUserSkills() ->queryGenerator(); ?> valid() ): ?>

getSkillCode() ) ?>
hasUserLovelicense() ): ?>
getUserLovelicense() ?>

%s."), $license->getLink() ) ?>

hasUserBio() ): ?>

getUserBioHTML( ['p' => 'flow-text'] ) ?>

whereConference( $conference ) ->whereUser( $user ) ->joinTrackChapterRoom() ->queryGenerator(); ?> valid() ): ?>
getEventURL(), sprintf( "%s", esc_html( $title = $event->getEventTitle() ) ), sprintf( __("Vedi %s"), $title ) ) ?> getChapterName() ) ?> getTrackName() ) ?> getLocationName() ) ?>
getRoomName() ) ?>
%s (il %s)"), $event->getEventStart("H:i"), $event->getEventStart("d/m/Y") ) ?>
getEventHumanStart() ?>

joinConference() ->joinLocation() ->joinChapter() ->whereUser( $user ) ->whereConferenceNot( $conference ) ->orderBy( Event::START, 'DESC' ) ->queryGenerator(); ?> valid() ): ?>

hasConferenceEventsURL() ) { // print the Event permalink echo HTML::a( $event->getEventURL(), esc_html( $event->getEventTitle() ) ); } else { // just print the Event title echo esc_html( $event->getEventTitle() ); } ?> getConferenceTitle() ) ?> getLocationName() ) ?>
%s (il %s)"), $event->getEventStart("H:i"), $event->getEventStart("d/m/Y") ) ?>
getEventHumanStart() ?>
isUserSocial() ): ?>

has( User::RSS ) and UserSocialBox::spawn( $user, "RSS", $user->get( User::RSS ), 'home.png' ); $user->has( User::FACEBOOK ) and UserSocialBox::spawn( $user, "Facebook", $user->getUserFacebruck(), 'facebook_logo.png' ); $user->has( User::GOOGLE_PLUS ) and UserSocialBox::spawn( $user, "Google+", $user->getUserGuggolpluz(), 'google.png' ); $user->has( User::TWITTER ) and UserSocialBox::spawn( $user, "Twitter", $user->getUserTuitt(), 'twitter.png' ); $user->has( User::LINKEDIN ) and UserSocialBox::spawn( $user, "Linkedin", $user->getUserLinkeddon(), 'linkedin.png' ); $user->has( User::GITHUB ) and UserSocialBox::spawn( $user, "Github", $user->getUserGithubbo(), 'github.png' ); ?>
. trait UserTrait { /** * Get the user ID * * @return int */ public function getUserID() { return $this->nonnull( User::ID ); } /** * Get the user UID * * @return string */ public function getUserUID() { return $this->get( User::UID ); } /** * Get the user e-mail * * @return string */ public function getUserEmail() { return $this->get( User::EMAIL ); } /** * Check if the user is public * * @return bool */ public function isUserPublic() { return $this->get( User::IS_PUBLIC ); } /** * Check if I can see this user * * @return bool */ public function isUserVisible() { return $this->isUserPublic() || $this->isUserMyself(); } /** * Get the user full name * * @return string */ public function getUserFullname() { return sprintf( __("%s %s"), $this->get( User::NAME ), $this->get( User::SURNAME ) ); } /** * Get the User URL * * The User URL is based on the CURRENT_CONFERENCE_UID. * * @param boolean $absolute Set to true to force an absolute URL * @return string */ public function getUserURL( $absolute = false ) { $url = sprintf( PERMALINK_USER, CURRENT_CONFERENCE_UID, $this->getUserUID() ); return site_page( $url, $absolute ); } /** * Force this request to the correct User permalink */ public function forceUserPermalink() { $from = BASE_URL . $_SERVER['REQUEST_URI']; $to = $this->getUserURL( true ); if( $from !== $to ) { http_redirect( $to, 303 ); } } function getUserLink( $base = null, $html_class = null) { $name = $this->getUserFullname(); return HTML::a( $this->getUserURL( $base ), esc_html( $name ), sprintf( __("Profilo utente di %s"), $name ), $html_class ); } /** * Check if the User has a Gravatar image * * @return int */ public function hasUserGravatar() { return $this->has( User::GRAVATAR ) || $this->has( User::EMAIL ); } /** * Get the md5 of the E-mail * * @return string|null */ public function getUserGravatarUID() { $gravatar = $this->get( User::GRAVATAR ); return $gravatar ? $gravatar : md5( $this->getUserEmail() ); } /** * Check if the User has an image * * @return boolean */ public function hasUserImage() { return $this->has( User::IMAGE ) || $this->has( User::GRAVATAR ); } /** * Get the URL of the user image * * @param int $size Suggested width * @param boolean $absolute Set true to force an absolute URL * @return string */ public function getUserImage( $size = 256, $absolute = false ) { $image = $this->get( User::IMAGE ); if( ! $image ) { $image = 'https://www.gravatar.com/avatar/' . $this->getUserGravatarUID() . '?s=' . $size; } return site_page( $image, $absolute ); } /** * It has an user bio? * * @return bool */ public function hasUserBio() { return null !== $this->get('user_bio'); } function getUserBio() { return $this->get('user_bio'); } function getUserBioHTML($args = []) { return Markdown::parse( __( $this->getUserBIO() ), $args); } /** * Check if the user is somehow social * * @return bool */ public function isUserSocial() { foreach( User::allSocialFields() as $field ) { if( $this->has( $field ) ) { return true; } } return false; } /** * Get the user Facebook profile URL * * @return string URL */ public function getUserFacebruck() { return 'https://facebook.com/' . $this->get( User::FACEBOOK ); } /** * Get the user Google+ profile URL * * @return string URL */ public function getUserGuggolpluz() { return 'https://plus.google.com/' . $this->get( User::GOOGLE_PLUS ); } /** * Get the user Twitter profile URL * * @return string URL */ public function getUserTuitt() { return 'https://twitter.com/' . $this->get( User::TWITTER ); } /** * Get the user Linkedin profile URL * * @return string URL */ public function getUserLinkeddon() { return 'https://www.linkedin.com/in/' . $this->get( User::LINKEDIN ); } /** * Get the user GitHub profile URL * * @return string URL */ public function getUserGithubbo() { return 'https://github.com/' . $this->get( User::GITHUB ); } /** * Get the edit URL to this user * * @return string */ public function getUserEditURL() { $url = http_build_get_query( '2016/user-edit.php', [ 'uid' => $this->getUserUID(), ] ); return site_page( $url ); } /** * Can you edit this user? * * @return bool */ public function hasPermissionToEditUser() { if( has_permission('edit-users') ) { return true; } if( has_permission('edit-account') && $this->isUserMyself() ) { return true; } return false; } /** * Is this user myself? * * @return bool */ public function isUserMyself() { return is_logged() && get_user()->getUserID() === $this->getUserID(); } /** * Check if the User has a loved license * * @return */ public function hasUserLovelicense() { return $this->has( User::LOVED_LICENSE ); } /** * Get the User loved license * * @return License */ public function getUserLovelicense() { return license( $this->get( User::LOVED_LICENSE ) ); } function factoryUserSkills() { return UserSkill::factorySkillByUser( $this->getUserID() ); } function factoryUserEvents() { return FullEvent::factoryByUser( $this->getUserID() ); } /** * Normalize a User object */ protected function normalizeUser() { $this->integers( User::ID ); $this->booleans( User::IS_PUBLIC, User::IS_ACTIVE ); } } class User extends Sessionuser { use UserTrait; /** * Name column */ const NAME = 'user_name'; /** * Aristocratic title column */ const ARISTOCRATIC_TITLE = 'user_title'; /** * ID column */ const SURNAME = 'user_surname'; /** * He/she public column */ const IS_PUBLIC = 'user_public'; /** * Image column */ const IMAGE = 'user_image'; /** * E-mail */ const EMAIL = 'user_email'; /** * Gravatar column */ const GRAVATAR = 'user_gravatar'; /** * RSS column */ const RSS = 'user_rss'; /** * Facebook username column */ const FACEBOOK = 'user_fb'; /** * Linkedin username column */ const LINKEDIN = 'user_lnkd'; /** * Google+ username column */ const GOOGLE_PLUS = 'user_googl'; /** * Twitter username */ const TWITTER = 'user_twtr'; /** * GitHub username */ const GITHUB = 'user_github'; /** * Personal website column */ const WEBSITE = 'user_site'; /** * Loved license column */ const LOVED_LICENSE = 'user_lovelicense'; /** * Biography column */ const BIO = 'user_bio'; /** * Complete ID column name */ const ID_ = self::T . DOT . self::ID; /** * Maximum UID length */ const MAXLEN_UID = 64; /** * Constructor */ public function __construct() { $this->normalizeUser(); } /** * Get all the user social fields * * @return array */ public static function allSocialFields() { return [ User::RSS, User::FACEBOOK, User::LINKEDIN, User::GOOGLE_PLUS, User::TWITTER, User::GITHUB, ]; } /** * Factory users by an event * * @param $event_ID int * @return Query */ public static function factoryByEvent( $event_ID ) { return self::factory() ->from('event_user') ->equals('event_user.user_ID', 'user.user_ID') ->whereInt('event_user.event_ID', $event_ID ); } + + /** + * Generate the appropriate SELECT for the User Bio + * + * @return string + */ + public static function BIO_L10N() { + return i18n_coalesce( 'user_bio', 'user_bio_%s' ); + } } diff --git a/includes/functions.php b/includes/functions.php index 50cabed..2ad7784 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,24 +1,60 @@ . 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' + * @return string + */ +function i18n_coalesce( $name, $i18n ) { + + // current language (e.g. 'en') + $lang = latest_language()->getISO(); + + // default language of the website + $default = 'it'; + + // 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"; +}