Skip to content

Commit

Permalink
feat(publications): Entries can be tagged
Browse files Browse the repository at this point in the history
  • Loading branch information
zooley committed Feb 5, 2024
1 parent c723a37 commit 53e8a01
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ public function store(Request $request)
return redirect()->back()->withError(trans('global.messages.save failed'));
}

if ($request->has('tags'))
{
$tags = $request->input('tags');
$tags = explode(',', $tags);
$tags = array_map('trim', $tags);
$tags = array_filter($tags);
$row->setTags($tags);
}

if ($request->has('file'))
{
// Doing this by file extension is iffy at best but
Expand Down
159 changes: 135 additions & 24 deletions app/Modules/Publications/Http/Controllers/Api/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,21 +431,54 @@ public function index(Request $request)
* "type": "string"
* }
* }
* @apiParameter {
* "in": "body",
* "name": "tags",
* "description": "A list of tags to apply to the entry",
* "required": false,
* "schema": {
* "type": "array",
* "default": null
* }
* }
* @apiResponse {
* "201": {
* "description": "Successful entry creation",
* "content": {
* "application/json": {
* "example": {
* "id": 2,
* "menutype": "about",
* "title": "About",
* "description": "About Side Menu",
* "client_id": 0,
* "created_at": null,
* "updated_at": null,
* "deleted_at": null,
* "items_count": 12,
* "title": "Praesent commodo cursus magna, vel scelerisque nisl consectetur et.",
* "type_id": 10,
* "author": "John Smith",
* "editor": null,
* "url": null,
* "series": null,
* "booktitle": null,
* "edition": null,
* "chapter": null,
* "issuetitle": null,
* "journal": null,
* "issue": null,
* "volume": null,
* "number": null,
* "pages": null,
* "publisher": null,
* "address": null,
* "institution": null,
* "organization": null,
* "school": null,
* "crossref": null,
* "isbn": null,
* "doi": null,
* "note": null,
* "state": 1,
* "published_at": "2022-06-01 00:00:00",
* "created_at": "2022-06-20T17:59:21.000000Z",
* "updated_at": "2022-06-20T17:59:21.000000Z",
* "deleted_at": null,
* "filename": "",
* "tags": ["foo","bar"],
* "api": "https://example.org/api/publications/2"
* }
* }
Expand Down Expand Up @@ -487,6 +520,7 @@ public function create(Request $request)
'note' => 'nullable|string|max:2000',
'state' => 'nullable|integer',
'published_at' => 'nullable|datetime',
'tags' => 'nullable|array',
];

$validator = Validator::make($request->all(), $rules);
Expand Down Expand Up @@ -519,6 +553,16 @@ public function create(Request $request)
return response()->json(['message' => trans('global.messages.save failed')], 500);
}

if ($request->has('tags'))
{
$tags = $request->input('tags', []);
$tags = explode(',', $tags);
$tags = array_map('trim', $tags);
$tags = array_filter($tags);

$row->setTags($tags);
}

$row->api = route('api.publications.read', ['id' => $row->id]);

return new JsonResource($row);
Expand Down Expand Up @@ -546,14 +590,37 @@ public function create(Request $request)
* "application/json": {
* "example": {
* "id": 2,
* "menutype": "about",
* "title": "About",
* "description": "About Side Menu",
* "client_id": 0,
* "created_at": null,
* "updated_at": null,
* "deleted_at": null,
* "items_count": 12,
* "title": "Praesent commodo cursus magna, vel scelerisque nisl consectetur et.",
* "type_id": 10,
* "author": "John Smith",
* "editor": null,
* "url": null,
* "series": null,
* "booktitle": null,
* "edition": null,
* "chapter": null,
* "issuetitle": null,
* "journal": null,
* "issue": null,
* "volume": null,
* "number": null,
* "pages": null,
* "publisher": null,
* "address": null,
* "institution": null,
* "organization": null,
* "school": null,
* "crossref": null,
* "isbn": null,
* "doi": null,
* "note": null,
* "state": 1,
* "published_at": "2022-06-01 00:00:00",
* "created_at": "2022-06-20T17:59:21.000000Z",
* "updated_at": "2022-06-20T17:59:21.000000Z",
* "deleted_at": null,
* "filename": "",
* "tags": ["foo","bar"],
* "api": "https://example.org/api/publications/2"
* }
* }
Expand Down Expand Up @@ -839,21 +906,54 @@ public function read(int $id)
* "type": "string"
* }
* }
* @apiParameter {
* "in": "body",
* "name": "tags",
* "description": "A list of tags to apply to the entry",
* "required": false,
* "schema": {
* "type": "array",
* "default": null
* }
* }
* @apiResponse {
* "202": {
* "description": "Successful entry modification",
* "content": {
* "application/json": {
* "example": {
* "id": 2,
* "menutype": "about",
* "title": "About",
* "description": "About Side Menu",
* "client_id": 0,
* "created_at": null,
* "updated_at": null,
* "deleted_at": null,
* "items_count": 12,
* "title": "Praesent commodo cursus magna, vel scelerisque nisl consectetur et.",
* "type_id": 10,
* "author": "John Smith",
* "editor": null,
* "url": null,
* "series": null,
* "booktitle": null,
* "edition": null,
* "chapter": null,
* "issuetitle": null,
* "journal": null,
* "issue": null,
* "volume": null,
* "number": null,
* "pages": null,
* "publisher": null,
* "address": null,
* "institution": null,
* "organization": null,
* "school": null,
* "crossref": null,
* "isbn": null,
* "doi": null,
* "note": null,
* "state": 1,
* "published_at": "2022-06-01 00:00:00",
* "created_at": "2022-06-20T17:59:21.000000Z",
* "updated_at": "2022-06-20T17:59:21.000000Z",
* "deleted_at": null,
* "filename": "",
* "tags": ["foo","bar"],
* "api": "https://example.org/api/publications/2"
* }
* }
Expand Down Expand Up @@ -899,6 +999,7 @@ public function update(Request $request, int $id)
'note' => 'nullable|string|max:2000',
'state' => 'nullable|integer',
'published_at' => 'nullable|datetime',
'tags' => 'nullable|array',
];

$validator = Validator::make($request->all(), $rules);
Expand Down Expand Up @@ -931,6 +1032,16 @@ public function update(Request $request, int $id)
return response()->json(['message' => trans('global.messages.save failed')], 500);
}

if ($request->has('tags'))
{
$tags = $request->input('tags', []);
$tags = explode(',', $tags);
$tags = array_map('trim', $tags);
$tags = array_filter($tags);

$row->setTags($tags);
}

$row->api = route('api.publications.read', ['id' => $row->id]);

return new JsonResource($row);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class PublicationsController extends Controller
{
/**
* Display a listing of the resource.
* Display a listing
*
* @param StatefulRequest $request
* @return View
Expand All @@ -32,6 +32,7 @@ public function index(StatefulRequest $request)
'state' => 'published',
'type' => null,
'year' => null,
'tag' => null,
// Paging
'limit' => config('list_limit', 20),
'page' => 1,
Expand Down Expand Up @@ -107,6 +108,11 @@ public function index(StatefulRequest $request)
}
}

if ($filters['tag'])
{
$query->withTag($filters['tag']);
}

$rows = $query
->orderBy($filters['order'], $filters['order_dir'])
->paginate($filters['limit'], ['*'], 'page', $filters['page']);
Expand Down Expand Up @@ -225,6 +231,7 @@ public function store(Request $request)
'note' => 'nullable|string|max:2000',
'state' => 'nullable|integer',
'published_at' => 'nullable|datetime',
'tags' => 'nullable|array',
];

$validator = Validator::make($request->all(), $rules);
Expand Down Expand Up @@ -256,6 +263,16 @@ public function store(Request $request)
return redirect()->back()->withError(trans('global.messages.save failed'));
}

if ($request->has('tags'))
{
$tags = $request->input('tags', []);
$tags = explode(',', $tags);
$tags = array_map('trim', $tags);
$tags = array_filter($tags);

$row->setTags($tags);
}

if ($request->has('file'))
{
// Doing this by file extension is iffy at best but
Expand Down
16 changes: 12 additions & 4 deletions app/Modules/Publications/Models/Publication.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Modules\Publications\Events\PublicationUpdated;
use App\Modules\Publications\Events\PublicationDeleted;
use App\Modules\Publications\Helpers\Formatter;
use App\Modules\Tags\Traits\Taggable;
use Carbon\Carbon;

/**
Expand Down Expand Up @@ -54,7 +55,7 @@
*/
class Publication extends Model
{
use Historable, SoftDeletes;
use Historable, SoftDeletes, Taggable;

/**
* The table to which the class pertains
Expand Down Expand Up @@ -101,11 +102,18 @@ class Publication extends Model
* @var array<string,string>
*/
protected $dispatchesEvents = [
'created' => PublicationCreated::class,
'updated' => PublicationUpdated::class,
'deleted' => PublicationDeleted::class,
'created' => PublicationCreated::class,
'updated' => PublicationUpdated::class,
'deleted' => PublicationDeleted::class,
];

/**
* Tag domain
*
* @var string
*/
protected static $entityNamespace = 'publications';

/**
* Get a list of associated users
*
Expand Down
27 changes: 27 additions & 0 deletions app/Modules/Publications/Resources/assets/js/admin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global TomSelect */ // modules/core/vendor/tom-select/js/tom-select.complete.min.js

document.addEventListener('DOMContentLoaded', function () {
var alias = document.getElementById('field-alias');
Expand Down Expand Up @@ -33,6 +34,32 @@ document.addEventListener('DOMContentLoaded', function () {
});
});

var sels = new Array(), sel;
var tag = document.getElementById('field-tags');
if (tag) {
sel = new TomSelect(tag, {
//maxItems: 1,
valueField: 'slug',
labelField: 'slug',
searchField: ['name', 'slug'],
plugins: ['remove_button'],
persist: false,
// Fetch remote data
load: function (query, callback) {
var url = tag.getAttribute('data-api') + '?api_token=' + document.querySelector('meta[name="api-token"]').getAttribute('content') + '&search=' + encodeURIComponent(query);

fetch(url)
.then(response => response.json())
.then(json => {
callback(json.data);
}).catch(() => {
callback();
});
}
});
sels.push(sel);
}

if (document.getElementById('upload')) {
// feature detection for drag&drop upload
var isAdvancedUpload = function () {
Expand Down
Loading

0 comments on commit 53e8a01

Please sign in to comment.