Skip to content

Commit

Permalink
182 UI be taxonomy improvements livewire (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
IsharaEkanayaka authored Jan 6, 2025
1 parent 4d253fc commit c2bda31
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 177 deletions.
6 changes: 6 additions & 0 deletions app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class Taxonomy extends Model
'updated_at' => 'datetime',
];

//mutator for saving properties
public function setPropertiesAttribute($value)
{
$this->attributes['properties'] = json_encode($value, JSON_UNESCAPED_SLASHES);
}

public function user()
{
return $this->belongsTo(User::class, 'created_by');
Expand Down
5 changes: 5 additions & 0 deletions app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class TaxonomyTerm extends Model
'updated_at' => 'datetime',
];

public function setMetadataAttribute($value)
{
$this->attributes['metadata'] = json_encode($value, JSON_UNESCAPED_SLASHES);
}

public function getFormattedMetadataAttribute()
{
$response = array();
Expand Down
57 changes: 32 additions & 25 deletions app/Http/Controllers/Backend/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function store(Request $request)
'code' => 'required|unique:taxonomies',
'name' => 'required',
'description' => 'nullable',
'properties' => 'string'
]);

try {
Expand Down Expand Up @@ -97,18 +98,18 @@ public function update(Request $request, Taxonomy $taxonomy)
'description' => 'nullable',
'properties' => 'string'
]);

