Skip to content

Commit e70f140

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 9334b8d + 664e398 commit e70f140

File tree

9 files changed

+76
-17
lines changed

9 files changed

+76
-17
lines changed

app/Http/Controllers/Assets/AssetsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public function update(ImageUploadRequest $request, Asset $asset) : RedirectResp
363363
$asset->purchase_cost = $request->input('purchase_cost', null);
364364
$asset->purchase_date = $request->input('purchase_date', null);
365365
$asset->next_audit_date = $request->input('next_audit_date', null);
366-
if ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && ($asset->model->eol > 0)) {
366+
if ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && ($asset->model?->eol > 0)) {
367367
$asset->purchase_date = $request->input('purchase_date', null);
368368
$asset->asset_eol_date = Carbon::parse($request->input('purchase_date'))->addMonths($asset->model->eol)->format('Y-m-d');
369369
$asset->eol_explicit = false;
@@ -379,7 +379,7 @@ public function update(ImageUploadRequest $request, Asset $asset) : RedirectResp
379379
} else {
380380
$asset->eol_explicit = true;
381381
}
382-
} elseif (!$request->filled('asset_eol_date') && (($asset->model->eol) == 0)) {
382+
} elseif (!$request->filled('asset_eol_date') && (($asset->model?->eol) == 0)) {
383383
$asset->asset_eol_date = null;
384384
$asset->eol_explicit = false;
385385
}

app/Http/Controllers/Assets/BulkAssetsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function edit(Request $request) : View | RedirectResponse
163163
$modelNames = [];
164164

165165
foreach($models as $model) {
166-
$modelNames[] = $model->model->name;
166+
$modelNames[] = $model->model?->name;
167167
}
168168

