Skip to content

Commit

Permalink
Several fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed May 17, 2024
1 parent 195a326 commit 033be34
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 45 deletions.
8 changes: 4 additions & 4 deletions lib/GaletteAuto/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract class AbstractObject

protected Db $zdb;
protected ?int $id;
protected string $value;
protected ?string $value;
protected ?PropertiesList $filters = null;

private int $count;
Expand Down Expand Up @@ -238,14 +238,14 @@ abstract public function getRouteName(): string;
*/
public function __get(string $name): mixed
{
if (isset($this->$name)) {
return $this->$name;
if (property_exists($this, $name)) {
return $this->$name ?? null;
} else {
Analog::log(
'[' . get_class($this) . '] Unable to retrieve `' . $name . '`',
Analog::INFO
);
return false;
throw new \RuntimeException('Unable to retrieve `' . $name . '`');
}
}

Expand Down
32 changes: 19 additions & 13 deletions lib/GaletteAuto/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
* @property Transmission $transmission
* @property Finition $finition
* @property Model $model
* @property Adherent|int $owner
* @property int $owner_id
* @property Adherent $owner
* @property Picture $picture
* @property History $history
*/
Expand Down Expand Up @@ -127,6 +128,7 @@ class Auto
private Body $body;
private History $history;
private State $state;
private int $owner_id;
private Adherent $owner;

public const FUEL_PETROL = 1;
Expand Down Expand Up @@ -273,7 +275,8 @@ private function loadFromRS(ArrayObject $r): void
$bpk = Body::PK;
$this->body->load((int)$r->$bpk);
$opk = Adherent::PK;
$this->owner->load((int)$r->$opk);
$this->owner_id = (int)$r->$opk;
$this->owner->load($this->owner_id);
$spk = State::PK;
$this->state->load((int)$r->$spk);
$this->history->load((int)$this->id);
Expand Down Expand Up @@ -347,7 +350,7 @@ public function store(bool $new = false): bool
switch ($v) {
case 'string':
case 'date':
$values[$k] = $this->$propName;
$values[$k] = $this->$propName ?? null;
break;
case 'integer':
$values[$k] = (
Expand Down Expand Up @@ -503,7 +506,8 @@ public function hasPicture(): bool
*/
public function appropriateCar(Login $login): void
{
$this->owner->load($login->id);
$this->owner_id = $login->id;
$this->owner->load($this->owner_id);
}

/**
Expand Down Expand Up @@ -547,7 +551,7 @@ public function __get(string $name): mixed
case 'first_registration_date':
case 'first_circulation_date':
case 'creation_date':
if ($this->$name != '') {
if (isset($this->$name)) {
try {
$d = new \DateTime($this->$name);
return $d->format(_T("Y-m-d"));
Expand All @@ -561,12 +565,12 @@ public function __get(string $name): mixed
return $this->$name;
}
}
return null;
break;

Check failure on line 569 in lib/GaletteAuto/Auto.php

View workflow job for this annotation

GitHub Actions / Lint on PHP 8.1

Unreachable statement - code above always terminates.

Check failure on line 569 in lib/GaletteAuto/Auto.php

View workflow job for this annotation

GitHub Actions / Lint on PHP 8.1

Unreachable statement - code above always terminates.
case 'picture':
return $this->picture;
default:
return $this->$name;
break;
return $this->$name ?? '';
}
}

Expand Down Expand Up @@ -606,8 +610,9 @@ public function __set(string $name, mixed $value): void
case 'body':
$this->body->load((int)$value);
break;
case 'owner':
$this->owner->load((int)$value);
case 'owner_id':
$this->owner_id = (int)$value;
$this->owner->load($this->owner_id);
break;
case 'state':
$this->state->load((int)$value);
Expand Down Expand Up @@ -741,7 +746,7 @@ public function check(array $post): bool
//constants
case 'fuel':
if (in_array($value, array_keys($this->listFuels()))) {
$this->fuel = $value;
$this->fuel = (int)$value;
} else {
$this->errors[] = _T("- You must choose a fuel in the list", "auto");
}
Expand All @@ -754,7 +759,7 @@ public function check(array $post): bool
case 'body':
case 'state':
if ($value > 0) {
$this->$prop->load($value);
$this->$prop->load((int)$value);
} else {
$class = 'GaletteAuto\\' . ucwords($prop);
$name = $class::FIELD;
Expand All @@ -765,11 +770,12 @@ public function check(array $post): bool
);
}
break;
case 'owner':
case 'owner_id':
if (isset($post['change_owner']) || !isset($this->id)) {
$value = (int)$value;
if ($value > 0) {
$this->$prop->load($value);
$this->owner_id = $value;
$this->owner->load($value);
} else {
$this->errors[] = _T("- you must attach an owner to this car", "auto");
}
Expand Down
14 changes: 8 additions & 6 deletions lib/GaletteAuto/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public function showAddEditVehicle(Request $request, Response $response, string
'require_calendar' => true,
'require_dialog' => true,
'car' => $auto,
'models' => $models->getList($auto->model->brand->id),
'models' => $models->getList($auto->model->brand->id ?? null),
'js_init_models' => !isset($auto->model->brand),
'brands' => $auto->model->brand->getList(),
'colors' => $auto->color->getList(),
Expand Down Expand Up @@ -363,6 +363,7 @@ public function showAddEditVehicle(Request $request, Response $response, string
if (count($members)) {
$params['members']['list'] = $members;
}
$params['autocomplete'] = true;

// display page
$this->view->render(
Expand Down Expand Up @@ -414,7 +415,7 @@ public function doAddEditVehicle(Request $request, Response $response, string $a
{
$post = $request->getParsedBody();

$is_new = ($action === 'add');
$is_new = ($action === 'add' || $action === 'new');

// initialize warnings
$error_detected = array();
Expand All @@ -426,7 +427,7 @@ public function doAddEditVehicle(Request $request, Response $response, string $a

$auto = new Auto($this->plugins, $this->zdb);
if (!$is_new) {
$auto->load($post[Auto::PK]);
$auto->load((int)$post[Auto::PK]);
}

$res = $auto->check($post);
Expand Down Expand Up @@ -490,14 +491,15 @@ public function doAddEditVehicle(Request $request, Response $response, string $a
*/
public function vehicleHistory(Request $request, Response $response, int $id): Response
{
$apk = Auto::PK;
$history = new History($this->zdb, $id);
$auto = new Auto($this->plugins, $this->zdb, $history->{Auto::PK});
$auto = new Auto($this->plugins, $this->zdb);
$auto->load($history->$apk);
$this->checkAclsFor($response, $auto->owner->id);

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

Expand Down
4 changes: 2 additions & 2 deletions lib/GaletteAuto/Controllers/Crud/ModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ public function edit(Request $request, Response $response, int $id = null, strin
{
$model = new Model($this->zdb);

if ($this->session->automodel !== null) {
if ($this->session->auto_model !== null) {
$model->check($this->session->auto_model);
$this->session->automodel = null;
unset($this->session->auto_model);
}

$model_id = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/GaletteAuto/Controllers/Crud/PropertiesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public function doPropertyEdit(

if (!$is_new) {
if (isset($post[$object->pk])) {
$object->load($post[$object->pk]);
$object->load((int)$post[$object->pk]);
} else {
$error_detected[]
= _T("- No id provided for modifying this record! (internal)", "auto");
Expand Down
2 changes: 1 addition & 1 deletion lib/GaletteAuto/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function delete(array $ids): bool
*/
public function __get(string $name): mixed
{
return $this->$name;
return $this->$name ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion templates/default/history.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

{% block content %}
{% if entries|length > 0 %}
<table id="listing">
<table id="listing" class="ui celled striped table">
<thead>
<tr>
<th class="listing left">
Expand Down
29 changes: 12 additions & 17 deletions templates/default/vehicles.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,20 @@
</span>
</p>
{% else %}
<label>{{ _T("Owner", "auto") }}</label>
<input type="hidden" name="change_owner" id="change_owner" value="1"/>
{% endif %}

{% if login.isAdmin() or login.isStaff() %}
<span id="owner_elt"{% if car.id %} class="displaynone"{% endif %}>
<div id="owner" class="jsonly search-dropdown ui input nochosen paginated">
<input id="owner_input" type="text" name="owner" value="{{ car.owner.id }}" placeholder="{{ _T("Owner", "auto") }}" required="required">
<i class="jsonly displaynone dropdown icon"></i>
<span class="ui mini compact icon disabled button prev-results"><i class="jsonly displaynone chevron circle left icon disabled button tooltip" title="{{ _T("Load previous members...") }}"></i></span>
<span class="ui mini compact icon disabled button next-results"><i class="jsonly displaynone chevron circle right icon disabled button tooltip" title="{{ _T("Load following members...") }}"></i></span>
<div class="jsonly displaynone default text">{{ _T("Search for name or ID and pick member") }}</div>
<div class="jsonly displaynone menu">
{% for k, v in members.list %}
<div class="item" data-value="{{ k }}">{{ v }}</div>
{% endfor %}
</div>
</div>
</span>
{% set cclass = 'field' %}
{% if car.id %}{% set cclass = cclass ~ ' displaynone' %}{% endif %}
{% include 'components/forms/member_dropdown.html.twig' with {
'required': true,
'component_id': 'owner_id_elt',
'id': 'owner_id',
'label': _T("Owner", "auto"),
'value': car.owner.id,
'component_class': cclass
} %}
</div>
{% else %}
<input type="hidden" name="owner" value="{{ car.owner.id }}"/>
Expand Down Expand Up @@ -347,10 +342,10 @@
{# Popup for owner change #}
$('#change_owner-checkbox').checkbox({
onChecked: function() {
$('#owner_elt').removeClass('displaynone');
$('#owner_id_elt').removeClass('displaynone');
},
onUnchecked: function() {
$('#owner_elt').addClass('displaynone');
$('#owner_id_elt').addClass('displaynone');
}
});
Expand Down

0 comments on commit 033be34

Please sign in to comment.