Skip to content

Commit df43f99

Browse files
committed
Fixes, test Auto Crud
1 parent 5ef94ea commit df43f99

File tree

6 files changed

+366
-26
lines changed

6 files changed

+366
-26
lines changed

lib/GaletteAuto/Auto.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ class Auto
124124
private Transmission $transmission;
125125
private Body $body;
126126
private History $history;
127-
private Adherent $owner;
128127
private State $state;
128+
private Adherent $owner;
129129

130130
public const FUEL_PETROL = 1;
131131
public const FUEL_DIESEL = 2;
@@ -349,7 +349,7 @@ public function store(bool $new = false): bool
349349
break;
350350
case 'integer':
351351
$values[$k] = (
352-
($this->$propName != 0 && $this->$propName != '')
352+
(!empty($this->$propName))
353353
? $this->$propName
354354
: new Expression('NULL')
355355
);
@@ -380,6 +380,7 @@ public function store(bool $new = false): bool
380380
_T("New car added", "auto"),
381381
strtoupper($this->name)
382382
);
383+
$this->history->load((int)$this->id);
383384

384385
//handle picture for newly added cars
385386
$this->picture = new Picture($this->plugins, (int)$this->id);
@@ -769,7 +770,7 @@ public function check(array $post): bool
769770
}
770771
break;
771772
case 'owner':
772-
if (isset($post['change_owner'])) {
773+
if (isset($post['change_owner']) || !isset($this->id)) {
773774
$value = (int)$value;
774775
if ($value > 0) {
775776
$this->$prop->load($value);

lib/GaletteAuto/Autos.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function removeVehicles(int|array $ids): bool
9494
$infos = null;
9595
foreach ($vehicles as $vehicle) {
9696
$str_v = $vehicle->id_car . ' - ' . $vehicle->car_name .
97-
' (' . $vehicle->brand->brand . ' ' . $vehicle->model->model . ')';
97+
' (' . $vehicle->brand . ' ' . $vehicle->model . ')';
9898
$infos .= $str_v . "\n";
9999

100100
$p = new Picture($this->plugins, $vehicle->id_car);
@@ -269,12 +269,12 @@ public function getList(
269269
/**
270270
* Count vehicles from the query
271271
*
272-
* @param Select $select Original select
273-
* @param AutosList $filters Filters
272+
* @param Select $select Original select
273+
* @param ?AutosList $filters Filters
274274
*
275275
* @return void
276276
*/
277-
private function proceedCount($select, AutosList $filters): void
277+
private function proceedCount($select, ?AutosList $filters): void
278278
{
279279
try {
280280
$countSelect = clone $select;
@@ -296,7 +296,7 @@ private function proceedCount($select, AutosList $filters): void
296296

297297
$results = $this->zdb->execute($countSelect);
298298
$this->count = $results->current()->count;
299-
if ($this->count > 0) {
299+
if ($this->count > 0 && $filters !== null) {
300300
$filters->setCounter($this->count);
301301
}
302302
} catch (\Exception $e) {

lib/GaletteAuto/Controllers/Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public function vehicleHistory(Request $request, Response $response, int $id): R
497497

498498
$apk = Auto::PK;
499499
$params = [
500-
'entries' => $history->entries,
500+
'entries' => $history->getEntries(),
501501
'page_title' => str_replace('%d', $history->$apk, _T("History of car #%d", "auto")),
502502
'mode' => $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest' ? 'ajax' : ''
503503
];

lib/GaletteAuto/History.php

+33-15
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ class History
5151
State::PK => 'integer'
5252
);
5353

54-
//history entries
54+
/**
55+
* history entries
56+
*
57+
* @var array<int, array<string,mixed>> $entries
58+
*/
5559
private array $entries;
5660
private int $id_car;
5761

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

9195
$results = $this->zdb->execute($select);
92-
$this->entries = $results->toArray();
93-
$this->formatEntries();
96+
$this->formatEntries($results->toArray());
9497
return true;
9598
} catch (\Exception $e) {
9699
Analog::log(
@@ -137,23 +140,30 @@ public function getLatest(): ArrayObject|false
137140
/**
138141
* Format entries dates, also loads Member
139142
*
143+
* @param array<int, array<string,mixed>> $entries list of entries to format
144+
*
140145
* @return void
141146
*/
142-
private function formatEntries(): void
147+
private function formatEntries(array $entries): void
143148
{
144-
for ($i = 0; $i < count($this->entries); $i++) {
149+
$this->entries = [];
150+
foreach ($entries as $entry) {
145151
//put a formatted date to show
146-
$date = new \DateTime($this->entries[$i]['history_date']);
147-
$this->entries[$i]['formatted_date'] = $date->format(__('Y-m-d'));
152+
$date = new \DateTime($entry['history_date']);
153+
$entry['formatted_date'] = $date->format(__('Y-m-d'));
154+
148155
//associate member to current history entry
149-
$this->entries[$i]['owner']
150-
= new Adherent($this->zdb, (int)$this->entries[$i]['id_adh']);
156+
$entry['owner'] = new Adherent($this->zdb, (int)$entry['id_adh']);
157+
151158
//associate color
152-
$this->entries[$i]['color']
153-
= new Color($this->zdb, (int)$this->entries[$i]['id_color']);
159+
$color = new Color($this->zdb, (int)$entry['id_color']);
160+
$entry['color'] = $color->value;
161+
154162
//associate state
155-
$this->entries[$i]['state']
156-
= new State($this->zdb, (int)$this->entries[$i]['id_state']);
163+
$state = new State($this->zdb, (int)$entry['id_state']);
164+
$entry['state'] = $state->value;
165+
166+
$this->entries[] = $entry;
157167
}
158168
}
159169

@@ -220,8 +230,6 @@ public function __get(string $name)
220230
return $this->$name;
221231
case 'fields':
222232
return array_keys($this->fields);
223-
case 'entries':
224-
return $this->entries;
225233
default:
226234
Analog::log(
227235
'[' . get_class($this) . '] Trying to get an unknown property (' .
@@ -231,4 +239,14 @@ public function __get(string $name)
231239
break;
232240
}
233241
}
242+
243+
/**
244+
* Get current car history entries
245+
*
246+
* @return array<int, array<string,mixed>>
247+
*/
248+
public function getEntries(): array
249+
{
250+
return $this->entries;
251+
}
234252
}

templates/default/history.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
{{ owner.sfullname }}
6161
</td>
6262
<td data-col-label="{{ _T("Registration", "auto") }}">{{ entry.car_registration }}</td>
63-
<td data-col-label="{{ _T("Color", "auto") }}">{{ entry.color.value }}</td>
64-
<td data-col-label="{{ _T("State", "auto") }}">{{ entry.state.value }}</td>
63+
<td data-col-label="{{ _T("Color", "auto") }}">{{ entry.color }}</td>
64+
<td data-col-label="{{ _T("State", "auto") }}">{{ entry.state }}</td>
6565
</tr>
6666
{% endfor %}
6767
</tbody>

0 commit comments

Comments
 (0)