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