Skip to content

Commit

Permalink
wip ban entity
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvanspauwen committed Oct 19, 2023
1 parent 40544ce commit 08b6e33
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 31 deletions.
35 changes: 35 additions & 0 deletions migrations/Version20231019133431.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace Migrations;

use Doctrine\DBAL\Schema\Schema;

/**
* Version 20231019133431
*/
class Version20231019133431 extends \Doctrine\Migrations\AbstractMigration
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
* @return void
*/
public function up(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('CREATE SEQUENCE shop_reservations_bans_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE shop_reservations_bans (id BIGINT NOT NULL, person_username VARCHAR(50) DEFAULT NULL, startTimestamp TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, endTimestamp TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_197F9CC06C18A340 ON shop_reservations_bans (person_username)');
$this->addSql('ALTER TABLE shop_reservations_bans ADD CONSTRAINT FK_197F9CC06C18A340 FOREIGN KEY (person_username) REFERENCES users_people (username) NOT DEFERRABLE INITIALLY IMMEDIATE');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
* @return void
*/
public function down(Schema $schema) : void
{
$this->throwIrreversibleMigrationException();
}
}
37 changes: 37 additions & 0 deletions migrations/Version20231019134846.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace Migrations;

use Doctrine\DBAL\Schema\Schema;

/**
* Version 20231019134846
*/
class Version20231019134846 extends \Doctrine\Migrations\AbstractMigration
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
* @return void
*/
public function up(Schema $schema) : void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('ALTER TABLE shop_reservations_bans DROP CONSTRAINT fk_197f9cc06c18a340');
$this->addSql('DROP INDEX idx_197f9cc06c18a340');
$this->addSql('ALTER TABLE shop_reservations_bans ADD person BIGINT DEFAULT NULL');
$this->addSql('ALTER TABLE shop_reservations_bans DROP person_username');
$this->addSql('ALTER TABLE shop_reservations_bans ADD CONSTRAINT FK_197F9CC034DCD176 FOREIGN KEY (person) REFERENCES users_people (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_197F9CC034DCD176 ON shop_reservations_bans (person)');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
* @return void
*/
public function down(Schema $schema) : void
{
$this->throwIrreversibleMigrationException();
}
}
79 changes: 49 additions & 30 deletions module/ShopBundle/Controller/Admin/ReservationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laminas\Http\Headers;
use Laminas\View\Model\ViewModel;
use ShopBundle\Entity\Reservation;
use ShopBundle\Entity\Reservation\Ban;
use ShopBundle\Entity\Reservation\Permission as ReservationPermission;
use ShopBundle\Entity\Session as SalesSession;

Expand Down Expand Up @@ -84,43 +85,61 @@ public function noshowAction()
return new ViewModel();
}

$reservation->setNoShow(!$reservation->getNoShow());
$blacklisted = false;
$blacklistAvoided = false;
$ban = new Ban();
$ban->SetPerson($reservation->getPerson());
$ban->setStartTimestamp(time());
$ban->setEndTimestamp(time() + (7 * 24 * 60 * 60));
error_log("after ban");

$this->getEntityManager()->persist($reservation);
$this->getEntityManager()->persist($ban);
$this->getEntityManager()->flush();

if ($reservation->getNoShow()) {
$maxNoShows = $this->getEntityManager()
->getRepository('CommonBundle\Entity\General\Config')
->getConfigValue('shop.maximal_no_shows');
$currentNoShows = $this->getEntityManager()
->getRepository('ShopBundle\Entity\Reservation')
->getNoShowSessionCount($reservation->getPerson());
if ($currentNoShows >= $maxNoShows) {
$reservationPermission = $this->getEntityManager()
->getRepository('ShopBundle\Entity\Reservation\Permission')
->find($reservation->getPerson());
if ($reservationPermission) {
$blacklistAvoided = $reservationPermission->getReservationsAllowed();
} else {
$blacklisted = true;
$reservationPermission = new ReservationPermission();
$reservationPermission->setPerson($reservation->getPerson());
$reservationPermission->setReservationsAllowed(false);
$this->getEntityManager()->persist($reservationPermission);
$this->getEntityManager()->flush();
}
}
}
error_log("after flush");


// $this->initAjax();
//
// $reservation = $this->getReservationEntity();
// if ($reservation === null) {
// return new ViewModel();
// }
//
// $reservation->setNoShow(!$reservation->getNoShow());
// $blacklisted = false;
// $blacklistAvoided = false;
//
// $this->getEntityManager()->persist($reservation);
// $this->getEntityManager()->flush();
//
// if ($reservation->getNoShow()) {
// $maxNoShows = $this->getEntityManager()
// ->getRepository('CommonBundle\Entity\General\Config')
// ->getConfigValue('shop.maximal_no_shows');
// $currentNoShows = $this->getEntityManager()
// ->getRepository('ShopBundle\Entity\Reservation')
// ->getNoShowSessionCount($reservation->getPerson());
// if ($currentNoShows >= $maxNoShows) {
// $reservationPermission = $this->getEntityManager()
// ->getRepository('ShopBundle\Entity\Reservation\Permission')
// ->find($reservation->getPerson());
// if ($reservationPermission) {
// $blacklistAvoided = $reservationPermission->getReservationsAllowed();
// } else {
// $blacklisted = true;
// $reservationPermission = new ReservationPermission();
// $reservationPermission->setPerson($reservation->getPerson());
// $reservationPermission->setReservationsAllowed(false);
// $this->getEntityManager()->persist($reservationPermission);
// $this->getEntityManager()->flush();
// }
// }
// }

