<?php

namespace app\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Common\CommonLib;
use App\Common\DatabaseQuery;
use App\Interfaces\MenuInterface;
use Response;
use View;

class MenuPagesController extends Controller
{
    // declarations
    public $data = [];
    public $menu;
    public $clib;

    // constructor
    public function __construct(MenuInterface $menu)
    {
        // instance inits
        $this->menu = $menu;
        $this->clib = new CommonLib();
        $this->db = new DatabaseQuery();
    }

    // menu pages index controller
    public function index($slug)
    {
        // clear slug
        $slug = $this->clib->clear($slug);
        // get page
        $this->data['page'] = $this->menu->bySlug($slug);
        // check if page found
        if (!is_object($this->data['page'])) {
            // load view
            return Response::view('errors.404', array(), 404);
        }
        // get subpages
        $this->data['subpages'] = $this->menu->byPID($this->data['page']->id);
        // get parent subpages
        $this->data['parent_subpages'] = $this->menu->byPID($this->data['page']->pid);
        // get widgets
        //$this->data['widgets'] = $this->menu->widgets($this->data['page']->id);

        // generate meta description
        $this->data['page']->meta_description = $this->clib->metaGenerator($this->data['page']->description_ba, $this->data['page']->content_ba);
        // load view
        $data = $this->data;

        return View::make('frontend/pages/news-single-page', compact('data'));
    }
}
