Skip to content

Commit

Permalink
Fixes, test Auto Crud
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Feb 14, 2024
1 parent 5ef94ea commit 41a8396
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/GaletteAuto/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @author Johan Cwiklinski <[email protected]>
*
* @property int $id
* @property string $value
*/
abstract class AbstractObject
{
Expand Down
7 changes: 4 additions & 3 deletions lib/GaletteAuto/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class Auto
private Transmission $transmission;
private Body $body;
private History $history;
private Adherent $owner;
private State $state;
private Adherent $owner;

public const FUEL_PETROL = 1;
public const FUEL_DIESEL = 2;
Expand Down Expand Up @@ -349,7 +349,7 @@ public function store(bool $new = false): bool
break;
case 'integer':
$values[$k] = (
($this->$propName != 0 && $this->$propName != '')
(!empty($this->$propName))
? $this->$propName
: new Expression('NULL')
);
Expand Down Expand Up @@ -380,6 +380,7 @@ public function store(bool $new = false): bool
_T("New car added", "auto"),
strtoupper($this->name)
);
$this->history->load((int)$this->id);

//handle picture for newly added cars
$this->picture = new Picture($this->plugins, (int)$this->id);
Expand Down Expand Up @@ -769,7 +770,7 @@ public function check(array $post): bool
}
break;
case 'owner':
if (isset($post['change_owner'])) {
if (isset($post['change_owner']) || !isset($this->id)) {
$value = (int)$value;
if ($value > 0) {
$this->$prop->load($value);
Expand Down
10 changes: 5 additions & 5 deletions lib/GaletteAuto/Autos.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function removeVehicles(int|array $ids): bool
$infos = null;
foreach ($vehicles as $vehicle) {
$str_v = $vehicle->id_car . ' - ' . $vehicle->car_name .
' (' . $vehicle->brand->brand . ' ' . $vehicle->model->model . ')';
' (' . $vehicle->brand . ' ' . $vehicle->model . ')';
$infos .= $str_v . "\n";

$p = new Picture($this->plugins, $vehicle->id_car);
Expand Down Expand Up @@ -269,12 +269,12 @@ public function getList(
/**
* Count vehicles from the query
*
* @param Select $select Original select
* @param AutosList $filters Filters
* @param Select $select Original select
* @param ?AutosList $filters Filters
*
* @return void
*/
private function proceedCount($select, AutosList $filters): void
private function proceedCount($select, ?AutosList $filters): void
{
try {
$countSelect = clone $select;
Expand All @@ -296,7 +296,7 @@ private function proceedCount($select, AutosList $filters): void

$results = $this->zdb->execute($countSelect);
$this->count = $results->current()->count;
if ($this->count > 0) {
if ($this->count > 0 && $filters !== null) {
$filters->setCounter($this->count);
}
} catch (\Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/GaletteAuto/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public function vehicleHistory(Request $request, Response $response, int $id): R

$apk = Auto::PK;
$params = [
'entries' => $history->entries,
'entries' => $history->getEntries(),
'page_title' => str_replace('%d', $history->$apk, _T("History of car #%d", "auto")),
'mode' => $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest' ? 'ajax' : ''
];
Expand Down
48 changes: 33 additions & 15 deletions lib/GaletteAuto/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ class History
State::PK => 'integer'
);

//history entries
/**
* history entries
*
* @var array<int, array<string,mixed>> $entries
*/
private array $entries;
private int $id_car;

Expand Down Expand Up @@ -89,8 +93,7 @@ public function load(int $id): bool
)->order('history_date ASC');

$results = $this->zdb->execute($select);
$this->entries = $results->toArray();
$this->formatEntries();
$this->formatEntries($results->toArray());
return true;
} catch (\Exception $e) {
Analog::log(
Expand Down Expand Up @@ -137,23 +140,30 @@ public function getLatest(): ArrayObject|false
/**
* Format entries dates, also loads Member
*
* @param array<int, array<string,mixed>> $entries list of entries to format
*
* @return void
*/
private function formatEntries(): void
private function formatEntries(array $entries): void
{
for ($i = 0; $i < count($this->entries); $i++) {
$this->entries = [];
foreach ($entries as $entry) {
//put a formatted date to show
$date = new \DateTime($this->entries[$i]['history_date']);
$this->entries[$i]['formatted_date'] = $date->format(__('Y-m-d'));
$date = new \DateTime($entry['history_date']);
$entry['formatted_date'] = $date->format(__('Y-m-d'));

//associate member to current history entry
$this->entries[$i]['owner']
= new Adherent($this->zdb, (int)$this->entries[$i]['id_adh']);
$entry['owner'] = new Adherent($this->zdb, (int)$entry['id_adh']);

//associate color
$this->entries[$i]['color']
= new Color($this->zdb, (int)$this->entries[$i]['id_color']);
$color = new Color($this->zdb, (int)$entry['id_color']);
$entry['color'] = $color->value;

//associate state
$this->entries[$i]['state']
= new State($this->zdb, (int)$this->entries[$i]['id_state']);
$state = new State($this->zdb, (int)$entry['id_state']);
$entry['state'] = $state->value;

$this->entries[] = $entry;
}
}

Expand Down Expand Up @@ -220,8 +230,6 @@ public function __get(string $name)
return $this->$name;
case 'fields':
return array_keys($this->fields);
case 'entries':
return $this->entries;
default:
Analog::log(
'[' . get_class($this) . '] Trying to get an unknown property (' .
Expand All @@ -231,4 +239,14 @@ public function __get(string $name)
break;
}
}

/**
* Get current car history entries
*
* @return array<int, array<string,mixed>>
*/
public function getEntries(): array
{
return $this->entries;
}
}
4 changes: 2 additions & 2 deletions templates/default/history.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
{{ owner.sfullname }}
</td>
<td data-col-label="{{ _T("Registration", "auto") }}">{{ entry.car_registration }}</td>
<td data-col-label="{{ _T("Color", "auto") }}">{{ entry.color.value }}</td>
<td data-col-label="{{ _T("State", "auto") }}">{{ entry.state.value }}</td>
<td data-col-label="{{ _T("Color", "auto") }}">{{ entry.color }}</td>
<td data-col-label="{{ _T("State", "auto") }}">{{ entry.state }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
Loading

0 comments on commit 41a8396

Please sign in to comment.