Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taxonomy Integration - Release 3.0.0 #171

Merged
merged 25 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
23c8886
feat: add taxonomy model, migration, controller, factory (#170)
IsharaEkanayaka Oct 11, 2024
999990a
composer.lock Updated
NuwanJ Oct 11, 2024
66d0f0b
Taxonomy index create edit and delete pages (#172)
kusaljayawardhana Oct 12, 2024
52ce4c5
taxonomy term model ,migration and template controller (#173)
supundulara Oct 12, 2024
0ace0a6
CI CD fix
NuwanJ Oct 12, 2024
787592f
Merge branch 'taxonomy-integration' of https://github.com/cepdnaclk/p…
NuwanJ Oct 12, 2024
4533dd4
128 beui create and edit taxonomy (#174)
IsharaEkanayaka Oct 14, 2024
0a82200
126 UI taxonomy terms index create edit and delete pages (#177)
IsharaEkanayaka Oct 15, 2024
ff2e611
140 BE taxonomy terms create edit and delete pages (#179)
kusaljayawardhana Oct 16, 2024
df71d83
UI improvements
NuwanJ Oct 16, 2024
b4db0cb
Logical Improvements
NuwanJ Oct 16, 2024
46b839a
seed taxonomy tables (#192)
IsharaEkanayaka Oct 20, 2024
29fa54b
[UI] taxonomy preview #187 (#188)
NuwanJ Oct 20, 2024
9cf5654
Term Display improvements (#189)
NuwanJ Oct 20, 2024
610ad5e
Handle cascaded delete constraints #184 (#185)
NuwanJ Oct 20, 2024
2b168d9
add taxonomy sidebar
IsharaEkanayaka Oct 22, 2024
a857d5a
Merge remote-tracking branch 'origin/main' into taxonomy-integration
NuwanJ Nov 13, 2024
b821a39
Fix script comments
NuwanJ Nov 13, 2024
773baba
Merge remote-tracking branch 'origin/main' into taxonomy-integration
NuwanJ Nov 13, 2024
a5ef150
Temporary warning message
NuwanJ Dec 4, 2024
14ee5f9
Temporary disable unavailable options
NuwanJ Dec 4, 2024
1b0ab5f
Taxonomy API endpoints (#127) (#235)
NuwanJ Dec 4, 2024
a733d2b
Contributors (#238)
NuwanJ Dec 4, 2024
13e2762
UI Update
NuwanJ Dec 8, 2024
f8c2f19
Create Taxonomy bug fix
NuwanJ Dec 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public function scopeOfVersion($query, $version)
{
return $query->where('version', $version);
}
}
}
94 changes: 94 additions & 0 deletions app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Domains\Taxonomy\Models;

use App\Domains\Auth\Models\User;
use Database\Factories\TaxonomyFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use App\Domains\Taxonomy\Models\TaxonomyTerm;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Domains\Taxonomy\Models\Traits\Scope\TaxonomyScope;

/**
* Class Taxonomy.
*/
class Taxonomy extends Model
{
use HasFactory,
LogsActivity;


protected static $logFillable = true;
protected static $logOnlyDirty = true;

protected $fillable = [
'code',
'name',
'description',
'properties',
];

public static $propertyType = [
'string' => 'String',
'integer' => 'Integer Number',
'float' => 'Floating Point Number',
'date' => 'Date',
'datetime' => 'Date Time',
'boolean' => 'Boolean',
'url' => 'URL',
// 'image' => 'Image'
// 'pdf' => 'PDF File'
];

protected $casts = [
'properties' => 'json',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

public function user()
{
return $this->belongsTo(User::class, 'created_by');
}

public function user_created()
{
return $this->belongsTo(User::class, 'created_by');
}

public function user_updated()
{
return $this->belongsTo(User::class, 'created_by');
}

public function terms()
{
return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')
->orderBy('parent_id', 'asc')
->orderBy('code', 'asc');
}

public function first_child_terms()
{
return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')
->whereNull('parent_id')
->orderBy('code', 'asc');
}

public function to_dict()
{
$taxonomy = $this->toArray();
foreach (['properties', 'created_at', 'updated_at', 'created_by', 'updated_by'] as $attribute) {
unset($taxonomy[$attribute]);
}
$taxonomy['properties'] = $this->properties;
$taxonomy['terms'] = TaxonomyTerm::getByTaxonomy($this->id);
return $taxonomy;
}

protected static function newFactory()
{
return TaxonomyFactory::new();
}
}
151 changes: 151 additions & 0 deletions app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace App\Domains\Taxonomy\Models;

use App\Domains\Auth\Models\User;
use Database\Factories\TaxonomyTermFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
* Class TaxonomyTerm.
*/
class TaxonomyTerm extends Model
{
use HasFactory,
LogsActivity;

protected static $logFillable = true;
protected static $logOnlyDirty = true;

protected $fillable = [
'code',
'name',
'metadata',
'taxonomy_id',
'parent_id',
];

protected $casts = [
'metadata' => 'json',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

public function getFormattedMetadataAttribute()
{
$response = array();
if (is_array($this->metadata)) {
$filteredMetadata = array_filter($this->metadata, function ($value) {
return !is_null($value['value']);
});

foreach ($filteredMetadata as $metadata) {
$response[$metadata['code']] = $metadata['value'];
}
}
return $response;
}

public function user()
{
return $this->belongsTo(User::class, 'created_by');
}

public function user_created()
{
return $this->belongsTo(User::class, 'created_by');
}

public function user_updated()
{
return $this->belongsTo(User::class, 'created_by');
}

public function taxonomy()
{
return $this->belongsTo(Taxonomy::class, 'taxonomy_id');
}

public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
}

public function children()
{
return $this->hasMany(self::class, 'parent_id')
->orderBy('code', 'asc');;
}

public function getMetadata($code)
{
if (is_array($this->metadata)) {
foreach ($this->metadata as $item) {
if ($item['code'] === $code && $item['value'] != null) {
return $item['value'];
}
}
}
return null;
}

protected static function boot()
{
parent::boot();

static::deleting(function ($taxonomyTerm) {
$taxonomyTerm->children()->delete();
});
}

public static function getHierarchicalPath($id)
{
$term = TaxonomyTerm::find($id);
if ($term != null) {
if ($term->parent_id != null) {
return TaxonomyTerm::getHierarchicalPath($term->parent_id) . " > " . $term->name;
} else {
return $term->name;
}
} else {
return '';
}
}

public static function getByTaxonomy($taxonomyId, $parent = null)
{
if ($parent == null) {
$res = TaxonomyTerm::where('taxonomy_id', $taxonomyId)->whereNull('parent_id');
} else {
$res = TaxonomyTerm::where('taxonomy_id', $taxonomyId)->where('parent_id', $parent);
}

$taxonomyTerms = [];
foreach ($res->get() as $term) {
$termData = $term->to_dict();

if ($term->children()->count() > 0) {
$termData['terms'] = $term->getByTaxonomy($taxonomyId, $term->id);
}
array_push($taxonomyTerms, $termData);
}
return $taxonomyTerms;
}

public function to_dict()
{
$taxonomyTerm = $this->toArray();
foreach (['id', 'taxonomy_id', 'parent_id', 'metadata', 'created_at', 'updated_at', 'created_by', 'updated_by'] as $attribute) {
unset($taxonomyTerm[$attribute]);
}
$taxonomyTerm['metadata'] = $this->formatted_metadata;
return $taxonomyTerm;
}

protected static function newFactory()
{
return TaxonomyTermFactory::new();
}
}
22 changes: 22 additions & 0 deletions app/Domains/Taxonomy/Services/TaxonomyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Domains\Taxonomy\Services;

use App\Domains\Taxonomy\Models\Taxonomy;
use App\Services\BaseService;

/**
* Class TaxonomyService.
*/
class TaxonomyService extends BaseService
{
/**
* TaxonomyService constructor.
*
* @param Taxonomy $taxonomy
*/
public function __construct(Taxonomy $taxonomy)
{
$this->model = $taxonomy;
}
}
82 changes: 82 additions & 0 deletions app/Http/Controllers/API/TaxonomyApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Resources\TaxonomyListResource;
use App\Http\Resources\TaxonomyResource;
use App\Http\Resources\TaxonomyTermResource;
use App\Domains\Taxonomy\Models\Taxonomy;
use App\Domains\Taxonomy\Models\TaxonomyTerm;
use Illuminate\Support\Facades\Log;

class TaxonomyApiController extends Controller
{

public function index()
{
try {
$result = Taxonomy::all();

if ($result) {
return response()->json(
[
'status' => 'success',
'data' => TaxonomyListResource::collection($result)
]
);
} else {
return response()->json(['status' => 'error', 'message' => 'No Taxonomies found'], 404);
}
} catch (\Exception $e) {
Log::error('Error in TaxonomyApiController@index', ['error' => $e->getMessage()]);
return response()->json(['status' => 'error', 'message' => 'An error occurred while fetching taxonomy index'], 500);
}
}

public function get_taxonomy($taxonomy_code)
{
try {
$result = Taxonomy::where('code', $taxonomy_code)->first();

if ($result) {
return response()->json(
[
'status' => 'success',
'data' => TaxonomyResource::collection([$result])->resolve()[0]
]
);
} else {
return response()->json(['status' => 'error', 'message' => 'Taxonomy not found'], 404);
}
} catch (\Exception $e) {
Log::error('Error in TaxonomyApiController@get_taxonomy', ['error' => $e->getMessage()]);
return response()->json(['status' => 'error', 'message' => 'An error occurred while fetching a taxonomy'], 500);
}
}


public function get_term($term_code)
{
try {
$result = TaxonomyTerm::where('code', $term_code)->first();

if ($result) {
$data = TaxonomyTermResource::collection([$result])->resolve()[0];
$data['taxonomy'] = $result->taxonomy->code; // Add the taxonomy code at the top-level term

return response()->json(
[
'status' => 'success',
'data' => $data
]
);
} else {
return response()->json(['status' => 'error', 'message' => 'Taxonomy term not found'], 404);
}
} catch (\Exception $e) {
Log::error('Error in TaxonomyApiController@get_term', ['error' => $e->getMessage()]);
return response()->json(['status' => 'error', 'message' => 'An error occurred while fetching a taxonomy term'], 500);
}
}
}
Loading
Loading