<?php
    class StoryController extends Controller
    {

        /*
        | Stories Controller - grabs all story routes and tries to fulfill
        | the request by looking to the story model
        */

        public function getIndex()
        {
            App::abort(404);
        }

        public function showDetail($storySlug)
        {
            $story = Story::full()
                ->where('lang_slug', $storySlug)
                ->first();

            // Didn't Find Story?
            if (!$story) {
                App::abort(404);
            }

            // If story is not published and not admin throw 404
            if ($story->status != 'published') {
                $user = Auth::backend()->user();
                if (!$user) {
                    App::abort(404);
                }
            }

            $links = [];
            foreach (getAvailableLanguages() as $langCode => $lang) {
                $storySlug = $story->websites->first()->languages
                    ->findByLocale($langCode)->lang_slug;
                $links[ $langCode ] = url('/' . $langCode . Lang::get('routes.uri_stories', [], $langCode)
                    . '/' . $storySlug);
            }

            // Parse Meta Data
            $metaData = new stdClass();
            $metaData->meta_title = $story->meta_title;
            $metaData->meta_keywords = $story->meta_keywords;
            $metaData->meta_description = $story->meta_description;

            $view = 'frontend.pages.stories.stories';
            $view = View::make($view)
                ->with([
                    'langLinks' => $links,
                    'metaData'  => $metaData,
                ]);

            return Response::make($view);
        }
    }
