Skip to content

Commit

Permalink
Merge pull request #908 from heiglandreas/addMultipleHosts
Browse files Browse the repository at this point in the history
Provide UI to manage hosts of an event.
  • Loading branch information
iansltx authored Aug 31, 2022
2 parents 8a264f7 + fb8e1ef commit f5a6bdb
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 10 deletions.
66 changes: 57 additions & 9 deletions app/src/Event/EventApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function getEvent($event_uri, $verbose = true)
foreach ($event->getHosts() as $hostsInfo) {
if (isset($hostsInfo->host_uri)) {
$hostsInfo->username = $this->userApi->getUsername($hostsInfo->host_uri);
$hostsInfo->entity = $this->userApi->getUser($hostsInfo->host_uri);
}
}
return $event;
Expand Down Expand Up @@ -171,7 +172,7 @@ public function addComment($event, $comment, $rating = 0)
'comment' => $comment,
'rating' => $rating,
];
list($status, $result) = $this->apiPost($uri, $params);
[$status, $result] = $this->apiPost($uri, $params);

if ($status == 201) {
return true;
Expand All @@ -181,7 +182,7 @@ public function addComment($event, $comment, $rating = 0)

public function reportComment($uri)
{
list($status, $result) = $this->apiPost($uri);
[$status, $result] = $this->apiPost($uri);

if ($status == 202) {
return true;
Expand All @@ -191,7 +192,7 @@ public function reportComment($uri)

public function attend(EventEntity $event)
{
list($status, $result) = $this->apiPost($event->getApiUriToMarkAsAttending());
[$status, $result] = $this->apiPost($event->getApiUriToMarkAsAttending());

if ($status == 201) {
return true;
Expand All @@ -202,7 +203,7 @@ public function attend(EventEntity $event)

public function unattend(EventEntity $event)
{
list($status, $result) = $this->apiDelete($event->getApiUriToMarkAsAttending());
[$status, $result] = $this->apiDelete($event->getApiUriToMarkAsAttending());

if ($status == 200) {
return true;
Expand Down Expand Up @@ -265,7 +266,7 @@ public function submit(array $data)
}
}

list($status, $result, $headers) = $this->apiPost($this->baseApiUrl . '/v2.1/events', $data);
[$status, $result, $headers] = $this->apiPost($this->baseApiUrl . '/v2.1/events', $data);

// if successful, return event entity represented by the URL in the Location header
if ($status == 201) {
Expand Down Expand Up @@ -312,7 +313,7 @@ public function edit(array $data)
}


list($status, $result, $headers) = $this->apiPut($data['uri'], $data);
[$status, $result, $headers] = $this->apiPut($data['uri'], $data);
// if successful, return event entity represented by the URL in the Location header
if ($status == 204) {
$response = $this->getCollection($headers['location']);
Expand All @@ -322,6 +323,53 @@ public function edit(array $data)
throw new Exception('Your event submission was not accepted, the server reports: ' . $result);
}

/**
* Submit a new host to the API and return it.
*
* If something happened NULL is returned
*
* @param array $data
*
* @throws Exception if a status code other than 201 is returned.
* @see EventHostFormType::buildForm() for a list of supported fields in the $data array
* @return EventEntity|null
*/
public function editHost(array $data)
{
[$status, $result, $headers] = $this->apiPost($data['hosts_uri'], [
'host_name' => $data['host']
]);
// if successful, return event entity represented by the URL in the Location header
if ($status == 204) {
$response = $this->getCollection($headers['location']);
return current($response['events']);
}

throw new Exception('Your new host was not accepted, the server reports: ' . $result);
}

/**
* Submit a new host to the API and return it.
*
* If something happened NULL is returned
*
* @param array $data
*
* @throws Exception if a status code other than 201 is returned.
* @see EventHostFormType::buildForm() for a list of supported fields in the $data array
* @return true
*/
public function removeHost(array $data)
{
[$status, $result, $headers] = $this->apiDelete($data['hosts_uri'] . '/' . $data['host']);
// if successful, return event entity represented by the URL in the Location header
if ($status == 204) {
return true;
}

throw new Exception('Removing the host failed. The server reports: ' . $result);
}

/**
* Upload event image - this one's a bit special as it's a form post
*
Expand Down Expand Up @@ -455,7 +503,7 @@ public function getTalkComments($comment_uri, $limit = 10, $start = 1, $verbose
*/
public function approveEvent($approval_uri)
{
list($status, $result, $headers) = $this->apiPost($approval_uri);
[$status, $result, $headers] = $this->apiPost($approval_uri);

if ($status == 204) {
return true;
Expand All @@ -471,7 +519,7 @@ public function approveEvent($approval_uri)
*/
public function rejectEvent($approval_uri)
{
list($status, $result, $headers) = $this->apiDelete($approval_uri);
[$status, $result, $headers] = $this->apiDelete($approval_uri);

if ($status == 204) {
return true;
Expand Down Expand Up @@ -534,7 +582,7 @@ public function moderateComment($reported_uri, $decision)
{
$data['decision'] = $decision;

list($status, $result, $headers) = $this->apiPut($reported_uri, $data);
[$status, $result, $headers] = $this->apiPut($reported_uri, $data);

// if successful, return event entity represented by the URL in the Location header
if ($status == 204) {
Expand Down
145 changes: 145 additions & 0 deletions app/src/Event/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ protected function defineRoutes(Slim $app)
->name("event-edit-tracks");
$app->map('/event/:friendly_name/claims', [$this, 'talkClaims'])->via('GET', 'POST')
->name("event-talk-claims");
$app->map('/event/:friendly_name/host/:host_name/remove', [$this, 'removeHost'])->via('GET', 'POST')
->name('event-host-remove');
$app->map('/event/:friendly_name/host', [$this, 'addHost'])->via('GET', 'POST')
->name('event-hosts');
}

public function index()
Expand Down Expand Up @@ -767,6 +771,28 @@ private function editEventUsingForm(Form $form)
return $result;
}

/**
* Submits the form data to the API and returns the edited event, false if there is an error or null
* if it is held for moderation.
*
* Should an error occur will this method append an error message to the form's error collection.
*
* @param Form $form
*
* @return EventEntity|null|false
*/
private function editEventHostUsingForm(EventEntity $event, $hostUsername)
{
$eventApi = $this->getEventApi();

$values = [
'hosts_uri' => $event->getHostsUri(),
'host' => $hostUsername,
];

return $eventApi->editHost($values);
}

protected function getEventApi()
{
$cache = $this->getCache();
Expand Down Expand Up @@ -982,6 +1008,125 @@ public function talkClaims($friendly_name)
);
}
}

public function removeHost($friendly_name, $host_name)
{
$request = $this->application->request();

$eventApi = $this->getEventApi();
$event = $eventApi->getByFriendlyUrl($friendly_name);
if (! $event) {
$this->redirectToListPage();
}

$userApi = $this->getUserApi();
$user = $userApi->getUserByUsername($host_name);

$data = [
'hosts_uri' => $event->getHostsUri(),
'host' => $user->getId(),
];

if (!isset($_SESSION['user'])) {
$this->application->redirect(
$this->application->urlFor('not-allowed') . '?redirect='
. $this->application->urlFor('event-hosts', ['friendly_name' => $friendly_name])
);
}

try {
$eventApi->removeHost($data);
$this->application->flash(
'message',
sprintf(
'<strong>%1$s</strong> was removed as host for this event. Make sure they know about that!',
$user->getFullName()
)
);
$this->application->redirect(
$this->application->urlFor('event-hosts', ['friendly_name' => $friendly_name]),
204
);
} catch (Exception $e) {
$this->application->flash(
'error',
$e->getMessage()
);
}

$this->application->redirect(
$this->application->urlFor('event-hosts', ['friendly_name' => $friendly_name]),
);
}

public function addHost($friendly_name)
{
$request = $this->application->request();

$eventApi = $this->getEventApi();
$event = $eventApi->getByFriendlyUrl($friendly_name);
if (! $event) {
$this->redirectToListPage();
}

$errors = [];

/** @var FormFactoryInterface $factory */
$factory = $this->application->formFactory;
$form = $factory->create(new EventHostFormType(), array_merge($event->toArray(), [
'host' => '',
]));
if ($request->isPost()) {
if (!isset($_SESSION['user'])) {
$this->application->redirect(
$this->application->urlFor('not-allowed') . '?redirect='
. $this->application->urlFor('event-hosts', ['friendly_name' => $friendly_name])
);
}
$form->submit($request->post($form->getName()));

if ($form->isValid()) {
$values = $form->getData();
try {
$result = $this->editEventHostUsingForm($event, $values['host']);
if ($result instanceof EventEntity) {
$userApi = $this->getUserApi();
$user = $userApi->getUserByUsername($values['host']);
$this->application->flash(
'message',
sprintf(
'<strong>%1$s</strong> was added as host. Make sure they know about their new role!',
$user->getFullName()
)
);

$this->application->redirect(
$this->application->urlFor('event-hosts', ['friendly_name' => $friendly_name]),
204
);
}
} catch (Exception $e) {
$this->application->flash(
'error',
$e->getMessage()
);
$this->application->redirect(
$this->application->urlFor('event-hosts', ['friendly_name' => $friendly_name]),
204
);
}
}
}

$this->render(
'Event/eventhosts.html.twig',
[
'event' => $event,
'form' => $event->getCanEdit() ? $form->createView(): '',
]
);
}

private function appoveClaimPendingTalk($talkApi, $claim, $data)
{
$talkApi->claimTalk($claim->approve_claim_uri, $data);
Expand Down
5 changes: 5 additions & 0 deletions app/src/Event/EventEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@ public function getTracksUri()
return $this->data->tracks_uri;
}

public function getHostsUri()
{
return $this->data->hosts_uri;
}

/**
* Used by the edit form
*/
Expand Down
71 changes: 71 additions & 0 deletions app/src/Event/EventHostFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Event;

use Form\Constraint\UrlResolverConstraint;
use Form\DataTransformer\DateTransformer;
use Form\DataTransformer\EventTagsTransformer;
use Form\Listener\GetResolvedUrlListener;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
* Form used to render and validate the submission of a new event.
*
* Usage (extraneous use of variables is made to illustrate which parts are used):
*
* ```
* $formType = new EventFormType();
* $factory = $this->application->formFactory;
* $form = $factory->create($formType);
* $formName = $form->getName();
*
* if ($this->application->request()->isPost()) {
* $data = $request->post($formName);
*
* $form->submit($data);
*
* if ($form->isValid()) {
* // ... perform success actions
* }
* }
* ```
*/
class EventHostFormType extends AbstractType
{
/**
* Returns the name of this form type.
*
* @return string
*/
public function getName()
{
return 'eventhost';
}

/**
* Adds fields with their types and validation constraints to this definition.
*
* This method is automatically called by the Form Factory builder and does not need
* to be called manually, see the class description for usage information.
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'host',
'text',
[
'label' => 'Add additional host',
'constraints' => [new Assert\NotBlank(), new Assert\Length(['min' => 2])],
]
)
;
}
}
Loading

0 comments on commit f5a6bdb

Please sign in to comment.