Skip to content

Commit

Permalink
feat: multiple titles on characters
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Oct 25, 2024
1 parent bab2d2f commit 4fe01ae
Show file tree
Hide file tree
Showing 25 changed files with 546 additions and 155 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/Admin/Characters/CharacterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getCreateCharacter() {
'subtypes' => ['0' => 'Pick a Species First'],
'features' => Feature::getDropdownItems(1),
'isMyo' => false,
'titles' => ['0' => 'Select Title', 'custom' => 'Custom Title'] + CharacterTitle::orderBy('sort', 'DESC')->pluck('title', 'id')->toArray(),
'titles' => ['custom' => 'Custom Title'] + CharacterTitle::orderBy('sort', 'DESC')->pluck('title', 'id')->toArray(),
]);
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public function postCreateCharacter(Request $request, CharacterManager $service)
'designer_id', 'designer_url',
'artist_id', 'artist_url',
'species_id', 'subtype_id', 'rarity_id', 'feature_id', 'feature_data',
'image', 'thumbnail', 'image_description', 'title_id', 'title_data',
'image', 'thumbnail', 'image_description', 'title_ids', 'title_data',
]);
if ($character = $service->createCharacter($data, Auth::user())) {
flash('Character created successfully.')->success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function getEditImageFeatures($id) {
* @return \Illuminate\Http\RedirectResponse
*/
public function postEditImageFeatures(Request $request, CharacterManager $service, $id) {
$data = $request->only(['species_id', 'subtype_id', 'rarity_id', 'feature_id', 'feature_data', 'title_id', 'title_data']);
$data = $request->only(['species_id', 'subtype_id', 'rarity_id', 'feature_id', 'feature_data', 'title_ids', 'title_data']);
$image = CharacterImage::find($id);
if (!$image) {
abort(404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getEditTitle($id) {
public function postCreateEditTitle(Request $request, CharacterTitleService $service, $id = null) {
$id ? $request->validate(CharacterTitle::$updateRules) : $request->validate(CharacterTitle::$createRules);
$data = $request->only([
'title', 'short_title', 'rarity_id', 'description', 'image', 'remove_image',
'title', 'short_title', 'rarity_id', 'description', 'image', 'remove_image', 'colour',
]);
if ($id && $service->updateTitle(CharacterTitle::find($id), $data, Auth::user())) {
flash('Title updated successfully.')->success();
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/BrowseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ public function getCharacters(Request $request) {

if ($request->get('title_id')) {
if ($request->get('title_id') == 'custom') {
$imageQuery->whereNull('title_id')->whereNotNull('title_data');
$imageQuery->whereRelation('titles', 'title_id', null)->whereNotNull('title_data');
} else {
$imageQuery->where('title_id', $request->get('title_id'));
$imageQuery->whereRelation('titles', 'title_id', $request->get('title_id'));
}
}
if ($request->get('title_id') == 'custom' && $request->get('title_data')) {
$imageQuery->where('title_data', 'LIKE', '%'.$request->get('title_data').'%');
$imageQuery->whereRelation('titles', 'title_data', 'LIKE', '%'.$request->get('title_data').'%');
}

if ($request->get('artist')) {
Expand Down
18 changes: 1 addition & 17 deletions app/Models/Character/CharacterDesignUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CharacterDesignUpdate extends Model {
'hash', 'species_id', 'subtype_id', 'rarity_id',
'has_comments', 'has_image', 'has_addons', 'has_features',
'submitted_at', 'update_type', 'fullsize_hash',
'approval_votes', 'rejection_votes', 'title_id', 'title_data',
'approval_votes', 'rejection_votes',
];

/**
Expand Down Expand Up @@ -111,13 +111,6 @@ public function rarity() {
return $this->belongsTo(Rarity::class, 'rarity_id');
}

/**
* Get the title of the design update.
*/
public function title() {
return $this->belongsTo('App\Models\Character\CharacterTitle', 'title_id');
}

/**
* Get the features (traits) attached to the design update, ordered by display order.
*/
Expand Down Expand Up @@ -348,15 +341,6 @@ public function getVoteDataAttribute() {
return collect(json_decode($this->attributes['vote_data'], true));
}

/**
* Get the title data attribute as an associative array.
*
* @return array
*/
public function getTitleDataAttribute() {
return json_decode($this->attributes['title_data'], true);
}

/**********************************************************************************************
OTHER FUNCTIONS
Expand Down
35 changes: 21 additions & 14 deletions app/Models/Character/CharacterImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CharacterImage extends Model {
'extension', 'use_cropper', 'hash', 'fullsize_hash', 'fullsize_extension', 'sort',
'x0', 'x1', 'y0', 'y1',
'description', 'parsed_description',
'is_valid', 'title_id', 'title_data',
'is_valid',
];

/**
Expand Down Expand Up @@ -110,8 +110,8 @@ public function rarity() {
/**
* Get the title of the character image.
*/
public function title() {
return $this->belongsTo('App\Models\Character\CharacterTitle', 'title_id');
public function titles() {
return $this->hasMany(CharacterImageTitle::class, 'character_image_id');
}

/**
Expand Down Expand Up @@ -275,24 +275,31 @@ public function getThumbnailUrlAttribute() {
}

/**
* Checks if the image has title info associated with it.
*
* Displays all of the images titles.
*
* @return string
*/
public function getHasTitleAttribute() {
if (isset($this->title_id) || isset($this->title_data)) {
return true;
} else {
return false;
public function getDisplayTitlesAttribute() {
$titles = [];
foreach ($this->titles as $title) {
$titles[] = $title->displayTitle;
}

return implode(' ', $titles);
}

/**
* Get the title data attribute as an associative array.
* Gets the id array of titles for select forms.
*
* @return array
* @return string
*/
public function getTitleDataAttribute() {
return json_decode($this->attributes['title_data'], true);
public function getTitleIdsAttribute() {
$ids = [];
// we have to do foreach because null id means 'custom' title
foreach ($this->titles as $title) {
$ids[] = $title->title_id ?? 'custom';
}

return $ids;
}
}
71 changes: 71 additions & 0 deletions app/Models/Character/CharacterImageTitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Models\Character;

use App\Models\Model;

class CharacterImageTitle extends Model {
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'character_image_id', 'title_id', 'data',
];

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'character_image_titles';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
];

/**********************************************************************************************
RELATIONS
**********************************************************************************************/

/**
* Get the character image.
*/
public function image() {
return $this->belongsTo(CharacterImage::class, 'character_image_id');
}

/**
* Get the title.
*/
public function title() {
return $this->belongsTo(CharacterTitle::class, 'title_id');
}

/**********************************************************************************************
ATTRIBUTES
**********************************************************************************************/

/**
* Displays the title.
*
* @return string
*/
public function getDisplayTitleAttribute() {
if ($this->title_id) {
return $this->title->displayTitle($this->data);
}

return '<div><span class="badge ml-1" style="color: white; background-color: #ddd;">'.isset($this->data['short']) ?? $this->data['full'].'</span></div>';
}
}
42 changes: 38 additions & 4 deletions app/Models/Character/CharacterTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models\Character;

use App\Models\Model;
use App\Models\Rarity;

class CharacterTitle extends Model {
/**
Expand All @@ -11,7 +12,7 @@ class CharacterTitle extends Model {
* @var array
*/
protected $fillable = [
'title', 'short_title', 'sort', 'has_image', 'description', 'parsed_description', 'rarity_id',
'title', 'short_title', 'sort', 'has_image', 'description', 'parsed_description', 'rarity_id', 'colour',
];

/**
Expand Down Expand Up @@ -55,12 +56,12 @@ class CharacterTitle extends Model {
* Get the rarity of the character image.
*/
public function rarity() {
return $this->belongsTo('App\Models\Rarity', 'rarity_id');
return $this->belongsTo(Rarity::class, 'rarity_id');
}

/**********************************************************************************************
ACCESSORS
ATTRIBUTES
**********************************************************************************************/

Expand Down Expand Up @@ -88,7 +89,7 @@ public function getDisplayNamePartialAttribute() {
* @return string
*/
public function getDisplayNameFullAttribute() {
return '<a href="'.$this->url.'" class="display-rarity">'.$this->title.'</a>'.($this->short_title ? ' ('.$this->short_title.')' : '').($this->rarity ? ' ('.$this->rarity->displayName.')' : '');
return '<a href="'.$this->url.'" style="color: '.$this->colour.' !important;" class="display-rarity">'.$this->title.'</a>'.($this->short_title ? ' ('.$this->short_title.')' : '').($this->rarity ? ' ('.$this->rarity->displayName.')' : '');
}

/**
Expand Down Expand Up @@ -149,6 +150,15 @@ public function getUrlAttribute() {
return url('world/character-titles?title='.$this->title);
}

/**
* Gets the URL of the model's encyclopedia page.
*
* @return string
*/
public function getIdUrlAttribute() {
return url('world/character-titles/'.$this->id);
}

/**
* Gets the URL for a masterlist search of characters of this rarity.
*
Expand All @@ -157,4 +167,28 @@ public function getUrlAttribute() {
public function getSearchCharactersUrlAttribute() {
return url('masterlist?title_id='.$this->id);
}

/**
* Gets the currency's asset type for asset management.
*
* @return string
*/
public function getAssetTypeAttribute() {
return 'character_title';
}

/**********************************************************************************************
OTHER FUNCTIONS
**********************************************************************************************/

/**
* Displays the title like a typing.
*/
public function displayTitle($data) {
return '<a href="'.$this->idUrl.'"><span class="badge ml-1" style="color: white; background-color: '.$this->colour.';"'.
($data['full'] ? 'data-toggle="tooltip" title="'.$this->title.'">'.$data['full'] : '>'.$this->title)
.'</span></a>';
}
}
Loading

0 comments on commit 4fe01ae

Please sign in to comment.