| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Submission; |
| 4 |
|
| 5 |
|
| 6 |
use FluentForm\App\Helpers\Helper; |
| 7 |
use FluentForm\App\Models\Submission; |
| 8 |
use FluentForm\App\Services\FormBuilder\ShortCodeParser; |
| 9 |
use FluentForm\Framework\Helpers\ArrayHelper as Arr; |
| 10 |
|
| 11 |
|
| 12 |
class SubmissionPrint |
| 13 |
{ |
| 14 |
public function getContent($attr) |
| 15 |
{ |
| 16 |
$submissionIds = Arr::get($attr, 'submission_ids', []); |
| 17 |
$submissionIds = array_filter(array_map('intval', $submissionIds)); |
| 18 |
$orderBy = Helper::sanitizeOrderValue(Arr::get($attr, 'sort_by', 'DESC')); |
| 19 |
$formId = intval(Arr::get($attr, 'form_id')); |
| 20 |
$form = Helper::getForm($formId); |
| 21 |
$submissions = Submission::where('form_id', $formId) |
| 22 |
->whereIn('id', $submissionIds) |
| 23 |
->orderBy('id', $orderBy) |
| 24 |
->get(); |
| 25 |
|
| 26 |
if (!$form || count($submissionIds) === 0 || count($submissions) !== count($submissionIds)) { |
| 27 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception message, not output |
| 28 |
throw new \Exception(__('Invalid Submissions', 'fluentform')); |
| 29 |
} |
| 30 |
$pdfBody = $this->getBody($submissions, $form); |
| 31 |
$pdfCss = '<style>' . $this->getCss() . '</style>'; |
| 32 |
return fluentform_sanitize_html($pdfCss . $pdfBody); |
| 33 |
} |
| 34 |
|
| 35 |
protected function getBody($submissions, $form) |
| 36 |
{ |
| 37 |
$pdfBody = ""; |
| 38 |
foreach ($submissions as $index => $submission) { |
| 39 |
$formData = json_decode($submission->response, true); |
| 40 |
$htmlBody = ShortCodeParser::parse('{all_data}', $submission, $formData, $form, false, true); |
| 41 |
$htmlBody = '<h3>Submission - #' . $submission->id . '</h3>' . $htmlBody; |
| 42 |
if ($index !== 0 && apply_filters('fluentform/bulk_entries_print_start_on_new_page', __return_true(), |
| 43 |
$submission, $form)) { |
| 44 |
$htmlBody = '<div class="ff-new-entry-page-break"></div>' . $htmlBody; |
| 45 |
} |
| 46 |
if (apply_filters('fluentform/entry_print_with_notes', __return_true(), $form, $submission)) { |
| 47 |
$htmlBody = $this->addNotes($htmlBody, $submission, $form); |
| 48 |
} |
| 49 |
$pdfBody .= apply_filters('fluentform/entry_print_body', $htmlBody, $submission, $form, $formData); |
| 50 |
} |
| 51 |
return apply_filters('fluentform/entries_print_body', $pdfBody, $submissions, $form); |
| 52 |
} |
| 53 |
|
| 54 |
protected function addNotes($htmlBody, $submission, $form) |
| 55 |
{ |
| 56 |
$notes = (new SubmissionService())->getNotes($submission->id, ['form_id' => $form->id]); |
| 57 |
if ($notes && count($notes) > 0) { |
| 58 |
$notesHtml = '<br\><h3>Submission Notes</h3><table class="ff_all_data" width="600" cellpadding="0" cellspacing="0"><tbody>'; |
| 59 |
foreach ($notes as $note) { |
| 60 |
if (isset($note->created_by)) { |
| 61 |
$label = '' . $note->created_by . ' - ' . $note->created_at; |
| 62 |
} else { |
| 63 |
$label = '' . $note->name . ' - ' . $note->created_at; |
| 64 |
} |
| 65 |
$notesHtml .= '<tr class="field-label"><th style="padding: 6px 12px; background-color: #f8f8f8; text-align: left;"><strong>' . $label . '</strong></th></tr><tr class="field-value"><td style="padding: 6px 12px 12px 12px;">' . wp_kses_post($note->value) . '</td></tr>'; |
| 66 |
} |
| 67 |
$htmlBody = $htmlBody . $notesHtml . '</tbody></table>'; |
| 68 |
} |
| 69 |
return $htmlBody; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
protected function getCss() |
| 74 |
{ |
| 75 |
$mainColor = '#4F4F4F'; |
| 76 |
$fontSize = 14; |
| 77 |
$secondaryColor = '#EAEAEA'; |
| 78 |
ob_start(); |
| 79 |
?> |
| 80 |
.ff_pdf_wrapper, p, li, td, th { |
| 81 |
color: <?php |
| 82 |
echo esc_attr($mainColor); ?>; |
| 83 |
font-size: <?php |
| 84 |
echo esc_attr($fontSize); ?>px; |
| 85 |
} |
| 86 |
|
| 87 |
.ff_all_data, table { |
| 88 |
empty-cells: show; |
| 89 |
border-collapse: collapse; |
| 90 |
border: 1px solid <?php |
| 91 |
echo esc_attr($secondaryColor); ?>; |
| 92 |
width: 100%; |
| 93 |
color: <?php |
| 94 |
echo esc_attr($mainColor); ?>; |
| 95 |
} |
| 96 |
hr { |
| 97 |
color: <?php |
| 98 |
echo esc_attr($secondaryColor); ?>; |
| 99 |
background-color: <?php |
| 100 |
echo esc_attr($secondaryColor); ?>; |
| 101 |
} |
| 102 |
.ff_all_data th { |
| 103 |
border-bottom: 1px solid <?php |
| 104 |
echo esc_attr($secondaryColor); ?>; |
| 105 |
border-top: 1px solid <?php |
| 106 |
echo esc_attr($secondaryColor); ?>; |
| 107 |
padding-bottom: 10px !important; |
| 108 |
} |
| 109 |
.ff_all_data tr td { |
| 110 |
padding-left: 30px !important; |
| 111 |
padding-top: 15px !important; |
| 112 |
padding-bottom: 15px !important; |
| 113 |
} |
| 114 |
|
| 115 |
.ff_all_data tr td, .ff_all_data tr th { |
| 116 |
border: 1px solid <?php |
| 117 |
echo esc_attr($secondaryColor); ?>; |
| 118 |
text-align: left; |
| 119 |
} |
| 120 |
|
| 121 |
table, .ff_all_data {width: 100%; overflow:wrap;} img.alignright { float: right; margin: 0 0 1em 1em; } |
| 122 |
img.alignleft { float: left; margin: 0 10px 10px 0; } |
| 123 |
.center-image-wrapper {text-align:center;} |
| 124 |
.center-image-wrapper img.aligncenter {display: initial; margin: 0; text-align: center;} |
| 125 |
.alignright { float: right; } |
| 126 |
.alignleft { float: left; } |
| 127 |
.aligncenter { display: block; margin-left: auto; margin-right: auto; text-align: center; } |
| 128 |
|
| 129 |
.invoice_title { |
| 130 |
padding-bottom: 10px; |
| 131 |
display: block; |
| 132 |
} |
| 133 |
.ffp_table thead th { |
| 134 |
background-color: #e3e8ee; |
| 135 |
color: #000; |
| 136 |
text-align: left; |
| 137 |
vertical-align: bottom; |
| 138 |
} |
| 139 |
table th { |
| 140 |
padding: 5px 10px; |
| 141 |
} |
| 142 |
.ff_rtl table th, .ff_rtl table td { |
| 143 |
text-align: right !important; |
| 144 |
} |
| 145 |
/* Add CSS for page breaks */ |
| 146 |
@media print { .ff-new-entry-page-break { page-break-before: always; } } |
| 147 |
<?php |
| 148 |
return apply_filters('fluentform/entries_print_css', ob_get_clean()); |
| 149 |
} |
| 150 |
} |
| 151 |
|