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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .github/workflows/laravel_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"

- name: Remove existing composer.lock
run: rm ./composer.lock

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/laravel_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ jobs:
- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"

- name: Remove existing composer.lock
run: rm ./composer.lock

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

- name: Generate key
run: php artisan key:generate

- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache

- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite

- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
Expand Down
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);
}
}
}
86 changes: 86 additions & 0 deletions app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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 TaxonomyScope,
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'
];

protected $casts = [
'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 to_dict()
{
$taxonomy = $this->toArray();
foreach (['properties', 'created_at', 'updated_at', 'created_by', 'updated_by'] as $attribute) {
unset($taxonomy[$attribute]);
}
$taxonomy['properties'] = json_decode($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();
$filteredMetadata = array_filter(json_decode($this->metadata, true), 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');
}

public function getMetadata($code)
{
$metadata = json_decode($this->metadata, true);

if (is_array($metadata)) {
foreach ($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();
}
}
11 changes: 11 additions & 0 deletions app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Domains\Taxonomy\Models\Traits\Scope;

/**
* Class TaxonomyScope.
*/
trait TaxonomyScope
{

}
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;
}
}
Loading
Loading