Skip to content

Commit

Permalink
Fix multiple pagination, add /events/check-dates/{slug} to check for …
Browse files Browse the repository at this point in the history
…overlapping events at the creation or modification of an event. Check for authorization on drafts on /events and /newsitems, create all personnel messages as 'Published'
  • Loading branch information
PhilippeFerreiraDeSousa committed Oct 20, 2017
1 parent 0e72392 commit e81e438
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 57 deletions.
4 changes: 2 additions & 2 deletions back/src/KI/CoreBundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class ResourceController extends LikeableController
* @param boolean $auth Un override éventuel pour le check des permissions
* @return Response
*/
public function getAll($auth = false)
public function getAll($auth = false, array $findBy = [])
{
$this->trust(!$this->is('EXTERIEUR') || $auth);

$paginateHelper = $this->get('ki_core.helper.paginate');
extract($paginateHelper->paginateData($this->repository));
extract($paginateHelper->paginateData($this->repository, $findBy));

list($results, $links, $count) = $paginateHelper->paginateView($results, $limit, $page, $totalPages, $count);

Expand Down
5 changes: 0 additions & 5 deletions back/src/KI/CoreBundle/Helper/PaginateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public function paginateData(EntityRepository $repository, array $findBy = [])
$queryBuilder = $repository->createQueryBuilder('o');
$request = $this->request->query;

// On s'assure de bien recevoir des arrays

This comment has been minimized.

Copy link
@PhilippeFerreiraDeSousa

PhilippeFerreiraDeSousa Oct 20, 2017

Author Member

La pagination multiple ne marchait pas car si A est un array, array(A) ne s'assure pas que A en est un mais met dans A array(0 => A).

foreach ($findBy as $key => $value) {
$findBy[$key] = array($value);
}

// On récupère les paramètres de la requête
$page = $request->has('page') ? $request->get('page') : 1;
$limit = $request->has('limit') ? $request->get('limit') : 100;
Expand Down
35 changes: 33 additions & 2 deletions back/src/KI/PublicationBundle/Controller/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function setContainer(ContainerInterface $container = null)
*/
public function getEventsAction()
{
return $this->getAll();
$findBy = array('publicationState' => array('Scheduled', 'Published', 'Emailed'));
return $this->getAll(false, $findBy);
}

/**
Expand All @@ -64,7 +65,7 @@ public function getEventAction($slug)
{
$event = $this->getOne($slug);
if ($event->getPublicationState() == 'Draft' && !$this->isClubMember($event->getAuthorClub())) {
throw new BadRequestHttpException('Tu n\'es pas autorisé à lire ce brouillon !');
return $this->json('Tu n\'es pas autorisé à lire ce brouillon !', 403);
}

return $this->json($event);
Expand Down Expand Up @@ -162,6 +163,36 @@ public function deleteEventAction($slug)
return $this->json(null, 204);
}

/**
* @ApiDoc(
* description="Renvoie la liste des événements qui chevauchent un créneau horaire",
* statusCodes={
* 200="Requête traitée avec succès",
* 401="Une authentification est nécessaire pour effectuer cette action",
* 503="Service temporairement indisponible ou en maintenance",
* },
* section="Publications"
* )
* @Route("/events/{slug}/check-dates")
* @Method("GET")
*/
public function getEventCheckDatesAction(Request $request, $slug)
{
$startDate = $request->query->get('startDate');
$endDate = $request->query->get('endDate');
$events = $this->repository->findBy(array('publicationState' => array('Scheduled', 'Published', 'Emailed')));
$matchedEvents = array();
foreach ($events as $event) {
$eventStartDate = $event->getStartDate();
$eventEndDate = $event->getEndDate();
if ($startDate < $eventEndDate && $eventStartDate < $endDate && $slug != $event->getSlug()) {
$matchedEvents[] = $event;
}
}
return $this->json($matchedEvents);

}

/**
* @ApiDoc(
* description="Shotgunne un événement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function setContainer(ContainerInterface $container = null)
*/
public function getNewsitemsAction()
{
return $this->getAll($this->is('EXTERIEUR'));
$findBy = array('publicationState' => array('Scheduled', 'Published', 'Emailed'));
return $this->getAll($this->is('EXTERIEUR'), $findBy);
}

/**
Expand All @@ -58,7 +59,7 @@ public function getNewsitemAction($slug)
{
$newsitem = $this->getOne($slug, $this->is('EXTERIEUR'));
if ($newsitem->getPublicationState() == 'Draft' && !$this->isClubMember($newsitem->getAuthorClub())) {
throw new BadRequestHttpException('Tu n\'es pas autorisé à lire ce brouillon !');
return $this->json('Tu n\'es pas autorisé à lire ce brouillon !', 403);
}

return $this->json($newsitem);
Expand Down
33 changes: 17 additions & 16 deletions back/src/KI/PublicationBundle/DataFixtures/ORM/LoadNewsitem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function load(ObjectManager $manager)
$newsitem->setDate(1414242424);
$newsitem->setAuthorClub($this->getReference('club-ki'));
$newsitem->setAuthorUser($this->getReference('user-taquet-c'));
$newsitem->setPublicationState('Draft');
$newsitem->setPublicationState('Published');
$newsitem->setLikes([$this->getReference('user-taquet-c')]);
$newsitem->setDislikes([$this->getReference('user-trancara')]);
$manager->persist($newsitem);
Expand All @@ -28,7 +28,7 @@ public function load(ObjectManager $manager)
$newsitem->setDate(1418325122);
$newsitem->setAuthorClub($this->getReference('club-ki'));
$newsitem->setAuthorUser($this->getReference('user-trancara'));
$newsitem->setPublicationState('Published');
$newsitem->setPublicationState('Draft');
$newsitem->setImage($this->getReference('image-newsitem-git'));
$manager->persist($newsitem);

Expand Down Expand Up @@ -73,34 +73,35 @@ public function load(ObjectManager $manager)
$newsitem->setPublicationState('Published');
$manager->persist($newsitem);

$newsitem = new Newsitem();
$newsitem->setName('Le béton c\'est bon.');
$newsitem->setText('L\'acier aussi, c\'est complètement METAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAL.');
$newsitem->setDate(time() - 21*3600);
$newsitem->setAuthorUser($this->getReference('user-gcc'));
$newsitem->setAuthorClub($this->getReference('club-gcc'));
$newsitem->setLikes([$this->getReference('user-taquet-c')]);
$newsitem->setDislikes([$this->getReference('user-trancara'), $this->getReference('user-dziris')]);
$newsitem->setPublicationState('Emailed');
$newsitem->addComment($this->getReference('comment-rage'));
$newsitem->addComment($this->getReference('comment-arret'));
$manager->persist($newsitem);

// Messages persos
$newsitem = new Newsitem();
$newsitem->setName('message');
$newsitem->setText('Venez tous jouer à AgeOf !');
$newsitem->setDate(time() - 3600);
$newsitem->setAuthorUser($this->getReference('user-trancara'));
$newsitem->setImage($this->getReference('image-game-age-of-empires-2'));
$newsitem->setPublicationState('Published');
$manager->persist($newsitem);

$newsitem = new Newsitem();
$newsitem->setName('message');
$newsitem->setText('[Le rêve de Jeanine]<br>Est ce que vous voyez la pluie tomber sur notre calme Champs sur Marne? Vous êtes vous seulement posés la question, ne serait-ce qu\'une fois, de ce que toute cette eau devenait? Cette nonchalance de votre part est permise seulement par la puissance de nouveaux radars qui mesurent précisément la minute et la rue où va se déverser le prochain orage sur les villes d\'Ile de France.');
$newsitem->setDate(time() - 42*3600);
$newsitem->setAuthorUser($this->getReference('user-dziris'));
$newsitem->setPublicationState('Scheduled');
$manager->persist($newsitem);

$newsitem = new Newsitem();
$newsitem->setName('Le béton c\'est bon.');
$newsitem->setText('L\'acier aussi, c\'est complètement METAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAL.');
$newsitem->setDate(time() - 21*3600);
$newsitem->setAuthorUser($this->getReference('user-gcc'));
$newsitem->setAuthorClub($this->getReference('club-gcc'));
$newsitem->setLikes([$this->getReference('user-taquet-c')]);
$newsitem->setDislikes([$this->getReference('user-trancara'), $this->getReference('user-dziris')]);
$newsitem->setPublicationState('Emailed');
$newsitem->addComment($this->getReference('comment-rage'));
$newsitem->addComment($this->getReference('comment-arret'));
$newsitem->setPublicationState('Published');
$manager->persist($newsitem);

$manager->flush();
Expand Down
3 changes: 2 additions & 1 deletion front/app/js/controllers/users/publications/post-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ angular.module('upont')
$scope.post = function(msg, image){
var params = {
text: msg.text,
name: 'message'
name: 'message',
publicationState: 'Published'
};

if (image) {
Expand Down
83 changes: 54 additions & 29 deletions front/app/js/controllers/users/publications/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ angular.module('upont')
var init = function() {
$scope.focus = false;
$scope.post = {
entry_method: 'Entrée libre',
entry_method: 'Libre',
publication_state: 'Published',
text: '',
start_date: '',
Expand Down Expand Up @@ -79,6 +79,36 @@ angular.module('upont')
$scope.postFiles = files;
};

$scope.submitEvent = function(params, postSlug) {
if (!$scope.modify) {
Upload.upload({
method: "POST",
url: apiPrefix + 'events',
data: params
}).then(function() {
$rootScope.$broadcast('newEvent');
Achievements.check();
init();
alertify.success('Événement publié');
$scope.isLoading = false;
}, function() {
alertify.error('Formulaire vide ou mal rempli');
$scope.isLoading = false;
});
} else {
$http.patch(apiPrefix + 'events/' + postSlug, params)
.then(function() {
$scope.$emit('modifiedEvent');
alertify.success('Événement modifié');
init();
$scope.isLoading = false;
}, function() {
alertify.error('Formulaire vide ou mal rempli');
$scope.isLoading = false;
});
}
};

$scope.publish = function(post, files) {
var params = {
text: nl2br(post.text),
Expand Down Expand Up @@ -170,34 +200,29 @@ angular.module('upont')

if(!$scope.isLoading) {
$scope.isLoading = true;

if (!$scope.modify) {
Upload.upload({
method: "POST",
url: apiPrefix + 'events',
data: params
}).then(function() {
$rootScope.$broadcast('newEvent');
Achievements.check();
init();
alertify.success('Événement publié');
$scope.isLoading = false;
}, function() {
alertify.error('Formulaire vide ou mal rempli');
$scope.isLoading = false;
});
} else {
$http.patch(apiPrefix + 'events/' + post.slug, params)
.then(function() {
$scope.$emit('modifiedEvent');
alertify.success('Événement modifié');
init();
$scope.isLoading = false;
}, function() {
alertify.error('Formulaire vide ou mal rempli');
$scope.isLoading = false;
});
}
$http.get(apiPrefix + 'events/' + post.slug + '/check-dates?startDate=' + params.startDate + '&endDate=' + params.endDate).then(function(response){
var unravellingEvents = response.data;
var alertMessage = 'Ces événements sont déjà prévus sur ce créneau : ';
for (var i = 0; i < unravellingEvents.length; i++) {
if (i > 0) { alertMessage += ', '}
alertMessage += '[' + unravellingEvents[i].author_club.name + '] ' + unravellingEvents[i].name;
}
alertMessage += '. Continuer tout de même ?';
if(unravellingEvents.length > 0) {
alertify.confirm(
alertMessage,
function(e) {
if(!e) {
$scope.isLoading = false;
return;
}
$scope.submitEvent(params, post.slug);
}
);
} else {
$scope.submitEvent(params, post.slug);
}
});
}
break;
default:
Expand Down

0 comments on commit e81e438

Please sign in to comment.