Skip to content

Commit

Permalink
move slideshow
Browse files Browse the repository at this point in the history
  • Loading branch information
bastihilger committed Jul 11, 2022
1 parent a3de3cb commit 67976bf
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 22 deletions.
30 changes: 8 additions & 22 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/nova-cms-portfolio.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'has_category_slideshows' => true,
'has_custom_portfolio_image' => true,
'has_embed_code' => true,
'has_move_to_slideshow' => false,
'has_select_portfolio_image' => true,
'has_projects_zip_upload' => false,
'has_quick_upload' => true,
Expand Down
3 changes: 3 additions & 0 deletions resources/lang/nova-cms-portfolio/de/portfolio.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

return [
'afterwards' => 'Danach',
'background_color' => 'Hintergrund-Farbe',
'description' => 'Beschreibung',
'go_where_moved_to' => 'Gehe dorthin, wohin bewegt wurde',
'published' => 'Veröffentlicht',
'stay_here' => 'Bleibe hier',
'subtitle' => 'Untertitel',
'title' => 'Titel',
];
1 change: 1 addition & 0 deletions resources/lang/nova-cms-portfolio/de/works.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'embed_url' => 'Embed URL (Youtube, Vimeo etc.)',
'is_artist_discipline_image' => 'Repräsentiert Künstler in Disziplin',
'is_artist_portfolio_image' => 'Ist Künstler-Portfolio-Bild',
'move_to_different_project' => 'In anderes :attribute verschieben',
'overview_categories' => 'Kat. (Übersicht)',
'representation_categories' => 'Kat. (Repräsentiert...)',
'represents_artist_in_discipline_category' => 'In allgemeiner Kategorie-Übersicht zeigen',
Expand Down
3 changes: 3 additions & 0 deletions resources/lang/nova-cms-portfolio/en/portfolio.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

return [
'afterwards' => 'afterwards',
'background_color' => 'background color',
'description' => 'description',
'go_where_moved_to' => 'go where they have been moved to',
'published' => 'published',
'stay_here' => 'stay here',
'subtitle' => 'subtitle',
'title' => 'title',
];
1 change: 1 addition & 0 deletions resources/lang/nova-cms-portfolio/en/works.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'embed_url' => 'Embed URL (Youtube, Vimeo etc.)',
'is_artist_discipline_image' => 'represents artist in discipline',
'is_artist_portfolio_image' => 'is artist portfolio image',
'move_to_different_project' => 'move to different :attribute',
'overview_categories' => 'category prio in overview',
'representation_categories' => 'cat. (represents...)',
'represents_artist_in_discipline_category' => 'represents artist on frontpage category',
Expand Down
12 changes: 12 additions & 0 deletions src/Models/Slideshow.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,16 @@ public function workForNews()

return $markedWork;
}

public function refreshWorksOrder()
{
$newPosition = 1;
foreach ($this->works as $work) {
Work::withoutEvents(function () use ($work, $newPosition) {
$work->update(['sort_order' => $newPosition]);
});

$newPosition++;
}
}
}
109 changes: 109 additions & 0 deletions src/Nova/Actions/MoveToSlideshow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Kraenkvisuell\NovaCmsPortfolio\Nova\Actions;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Kraenkvisuell\NovaCmsPortfolio\Models\Slideshow;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Select;

