Skip to content

Instantly share code, notes, and snippets.

@jackbarham
Created November 19, 2017 18:06
Show Gist options
  • Save jackbarham/01f8e2d9fcbe4daac5c9e73df99bc457 to your computer and use it in GitHub Desktop.
Save jackbarham/01f8e2d9fcbe4daac5c9e73df99bc457 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Page;
use App\Models\NewsItem;
use App\Models\VideoItem;
use App\Models\AudioItem;
use App\Models\GalleryItem;
use App\Models\ReleaseItem;
use App\Models\SliderModule;
class PageController extends Controller
{
/**
* @param $domain
* @return View
*/
public function home($domain)
{
$user = User::where('live_domain', $domain)->first();
$data = Page::where(['user_id' => $user->id, 'visibility' => 'visible'])
->with('modules.content')
->first();
foreach ($data->modules as $module) {
if ($module->type === 'newsfeed') {
$newsfeed = NewsItem::where('page_id', $module->content->news_page_id)
->with('modules.content')
->limit($module->content->limit)
->get();
}
if ($module->type === 'slider') {
$slider = SliderModule::where('id', $module->content->id)
->with('slideritems')
->first();
}
}
//return compact('data', 'newsfeed', 'slider');
return view($user->settings->active_theme . '.home', compact('data', 'newsfeed', 'slider'));
}
/**
* @param $domain
* @param $pageSlug
* @return View
*/
public function page($domain, $pageSlug)
{
$user = User::where('live_domain', $domain)->first();
$page = Page::where(['slug' => $pageSlug, 'user_id' => $user->id])->first();
$settings = $user->settings;
if (!$page) {
return view($settings->active_theme . '.' . 'errors.404', compact('data'));
}
switch ($page->type) {
case 'custom':
$data = $page->where(['slug' => $pageSlug, 'user_id' => $user->id])
->with('modules.content')
->first();
break;
case 'audio':
$data = $page->where(['slug' => $pageSlug, 'user_id' => $user->id])
->with('audioitems.content')
->first();
break;
case 'video':
$data = $page->where(['slug' => $pageSlug, 'user_id' => $user->id])
->with('videoitems.content')
->first();
break;
case 'gallery':
$data = $page->where(['slug' => $pageSlug, 'user_id' => $user->id])
->with('galleryitems.content.galleryimages')
->first();
break;
case 'image':
$data = $page->where(['slug' => $pageSlug, 'user_id' => $user->id])
->with('modules.content.galleryimages')
->first();
break;
case 'gig':
$data = $page->where(['slug' => $pageSlug, 'user_id' => $user->id])
->with('gigitems.giglinks')
->first();
break;
case 'release':
$data = $page->where(['slug' => $pageSlug, 'user_id' => $user->id])
->with('releaseitems')
->first();
break;
case 'news':
$data = $page->where(['slug' => $pageSlug, 'user_id' => $user->id])
->with('newsitems.modules.content')
->first();
break;
default:
return view($settings->active_theme . '.' . 'home');
}
//return compact('data', 'settings');
return view($user->settings->active_theme . '.' . $page->type, compact('data', 'settings'));
}
/**
* @param $domain
* @param $pageSlug
* @param $itemSlug
* @return View
*/
public function item($domain, $pageSlug, $itemSlug)
{
$user = User::where('live_domain', $domain)->first();
$page = Page::where(['slug' => $pageSlug, 'user_id' => $user->id])->first();
$settings = $user->settings;
switch ($page->type) {
case 'audio':
$data = AudioItem::where(['page_id' => $page->id, 'slug' => $itemSlug])->with('content')->first();
if (!$data) {
return view($settings->active_theme . '.' . 'errors.404', compact('data'));
}
break;
case 'video':
$data = VideoItem::where(['page_id' => $page->id, 'slug' => $itemSlug])->with('content')->first();
if (!$data) {
return view($settings->active_theme . '.' . 'errors.404', compact('data'));
}
break;
case 'gallery':
$data = GalleryItem::where(['page_id' => $page->id, 'slug' => $itemSlug])->with('content.galleryimages')->first();
if (!$data) {
return view($settings->active_theme . '.' . 'errors.404', compact('data'));
}
break;
case 'release':
$data = ReleaseItem::where(['page_id' => $page->id, 'slug' => $itemSlug])->with('releaseclips', 'releaselinks')->first();
if (!$data) {
return view($settings->active_theme . '.' . 'errors.404', compact('data'));
}
break;
case 'news':
$data = NewsItem::where(['page_id' => $page->id, 'slug' => $itemSlug])->with('modules.content')->first();
if (!$data) {
return view($settings->active_theme . '.' . 'errors.404', compact('data'));
}
break;
default:
return view($settings->active_theme . '.' . 'home');
}
//return compact('data', 'settings');
return view($settings->active_theme . '.' . $page->type . '-item', compact('data', 'settings'));
}
}
@jackbarham
Copy link
Author