169169
if ($request->filled('bulk_actions')) {
@@ -470,7 +470,7 @@ public function update(Request $request) : RedirectResponse
470470
*/
471471

472472
// Does the model have a fieldset?
473-
if ($asset->model->fieldset) {
473+
if ($asset->model?->fieldset) {
474474
foreach ($asset->model->fieldset->fields as $field) {
475475

476476
// null custom fields

app/Models/Labels/DefaultLabel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function write($pdf, $record)
229229
static::writeText(
230230
$pdf, $record->get('title'),
231231
$textX1, 0,
232-
'freesans', 'b', $this->textSize, 'L',
232+
Helper::isCjk($record->get('title')) ? 'cid0cs' : 'freesans', 'b', $this->textSize, 'L',
233233
$textW, $this->textSize,
234234
true, 0
235235
);
@@ -246,11 +246,12 @@ public function write($pdf, $record)
246246
static::writeText(
247247
$pdf, (($field['label']) ? $field['label'].' ' : '') . $field['value'],
248248
$textX1, $textY,
249-
'freesans', '', $this->textSize, 'L',
249+
Helper::isCjk($field['label']) ? 'cid0cs' : 'freesans', '', $this->textSize, 'L',
250250
$textW, $this->textSize,
251251
true, 0
252252
);
253253

254+
254255
$textY += $this->textSize + self::TEXT_MARGIN;
255256
$fieldsDone++;
256257
}

app/Models/Labels/Label.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,29 +211,36 @@ public final function getPrintableArea()
211211
*/
212212
public final function writeText(TCPDF $pdf, $text, $x, $y, $font=null, $style=null, $size=null, $align='L', $width=null, $height=null, $squash=false, $border=0, $spacing=0)
213213
{
214+
215+
214216
$prevFamily = $pdf->getFontFamily();
215217
$prevStyle = $pdf->getFontStyle();
216218
$prevSizePt = $pdf->getFontSizePt();
217219

220+
218221
$text = !empty($text) ? $text : '';
219222

220223
$fontFamily = !empty($font) ? $font : $prevFamily;
221224
$fontStyle = !empty($style) ? $style : $prevStyle;
222-
if ($size) { $fontSizePt = Helper::convertUnit($size, $this->getUnit(), 'pt', true);
223-
} else { $fontSizePt = $prevSizePt;
225+
226+
227+
if ($size) {
228+
$fontSizePt = Helper::convertUnit($size, $this->getUnit(), 'pt', true);
229+
} else {
230+
$fontSizePt = $prevSizePt;
224231
}
225232

226233
$pdf->SetFontSpacing($spacing);
227234

228235
$parts = collect(explode('**', $text))
229236
->map(
230-
function ($part, $index) use ($pdf, $fontFamily, $fontStyle, $fontSizePt) {
237+
function ($part, $index) use ($pdf, $fontFamily, $fontStyle, $fontSizePt, $text) {
231238
$modStyle = ($index % 2 == 1) ? 'B' : $fontStyle;
232239
$pdf->setFont($fontFamily, $modStyle, $fontSizePt);
233240
return [
234241
'text' => $part,
235242
'text_width' => $pdf->GetStringWidth($part),
236-
'font_family' => $fontFamily,
243+
'font_family' => Helper::isCjk($text) ? 'cid0cs' : $fontFamily,
237244
'font_style' => $modStyle,
238245
'font_size' => $fontSizePt,
239246
];

app/Observers/AssetObserver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ public function restoring(Asset $asset)
168168
public function saving(Asset $asset)
169169
{
170170
// determine if calculated eol and then calculate it - this should only happen on a new asset
171-
if (is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && ($asset->model->eol > 0)){
171+
if (is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && ($asset->model?->eol > 0)) {
172172
$asset->asset_eol_date = $asset->purchase_date->addMonths($asset->model->eol)->format('Y-m-d');
173173
$asset->eol_explicit = false;
174174
}
175175

176176
// determine if explicit and set eol_explicit to true
177177
if (!is_null($asset->asset_eol_date) && !is_null($asset->purchase_date)) {
178-
if($asset->model->eol > 0) {
178+
if ($asset->model?->eol > 0) {
179179
$months = (int) Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date, true);
180180
if($months != $asset->model->eol) {
181181
$asset->eol_explicit = true;
@@ -184,9 +184,9 @@ public function saving(Asset $asset)
184184
} elseif (!is_null($asset->asset_eol_date) && is_null($asset->purchase_date)) {
185185
$asset->eol_explicit = true;
186186
}
187-
if ((!is_null($asset->asset_eol_date)) && (!is_null($asset->purchase_date)) && (is_null($asset->model->eol) || ($asset->model->eol == 0))) {
187+
188+
if ((!is_null($asset->asset_eol_date)) && (!is_null($asset->purchase_date)) && (is_null($asset->model?->eol) || ($asset->model?->eol == 0))) {
188189
$asset->eol_explicit = true;
189190
}
190-
191191
}
192192
}

app/View/Label.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function render(callable $callback = null)
7474
[0 => $template->getWidth(), 1 => $template->getHeight(), 'Rotate' => $template->getRotation()]
7575
);
7676

77+
// Required for CJK languages, otherwise the embedded font can get too massive
78+
$pdf->SetFontSubsetting(true);
79+
80+
7781
// Reset parameters
7882
$pdf->SetPrintHeader(false);
7983
$pdf->SetPrintFooter(false);
@@ -176,14 +180,14 @@ public function render(callable $callback = null)
176180
// For fields that have multiple options, we need to combine them
177181
// into a single field so all values are displayed.
178182
->reduce(function ($previous, $current) {
179-
// On the first iteration we simply return the item.
183+
// On the first iteration, we simply return the item.
180184
// If there is only one item to be processed for the row
181185
// then this effectively skips everything below this if block.
182186
if (is_null($previous)) {
183187
return $current;
184188
}
185189

186-
// At this point we are dealing with a row with multiple items being displayed.
190+
// At this point, we are dealing with a row with multiple items being displayed.
187191
// We need to combine the label and value of the current item with the previous item.
188192

189193
// The end result of this will be in this format:

resources/views/models/custom_fields_form_bulk_edit.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@endphp
66

77
@foreach($models as $model)
8-
@if ($model->fieldset ? $model->fieldset->count() > 0 : false)
8+
@if (($model) && ($model->fieldset ? $model->fieldset->count() > 0 : false))
99
@php
1010
$anyModelHasCustomFields++;
1111
@endphp

tests/Feature/Assets/Ui/BulkEditAssetsTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ public function testUserWithPermissionsCanAccessPage()
2929
])->assertStatus(200);
3030
}
3131

32+
public function test_handles_model_being_deleted()
33+
{
34+
$this->withoutExceptionHandling();
35+
36+
$user = User::factory()->viewAssets()->editAssets()->create();
37+
$assets = Asset::factory()->count(2)->create();
38+
39+
$assets->first()->model->forceDelete();
40+
41+
$id_array = $assets->pluck('id')->toArray();
42+
43+
$this->actingAs($user)->post('/hardware/bulkedit', [
44+
'ids' => $id_array,
45+
'order' => 'asc',
46+
'bulk_actions' => 'edit',
47+
'sort' => 'id'
48+
])->assertStatus(200);
49+
}
50+
3251
public function test_standard_user_cannot_access_page()
3352
{
3453
$user = User::factory()->create();

tests/Feature/Assets/Ui/EditAssetTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,32 @@ public function testCurrentLocationIsNotUpdatedOnEdit()
125125
$this->assertEquals($currentLocation->id, $asset->location_id);
126126
}
127127

128+
129+
public function test_handles_model_being_deleted()
130+
{
131+
$this->withoutExceptionHandling();
132+
133+
$newStatus = StatusLabel::factory()->create();
134+
135+
$asset = Asset::factory()->create();
136+
137+
$asset->model()->forceDelete();
138+
139+
$this->actingAs(User::factory()->viewAssets()->editAssets()->create())
140+
->from(route('hardware.edit', $asset))
141+
->put(route('hardware.update', $asset), [
142+
'redirect_option' => 'index',
143+
'purchase_date' => '2025-08-30',
144+
'name' => 'New name',
145+
'asset_tags' => 'New Asset Tag',
146+
'status_id' => $newStatus->id,
147+
// triggers potential issue in AssetObserver's saving method
148+
'model_id' => AssetModel::factory()->create()->id,
149+
]);
150+
151+
$this->assertDatabaseHas('assets', [
152+
'id' => $asset->id,
153+
'status_id' => $newStatus->id,
154+
]);
155+
}
128156
}

0 commit comments

Comments
 (0)