diff --git a/admin/event-edit.php b/admin/event-edit.php
index 043ade3..86f3259 100644
--- a/admin/event-edit.php
+++ b/admin/event-edit.php
@@ -1,666 +1,666 @@
.
/*
* Event edit
*
* From this page you can create/edit an User and assign some skills/interests etc.
*/
// load configurations and framework
require 'load.php';
// inherit the Conference or specify one
$conference_uid = $_GET['conference_uid'] ?? $_POST['conference_uid'] ?? CURRENT_CONFERENCE_UID;
// check if the Conference exists
$conference = ( new QueryConference() )
->whereConferenceUID( $conference_uid )
->queryRow();
// no Conference no party
if( !$conference ) {
error( "missing conference with UID $conferene_uid" );
die_with_404();
}
// retrieve the Event (if existing)
$event = null;
if( isset( $_GET['id'] ) ) {
// no Event no party
$event = ( new QueryEvent() )
->whereConference( $conference )
->joinConference()
->joinChapter( 'LEFT' )
->whereEventID( $_GET['id'] )
->queryRow();
// no Event no party
if( !$event ) {
die_with_404();
}
// no editable no party
if( !$event->isEventEditable() ) {
missing_privileges();
}
} else {
// check if there are permissions to add event
if( !has_permission( 'add-event' ) ) {
missing_privileges();
}
}
$warning = null;
// check if the user submitted a form
// check which one
if( $_POST ) {
// the user is submitting the save form
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::START, $_POST['start'], 's' );
$data[] = new DBCol( Event::END, $_POST['end'], 's' );
$data[] = new DBCol( Event::EXTERNAL_URL,$_POST['url'], 's' );
$data[] = new DBCol( Event::ABORTED, $_POST['aborted'], 'd' );
$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( Event::fields_i18n() 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
( 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->getEventEditURL(), 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 ) {
( new QueryEventUser() )
->whereEvent( $event )
->whereUser( $user )
->delete();
( new QueryEventUser() )->insertRow( [
new DBCol( Event::ID, $event->getEventID(), 'd' ),
new DBCol( User ::ID, $user->getUserID(), 'd' ),
new DBCol( EventUser::ORDER, 0, '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
( new QueryEventUser() )
->whereEvent( $event )
->whereUser( $user )
->delete();
} elseif( isset( $_POST['order'] ) ) {
// change order
( new QueryEventUser() )
->whereEvent( $event )
->whereUser( $user )
->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" )
),
] );
}
?>
= esc_html( $warning ) ?>
= HTML::a(
$conference->getConferenceURL(),
esc_html( $conference->getConferenceTitle() ) . icon( 'home', 'left' )
) ?>
hasEventPermalink() ): ?>
= HTML::a(
// href
$event->getEventURL(),
// text
__( "Vedi" ) . icon( 'account_box', 'left' )
) ?>
= HTML::a(
- ROOT . '/2016/user-edit.php',
+ User::editURL(),
sprintf(
__( "Aggiungi %s" ),
sprintf(
__( "Nuovo %s" ),
__( "Utente" )
)
)
) ?>
factoryUserByEvent()
->select( [
User::UID,
EventUser::ORDER,
] )
->defaultClass( EventUser::class )
->orderBy( EventUser::ORDER )
->queryGenerator();
?>
valid() ): ?>
.
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::IMAGE, $_POST['image'], 'snull' );
$data[] = new DBCol( User::GRAVATAR, $_POST['gravatar'], 'snull' );
$data[] = new DBCol( User::FACEBOOK, $_POST['facebook'], 'snull' );
$data[] = new DBCol( User::LINKEDIN, $_POST['linkedin'], 'snull' );
$data[] = new DBCol( User::GITHUB, $_POST['github'], 'snull' );
$data[] = new DBCol( User::TWITTER, $_POST['twitter'], 'snull' );
$data[] = new DBCol( User::META_WIKI, $_POST['metawiki'], 'snull' );
$data[] = new DBCol( User::LOVED_LICENSE, $_POST['lovelicense'], 'snull' );
// for each language save the biography
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(), 303 );
+ http_redirect( $user->getUserEditURL() );
}
/**
* Change the Image
*/
if( $user && is_action( 'change-image' ) ) {
// prepare the image uploader
$image = new FileUploader( 'image', [
'category' => 'image',
'override-filename' => "user-" . $user->getUserUID(),
] );
// prepare the image pathnames
$img_url = LATEST_CONFERENCE_UID . _ . 'images';
$img_path = ABSPATH . __ . LATEST_CONFERENCE_UID . __ . 'images';
// really upload that shitty image somewhere
if( $image->fileChoosed() ) {
$ok = $image->uploadTo( $img_path, $status, $filename, $ext );
if( $ok ) {
// now update
( new QueryUser() )
->whereUser( $user )
->update( [
'user_image' => $img_url . "/$filename.$ext",
] );
// POST-redirect-GET
- http_redirect( $user->getUserEditURL(), 303 );
+ http_redirect( $user->getUserEditURL() );
} else {
die( $image->getErrorMessage() );
}
}
}
// register action to create a Skill
if( is_action( 'create-skill' ) && isset( $_POST['skill_title'], $_POST['skill_type'] ) ) {
// generate a Skill UID
$skill_uid = generate_slug( $_POST['skill_title'], 32 );
// check if already exists
$skill = ( new QuerySkill() )
->whereSkillUID( $skill_uid )
->queryRow();
// create the Skill
if( !$skill ) {
( new QuerySkill() )
->insertRow( [
'skill_uid' => $skill_uid,
'skill_title' => $_POST['skill_title'],
'skill_type' => $_POST['skill_type'],
] );
}
}
// register action to edit an existing Skill
if( isset( $_POST['skill_uid'], $_POST['skill_score'] ) ) {
// find existing Skill
$skill = Skill::factoryFromUID( $_POST['skill_uid'] )
->queryRow();
// eventually create the Skill
if( !$skill ) {
( new QuerySkill() )
->insertRow( [
'skill_uid' => $_POST['skill_uid'],
] );
// retrieve last inserted Skill
$skill = ( new QuerySkill() )
->whereSkillID( last_inserted_ID() )
->queryRow();
}
// query the UserSkill
$query_userskill = ( new QueryUserSkill() )
->whereUser( $user )
->whereSkill( $skill );
// eventually change an existing skill
if( is_action( 'change-skill' ) ) {
// delete the Skill or just update?
if( isset( $_POST['skill_delete'] ) ) {
$query_userskill->delete();
} else {
// update the score
$query_userskill->update( [
'skill_score' => (int)$_POST['skill_score'],
] );
}
}
// eventually add a skill
if( is_action( 'add-skill' ) ) {
// eventually delete
$query_userskill->delete();
// then add the skill
( new QueryUserSkill() )
->insertRow( [
'user_ID' => $user->getUserID(),
'skill_ID' => $skill->getSkillID(),
'skill_score' => $_POST['skill_score'],
] );
}
}
// 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() )
+ ( new QueryUser() )
+ ->whereUser( $user )
->delete();
// POST -> redirect -> GET
- http_redirect( $user->getUserEditURL(), 303 );
+ http_redirect( $user->getUserEditURL() );
}
Header::spawn( null, [
'title' =>
$user
? sprintf(
__("Modifica %s"),
$user->getUserFullname()
)
: __( "Aggiungi Utente" )
,
] );
?>
= HTML::a(
$user->getUserURL(),
__( "Vedi" ) . icon('account_box', 'left')
) ?>
factoryUserSkills()
->queryGenerator();
?>
valid() ): ?>
.
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 trim( sprintf(
__("%s %s"),
$this->get( User::NAME ),
$this->get( User::SURNAME )
) );
}
/**
* Get the name that should be displayed
*
* @return string
*/
public function getUserDisplayName() {
return $this->getUserFullname();
}
/**
* 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()
);
$url = site_page( $url, $absolute );
return keep_url_in_language( $url );
}
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() {
return $this->get( User::GRAVATAR );
}
/**
* 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 user Wikimedia Meta-wiki URL
*
* @return string
*/
public function getUserMetaWikiURL() {
$username = $this->get( User::META_WIKI );
$username = str_replace( ' ', '_', $username );
return sprintf(
'https://meta.wikimedia.org/wiki/User:%s',
$username
);
}
/**
* Get the edit URL to this user
*
* @return string
*/
public function getUserEditURL() {
- $url = http_build_get_query( '2016/user-edit.php', [
+ return User::editURL( [
'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';
/**
* Username in Meta-wiki
*/
const META_WIKI = 'user_metawiki';
/**
* 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,
User::META_WIKI,
];
}
/**
* 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' );
}
/**
* Get all the public User fields
*
* @return array
*/
public static function fields() {
return [
self::ID,
self::UID,
self::GRAVATAR,
self::NAME,
self::SURNAME,
self::IMAGE,
self::WEBSITE,
self::LOVED_LICENSE,
self::BIO_L10N(),
];
}
+ /**
+ * Get the edit URL to an User
+ *
+ * @param array $args Arguments for the edit page
+ * @param boolean $absolute Flag to require an absolute URL
+ * @return string
+ */
+ public static function editURL( $args = [], $absolute = false ) {
+ $url = site_page( ADMIN_BASE_URL . '/user-edit.php', $absolute );
+ return http_build_get_query( $url, $args );
+ }
}