コード例 #1
0
ファイル: Cms.php プロジェクト: vincium/bourg-la-reine
 public function run($app, $friendlyUrl)
 {
     // is site active
     if ($app->site()->getStatus() == 0) {
         $friendlyUrl = '/inactive';
     }
     // retrieve page
     $page = \Rebond\Cms\Page\Data::loadByUrl($friendlyUrl);
     // this should never occur
     if (!isset($page)) {
         Error::kill(Error::PAGE_NOT_FOUND, $friendlyUrl, __FILE__, __LINE__);
     }
     // redirection
     if ($page->getRedirect() != '' && $page->getRedirect() != $friendlyUrl) {
         $friendlyUrl = Nav::readCmsRequest($page->getRedirect());
         echo $this->run($app, $friendlyUrl);
         return;
     }
     // permission
     Auth::isAuthorized($app->user(), $page->getPermission(), true, $app->site()->getSignInUrl());
     $app->setUrl($friendlyUrl);
     // layout
     $layout = \Rebond\Cms\Layout\Data::loadById($page->getLayoutId());
     $tplLayout = new Template(Template::SITE, ['www']);
     // page components
     $options = [];
     $options['where'][] = 'gadget.status = 1';
     $options['order'][] = 'gadget.display_order';
     $gadgets = \Rebond\Cms\Gadget\Data::loadAllByPageId($page->getId(), $options);
     $cacheTime = $app->site()->getCacheTime();
     if ($gadgets) {
         $this->app = $app;
         $this->gadgets = [];
         foreach ($gadgets as $gadget) {
             if ($gadget->getComponent()->getId() == 0) {
                 Log::log(Error::COMPONENT_NOT_FOUND, 'title: ' . $gadget->getTitle() . ', componentId: ' . $gadget->getComponentId(), __FILE__, __LINE__);
                 continue;
             }
             $component = $gadget->getComponent();
             $appGadget = '\\Rebond\\App\\' . $component->getModule()->getTitle() . '\\Gadget';
             $appMethod = $component->getMethod();
             if (!method_exists($appGadget, $appMethod)) {
                 Log::log(Error::GADGET_NOT_FOUND, 'gadget: ' . $appGadget . ', method: ' . $appMethod, __FILE__, __LINE__);
                 continue;
             }
             if ($component->getStatus() !== StatusType::ACTIVE) {
                 continue;
             }
             if ($component->getModule()->getStatus() !== StatusType::ACTIVE) {
                 continue;
             }
             $name = $component->getModule()->getTitle() . '_' . $component->getMethod();
             // look for cache
             if ($cacheTime > 0) {
                 $html = Cache::getGadgetCache($name, $cacheTime);
                 if (isset($html)) {
                     $tplLayout->add('column' . $gadget->getCol(), $html);
                     continue;
                 }
             }
             $gadgetClass = $this->getGadget($appGadget);
             // get filter
             if ($component->getType() == ComponentType::SINGLE_ITEM || $component->getType() == ComponentType::FILTERED_LISTING) {
                 if ($gadget->getFilterId() == 0) {
                     Log::log(Error::EMPTY_FILTER, $name, __FILE__, __LINE__);
                     continue;
                 } else {
                     $render = $gadgetClass->{$appMethod}($gadget->getFilterId());
                 }
             } else {
                 if ($component->getType() == ComponentType::CUSTOM_LISTING) {
                     $render = $gadgetClass->{$appMethod}($gadget->getCustomFilter());
                 } else {
                     $render = $gadgetClass->{$appMethod}();
                 }
             }
             $tplLayout->add('column' . $gadget->getCol(), $render);
             // save cache
             if ($cacheTime > 0) {
                 Cache::saveGadgetCache($name, $cacheTime, $render);
             }
         }
     }
     // check for empty columns
     $columns = $layout->getColumns();
     $noComponent = '<div class="no-component">' . Lang::lang('noComponent') . '</div>';
     $emptyValue = $app->logLevel() == 0 ? $noComponent : '';
     for ($i = 1; $i <= $columns; $i++) {
         if ($tplLayout->varIsSet('column' . $i) === false) {
             $tplLayout->set('column' . $i, $emptyValue);
         }
     }
     // template
     $template = \Rebond\Cms\Template\Data::loadById($page->getTemplateId());
     $tplMaster = new Template(Template::SITE, ['www']);
     // navHeader
     $isMenuCached = false;
     if ($template->getMenu() != \Rebond\Cms\MenuType::NONE) {
         // Get cache
         if ($cacheTime > 0) {
             $navHeader = Cache::getCache('navHeader', $template->getMenu() . '_' . $template->getMenuLevel(), $cacheTime);
             if (isset($navHeader)) {
                 $isMenuCached = true;
             }
         }
         if (!$isMenuCached) {
             $navHeader = \Rebond\Cms\Page\Data::buildHeaderNav($template->getMenuLevel(), $template->getMenu());
             // Save cache
             if ($cacheTime > 0) {
                 Cache::saveCache('navHeader', $template->getMenu() . '_' . $template->getMenuLevel(), $cacheTime, $navHeader);
             }
         }
         $tpl = new Template(Template::SITE, ['www']);
         $tpl->set('title', $app->site()->getTitle());
         $tpl->set('nav', \Rebond\Cms\Page\Service::renderNav($navHeader, $page->getFullUrl()));
         $tplMaster->set('navHeader', $tpl->render('nav-header'));
     }
     // breacrumb
     $isMenuCached = false;
     if ($template->getInBreadcrumb()) {
         // Get cache
         if ($cacheTime > 0) {
             $breadcrumb = Cache::getCache('breadcrumb', $page->getId(), $cacheTime);
             if (isset($breadcrumb)) {
                 $isMenuCached = true;
             }
         }
         if (!$isMenuCached) {
             $breadcrumb = \Rebond\Cms\Page\Service::renderBreadcrumb($page->getId());
             // Save cache
             if ($cacheTime > 0) {
                 Cache::saveCache('breadcrumb', $page->getId(), $cacheTime, $breadcrumb);
             }
         }
         $tplMaster->set('breadcrumb', $breadcrumb);
     }
     // navSide
     $isMenuCached = false;
     if ($template->getSideNav() != \Rebond\Cms\SideNavType::NONE) {
         // Get cache
         if ($cacheTime > 0) {
             $navSide = Cache::getCache('navSide', $template->getSideNav() . '_' . $template->getSideNavLevel(), $cacheTime);
             if (isset($navSide)) {
                 $isMenuCached = true;
             }
         }
         if (!$isMenuCached) {
             switch ($template->getSideNav()) {
                 case \Rebond\Cms\SideNavType::PARENT_1:
                     $currentId = $page->getParentId();
                     break;
                 case \Rebond\Cms\SideNavType::CHILDREN:
                     $currentId = $page->getId();
                     break;
                 case \Rebond\Cms\SideNavType::HOME:
                     $currentId = 1;
                     break;
                 case \Rebond\Cms\SideNavType::PARENT_2:
                     $currentId = \Rebond\Cms\Page\Data::loadByParent($page->getParentId(), 1);
                     break;
                 case \Rebond\Cms\SideNavType::PARENT_3:
                     $currentId = \Rebond\Cms\Page\Data::loadByParent($page->getParentId(), 2);
                     break;
             }
             $navSide = \Rebond\Cms\Page\Data::buildSideNav($currentId, $template->getSideNavLevel());
             // Save cache
             if ($cacheTime > 0) {
                 Cache::saveCache('navSide', $template->getSideNav() . '_' . $template->getSideNavLevel(), $cacheTime, $navSide);
             }
         }
         $tpl = new Template(Template::SITE, ['www']);
         $tpl->set('nav', \Rebond\Cms\Page\Service::renderNav($navSide, $page->getFullUrl()));
         $tplMaster->set('navSide', $tpl->render('nav-side'));
     }
     // navFooter
     $isMenuCached = false;
     if ($template->getInFooter() != 0) {
         // Get cache
         if ($cacheTime > 0) {
             $navFooter = Cache::getCache('navFooter', $template->getFooterLevel(), $cacheTime);
             if (isset($navFooter)) {
                 $isMenuCached = true;
             }
         }
         if (!$isMenuCached) {
             $navFooter = \Rebond\Cms\Page\Data::buildFooterNav($template->getFooterLevel());
             // Save cache
             if ($cacheTime > 0) {
                 Cache::saveCache('navFooter', $template->getFooterLevel(), $cacheTime, $navFooter);
             }
         }
         $tpl = new Template(Template::SITE, ['www']);
         $tpl->set('nav', \Rebond\Cms\Page\Service::renderNav($navFooter, $page->getFullUrl()));
         $tplMaster->set('navFooter', $tpl->render('nav-footer'));
     }
     $tplMaster->set('layout', $tplLayout->render($layout->getFilename()));
     // master
     // head
     if ($app->site()->getKeywords() != '') {
         $tplMaster->addMeta('keywords', $app->site()->getKeywords());
     }
     if ($app->site()->getDescription() != '') {
         $tplMaster->addMeta('description', $app->site()->getDescription());
     }
     $tplMaster->set('title', $page->getTitle());
     $tplMaster->set('site', $app->site()->getTitle());
     $tplMaster->set('skin', $app->skin());
     $tplMaster->set('siteUrl', \Rebond\Config::getPath('siteUrl'));
     // bodyClass
     $tplMaster->set('bodyClass', $page->getClass());
     // footer
     $ga = \Rebond\Core\Site\Service::renderGoogleAnalytics($app->site()->getGoogleAnalytics());
     $tplMaster->set('footer', $ga);
     $tplMaster->addJs('//code.jquery.com/jquery-2.1.4.min.js');
     $tplMaster->addJs('/js/lang-' . $app->lang() . '.js');
     if ($app->site()->getJs() != '') {
         $jsList = explode(',', $app->site()->getJs());
         foreach ($jsList as $js) {
             $tplMaster->addJs($js);
         }
     }
     if ($page->getJs() != '') {
         $jsList = explode(',', $page->getJs());
         foreach ($jsList as $js) {
             $tplMaster->addJs($js);
         }
     }
     if ($app->env() == \Rebond\Config::ENV_PROD) {
         $tplMaster->addCss('/css/skin/' . $app->skin() . '/min.css');
         $tplMaster->addJs('/js/min.js');
     } else {
         $tplMaster->addCss('/css/normalize.css');
         $tplMaster->addCss('/css/skin/' . $app->skin() . '/rebond.css');
         $tplMaster->addCss('/css/skin/' . $app->skin() . '/custom.css');
         $tplMaster->addJs('/js/rebond.js');
         $tplMaster->addJs('/js/custom.js');
     }
     if ($app->site()->getCss() != '') {
         $cssList = explode(',', $app->site()->getCss());
         foreach ($cssList as $css) {
             $tplMaster->addCss($css);
         }
     }
     if ($page->getCss() != '') {
         $cssList = explode(',', $page->getCss());
         foreach ($cssList as $css) {
             $tplMaster->addCss($css);
         }
     }
     return $tplMaster->render($template->getFilename());
 }