Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@

$app->get(
'/bookings/{event:guess|all|\d+}[/{option:page|order|clear_filter}/{value:\d+}]',
[BookingsController::class, 'listBookings']
[BookingsController::class, 'list']
)->setName('events_bookings')->add(Authenticate::class);

//bookings list filtering
$app->post(
'/bookings/filter/{event:guess|all|\d+}',
[BookingsController::class, 'filterBookings']
[BookingsController::class, 'filter']
)->setName('filter-bookingslist')->add(Authenticate::class);

$app->get(
Expand Down
3 changes: 1 addition & 2 deletions lib/GaletteEvents/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ public function store(): void
$this->transactional(function (): void {
$values = [
'name' => $this->name,
'is_active' => ($this->active ? $this->active
: ($this->zdb->isPostgres() ? 'false' : 0)),
'is_active' => (int)$this->active,
'comment' => $this->comment
];

Expand Down
164 changes: 50 additions & 114 deletions lib/GaletteEvents/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use Galette\Core\Login;
use Galette\Entity\Adherent;
use Galette\Entity\PaymentType;
use Galette\Repository\Groups;
use Analog\Analog;
use GaletteEvents\Repository\Events;

/**
* Booking entity
Expand Down Expand Up @@ -53,8 +53,6 @@ class Booking

/** @var array<int, array<string,mixed>> */
private array $activities = [];
/** @var array<int, array<string,mixed>> */
private array $activities_removed = [];
private ?string $creation_date = null;

/**
Expand Down Expand Up @@ -284,10 +282,6 @@ private function checkActivities(Event $event, array $checked): void
}
foreach (array_keys($this->activities) as $aid) {
if (!isset($activities[$aid])) {
$this->activities_removed[$aid] = [
Activity::PK => $aid,
self::PK => $this->id
];
unset($this->activities[$aid]);
}
}
Expand All @@ -303,8 +297,7 @@ public function store(): void
Event::PK => $this->event,
Adherent::PK => $this->member,
'booking_date' => $this->date,
'is_paid' => ($this->paid ? $this->paid
: ($this->zdb->isPostgres() ? 'false' : 0)),
'is_paid' => (int)$this->paid,
'payment_method' => $this->payment_method,
'payment_amount' => $this->amount,
'bank_name' => $this->bank_name,
Expand Down Expand Up @@ -352,117 +345,67 @@ public function store(): void
}
}

//store booking activities
$void = [];
$update = [];
$insert = [];
$delete = $this->activities_removed;

foreach ($this->activities as $aid => $data) {
$activity = $data['activity'];
$checked = $data['checked'];
$key_values = [
self::PK => $this->id,
$activity::PK => $activity->getId()
];
$this->storeActivities();
});
}

$select = $this->zdb->select(EVENTS_PREFIX . 'activitiesbookings', 'acb');
$select->where($key_values);
$results = $this->zdb->execute($select);

foreach ($results as $result) {
if (!isset($this->activities[$result[Activity::PK]])) {
$delete[$result[Activity::PK]] = [
Activity::PK => $result[Activity::PK],
self::PK => $this->id,
];
} elseif ($result['checked'] != $this->activities[$result[Activity::PK]]['checked']) {
$update[$result[Activity::PK]] = [
'checked' => ($checked ? $checked
: ($this->zdb->isPostgres() ? 'false' : 0))
];
} else {
$void[$result[Activity::PK]] = true;
}
}
/**
* Store activities of the booking, compared to the stored ones
*/
private function storeActivities(): void
{
$table = EVENTS_PREFIX . 'activitiesbookings';

if (!isset($void[$aid]) && !isset($update[$aid]) && !isset($delete[$aid])) {
$insert[$aid] = [
Activity::PK => $aid,
self::PK => $this->id,
'checked' => ($checked ? $checked
: ($this->zdb->isPostgres() ? 'false' : 0))
];
}
}
$stored = [];
$select = $this->zdb->select($table);
$select->where([self::PK => $this->id]);
foreach ($this->zdb->execute($select) as $row) {
$stored[(int)$row[Activity::PK]] = (bool)$row['checked'];
}

if (count($delete)) {
$prepare = $this->zdb->delete(EVENTS_PREFIX . 'activitiesbookings');
$prepare->where([
$counts = ['added' => 0, 'updated' => 0, 'removed' => 0];
foreach ($this->activities as $aid => $data) {
$checked = (bool)$data['checked'];
if (!isset($stored[$aid])) {
$insert = $this->zdb->insert($table);
$insert->values([
self::PK => $this->id,
Activity::PK => ':aid'
Activity::PK => $aid,
'checked' => (int)$checked
]);
$stmt = $this->zdb->sql->prepareStatementForSqlObject($prepare);

$count = 0;
foreach ($delete as $values) {
$stmt->execute([':aid' => $values[Activity::PK]]);
++$count;
}
Analog::log(
sprintf('%1$s activities removed', $count),
Analog::INFO
);
$this->zdb->execute($insert);
++$counts['added'];
} elseif ($stored[$aid] !== $checked) {
$update = $this->zdb->update($table);
$update->set(['checked' => (int)$checked])->where([
self::PK => $this->id,
Activity::PK => $aid
]);
$this->zdb->execute($update);
++$counts['updated'];
}
}

if (count($update)) {
$prepare = $this->zdb->update(EVENTS_PREFIX . 'activitiesbookings');
$prepare->set([
'checked' => ':checked'
])->where([
foreach (array_keys($stored) as $aid) {
if (!isset($this->activities[$aid])) {
$delete = $this->zdb->delete($table);
$delete->where([
self::PK => $this->id,
Activity::PK => ':aid'
Activity::PK => $aid
]);
$stmt = $this->zdb->sql->prepareStatementForSqlObject($prepare);
$count = 0;
foreach ($update as $aid => $values) {
$params = [
'where2' => $aid,
':checked' => $values['checked']
];
$stmt->execute($params);
++$count;
}
Analog::log(
sprintf('%1$s activities updated', $count),
Analog::INFO
);
$this->zdb->execute($delete);
++$counts['removed'];
}
}

if (count($insert)) {
$prepare = $this->zdb->insert(EVENTS_PREFIX . 'activitiesbookings');
$prepare->values([
self::PK => ':id',
Activity::PK => ':aid',
'checked' => ':checked'
]);
$stmt = $this->zdb->sql->prepareStatementForSqlObject($prepare);
$count = 0;
foreach ($insert as $aid => $values) {
$params = [
$this->id,
$aid,
$values['checked']
];
$stmt->execute($params);
++$count;
}
foreach ($counts as $action => $count) {
if ($count > 0) {
Analog::log(
sprintf('%1$s activities added', $count),
sprintf('%1$s activities %2$s', $count, $action),
Analog::INFO
);
}
});
}
}

/**
Expand Down Expand Up @@ -689,17 +632,10 @@ public function getActivities(): array
private function canBook(Event $event): bool
{
if ($this->login->isAdmin() || $this->login->isStaff()) {
return $event->getId() !== null;
}

if ($event->getId() === null || !$event->isOpen()) {
return false;
return true;
}

$group = $event->getGroup();
return $group === null
|| $this->login->isGroupManager($group)
|| in_array($group, array_map('intval', Groups::loadGroups($this->login->id, false, false)), true);
return $event->isOpen() && Events::isVisible($event->getGroup(), $this->login);
}

/**
Expand Down
Loading
Loading