return new ViewModel(
array(
'result' => array(
'status' => 'success',
'blacklisted' => $blacklisted,
'blacklistAvoided' => $blacklistAvoided,
// 'blacklisted' => $blacklisted,
// 'blacklistAvoided' => $blacklistAvoided,
),
)
);
Expand Down
132 changes: 132 additions & 0 deletions module/ShopBundle/Entity/Reservation/Ban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace ShopBundle\Entity\Reservation;

use DateTime;
use CommonBundle\Entity\User\Person;
use Doctrine\ORM\Mapping as ORM;

/**
* This entity stores a reservation ban for a user.
*
* @ORM\Entity(repositoryClass="ShopBundle\Repository\Reservation\Ban")
* @ORM\Table(name="shop_reservations_bans")
*/
class Ban
{
/**
* @var integer The ID of this ban
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="bigint")
*/
private $id;

/**
* @var Person The person this ban belongs to
*
* @ORM\ManyToOne(targetEntity="CommonBundle\Entity\User\Person")
* @ORM\JoinColumn(name="person", referencedColumnName="id")
*/
private $person;

/**
* @var DateTime The timestamp at which the ban starts
*
* @ORM\Column(type="datetime")
*/
private $startTimestamp;

/**
* @var DateTime The timestamp at which the ban ends (null if it does not end)
*
* @ORM\Column(type="datetime", nullable=true)
*/
private $endTimestamp;

public function __construct() {
}

/**
* @return Person The person this ban applies to
*/
public function GetPerson() {
return $this->person;
}

/**
* @param $person
* @return $this
*/
public function SetPerson($person) {
$this->person = $person;

return $this;
}

/**
* @return DateTime
*/
public function getStartTimestamp() {
return $this->startTimestamp;
}

/**
* @param $timestamp
* @return $this
*/
public function setStartTimestamp($timestamp) {
$this->startTimestamp = $timestamp;

return $this;
}

/**
* @return DateTime|null
*/
public function getEndTimestamp() {
return $this->endTimestamp;
}

/**
* @param $timestamp
* @return $this
*/
public function setEndTimestamp($timestamp) {
$this->endTimestamp = $timestamp;

return $this;
}

/**
* Set end timestamp to null so that ban will never end
*
* @return $this
*/
public function removeEndTimestamp() {
$this->endTimestamp = null;

return $this;
}

/**
* @return bool Whether the ban is currently active on the user
*/
public function IsActive() {
// Get the current timestamp
$currentTimestamp = time();

if ($currentTimestamp >= $this->startTimestamp) {
if ($this->endTimestamp === null) {
return true; // User is banned indefinitely
}

if ($currentTimestamp <= $this->endTimestamp) {
return true; // User is currently banned
}
}

return false;
}
}
2 changes: 1 addition & 1 deletion module/ShopBundle/Entity/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Session
private $endDate;

/**
* @var DateTime The end date for reservations for this sales session
* @var DateTime|null The end date for reservations for this sales session
*
* @ORM\Column(type="datetime", nullable=true)
*/
Expand Down
48 changes: 48 additions & 0 deletions module/ShopBundle/Hydrator/Reservation/Ban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace ShopBundle\Hydrator\Reservation;

use ShopBundle\Entity\Reservation\Ban as BanEntity;

class Ban extends \CommonBundle\Component\Hydrator\Hydrator
{
protected function doExtract($object = null)
{
error_log("extract");
return array();
// error_log("extract");
// if ($object === null) {
// return array();
// }
//
// $data = $this->stdExtract($object);
// $data['start_timestamp'] = $object->getStartTimestamp()->format('d/m/Y H:i');
// $data['end_timestamp'] = $object->getEndTimestamp() ? $object->getEndTimestamp()->format('d/m/Y H:i') : ' ';
//// $data['person']['id'] = $object->getPerson()->getId();
//
// return $data;
}

protected function doHydrate(array $data, $object = null)
{
error_log("hydrate");

if ($object === null) {
$object = new BanEntity();
}

return $object;
//
//
//
// $object->setStartTimestamp(self::loadDateTime($data['start_timestamp']));
// $object->setEndTimestamp(self::loadDateTime($data['end_timestamp']));
//
// $person = $this->getEntityManager()
// ->getRepository('CommonBundle\Entity\User\Person')
// ->find($data['person']['id']);
// $object->setPerson($person);
//
// return $object;
}
}
8 changes: 8 additions & 0 deletions module/ShopBundle/Repository/Reservation/Ban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ShopBundle\Repository\Reservation;

class Ban extends \CommonBundle\Component\Doctrine\ORM\EntityRepository
{

}

0 comments on commit 08b6e33

Please sign in to comment.