| 1 |
<?php namespace FluentForm\App\Modules\Entries; |
| 2 |
use FluentForm\App; |
| 3 |
|
| 4 |
class EntryQuery { |
| 5 |
/** |
| 6 |
* @var \FluentForm\Framework\Request\Request $request |
| 7 |
*/ |
| 8 |
protected $request; |
| 9 |
protected $formModel; |
| 10 |
protected $responseModel; |
| 11 |
|
| 12 |
protected $formId = false; |
| 13 |
|
| 14 |
protected $per_page = 10; |
| 15 |
protected $page_number = 1; |
| 16 |
protected $status = false; |
| 17 |
protected $is_favourite = null; |
| 18 |
protected $sort_by = 'ASC'; |
| 19 |
protected $search = false; |
| 20 |
|
| 21 |
public function __construct() { |
| 22 |
$this->request = App::make( 'request' ); |
| 23 |
$this->formModel = wpFluent()->table( 'fluentform_forms' ); |
| 24 |
$this->responseModel = wpFluent()->table( 'fluentform_submissions' ); |
| 25 |
} |
| 26 |
|
| 27 |
public function getResponses() { |
| 28 |
$query = $this->responseModel |
| 29 |
->where('form_id', $this->formId) |
| 30 |
->limit($this->per_page) |
| 31 |
->orderBy('id', $this->sort_by) |
| 32 |
->offset(( $this->page_number - 1) * $this->per_page ); |
| 33 |
|
| 34 |
if($this->is_favourite) { |
| 35 |
$query->where('is_favourite', $this->is_favourite); |
| 36 |
$query->where('status', '!=', 'trashed'); |
| 37 |
} else { |
| 38 |
if(!$this->status) { |
| 39 |
$query->where('status', '!=', 'trashed'); |
| 40 |
} else { |
| 41 |
$query->where('status', $this->status); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if($this->search) { |
| 46 |
$query->where('response', 'LIKE', '%'.$this->search.'%'); |
| 47 |
} |
| 48 |
|
| 49 |
$total = $query->count(); |
| 50 |
$responses = $query->get(); |
| 51 |
$responses = apply_filters('fluentform_get_raw_responses', $responses, $this->formId); |
| 52 |
|
| 53 |
return [ |
| 54 |
'data' => $responses, |
| 55 |
'paginate' => [ |
| 56 |
'total' => $total, |
| 57 |
'per_page' => $this->per_page, |
| 58 |
'current_page' => $this->page_number, |
| 59 |
'last_page' => ceil($total / $this->per_page) |
| 60 |
] |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
public function getResponse($entryId, $operator = null, $currentSerialNo = null) { |
| 65 |
$queryString = 'SELECT @rownum:=@rownum + 1 AS myRowSerial, wp_fluentform_submissions.* '. |
| 66 |
'FROM wp_fluentform_submissions, (SELECT @rownum:=0) AS nothingButSetInitialValue '. |
| 67 |
'WHERE form_id='.$this->formId; |
| 68 |
|
| 69 |
if ($this->is_favourite) { |
| 70 |
$queryString .= ' AND is_favourite='.$this->is_favourite.' AND status!="trashed"'; |
| 71 |
} else { |
| 72 |
if (! $this->status) { |
| 73 |
$queryString .= ' AND status != "trashed"'; |
| 74 |
} else { |
| 75 |
$queryString .= ' AND status='.$this->status; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if ($this->search) { |
| 80 |
$queryString .= ' AND response LIKE %'.$this->search.'%'; |
| 81 |
} |
| 82 |
|
| 83 |
$queryString .= ' ORDER BY id '.$this->sort_by; |
| 84 |
|
| 85 |
$queryString = 'SELECT * FROM ('.$queryString.') filtered '; |
| 86 |
|
| 87 |
if ($operator) { |
| 88 |
$queryString .= 'WHERE filtered.myRowSerial = '.$currentSerialNo.$operator.'1'; |
| 89 |
} else { |
| 90 |
$queryString .= 'WHERE filtered.id='.$entryId; |
| 91 |
} |
| 92 |
|
| 93 |
return wpFluent()->query($queryString)->first(); |
| 94 |
} |
| 95 |
|
| 96 |
public function groupCount($form_id) { |
| 97 |
$statuses = $this->responseModel |
| 98 |
->select($this->responseModel->raw('status, COUNT(*) as count')) |
| 99 |
->where('form_id', $form_id) |
| 100 |
->groupBy('status') |
| 101 |
->get(); |
| 102 |
$counts = array(); |
| 103 |
foreach ($statuses as $status) { |
| 104 |
$counts[$status->status] = $status->count; |
| 105 |
} |
| 106 |
|
| 107 |
$counts['all'] = array_sum($counts); |
| 108 |
$favorites = wpFluent() |
| 109 |
->table('fluentform_submissions') |
| 110 |
->where('form_id', $form_id) |
| 111 |
->where('is_favourite', 1) |
| 112 |
->count(); |
| 113 |
$counts['favourites'] = $favorites; |
| 114 |
return $counts; |
| 115 |
} |
| 116 |
} |