<?php

/**
 *
 * PHP Pro Bid $Id$ 7ZGJ1ZRfgdRPhcxHtQnmjNAxhWqcFkF/MgJoYQWbLRs=
 *
 * @link        http://www.phpprobid.com
 * @copyright   Copyright (c) 2017 Online Ventures Software & CodeCube SRL
 * @license     http://www.phpprobid.com/license Commercial License
 *
 * @version     7.9 [rev.7.9.01]
 */
/**
 * content sections controller
 */

namespace App\Controller;

use Cube\Controller\Front,
    Ppb\Controller\Action\AbstractAction,
    Ppb\Service;

class Sections extends AbstractAction
{

    public function View()
    {
        $id = (int)$this->getRequest()->getParam('id');

        $contentPagesService = new Service\ContentPages();
        $contentSectionsService = new Service\Table\Relational\ContentSections();

        $select = $contentPagesService->getTable()->select()
            ->order(array('order_id ASC', 'title ASC'));

        $leftSideContainer = null;

        $container = $contentSectionsService->getData()->findOneBy('id', $id);

        if ($container !== null) {
            $metaTitle = ($container->get('meta_title')) ?
                $container->get('meta_title') : implode(' > ', $contentSectionsService->getBreadcrumbs($id));
            $metaDescription = $container->get('meta_description');

            $root = $contentSectionsService->getRoot($id);
            $leftSideContainer = $contentSectionsService->getData()->findOneBy('id', $root['id']);

            $bootstrap = Front::getInstance()->getBootstrap();
            $locale = $bootstrap->getResource('locale')->getLocale();

            // META TAGS
            $view = $bootstrap->getResource('view');
            $view->headTitle()->prepend(strip_tags($metaTitle));
            if ($metaDescription) {
                $view->headMeta()->setName('description', strip_tags($metaDescription));
            }

            $select->where('section_id = ?', $id)
                ->where('language = ?', $locale);
        }
        else {
            $this->_helper->redirector()->redirect('not-found', 'error', null, array());
        }

        return array(
            'container'         => $container,
            'leftSideContainer' => $leftSideContainer,
            'pages'             => $contentPagesService->fetchAll($select),
        );
    }
}

