<?php
namespace App\Http\View\Composers;

use App\Page;
use App\Product;
use Illuminate\View\View;
use App\Option;

class FrontendComposer
{
	public function compose(View $view)
	{
        $links = Page::whereNull('parent_id')->whereJsonContains('position_id', '1')->whereHas('post', function($q) {
            $q->where('active', 1);
        })->get()->sortBy('post.order');
        $footer_links = Page::whereJsonContains('position_id', '2')->whereHas('post', function($q) {
            $q->where('active', 1);
        })->get()->sortBy('post.order');
        $status = session()->get('status');
        $optionsAll = Option::all();
        foreach($optionsAll as $option) {
            $options[$option->name] = $option->value;
        }
        $options = json_decode(json_encode($options));
        $tags_raw = Product::pluck('tags')->toArray();

        foreach($tags_raw as $tag_raw) {
            foreach(explode(',', $tag_raw) as $tag) {
                $tags[] = $tag;
            }
        }

        $view->with([
            'links' => $links,
            'options' => $options,
            'status' => $status,
            'tags' => $tags,
            'footer_links' => $footer_links,
        ]);
	}
}
