/** * Display form view * @param string $content * @param array $options * @return \Illuminate\Contracts\View\View|string */ public function display($content, $options = []) { $formData = $this->_defaultData($content); $template = !empty($options['view']) ? $options['view'] : $formData->template; $templatePath = 'themes.' . PageBuilder::getData('theme') . '.blocks.forms.' . $template; return FormWrap::view($this->_block, $options, $templatePath, ['form_data' => $formData]); }
/** * Display repeater view * @param string $content * @param array $options * @return string */ public function display($content, $options = []) { $repeaterId = $content; $template = !empty($options['view']) ? $options['view'] : $this->_block->name; $repeatersViews = 'themes.' . PageBuilder::getData('theme') . '.blocks.repeaters.'; if (!empty($options['form'])) { return FormWrap::view($this->_block, $options, $repeatersViews . $template . '-form'); } if (View::exists($repeatersViews . $template)) { $renderedContent = ''; if ($repeaterBlocks = BlockRepeater::getRepeaterBlocks($this->_block->id)) { $random = !empty($options['random']) ? $options['random'] : false; $repeaterRows = PageBlockRepeaterData::loadRepeaterData($repeaterId, $options['version'], $random); // pagination if (!empty($options['per_page']) && !empty($repeaterRows)) { $pagination = new LengthAwarePaginator($repeaterRows, count($repeaterRows), $options['per_page'], Request::input('page', 1)); $pagination->setPath(Request::getPathInfo()); $paginationLinks = PaginatorRender::run($pagination); $repeaterRows = array_slice($repeaterRows, ($pagination->currentPage() - 1) * $options['per_page'], $options['per_page'], true); } else { $paginationLinks = ''; } if (!empty($repeaterRows)) { $i = 1; $isFirst = true; $isLast = false; $rows = count($repeaterRows); $cols = !empty($options['cols']) ? (int) $options['cols'] : 1; $column = !empty($options['column']) ? (int) $options['column'] : 1; foreach ($repeaterRows as $rowId => $row) { if ($i % $cols == $column % $cols) { $previousKey = PageBuilder::getCustomBlockDataKey(); PageBuilder::setCustomBlockDataKey('repeater' . $repeaterId . '.' . $rowId); foreach ($repeaterBlocks as $repeaterBlock) { if ($repeaterBlock->exists) { PageBuilder::setCustomBlockData($repeaterBlock->name, !empty($row[$repeaterBlock->id]) ? $row[$repeaterBlock->id] : '', null, false); } } if ($i + $cols - 1 >= $rows) { $isLast = true; } $renderedContent .= View::make($repeatersViews . $template, array('is_first' => $isFirst, 'is_last' => $isLast, 'count' => $i, 'total' => $rows, 'id' => $repeaterId, 'pagination' => $paginationLinks, 'links' => $paginationLinks))->render(); $isFirst = false; PageBuilder::setCustomBlockDataKey($previousKey); } $i++; } } } return $renderedContent; } else { return "Repeater view does not exist in theme"; } }
/** * Display video using info from youtube API * @param string $content * @param array $options * @return string */ public function display($content, $options = []) { if (!empty($content)) { $template = !empty($options['view']) ? $options['view'] : 'default'; $videoViews = 'themes.' . PageBuilder::getData('theme') . '.blocks.videos.'; if (!View::exists($videoViews . $template)) { return 'Video template not found'; } if (!($videoInfo = $this->_cache($content))) { return 'Video does not exist, it may have been removed from youtube'; } return View::make($videoViews . $template, ['video' => $videoInfo])->render(); } else { return ''; } }
/** * @param string $content * @param array $options * @return string */ public function display($content, $options = []) { if ($galleryData = $this->_defaultData($content)) { uasort($galleryData, [GalleryUploadHandler::class, 'order_items']); foreach ($galleryData as $image => $imageData) { $galleryData[$image]->file = '/uploads/system/gallery/' . $this->_block->name . $imageData->path . $image; } } $options['view'] = !empty($options['view']) ? $options['view'] : 'default'; $galleryViews = 'themes.' . PageBuilder::getData('theme') . '.blocks.gallery.'; if (empty($options['view']) && View::exists($galleryViews . $this->_block->name)) { return View::make($galleryViews . $this->_block->name, ['images' => $galleryData])->render(); } elseif (View::exists($galleryViews . $options['view'])) { return View::make($galleryViews . $options['view'], ['images' => $galleryData])->render(); } else { return 'Gallery template not found'; } }
/** * Display image, image can be cropped with croppa * @param string $content * @param array $options * @return string */ public function display($content, $options = []) { $imageData = $this->_defaultData($content); if (empty($imageData->file)) { return ''; } if (empty($imageData->title)) { if (empty($options['title'])) { $fileName = substr(strrchr($imageData->file, '/'), 1); $imageData->title = str_replace('_', ' ', preg_replace('/\\.[^.\\s]{3,4}$/', '', $fileName)); } else { $imageData->title = $options['title']; } } $imageData->extra_attrs = ''; $ignoreAttributes = ['height', 'width', 'group', 'view', 'title', 'croppaOptions', 'version']; foreach ($options as $option => $val) { if (!in_array($option, $ignoreAttributes)) { $imageData->extra_attrs .= $option . '="' . $val . '" '; } } $imageData->group = !empty($options['group']) ? $options['group'] : ''; $imageData->original = URL::to($imageData->file); $height = !empty($options['height']) ? $options['height'] : null; $width = !empty($options['width']) ? $options['width'] : null; if ($height || $width) { $croppaOptions = !empty($options['croppaOptions']) ? $options['croppaOptions'] : []; $imageData->file = str_replace(URL::to('/'), '', $imageData->file); $imageData->file = \Croppa::url($imageData->file, $width, $height, $croppaOptions); } else { $imageData->file = $imageData->original; } $template = !empty($options['view']) ? $options['view'] : 'default'; $imageViews = 'themes.' . PageBuilder::getData('theme') . '.blocks.images.'; if (View::exists($imageViews . $template)) { return View::make($imageViews . $template, array('image' => $imageData))->render(); } else { return 'Image template not found'; } }
/** * Display pages selected with view * Also reverse lookup option, find pages with the current page selected * @param string $content * @param array $options * @return string */ public function display($content, $options = []) { $pages = []; $page_ids = []; if (isset($options['reverse'])) { // get page_ids on which current page is selected in this block $currentPageId = PageBuilder::pageId(true); if ($currentPageId) { $same_blocks = PageBlock::where('block_id', '=', $this->_block->id)->get(); foreach ($same_blocks as $same_block) { $block_page_ids = @unserialize($same_block->content); if (!empty($block_page_ids)) { foreach ($block_page_ids as $k => $block_page_id) { $block_page_id = Path::unParsePageId($block_page_id); if ($currentPageId == $block_page_id) { $page_ids[] = $same_block->page_id; break; } } } } } } elseif (!empty($content)) { $page_ids = unserialize($content); } if (!empty($page_ids)) { foreach ($page_ids as $page_id) { $parsedPageId = Path::unParsePageId($page_id, false); $pages[$page_id] = new PageDetails($parsedPageId[0], !empty($parsedPageId[1]) ? $parsedPageId[1] : 0); } } $template = !empty($options['view']) ? $options['view'] : $this->_block->name; $selectPageViews = 'themes.' . PageBuilder::getData('theme') . '.blocks.selectpages.'; if (View::exists($selectPageViews . $template)) { return View::make($selectPageViews . $template, array('pages' => $pages))->render(); } else { return 'Select pages template not found'; } }
/** * @param string $viewPath * @param array $data * @return string */ protected static function _getRenderedView($viewPath, $data = []) { $viewPath = 'themes.' . PageBuilder::getData('theme') . '.menus.' . self::$_view . '.' . $viewPath; if (View::exists($viewPath)) { return View::make($viewPath, $data)->render(); } else { return 'View not found (' . $viewPath . ')'; } }
public static function sendFromFormData($templates, $formData, $subject, $to = null, $from = null) { // get email details to send to $emailDetails = ['subject' => $subject, 'to' => $to ?: config('coaster::site.email'), 'from' => $from ?: config('coaster::site.email'), 'userEmail' => null]; // split to addresses if (strpos($emailDetails['to'], ',') !== false) { $emailDetails['to'] = explode(',', $emailDetails['to']); } $emailCheck = Validator::make($emailDetails, ['from' => 'email|required']); $emailCheck->each('to.*', 'required|email'); if ($emailCheck->passes()) { // get templates $emailsViews = ['themes.' . PageBuilder::getData('theme') . '.emails.']; foreach ($templates as $template) { $emailsViews[] = $emailsViews[0] . $template . '.'; } $sendTemplate = null; $replyTemplate = null; foreach ($emailsViews as $emailsView) { if (!$sendTemplate && View::exists($emailsView . 'default')) { $sendTemplate = $emailsView . 'default'; } if (!$replyTemplate && View::exists($emailsView . 'reply')) { $replyTemplate = $emailsView . 'reply'; } } if (!$sendTemplate) { throw new CmsPageException('No default email template', 500); } $replyTemplate = $replyTemplate ?: $sendTemplate; // generate body $body = ''; foreach ($formData as $field => $value) { if (is_array($value)) { $value = implode(", ", $value); } if (strpos($value, "\r\n") !== false) { $value = "<br />" . str_replace("\r\n", "<br />", $value); } $body .= ucwords(str_replace('_', ' ', $field)) . ": {$value} <br />"; if (stristr($field, 'email') !== false) { $emailDetails['userEmail'] = $value; } } Mail::send($sendTemplate, ['body' => $body, 'formData' => $formData, 'form_data' => $formData], function (Message $message) use($emailDetails) { if ($emailDetails['userEmail']) { $message->replyTo($emailDetails['userEmail']); } $message->to($emailDetails['to']); $message->from($emailDetails['from']); $message->subject($emailDetails['subject']); }); if ($emailDetails['userEmail']) { Mail::send($replyTemplate, ['body' => $body, 'formData' => $formData, 'form_data' => $formData], function (Message $message) use($emailDetails) { $message->to($emailDetails['userEmail']); $message->from($emailDetails['from']); $message->subject($emailDetails['subject']); }); } return !Mail::failures(); } else { return false; } }
/** * @return \Symfony\Component\HttpFoundation\Response */ public function generatePage() { // update scheduled versions PageVersionSchedule::checkPageVersionIds(); $pageLoader = ['class' => PageLoader::class, 'args' => []]; $pageBuilder = ['class' => PageBuilderInstance::class, 'args' => []]; // try to load cms page for current request event(new InitializePageBuilder($pageLoader, $pageBuilder)); PageBuilder::setClass($pageBuilder['class'], $pageBuilder['args'], $pageLoader['class'], $pageLoader['args']); try { // check for forced redirects $redirect = PageRedirect::uriHasRedirect(); if (!empty($redirect) && $redirect->force == 1) { throw new CmsPageException('forced redirect', 0, null, redirect($redirect->to, $redirect->type)); } // check for unforced redirects if (PageBuilder::getData('is404') && !empty($redirect)) { throw new CmsPageException('redirect', 0, null, redirect($redirect->to, $redirect->type)); } // 404, no cms page for current request if (PageBuilder::getData('is404')) { throw new Exception('cms page not found', 404); } // 404, hidden page if (!PageBuilder::getData('previewVersion') && !PageBuilder::getData('isLive')) { throw new Exception('cms page not live', 404); } // check for form submissions if (!empty($_POST)) { $formData = PageBuilder::getData('externalTemplate') ? Request::input(config('coaster::frontend.external_form_input')) : Request::all(); if (!empty($formData['block_id']) && empty($formData['coaster_check'])) { // honeypot option if (!($block = Block::find($formData['block_id']))) { throw new Exception('no block handler for this form data', 500); } else { $pageId = !empty($formData['page_id']) ? $formData['page_id'] : 0; unset($formData['_token']); unset($formData['block_id']); unset($formData['page_id']); unset($formData['coaster_check']); if ($formSubmitResponse = $block->setPageId($pageId)->setVersionId(PageBuilder::pageLiveVersionId())->getTypeObject()->submission($formData)) { throw new CmsPageException('form submission response', 0, null, $formSubmitResponse); } } } } // load page with template $templatePath = PageBuilder::templatePath(); event(new LoadPageTemplate($templatePath)); if (View::exists($templatePath)) { $this->_setHeader('Content-Type', PageBuilder::getData('contentType')); $this->responseContent = View::make($templatePath)->render(); } else { throw new Exception('cms page found with non existent template - ' . $templatePath, 500); } // if declared as a search page, must have search block if (Search::searchBlockRequired() && !Search::searchBlockExists()) { throw new Exception('cms page found without search function', 404); } } catch (CmsPageException $e) { $this->responseContent = $e->getAlternateResponse(); } catch (Exception $e) { $this->_setErrorContent($e); } $response = $this->_createResponse(); event(new LoadedPageResponse($response)); // if response content is html, run modifications if (!empty($response->headers->get('content-type')) && stripos($response->headers->get('content-type'), 'html') !== false) { $domDocument = new DOMDocument(); $domDocument->loadHTML($response->getContent()); $domDocument->addMetaTag('generator', 'Coaster CMS ' . config('coaster::site.version')); if (config('coaster::frontend.strong_tags') == 1) { $keywords = explode(", ", str_replace(" and ", ", ", PageBuilder::block('meta_keywords'))); $domDocument->addStrongTags($keywords); } // save page content if (PageBuilder::getData('externalTemplate')) { $domDocument->appendInputFieldNames(config('coaster::frontend.external_form_input')); $response->setContent($this->responseContent = $domDocument->saveBodyHMTL()); } else { $response->setContent($domDocument->saveHTML($domDocument)); } } return $response; }
public static function processFiles(Theme $theme, $overwriteFile = true) { if (!empty($theme)) { $themePath = base_path('resources/views/themes/' . $theme->theme . '/templates'); PageBuilder::setClass(ThemeBuilderInstance::class, [$overwriteFile], PageLoaderDummy::class, [$theme->theme]); if (is_dir($themePath)) { foreach (scandir($themePath) as $templateFile) { if (($templateName = explode('.', $templateFile)[0]) && !is_dir($themePath . '/' . $templateFile)) { PageBuilder::setData('template', $templateName); View::make('themes.' . $theme->theme . '.templates.' . $templateName)->render(); } } if ($errors = PageBuilder::getData('errors')) { echo 'Could not complete, errors found in theme:'; dd($errors); } self::$_selectBlocks = PageBuilder::getData('selectBlocks'); self::$_formRules = PageBuilder::getData('formRules'); self::$_repeaterBlocks = PageBuilder::getData('repeaterBlocks'); self::$_blockSettings = PageBuilder::getData('blockSettings'); self::$_fileTemplateBlocks = PageBuilder::getData('templateBlocks'); self::$_coreTemplates = PageBuilder::getData('coreTemplates'); self::_processDatabaseBlocks($theme->id); self::_processFileBlocks(); } } if (empty(self::$_fileBlocks)) { throw new \Exception('no blocks found, theme or templates may not exist'); } }