Skip to content
Merged
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
202 changes: 111 additions & 91 deletions lang/events.pot

Large diffs are not rendered by default.

210 changes: 115 additions & 95 deletions lang/events_en_US.po

Large diffs are not rendered by default.

193 changes: 40 additions & 153 deletions lib/GaletteEvents/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use ArrayObject;
use Galette\Core\Db;
use Galette\Core\Login;
use Galette\Core\History;
use Analog\Analog;
use Laminas\Db\Sql\Expression;

Expand All @@ -23,6 +23,8 @@
*/
class Activity
{
use EntityTrait;

public const string TABLE = 'activities';
public const string PK = 'id_activity';

Expand All @@ -31,66 +33,37 @@ class Activity
public const int REQUIRED = 2;

private Db $zdb;
private Login $login;
private History $history;
/** @var array<string> */
private array $errors = [];

private int $id;
private string $name;
private ?int $id = null;
private string $name = '';
private bool $active = false;
private string $creation_date;
private ?string $creation_date = null;
private string $comment = '';

/**
* Default constructor
*
* @param Db $zdb Database instance
* @param Login $login Login instance
* @param null|int|ArrayObject<string, mixed> $args Either a ResultSet row or its id for to load
* a specific activity, or null to just
* instanciate object
* @param Db $zdb Database instance
* @param History $history History instance
* @param null|int|ArrayObject<string, mixed> $args Either a ResultSet row or its id for to load
* a specific activity, or null to just
* instanciate object
*/
public function __construct(Db $zdb, Login $login, int|ArrayObject|null $args = null)
public function __construct(Db $zdb, History $history, int|ArrayObject|null $args = null)
{
$this->zdb = $zdb;
$this->login = $login;
$this->history = $history;

if (is_int($args) && $args > 0) {
if (is_int($args)) {
$this->load($args);
} elseif (is_object($args)) {
$this->loadFromRS($args);
}
}

/**
* Loads an activity from its id
*
* @param int $id the identifiant for the activity to load
*
* @return bool true if query succeed, false otherwise
*/
public function load(int $id): bool
{
try {
$select = $this->zdb->select($this->getTableName());
$select->where([self::PK => $id]);
$results = $this->zdb->execute($select);

if ($results->count() > 0) {
$this->loadFromRS($results->current());
return true;
} else {
return false;
}
} catch (\Exception $e) {
Analog::log(
'Cannot load activity #`' . $id . '` | ' . $e->getMessage(),
Analog::WARNING
);
throw $e;
}
}

/**
* Populate object from a resultset row
*
Expand All @@ -105,42 +78,6 @@ private function loadFromRS(ArrayObject $r): void
$this->comment = $r['comment'] ?? '';
}

/**
* Remove specified event
*/
public function remove(): bool
{
$transaction = false;

try {
if (!$this->zdb->connection->inTransaction()) {
$this->zdb->connection->beginTransaction();
$transaction = true;
}

$delete = $this->zdb->delete($this->getTableName());
$delete->where([self::PK => $this->id]);
$this->zdb->execute($delete);

//commit all changes
if ($transaction) {
$this->zdb->connection->commit();
}

return true;
} catch (\Exception $e) {
if ($transaction) {
$this->zdb->connection->rollBack();
}
Analog::log(
'Unable to delete activity ' . $this->name
. ' (' . $this->id . ') |' . $e->getMessage(),
Analog::ERROR
);
return false;
}
}

/**
* Check posted values validity
*
Expand Down Expand Up @@ -186,51 +123,39 @@ public function check(array $values): bool
/**
* Store the activity
*/
public function store(): bool
public function store(): void
{
global $hist;

try {
$this->transactional(function (): void {
$values = [
'name' => $this->name,
'is_active' => ($this->active ? $this->active
: ($this->zdb->isPostgres() ? 'false' : 0)),
'comment' => $this->comment
];

if (empty($this->id)) {
//we're inserting a new event
if ($this->id === null) {
//we're inserting a new activity
$this->creation_date = date("Y-m-d");
$values['creation_date'] = $this->creation_date;

$insert = $this->zdb->insert($this->getTableName());
$insert->values($values);
$add = $this->zdb->execute($insert);
if ($add->count() > 0) {
if ($this->zdb->isPostgres()) {
/** @phpstan-ignore-next-line */
$this->id = (int)$this->zdb->driver->getLastGeneratedValue(
PREFIX_DB . $this->getTableName() . '_id_seq'
);
} else {
$this->id = (int)$this->zdb->driver->getLastGeneratedValue();
}

// logging
$hist->add(
_T("Activity added", "events"),
$this->name
);
return true;
} else {
$hist->add(_T("Fail to add new activity.", "events"));
throw new \Exception(
if ($add->count() === 0) {
$this->history->add(_T("Fail to add new activity.", "events"));
throw new \RuntimeException(
'An error occurred inserting new activity!'
);
}
$this->id = $this->getLastInsertId();

// logging
$this->history->add(
_T("Activity added", "events"),
$this->name
);
} else {
//we're editing an existing event
$values[self::PK] = $this->id;
//we're editing an existing activity
$update = $this->zdb->update($this->getTableName());
$update
->set($values)
Expand All @@ -241,67 +166,37 @@ public function store(): bool
//edit == 0 does not mean there were an error, but that there
//were nothing to change
if ($edit->count() > 0) {
$hist->add(
$this->history->add(
_T("Activity updated", "events"),
$this->name
);
}
return true;
}
} catch (\Exception $e) {
Analog::log(
'Something went wrong :\'( | ' . $e->getMessage() . "\n"
. $e->getTraceAsString(),
Analog::ERROR
);
throw $e;
}
});
}

/**
* Get event id
* Get activity id
*/
public function getId(): ?int
{
return $this->id ?? null;
return $this->id;
}

/**
* Get event name
* Get activity name
*/
public function getName(): string
{
return $this->name ?? '';
return $this->name;
}

/**
* Get date
*
* @param string $prop Property to use
* @param bool $formatted Return date formatted, raw if false
* Get creation date, as Y-m-d
*/
private function getDate(string $prop, bool $formatted = true): string
public function getCreationDate(): string
{
if (!isset($this->$prop)) {
return '';
}

if ($formatted === true) {
$date = new \DateTime($this->$prop);
return $date->format(__("Y-m-d"));
} else {
return $this->$prop;
}
}

/**
* Get creation date
*
* @param bool $formatted Return date formatted, raw if false
*/
public function getCreationDate(bool $formatted = true): string
{
return $this->getDate('creation_date', $formatted);
return $this->creation_date ?? '';
}

/**
Expand All @@ -312,14 +207,6 @@ public function isActive(): bool
return $this->active;
}

/**
* Get table's name
*/
protected function getTableName(): string
{
return EVENTS_PREFIX . self::TABLE;
}

/**
* Get comment
*/
Expand All @@ -333,7 +220,7 @@ public function getComment(): string
*/
public function countEvents(): int
{
if (empty($this->id)) {
if ($this->id === null) {
return 0;
}

Expand Down
Loading
Loading