Skip to content

Commit

Permalink
allow references on entity (w/o attribute)
Browse files Browse the repository at this point in the history
Signed-off-by: Vinzenz Rosenkranz <[email protected]>
  • Loading branch information
v1r0x committed Dec 6, 2024
1 parent 902b39c commit 24deaea
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 175 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## 0.11
### Added
- References can also be added to an Entity, not only to an Entity Attribute

## 0.10.1
### Added
- Option to display attributes in _Data Model Editor_ in groups
Expand All @@ -10,8 +14,8 @@ All notable changes to this project will be documented in this file.
- selects the first choice (if there is **only one choice** in the dropdown)*
- selects the exact match (**case insensitive**; e.g. "apple" + `Tab` will select the available choice "apple", but also "Apple")*
- nothing and focuses the next attribute (default)
- * Selected elements will be marked with a blue (Tab) badge
- Pressing `Delete` inside _Single Choice Dropdowns_ will clear the element
- * Selected elements will be marked with a blue (Tab) badge
- Pressing `Delete` inside _Single Choice Dropdowns_ will clear the element
- Importer now automatically removes BOM if present
- Better readable format for error message on validation
- Renamed _fromImport_ to _parseImport_ on the attribute classses. The base class now by default imports the passed string, removing redundancies on the string-based classes.
Expand Down
36 changes: 24 additions & 12 deletions app/Http/Controllers/ReferenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public function getByEntity($id) {

$groupedReferences = [];
foreach($references as $r) {
$key = $r->attribute->thesaurus_url;
if(isset($r->attribute)) {
$key = $r->attribute->thesaurus_url;
} else {
$key = 'on_entity';
}

if(!isset($groupedReferences[$key])) {
$groupedReferences[$key] = [];
}
Expand All @@ -53,7 +58,7 @@ public function getByEntity($id) {

// POST

public function addReference(Request $request, $id, $aid) {
public function addReference(Request $request, int $id, ?int $aid = null) {
$user = auth()->user();
if(!$user->can('entity_data_create')) {
return response()->json([
Expand All @@ -69,18 +74,25 @@ public function addReference(Request $request, $id, $aid) {
'error' => __('This entity does not exist')
], 400);
}
try {
Attribute::findOrFail($aid);
} catch(ModelNotFoundException $e) {
return response()->json([
'error' => __('This attribute does not exist')
], 400);
}

$props = array_merge([
$data = [
'entity_id' => $id,
'attribute_id' => $aid
], $request->only(array_keys(Reference::rules)));
];
if(isset($aid)) {
try {
Attribute::findOrFail($aid);
$data['attribute_id'] = $aid;
} catch(ModelNotFoundException $e) {
return response()->json([
'error' => __('This attribute does not exist')
], 400);
}
}

$props = array_merge(
$data,
$request->only(array_keys(Reference::rules))
);
$reference = Reference::add($props, $user);
return response()->json($reference, 201);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class Reference extends Model

const rules = [
'bibliography_id' => 'required|integer|exists:bibliography,id',
'description' => 'string|nullable'
'description' => 'required|string'
];

const patchRules = [
'description' => 'string|nullable'
'description' => 'required|string'
];

public function getActivitylogOptions() : LogOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use App\Reference;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
activity()->disableLogging();

Schema::table('references', function (Blueprint $table) {
$table->integer('attribute_id')->nullable()->change();
});

activity()->enableLogging();
}

/**
* Reverse the migrations.
*/
public function down(): void
{
activity()->disableLogging();

Reference::whereNull('attribute_id')->delete();
Schema::table('references', function (Blueprint $table) {
$table->integer('attribute_id')->nullable(false)->change();
});

activity()->enableLogging();
}
};
12 changes: 9 additions & 3 deletions resources/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,15 @@ export async function addEntityTypeAttribute(etid, aid, rank) {
}

export async function addReference(eid, aid, data) {
return $httpQueue.add(
() => http.post(`/entity/${eid}/reference/${aid}`, data).then(response => response.data)
);
if(aid) {
return $httpQueue.add(
() => http.post(`/entity/${eid}/reference/${aid}`, data).then(response => response.data)
);
} else {
return $httpQueue.add(
() => http.post(`/entity/${eid}/reference`, data).then(response => response.data)
);
}
}

export async function getFilteredActivity(pageUrl, payload) {
Expand Down
20 changes: 12 additions & 8 deletions resources/js/bootstrap/stores/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function updateSelectionTypeIdList(selection) {
const handleAddEntityType = (context, typeData, attributes = []) => {
context.entityTypeAttributes[typeData.id] = attributes.slice();
context.entityTypes[typeData.id] = typeData;
}
};

const handlePostDelete = (context, entityId) => {
const currentRoute = router.currentRoute.value;
Expand All @@ -87,7 +87,7 @@ const handlePostDelete = (context, entityId) => {
}
}
}
}
};

export const useEntityStore = defineStore('entity', {
state: _ => ({
Expand Down Expand Up @@ -180,7 +180,7 @@ export const useEntityStore = defineStore('entity', {
colors = state.entityTypeColors[id];
}
return colors;
}
};
},
getEntityTypeName(state) {
return id => {
Expand Down Expand Up @@ -512,8 +512,8 @@ export const useEntityStore = defineStore('entity', {
}
}

// Remove the data from the entity.
// We need to do this as the 'replace', 'add' 'remove'
// Remove the data from the entity.
// We need to do this as the 'replace', 'add' 'remove'
// operations are calculated based on this value.
for(const attributeId in removedData) {
if(entity.data[attributeId]) {
Expand Down Expand Up @@ -544,12 +544,17 @@ export const useEntityStore = defineStore('entity', {
});
},
handleReference(entityId, attributeUrl, action, data) {
const entity = this.getEntity(entityId);
let references;
if(attributeUrl) {
references = entity?.references[attributeUrl] || [];
} else {
references = entity?.references.on_entity || [];
}
if(action == 'add') {
const references = this.getEntity(entityId)?.references[attributeUrl] || [];
references.push(data);
return data;
} else if(action == 'update') {
const references = this.getEntity(entityId)?.references[attributeUrl] || [];
const id = data.id;
const refData = data.data;
const updateData = data.updates;
Expand All @@ -561,7 +566,6 @@ export const useEntityStore = defineStore('entity', {
reference.updated_at = updateData.updated_at;
}
} else if(action == 'delete') {
const references = this.getEntity(entityId)?.references[attributeUrl] || [];
const idx = references.findIndex(ref => ref.id == data.id);
if(idx > -1) {
references.splice(idx, 1);
Expand Down
Loading

0 comments on commit 24deaea

Please sign in to comment.