<?php namespace App\Libs\Composers;

use DefaultTemplateConfiguration;

trait SidebarComposerTrait
{
    protected function composeSidebar($view, $query, $viewVarName = 'sidebarWidgets', $sidebarField = 'sidebarWidgets')
    {
        $data = $view->getData();
        $page = $data['object'];
        $sidebarWidgets = array();

        $templateData = $query->with(array(
            $sidebarField => function ($query) {
                $query->full()->with(array(
                    'contact' => function ($query) {
                        $query->with('image');
                    }, 'multilangContact' => function ($query) {
                        $query->with('image');
                    }, 'testimonial' => function ($query) {
                        $query->with('photo');
                    }, 'sidebarObject' => function ($query) {
                        $query->with(array('image', 'bgImage'));
                    }
                ))->orderBy('vposition', 'DESC')
                    ->orderBy('website_objects.position', 'ASC');
            }
        ))
            ->where('entry_id', $page->id)
            ->first();

        //var_dump(\DB::getQueryLog()); die;
        if (!empty($templateData)) {
            $sidebarWidgets = $templateData->$sidebarField;
            foreach ($sidebarWidgets as $obj) {
                if ($obj->type == 'latest') {
                    $latestType = $obj->latest_type;
                    switch ($latestType) {
                        case 'news':
                            $obj->latestItems = \News::full()
                                ->where('status', 'published')
                                ->where('type', 'news')
                                ->orderBy('date', 'DESC')
                                ->take($obj->item_count)
                                ->get();

                            break;
                        case 'blogs':
                            $obj->latestItems = \News::full()
                                ->where('status', 'published')
                                ->where('type', 'blog')
                                ->orderBy('date', 'DESC')
                                ->take($obj->item_count)
                                ->get();

                            break;
                        case 'events':
                            $today = date("Y-m-d");
                            $obj->latestItems = \EventItem::full()
                                ->where('status', 'published')
                                ->where("end_date", '>=', $today)
                                ->orderBy('date', 'asc')
                                ->take($obj->item_count)
                                ->get();
                            break;
                        case 'press':
                            $obj->latestItems = \PressRelease::full()
                                ->where('status', 'published')
                                ->orderBy('date', 'DESC')
                                ->take($obj->item_count)
                                ->get();
                            break;
                        case 'publications':
                            $obj->latestItems = \Publication::full()
                                ->where('status', 'published')
                                ->where('list_on_publications', 1)
                                ->orderBy('date', 'DESC')
                                ->take($obj->item_count)
                                ->get();
                            break;
                        case 'jobs':
                            $obj->latestItems = \JobOffer::full()
                                ->with(array('document', 'office'))
                                ->where('status', 'published')
                                ->orderBy('job_offers.created_at', 'DESC')
                                ->take($obj->item_count)
                                ->get();
                            break;
                    }
                }
            }
        }
        //var_dump($sidebarWidgets->toArray()); die;
        $view->with(array(
            $viewVarName => $sidebarWidgets
        ));
    }

    protected function composeObjectSidebar($view, $object, $fieldName = 'sidebarWidgets')
    {
        $sidebarWidgets = array();

        $sidebarWidgets = $object->$fieldName()->full()->with(array(
            'contact' => function ($query) {
                $query->with('image');
            }, 'multilangContact' => function ($query) {
                $query->with('image');
            }, 'testimonial' => function ($query) {
                $query->with('photo');
            }, 'sidebarObject' => function ($query) {
                $query->with(array('image', 'bgImage'));
            }
        ))->orderBy('vposition', 'DESC')->orderBy('website_objects.position', 'ASC')->get();

        foreach ($sidebarWidgets as $obj) {
            if ($obj->type == 'latest') {
                $latestType = $obj->latest_type;
                switch ($latestType) {
                    case 'news':
                        $obj->latestItems = \News::full()
                            ->with('document')
                            ->where('status', 'published')
                            ->where('type', 'news')
                            ->orderBy('date', 'DESC')
                            ->take($obj->item_count)
                            ->get();
                        break;
                    case 'events':
                        $today = date("Y-m-d");
                        $obj->latestItems = \EventItem::full()
                            ->with('document')
                            ->where('status', 'published')
                            ->where("end_date", '>=', $today)
                            ->orderBy('date', 'asc')
                            ->take($obj->item_count)
                            ->get();
                        break;
                    case 'press':
                        $obj->latestItems = \PressRelease::full()
                            ->with('document')
                            ->where('status', 'published')
                            ->orderBy('date', 'DESC')
                            ->take($obj->item_count)
                            ->get();
                        break;
                    case 'publications':
                        $obj->latestItems = \Publication::full()
                            ->where('status', 'published')
                            ->where('list_on_publications', 1)
                            ->orderBy('date', 'DESC')
                            ->take($obj->item_count)
                            ->get();
                        break;
                    case 'jobs':
                        $obj->latestItems = \JobOffer::full()
                            ->with(array('document', 'office'))
                            ->where('status', 'published')
                            ->orderBy('job_offers.created_at', 'DESC')
                            ->take($obj->item_count)
                            ->get();
                        break;
                }
            }
        }
        $view->with(array(
            'sidebarWidgets' => $sidebarWidgets
        ));
    }
}

?>
