| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Entries; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* @deprecated Use SubmissionService or direct wpFluent() queries instead. |
| 9 |
* @todo Remove in next major version. |
| 10 |
*/ |
| 11 |
class EntryQuery |
| 12 |
{ |
| 13 |
protected $request; |
| 14 |
protected $formModel; |
| 15 |
protected $responseModel; |
| 16 |
|
| 17 |
protected $formId = false; |
| 18 |
protected $per_page = 10; |
| 19 |
protected $page_number = 1; |
| 20 |
protected $status = false; |
| 21 |
protected $is_favourite = null; |
| 22 |
protected $sort_by = 'ASC'; |
| 23 |
protected $search = false; |
| 24 |
protected $wheres = []; |
| 25 |
protected $startDate; |
| 26 |
protected $endDate; |
| 27 |
|
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
_deprecated_function(__CLASS__, '6.2.0', 'FluentForm\App\Models\Submission model or wpFluent() queries'); |
| 31 |
$this->request = wpFluentForm('request'); |
| 32 |
$this->formModel = wpFluent()->table('fluentform_forms'); |
| 33 |
$this->responseModel = wpFluent()->table('fluentform_submissions'); |
| 34 |
} |
| 35 |
|
| 36 |
public function getResponses() |
| 37 |
{ |
| 38 |
$query = $this->responseModel |
| 39 |
->where('form_id', $this->formId) |
| 40 |
->orderBy('id', Helper::sanitizeOrderValue($this->sort_by)); |
| 41 |
|
| 42 |
if ($this->per_page > 0) { |
| 43 |
$query = $query->limit($this->per_page); |
| 44 |
} |
| 45 |
|
| 46 |
if ($this->page_number > 0) { |
| 47 |
$query = $query->offset(($this->page_number - 1) * $this->per_page); |
| 48 |
} |
| 49 |
|
| 50 |
if ($this->is_favourite) { |
| 51 |
$query->where('is_favourite', $this->is_favourite); |
| 52 |
$query->where('status', '!=', 'trashed'); |
| 53 |
} else { |
| 54 |
if (!$this->status) { |
| 55 |
$query->where('status', '!=', 'trashed'); |
| 56 |
} else { |
| 57 |
$query->where('status', $this->status); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if ($this->startDate && $this->endDate) { |
| 62 |
$endDate = $this->endDate . ' 23:59:59'; |
| 63 |
$query->where('created_at', '>=', $this->startDate); |
| 64 |
$query->where('created_at', '<=', $endDate); |
| 65 |
} |
| 66 |
|
| 67 |
if ($this->search) { |
| 68 |
$searchString = $this->search; |
| 69 |
$query->where(function ($q) use ($searchString) { |
| 70 |
$q->where('id', 'LIKE', "%{$searchString}%") |
| 71 |
->orWhere('response', 'LIKE', "%{$searchString}%") |
| 72 |
->orWhere('status', 'LIKE', "%{$searchString}%") |
| 73 |
->orWhere('created_at', 'LIKE', "%{$searchString}%"); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
if ($this->wheres) { |
| 78 |
foreach ($this->wheres as $where) { |
| 79 |
if (is_array($where) && count($where) > 1) { |
| 80 |
if (count($where) > 2) { |
| 81 |
$column = $where[0]; |
| 82 |
$operator = $where[1]; |
| 83 |
$value = $where[2]; |
| 84 |
} else { |
| 85 |
$column = $where[0]; |
| 86 |
$operator = '='; |
| 87 |
$value = $where[1]; |
| 88 |
} |
| 89 |
if (is_array($value)) { |
| 90 |
$query->whereIn($column, $value); |
| 91 |
} else { |
| 92 |
$query->where($column, $operator, $value); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
$total = $query->count(); |
| 99 |
$responses = $query->get(); |
| 100 |
|
| 101 |
$responses = apply_filters('fluentform/get_raw_responses', $responses, $this->formId); |
| 102 |
|
| 103 |
return [ |
| 104 |
'data' => $responses, |
| 105 |
'paginate' => [ |
| 106 |
'total' => $total, |
| 107 |
'per_page' => $this->per_page, |
| 108 |
'current_page' => $this->page_number, |
| 109 |
'last_page' => ceil($total / $this->per_page), |
| 110 |
], |
| 111 |
]; |
| 112 |
} |
| 113 |
} |
| 114 |
|