try {
$originalProperties = json_decode($taxonomy->properties);
$originalProperties = json_decode(json_encode($taxonomy->properties));
$updatedProperties = json_decode($request->properties);
if ($this->validateProperties($originalProperties, $updatedProperties)) {
$taxonomy->update($data);
$taxonomy->properties = $request->properties;
$taxonomy->updated_by = Auth::user()->id;
$taxonomy->save();
}else{
return redirect()->route('dashboard.taxonomy.index')->withErrors('Can not update the Taxonomy Properties as it already has associated Taxonomy Terms. Please reassign or delete those first.');
if ($taxonomy->terms->count() > 0 && !$this->validateProperties($originalProperties, $updatedProperties)) {
return redirect()
->route('dashboard.taxonomy.index')
->withErrors('Can not update the Taxonomy Properties as it already has associated Taxonomy Terms. Please reassign or delete those first.');
}
$taxonomy->update($data);
$taxonomy->properties = $updatedProperties;
$taxonomy->updated_by = Auth::user()->id;
$taxonomy->save();
return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy updated successfully');
} catch (\Exception $ex) {
Log::error('Failed to update taxonomy', ['error' => $ex->getMessage()]);
Expand All @@ -117,24 +118,30 @@ public function update(Request $request, Taxonomy $taxonomy)
}
private function validateProperties(array $original, array $updated): bool
{
// Ensure existing items are not modified
foreach ($original as $index => $originalItem) {
if (!isset($updated[$index])) {
return false; // Missing an existing property
}
// $originalMap = [];
// $updatedMap = [];
// foreach ($original as $item) $originalMap[$item->code] = $item;
// foreach ($updated as $item) $updatedMap[$item->code] = $item;

$updatedItem = $updated[$index];
if (
$originalItem->code !== $updatedItem->code ||
$originalItem->name !== $updatedItem->name ||
$originalItem->data_type !== $updatedItem->data_type
) {
return false; // An existing property was altered
}
}
// // Ensure existing items are not modified
// foreach ($originalMap as $code => $originalItem) {
// if (!isset($updatedMap[$code])) {
// // TODO Let allow to delete if not used in any term
// return false; // Missing an existing property
// }

// $updatedItem = $updatedMap[$code];
// if (
// $originalItem->data_type !== $updatedItem->data_type
// ) {
// // An existing property data type was altered
// // TODO Let allow to delete if not used in any term
// return false;
// }
// }

// Allow additional properties
return count($updated) >= count($original);
// Allow changes for now
return true;
}
/**
* Confirm to delete the specified resource from storage.
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Livewire/Backend/TaxonomyTermMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Livewire\Backend;

use Livewire\Component;

class TaxonomyTermMetadata extends Component
{
public $property;
public $metadata = []; // Holds the pre-filled metadata for edit
public $term; // Holds the term object for edit

public function mount($property, $term = null)
{
$this->property = $property;
$this->term = $term;

// Populate metadata if $term is provided (Edit mode)
if ($this->term) {
$this->metadata = $this->term->metadata ?? [];
}
}

public function render()
{
return view('livewire.backend.taxonomy-term-metadata');
}
}
2 changes: 1 addition & 1 deletion database/seeders/TaxonomySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function run(){
'code' => $taxonomy['code'],
'name' => $taxonomy['name'],
'description' => $taxonomy['description'],
'properties' => json_encode($taxonomy['properties']),
'properties' => $taxonomy['properties'],
'created_by' => 1,
'updated_by' => 1
]);
Expand Down
4 changes: 2 additions & 2 deletions database/seeders/TaxonomyTermSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private function createTermsWithChildren($terms, $taxonomyId, $parentId = null)
'name' => $term['name'],
'taxonomy_id' => $taxonomyId,
'parent_id' => $parentId,
'metadata' => json_encode($term['metadata']),
'metadata' => $term['metadata'],
'created_by' => 1,
'updated_by' => 1
]);
Expand All @@ -190,7 +190,7 @@ private function createTermsWithChildren($terms, $taxonomyId, $parentId = null)
'name' => $child['name'],
'taxonomy_id' => $taxonomyId,
'parent_id' => $createdTerm->id,
'metadata' => json_encode($child['metadata']),
'metadata' => $child['metadata'],
'created_by' => 1,
'updated_by' => 1
]);
Expand Down
8 changes: 5 additions & 3 deletions resources/views/backend/taxonomy/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@section('title', __('Edit Taxonomy'))

@section('content')
<div x-data="{ properties: {{ $taxonomy->properties}}, is_editable: {{ $taxonomy->terms()->count() > 0 ? '0' : '1' }} }">
<div x-data="{ properties: {{ json_encode($taxonomy->properties) }}, is_editable: '1' }">
{!! Form::model($taxonomy, [
'url' => route('dashboard.taxonomy.update', $taxonomy->id),
'method' => 'PUT',
Expand Down Expand Up @@ -73,7 +73,7 @@
<div class="card-body">
<h5 class="card-title" style="text-align: left; text-decoration: none;">Properties</h5>

<div x-show="is_editable=='0'">
<div>
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol id="exclamation-triangle-fill" fill="currentColor" viewBox="0 0 16 16">
<path
Expand All @@ -88,7 +88,9 @@
</svg>

<div>
<b>Edit</b> and <b>Delete</b> options not available since already have <a href="{{ route('dashboard.taxonomy.terms.index', $taxonomy) }}">taxonomy terms</a>.<br> Please remove all of them to enable the <b>Edit</b> and <b>Delete</b> options.
<b>Edit</b> and <b>Delete</b> options should be carefully used since already have <a
href="{{ route('dashboard.taxonomy.terms.index', $taxonomy) }}">taxonomy
terms</a>.<br>
</div>
</div>
</div>
Expand Down
63 changes: 1 addition & 62 deletions resources/views/backend/taxonomy/terms/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,69 +73,8 @@
<div class="col-12 pb-3">
<strong>Metadata</strong>
</div>

@foreach ($taxonomy->properties as $property)
<div class="col-12 py-2">
<div class="col ps-0">
<label>{{ $property['name'] }}
({{ \App\Domains\Taxonomy\Models\Taxonomy::$propertyType[$property['data_type']] }})
</label>
</div>
<div class="col-md-12 px-0">
@switch($property['data_type'])
@case('string')
{!! Form::text("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!}
@break

@case('integer')
{!! Form::number("metadata[{$property['code']}]", null, [
'class' => 'form-control',
'id' => $property['code'],
'step' => '1',
]) !!}
@break

@case('float')
{!! Form::number("metadata[{$property['code']}]", null, [
'class' => 'form-control',
'id' => $property['code'],
'step' => 'any',
]) !!}
@break

@case('boolean')
<div class="form-check">
{!! Form::checkbox("metadata[{$property['code']}]", 1, null, [
'class' => 'form-check-input',
'id' => $property['code'],
]) !!}
</div>
@break

@case('date')
{!! Form::date("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!}
@break

@case('datetime')
{!! Form::datetimeLocal("metadata[{$property['code']}]", null, [
'class' => 'form-control',
'id' => $property['code'],
]) !!}
@break

@case('url')
{!! Form::url("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!}
@break

@case('image')
{!! Form::file("metadata[{$property['code']}]", ['class' => 'form-control', 'id' => $property['code']]) !!}
@break

@default
{!! Form::text("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!}
@endswitch
</div>
</div>
<livewire:backend.taxonomy-term-metadata :property="$property" />
@endforeach
</div>
</x-slot>
Expand Down
86 changes: 2 additions & 84 deletions resources/views/backend/taxonomy/terms/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,91 +81,9 @@
<div class="col-12 pb-3">
<strong>Metadata</strong>
</div>

@foreach (json_decode($taxonomy->properties,true) as $property)
<div class="col-12 py-2">
<div class="col ps-0">
<label>{{ $property['name'] }}
({{ \App\Domains\Taxonomy\Models\Taxonomy::$propertyType[$property['data_type']] }})
</label>
</div>
<div class="col-md-12 px-0">
@switch($property['data_type'])
@case('string')
{!! Form::text(
"metadata[{$property['code']}]",
old("metadata.{$property['code']}", $term->getMetadata($property['code'])),
['class' => 'form-control'],
) !!}
@break

@case('integer')
{!! Form::number(
"metadata[{$property['code']}]",
old("metadata.{$property['code']}", $term->getMetadata($property['code'])),
['class' => 'form-control', 'step' => '1'],
) !!}
@break

@case('float')
{!! Form::number(
"metadata[{$property['code']}]",
old("metadata.{$property['code']}", $term->getMetadata($property['code'])),
['class' => 'form-control', 'step' => 'any'],
) !!}
@break

@case('boolean')
<div class="form-check">
{!! Form::checkbox(
"metadata[{$property['code']}]",
1,
old("metadata.{$property['code']}", $term->getMetadata($property['code']) == 1 ? true : false),
['class' => 'form-check-input'],
) !!}
</div>
@break

@case('date')
{!! Form::date(
"metadata[{$property['code']}]",
old("metadata.{$property['code']}", $term->getMetadata($property['code'])),
['class' => 'form-control'],
) !!}
@break

@case('datetime')
{!! Form::datetimeLocal(
"metadata[{$property['code']}]",
old("metadata.{$property['code']}", $term->getMetadata($property['code'])),
['class' => 'form-control'],
) !!}
@break

@case('url')
{!! Form::url(
"metadata[{$property['code']}]",
old("metadata.{$property['code']}", $term->getMetadata($property['code'])),
['class' => 'form-control'],
) !!}
@break

@case('image')
{!! Form::file("metadata[{$property['code']}]", ['class' => 'form-control']) !!}
@if ($term->getMetadata($property['code']))
<small>Current: {{ $term->getMetadata($property['code']) }}</small>
@endif
@break

@default
{!! Form::text(
"metadata[{$property['code']}]",
old("metadata.{$property['code']}", $term->getMetadata($property['code'])),
['class' => 'form-control'],
) !!}
@endswitch
</div>
</div>
@foreach ($taxonomy->properties as $property)
<livewire:backend.taxonomy-term-metadata :property="$property" :term="$term" />
@endforeach
</div>
</x-slot>
Expand Down
Loading

0 comments on commit c2bda31

Please sign in to comment.