| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Entries; |
| 4 |
|
| 5 |
use FluentForm\App; |
| 6 |
|
| 7 |
class EntryQuery |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Request object |
| 11 |
* |
| 12 |
* @var \FluentForm\Framework\Request\Request $request |
| 13 |
*/ |
| 14 |
protected $request; |
| 15 |
protected $formModel; |
| 16 |
protected $responseModel; |
| 17 |
|
| 18 |
protected $formId = false; |
| 19 |
|
| 20 |
protected $per_page = 10; |
| 21 |
protected $page_number = 1; |
| 22 |
protected $status = false; |
| 23 |
protected $is_favourite = null; |
| 24 |
protected $sort_by = 'ASC'; |
| 25 |
protected $search = false; |
| 26 |
protected $wheres = []; |
| 27 |
|
| 28 |
protected $startDate; |
| 29 |
protected $endDate; |
| 30 |
|
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
$this->request = App::make('request'); |
| 34 |
$this->formModel = wpFluent()->table('fluentform_forms'); |
| 35 |
$this->responseModel = wpFluent()->table('fluentform_submissions'); |
| 36 |
} |
| 37 |
|
| 38 |
public function getResponses() |
| 39 |
{ |
| 40 |
$query = $this->responseModel |
| 41 |
->where('form_id', $this->formId) |
| 42 |
->orderBy('id', $this->sort_by); |
| 43 |
|
| 44 |
if ($this->per_page > 0) { |
| 45 |
$query = $query->limit($this->per_page); |
| 46 |
} |
| 47 |
|
| 48 |
if ($this->page_number > 0) { |
| 49 |
$query = $query->offset(($this->page_number - 1) * $this->per_page); |
| 50 |
} |
| 51 |
|
| 52 |
if ($this->is_favourite) { |
| 53 |
$query->where('is_favourite', $this->is_favourite); |
| 54 |
$query->where('status', '!=', 'trashed'); |
| 55 |
} else { |
| 56 |
if (!$this->status) { |
| 57 |
$query->where('status', '!=', 'trashed'); |
| 58 |
} else { |
| 59 |
$query->where('status', $this->status); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if ($this->startDate && $this->endDate) { |
| 64 |
$endDate = $this->endDate; |
| 65 |
$endDate .= ' 23:59:59'; |
| 66 |
$query->where('created_at', '>=', $this->startDate); |
| 67 |
$query->where('created_at', '<=', $endDate); |
| 68 |
} |
| 69 |
|
| 70 |
if ($this->search) { |
| 71 |
$searchString = $this->search; |
| 72 |
$query->where(function ($q) use ($searchString) { |
| 73 |
$q->where('id', 'LIKE', "%{$searchString}%") |
| 74 |
->orWhere('response', 'LIKE', "%{$searchString}%") |
| 75 |
->orWhere('status', 'LIKE', "%{$searchString}%") |
| 76 |
->orWhere('created_at', 'LIKE', "%{$searchString}%"); |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
if ($this->wheres) { |
| 81 |
foreach ($this->wheres as $where) { |
| 82 |
if (is_array($where) && count($where) > 1) { |
| 83 |
if (count($where) > 2) { |
| 84 |
$column = $where[0]; |
| 85 |
$operator = $where[1]; |
| 86 |
$value = $where[2]; |
| 87 |
} else { |
| 88 |
$column = $where[0]; |
| 89 |
$operator = '='; |
| 90 |
$value = $where[1]; |
| 91 |
} |
| 92 |
if (is_array($value)) { |
| 93 |
$query->whereIn($column, $value); |
| 94 |
} else { |
| 95 |
$query->where($column, $operator, $value); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
$total = $query->count(); |
| 102 |
$responses = $query->get(); |
| 103 |
$responses = apply_filters('fluentform_get_raw_responses', $responses, $this->formId); |
| 104 |
|
| 105 |
return [ |
| 106 |
'data' => $responses, |
| 107 |
'paginate' => [ |
| 108 |
'total' => $total, |
| 109 |
'per_page' => $this->per_page, |
| 110 |
'current_page' => $this->page_number, |
| 111 |
'last_page' => ceil($total / $this->per_page), |
| 112 |
], |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
public function getResponse($entryId) |
| 117 |
{ |
| 118 |
return wpFluent()->table('fluentform_submissions')->find($entryId); |
| 119 |
} |
| 120 |
|
| 121 |
public function getNextResponse($entryId) |
| 122 |
{ |
| 123 |
$query = $this->getNextPrevEntryQuery(); |
| 124 |
|
| 125 |
$operator = 'ASC' == $this->sort_by ? '>' : '<'; |
| 126 |
|
| 127 |
return $query->select('id') |
| 128 |
->where('id', $operator, $entryId) |
| 129 |
->orderBy('id', $this->sort_by) |
| 130 |
->first(); |
| 131 |
} |
| 132 |
|
| 133 |
public function getPrevResponse($entryId) |
| 134 |
{ |
| 135 |
$query = $this->getNextPrevEntryQuery(); |
| 136 |
|
| 137 |
$operator = 'ASC' == $this->sort_by ? '<' : '>'; |
| 138 |
|
| 139 |
$orderBy = 'ASC' == $this->sort_by ? 'DESC' : 'ASC'; |
| 140 |
|
| 141 |
return $query->select('id') |
| 142 |
->where('id', $operator, $entryId) |
| 143 |
->orderBy('id', $orderBy) |
| 144 |
->first(); |
| 145 |
} |
| 146 |
|
| 147 |
protected function getNextPrevEntryQuery() |
| 148 |
{ |
| 149 |
$query = wpFluent()->table('fluentform_submissions')->limit(1); |
| 150 |
|
| 151 |
if ($this->is_favourite) { |
| 152 |
$query->where('is_favourite', $this->is_favourite)->where('status', '!=', 'trashed'); |
| 153 |
} else { |
| 154 |
if (!$this->status) { |
| 155 |
$query->where('status', '!=', 'trashed'); |
| 156 |
} else { |
| 157 |
$query->where('status', $this->status); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if ($this->search) { |
| 162 |
$query->where('response', 'LIKE', "%{$this->search}%"); |
| 163 |
} |
| 164 |
|
| 165 |
return $query->where('form_id', $this->formId); |
| 166 |
} |
| 167 |
|
| 168 |
public function groupCount($form_id) |
| 169 |
{ |
| 170 |
$statuses = $this->responseModel |
| 171 |
->select($this->responseModel->raw('status, COUNT(*) as count')) |
| 172 |
->where('form_id', $form_id) |
| 173 |
->groupBy('status') |
| 174 |
->get(); |
| 175 |
|
| 176 |
$counts = []; |
| 177 |
foreach ($statuses as $status) { |
| 178 |
$counts[$status->status] = $status->count; |
| 179 |
} |
| 180 |
|
| 181 |
$counts['all'] = array_sum($counts); |
| 182 |
if (isset($counts['trashed'])) { |
| 183 |
$counts['all'] -= $counts['trashed']; |
| 184 |
} |
| 185 |
|
| 186 |
$favorites = wpFluent() |
| 187 |
->table('fluentform_submissions') |
| 188 |
->where('form_id', $form_id) |
| 189 |
->where('is_favourite', 1) |
| 190 |
->where('status', '!=', 'trashed') |
| 191 |
->count(); |
| 192 |
|
| 193 |
$counts['favourites'] = $favorites; |
| 194 |
|
| 195 |
return $counts; |
| 196 |
} |
| 197 |
} |
| 198 |
|