| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Api; |
| 4 |
|
| 5 |
defined('ABSPATH') or die; |
| 6 |
|
| 7 |
use FluentForm\App\Modules\Form\FormDataParser; |
| 8 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 9 |
use FluentForm\App\Services\Report\ReportHelper; |
| 10 |
|
| 11 |
class Entry |
| 12 |
{ |
| 13 |
private $form; |
| 14 |
|
| 15 |
public function __construct($form) |
| 16 |
{ |
| 17 |
$this->form = $form; |
| 18 |
} |
| 19 |
|
| 20 |
public function entries($atts = [], $includeFormats = false) |
| 21 |
{ |
| 22 |
if ($includeFormats) { |
| 23 |
if (!defined('FLUENTFORM_RENDERING_ENTRIES')) { |
| 24 |
define('FLUENTFORM_RENDERING_ENTRIES', true); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
$atts = wp_parse_args($atts, [ |
| 29 |
'per_page' => 10, |
| 30 |
'page' => 1, |
| 31 |
'search' => '', |
| 32 |
'sort_type' => 'DESC', |
| 33 |
'entry_type' => 'all', |
| 34 |
]); |
| 35 |
|
| 36 |
$offset = $atts['per_page'] * ($atts['page'] - 1); |
| 37 |
|
| 38 |
$entryQuery = \FluentForm\App\Models\Submission::where('form_id', $this->form->id) |
| 39 |
->orderBy('id', \FluentForm\App\Helpers\Helper::sanitizeOrderValue($atts['sort_type'])) |
| 40 |
->limit($atts['per_page']) |
| 41 |
->offset($offset); |
| 42 |
|
| 43 |
$type = $atts['entry_type']; |
| 44 |
|
| 45 |
if ($type && 'all' != $type) { |
| 46 |
$entryQuery->where('status', $type); |
| 47 |
} |
| 48 |
|
| 49 |
if ($searchString = sanitize_text_field($atts['search'])) { |
| 50 |
$entryQuery->where(function ($q) use ($searchString) { |
| 51 |
$q->where('id', 'LIKE', "%{$searchString}%") |
| 52 |
->orWhere('response', 'LIKE', "%{$searchString}%") |
| 53 |
->orWhere('status', 'LIKE', "%{$searchString}%") |
| 54 |
->orWhere('created_at', 'LIKE', "%{$searchString}%"); |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
$count = $entryQuery->count(); |
| 59 |
|
| 60 |
$data = $entryQuery->get(); |
| 61 |
|
| 62 |
$dataCount = count($data); |
| 63 |
|
| 64 |
$from = $dataCount > 0 ? ($atts['page'] - 1) * $atts['per_page'] + 1 : null; |
| 65 |
|
| 66 |
$to = $dataCount > 0 ? $from + $dataCount - 1 : null; |
| 67 |
$lastPage = (int) ceil($count / $atts['per_page']); |
| 68 |
|
| 69 |
if ($includeFormats) { |
| 70 |
$data = FormDataParser::parseFormEntries($data, $this->form); |
| 71 |
} |
| 72 |
|
| 73 |
foreach ($data as $datum) { |
| 74 |
$datum->response = json_decode($datum->response, true); |
| 75 |
} |
| 76 |
|
| 77 |
return [ |
| 78 |
'current_page' => $atts['page'], |
| 79 |
'per_page' => $atts['per_page'], |
| 80 |
'from' => $from, |
| 81 |
'to' => $to, |
| 82 |
'last_page' => $lastPage, |
| 83 |
'total' => $count, |
| 84 |
'data' => $data, |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
public function entry($entryId, $includeFormats = false) |
| 89 |
{ |
| 90 |
$submission = \FluentForm\App\Models\Submission::where('form_id', $this->form->id) |
| 91 |
->where('id', $entryId) |
| 92 |
->first(); |
| 93 |
return $this->getFormattedEntry($submission,$includeFormats); |
| 94 |
} |
| 95 |
|
| 96 |
public function entryBySerial($serialNumber, $includeFormats = false){ |
| 97 |
|
| 98 |
$submission = \FluentForm\App\Models\Submission::where('form_id', $this->form->id) |
| 99 |
->where('serial_number', $serialNumber) |
| 100 |
->first(); |
| 101 |
|
| 102 |
return $this->getFormattedEntry($submission,$includeFormats); |
| 103 |
} |
| 104 |
|
| 105 |
public function getFormattedEntry($submission,$includeFormats = false){ |
| 106 |
if (!$submission) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
$inputs = FormFieldsParser::getEntryInputs($this->form); |
| 111 |
$submission = FormDataParser::parseFormEntry($submission, $this->form, $inputs, true); |
| 112 |
|
| 113 |
if (!$includeFormats) { |
| 114 |
$submission->response = json_decode($submission->response, true); |
| 115 |
return [ |
| 116 |
'submission' => $submission, |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
if ($submission->user_id) { |
| 121 |
$user = get_user_by('ID', $submission->user_id); |
| 122 |
$user_data = [ |
| 123 |
'name' => $user->display_name, |
| 124 |
'email' => $user->user_email, |
| 125 |
'ID' => $user->ID, |
| 126 |
'permalink' => get_edit_user_link($user->ID), |
| 127 |
]; |
| 128 |
$submission->user = $user_data; |
| 129 |
} |
| 130 |
|
| 131 |
$submission= apply_filters_deprecated( |
| 132 |
'fluentform_single_response_data', |
| 133 |
[ |
| 134 |
$submission, |
| 135 |
$this->form->id |
| 136 |
], |
| 137 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 138 |
'fluentform/find_submission', |
| 139 |
'Use fluentform/find_submission instead of fluentform_single_response_data.' |
| 140 |
); |
| 141 |
|
| 142 |
$submission = apply_filters('fluentform/find_submission', $submission, $this->form->id); |
| 143 |
|
| 144 |
$submission->response = json_decode($submission->response); |
| 145 |
|
| 146 |
$inputLabels = FormFieldsParser::getAdminLabels($this->form, $inputs); |
| 147 |
|
| 148 |
return [ |
| 149 |
'submission' => $submission, |
| 150 |
'labels' => $inputLabels, |
| 151 |
]; |
| 152 |
} |
| 153 |
|
| 154 |
public function report($statuses = ['read', 'unread', 'unapproved', 'approved', 'declined', 'unconfirmed', 'confirmed']) |
| 155 |
{ |
| 156 |
return ReportHelper::generateReport($this->form, $statuses); |
| 157 |
} |
| 158 |
} |
| 159 |
|