class MoveToSlideshow extends Action
{
public function name()
{
return ucfirst(__(
'nova-cms-portfolio::works.move_to_different_project',
[
'attribute' => config('nova-cms-portfolio.custom_slideshow_label')
?: ucfirst(__('nova-cms-portfolio::slideshows.slideshow')),
]
));
}

public function handle(ActionFields $fields, Collection $works)
{
$oldSlideshowId = $works->first()->slideshow_id;

foreach ($works as $work) {
$work->slideshow_id = $fields->slideshow_id;
$work->sort_order += 1000;
$work->save();
}

Slideshow::find($oldSlideshowId)->refreshWorksOrder();
Slideshow::find($fields->slideshow_id)->refreshWorksOrder();

if ($fields->afterwards == 'go_to_other_slideshow') {
return Action::push('/resources/slideshows/'.$fields->slideshow_id);
}
}

public function fields()
{
$slideshows = $this->getSlideshows();

return [
Select::make(
(
config('nova-cms-portfolio.custom_slideshow_label')
?: ucfirst(__('nova-cms-portfolio::slideshows.slideshow'))
),
'slideshow_id'
)
->options($slideshows)
->required()
->rules('required'),

Select::make(
ucfirst(__('nova-cms-portfolio::portfolio.afterwards')),
'afterwards'
)
->required()
->rules('required')
->options([
'go_to_other_slideshow' => __(
'nova-cms-portfolio::portfolio.go_where_moved_to'
),
'stay_on_this_slideshow' => __(
'nova-cms-portfolio::portfolio.stay_here'
),

])
->default('go_to_other_slideshow'),
];
}

protected function getSlideshows()
{
return Cache::remember(
'nova.artist.projects.'.request()->input('viaResourceId'),
now()->addSeconds(5),
function () {
$currentSlideshow = Slideshow::where('id', request()->input('viaResourceId'))
->with([
'artist.slideshows',
'artist.categories',
'categories',
])
->first();

$slideshows = [];

foreach ($currentSlideshow->artist->categories as $category) {
foreach ($currentSlideshow->artist->slideshows as $slideshow) {
if (
$currentSlideshow->id != $slideshow->id
&& $slideshow->categories->firstWhere('id', $category->id)
) {
$slideshows[$slideshow->id] = $category->title.': '.$slideshow->title;
}
}
}

return $slideshows;
}
);
}
}
5 changes: 5 additions & 0 deletions src/Nova/Work.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Cache;
use Kraenkvisuell\NovaCmsMedia\MediaLibrary;
use Kraenkvisuell\NovaCmsPortfolio\Models\Slideshow;
use Kraenkvisuell\NovaCmsPortfolio\Nova\Actions\MoveToSlideshow;
use Kraenkvisuell\NovaCmsPortfolio\Nova\Actions\ToggleArtistPortfolioImage;
use Kraenkvisuell\NovaCmsPortfolio\Nova\Actions\ToggleRepresentsArtistInCategory;
use Kraenkvisuell\NovaCmsPortfolio\Nova\Actions\ToggleShowInOverview;
Expand Down Expand Up @@ -294,6 +295,10 @@ public function actions(Request $request)
{
$actions = [];

if (config('nova-cms-portfolio.has_move_to_slideshow')) {
$actions[] = MoveToSlideshow::make();
}

if (config('nova-cms-portfolio.has_select_portfolio_image')) {
$actions[] = ToggleArtistPortfolioImage::make()
->onlyOnTableRow()
Expand Down
8 changes: 8 additions & 0 deletions src/Observers/SlideshowObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function saved(Slideshow $slideshow)
json_decode(request()->get('categories'))
);
}

$slideshow->refreshWorksOrder();
}

public function reordered(Slideshow $slideshow)
{
ray('asdf');
$slideshow->refreshWorksOrder();
}

protected function syncArtistCategories($artist, $categories)
Expand Down
6 changes: 6 additions & 0 deletions src/Observers/WorkObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kraenkvisuell\NovaCmsPortfolio\Observers;

use Kraenkvisuell\NovaCmsPortfolio\Models\Slideshow;
use Kraenkvisuell\NovaCmsPortfolio\Models\Work;

class WorkObserver
Expand All @@ -21,6 +22,11 @@ public function updated(Work $work)
$this->ensureOnlyOneArtistDisciplineImage($work);
}

public function saved(Work $work)
{
Slideshow::find($work->slideshow_id)->refreshWorksOrder();
}

protected function ensureOnlyOneArtistPortfolioImage($work)
{
if ($work->is_artist_portfolio_image) {
Expand Down

0 comments on commit 67976bf

Please sign in to comment.