public function postIndex()
 {
     $block = Block::find(Request::input('block_id'));
     if (($repeaterId = Request::input('repeater_id')) && $block && $block->type == 'repeater') {
         return $block->setPageId(Request::input('page_id'))->getTypeObject()->edit($repeaterId, true);
     }
     return 0;
 }
 /**
  * @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 function getCsv($pageId = 0, $blockId = 0)
 {
     $block_data = Block::getBlockOnPage($blockId, $pageId);
     if (empty($block_data) || $block_data->type != 'form') {
         \abort('404', 'Form not found on page');
     } else {
         $csv = array();
         $columns = array();
         $column = 2;
         $row = 1;
         $submissions = FormSubmission::where('form_block_id', '=', $blockId)->orderBy('id', 'desc')->get();
         if (!$submissions->isEmpty()) {
             foreach ($submissions as $submission) {
                 $csv[$row] = array();
                 $csv[$row][0] = $submission->created_at;
                 $csv[$row][1] = !empty($submission->from_page_id) ? Path::getFullName($submission->from_page_id) : '-';
                 $form_data = @unserialize($submission->content);
                 if (!empty($form_data)) {
                     foreach ($form_data as $k => $v) {
                         if (!isset($columns[$k])) {
                             $columns[$k] = $column;
                             $column++;
                         }
                         if (is_array($v)) {
                             $v = implode(", ", $v);
                         }
                         $csv[$row][$columns[$k]] = $v;
                     }
                 } else {
                     preg_match_all('/\\"(.*?)\\";s:\\d*:\\"(.*?)\\";/si', $submission->content, $matches);
                     foreach ($matches[1] as $k => $field_key) {
                         if (!isset($columns[$field_key])) {
                             $columns[$field_key] = $column;
                             $column++;
                         }
                         $csv[$row][$columns[$field_key]] = $matches[2][$k];
                     }
                 }
                 $row++;
             }
             // add row titles
             $csv[0][0] = 'Date/Time';
             $csv[0][1] = 'Page';
             foreach ($columns as $name => $col) {
                 $csv[0][$col] = ucwords($name);
             }
             $numb_columns = count($columns);
             foreach ($csv as $row_id => $csv_row) {
                 for ($i = 0; $i < $numb_columns; $i++) {
                     if (!isset($csv_row[$i])) {
                         $csv[$row_id][$i] = '';
                     }
                 }
                 ksort($csv[$row_id]);
             }
             ksort($csv);
             $block_data = Block::find($blockId);
             header("Content-type: text/csv");
             header("Content-Disposition: attachment; filename=" . $block_data->name . ".csv");
             header("Pragma: no-cache");
             header("Expires: 0");
             $output = fopen("php://output", "w");
             foreach ($csv as $csv_row) {
                 fputcsv($output, $csv_row);
                 // here you can change delimiter/enclosure
             }
             fclose($output);
         }
         exit;
     }
 }