Exemplo n.º 1
0
 /**
  * Save form data
  * @param array $formData
  * @return FormSubmission
  */
 public function submissionSaveData(array $formData)
 {
     // remove empty values
     $formData = array_filter($formData);
     // get array of files to upload
     $files = [];
     foreach ($formData as $field => $value) {
         if (Request::hasFile($field)) {
             $files[$field] = Request::file($field);
             unset($formData[$field]);
         }
     }
     // save form submission
     $form_submission = new FormSubmission();
     $form_submission->form_block_id = $this->_block->id;
     $form_submission->content = serialize($formData);
     $form_submission->sent = 0;
     $form_submission->from_page_id = PageBuilder::pageId();
     $form_submission->uploadFiles($files);
     $form_submission->save();
     return $form_submission;
 }
Exemplo n.º 2
0
 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;
     }
 }