jackbarham commented Nov 19, 2017

Example fix:

I have to loop through page modules (line 24) and if those modules exist I have to recall them again (from line 27) and output them separately. I the module items to be attached to the modules. At the moment I use Blade to attach them together, but this does not work as there can be multiple versions of the same module.

I've tried ->with('modules.content.newsfeed.content.modules') but it doesn't work.

You can see this in action here: http://carlcox.vibecast.com

{
	"data": {
	"page_title": "Home",
		"menu_title": "Home",
		"slug": "home",
		"type": "home",
		"order": 0,
		"visibility": "visible",
		"meta_image": null,
		"meta_description": null,
		"modules": [
		{
			"type": "slider",
			"order": 1,
			"preview": 0,
			"size": "wide",
			"visibility": "visible",
			"content": {
				"delay": 7000,
				"speed": 600
			}
		},
		{
			"type": "newsfeed",
			"order": 2,
			"preview": 0,
			"size": "wide",
			"visibility": "visible",
			"content": {
				"news_page_id": 10,
				"limit": 6
			}
		}
	]
},
	"newsfeed": [
	{
		"title": "Carl Cox - Tomorrowland 2015",
		"slug": "carl-cox-tomorrowland-2015",
		"visibility": "visible",
		"meta_image": null,
		"meta_description": null,
		"published_at": "2017-11-09 19:10:22",
		"modules": [
			{
				"type": "video",
				"order": 1,
				"preview": 1,
				"size": "wide",
				"visibility": "visible",
				"content": {
					"source": "youtube",
					"source_id": "PCVI7qFU17c",
					"custom_cover": null,
					"source_title": "Demo video title",
					"cover": "https://i.ytimg.com/vi/PCVI7qFU17c/maxresdefault.jpg"
				}
			},
			{
				"type": "text",
				"order": 2,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"text": "<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <a href=\"#\">Excepteur sint occaecat</a> cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>\n                    <p><strong>Lorem ipsum dolor</strong> sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>"
				}
			},
			{
				"type": "audio",
				"order": 3,
				"preview": 0,
				"size": "wide",
				"visibility": "visible",
				"content": {
					"source": "mc_track",
					"source_id": "https://www.mixcloud.com/IbizaSpotlight/sptl186-anna-wall/",
					"source_title": "Demo audio title",
					"cover": "https://thumbnailer.mixcloud.com/unsafe/600x600/extaudio/4/1/3/6/f7ca-b705-4e59-a23c-d68716164057.jpg"
				}
			},
			{
				"type": "text",
				"order": 4,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"text": "<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>\n                    <blockquote>\n                        <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>\n                    </blockquote>\n                    <p>Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>"
				}
			},
			{
				"type": "audio",
				"order": 5,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"source": "sc_set",
					"source_id": "136120495",
					"source_title": "Demo audio title",
					"cover": "https://i1.sndcdn.com/artworks-000126828399-agfjvp-large.jpg"
				}
			},
			{
				"type": "gallery",
				"order": 8,
				"preview": 0,
				"size": "wide",
				"visibility": "visible",
				"content": []
			}
		]
	},
	{
		"title": "Recommended: Belle & Bex - We Like to Disco",
		"slug": "belle-and-bex-we-like-disco",
		"visibility": "visible",
		"meta_image": null,
		"meta_description": null,
		"published_at": "2017-11-09 19:10:22",
		"modules": [
			{
				"type": "audio",
				"order": 1,
				"preview": 1,
				"size": "wide",
				"visibility": "visible",
				"content": {
					"source": "sc_track",
					"source_id": "294905928",
					"source_title": "Demo audio title",
					"cover": "https://i1.sndcdn.com/artworks-000195451583-3y5hfa-large.jpg"
				}
			},
			{
				"type": "text",
				"order": 2,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
				}
			},
			{
				"type": "image",
				"order": 3,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"alt": "News image",
					"url": null,
					"new_tab": null
				}
			},
			{
				"type": "text",
				"order": 4,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"text": "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
				}
			}
		]
	},
	{
		"title": "Todays playground adventures",
		"slug": "todays-playground-adventures",
		"visibility": "visible",
		"meta_image": null,
		"meta_description": null,
		"published_at": "2017-11-09 19:10:22",
		"modules": [
			{
				"type": "image",
				"order": 1,
				"preview": 1,
				"size": "wide",
				"visibility": "visible",
				"content": {
					"alt": "News image",
					"url": null,
					"new_tab": null
				}
			},
			{
				"type": "text",
				"order": 2,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"text": "Today lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
				}
			}
		]
	},
	{
		"title": "Moodtrap - Make It Better (Cozzy D Remix)",
		"slug": "moodtrap-make-it-better-cozzy-d-remix",
		"visibility": "visible",
		"meta_image": null,
		"meta_description": null,
		"published_at": "2017-11-09 19:10:22",
		"modules": [
			{
				"type": "audio",
				"order": 1,
				"preview": 1,
				"size": "wide",
				"visibility": "visible",
				"content": {
					"source": "sc_track",
					"source_id": "230776311",
					"source_title": "Demo audio title",
					"cover": "https://i1.sndcdn.com/artworks-000134463152-z4jx9q-large.jpg"
				}
			},
			{
				"type": "text",
				"order": 2,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"text": "Trap lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
				}
			}
		]
	},
	{
		"title": "Hanging out with this lot",
		"slug": "hanging-out-with-this-lot",
		"visibility": "visible",
		"meta_image": null,
		"meta_description": null,
		"published_at": "2017-11-09 19:10:22",
		"modules": [
			{
				"type": "image",
				"order": 1,
				"preview": 1,
				"size": "wide",
				"visibility": "visible",
				"content": {
					"alt": "News image",
					"url": null,
					"new_tab": null
				}
			},
			{
				"type": "text",
				"order": 2,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"text": "Lot lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
				}
			}
		]
	},
	{
		"title": "LA Mix Tape",
		"slug": "la-mix-tape",
		"visibility": "visible",
		"meta_image": null,
		"meta_description": null,
		"published_at": "2017-11-09 19:10:22",
		"modules": [
			{
				"type": "audio",
				"order": 1,
				"preview": 1,
				"size": "wide",
				"visibility": "visible",
				"content": {
					"source": "mc_track",
					"source_id": "https://www.mixcloud.com/livbethknight/la-mix-tape-sept-1510/",
					"source_title": "Demo audio title",
					"cover": "//thumbnail.mixcloud.com/w/600/h/600/q/85/upload/images/extaudio/c23d15b2-c455-4493-9c66-b6ea2d674975.jpg"
				}
			},
			{
				"type": "text",
				"order": 2,
				"preview": 0,
				"size": "mid",
				"visibility": "visible",
				"content": {
					"text": "LA lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
				}
			}
		]
	}
],
	"slider": {
		"delay": 7000,
			"speed": 600,
			"slideritems": [
			{
				"image": "https://imagedev.vibecast.com/7ff746f01a534115aee8819cdfd13375.jpg",
				"title": "New track out now",
				"url": "http://carlcox.vibecast.com/releases/stand-up-right-ep",
				"visibility": "visible",
				"order": 1,
				"published_at": "2017-11-09 19:10:23"
			},
			{
				"image": "https://imagedev.vibecast.com/b884a350abc74e5d97c641f954a95541.jpg",
				"title": "Ultra music festival",
				"url": "http://carlcox.vibecast.com/video/martin-garrix-live-2014",
				"visibility": "visible",
				"order": 1,
				"published_at": "2017-11-09 19:10:23"
			}
		]
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment