| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Models; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentForm\Framework\Support\Arr; |
| 7 |
|
| 8 |
class Entry extends Model |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The table associated with the model. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $table = 'fluentform_submissions'; |
| 16 |
|
| 17 |
/** |
| 18 |
* A formMeta is owned by a form. |
| 19 |
* |
| 20 |
* @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo |
| 21 |
*/ |
| 22 |
public function form() |
| 23 |
{ |
| 24 |
return $this->belongsTo(Form::class, 'form_id', 'id'); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* An entry has many meta. |
| 29 |
* |
| 30 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 31 |
*/ |
| 32 |
public function entryMeta() |
| 33 |
{ |
| 34 |
return $this->hasMany(EntryMeta::class, 'response_id', 'id'); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* An entry has many logs. |
| 39 |
* |
| 40 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 41 |
*/ |
| 42 |
public function logs() |
| 43 |
{ |
| 44 |
return $this->hasMany(Log::class, 'source_id', 'id'); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* An entry has many entry details. |
| 49 |
* |
| 50 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 51 |
*/ |
| 52 |
public function entryDetails() |
| 53 |
{ |
| 54 |
return $this->hasMany(EntryDetails::class, 'submission_id', 'id'); |
| 55 |
} |
| 56 |
|
| 57 |
public function paginateEntries($attributes = []) |
| 58 |
{ |
| 59 |
$formId = Arr::get($attributes, 'form_id'); |
| 60 |
$isFavourite = Arr::get($attributes, 'is_favourite'); |
| 61 |
$status = Arr::get($attributes, 'status'); |
| 62 |
$startDate = Arr::get($attributes, 'start_date'); |
| 63 |
$endDate = Arr::get($attributes, 'end_date'); |
| 64 |
$search = Arr::get($attributes, 'search'); |
| 65 |
$wheres = Arr::get($attributes, 'wheres'); |
| 66 |
|
| 67 |
$query = $this->orderBy('id', $attributes['sort_by']) |
| 68 |
->when($formId, function ($q) use ($formId) { |
| 69 |
return $q->where('form_id', $formId); |
| 70 |
}) |
| 71 |
->when($isFavourite, function ($q) { |
| 72 |
return $q->where('is_favourite', true); |
| 73 |
}) |
| 74 |
->where(function ($q) use ($status) { |
| 75 |
$operator = '='; |
| 76 |
|
| 77 |
if (!$status) { |
| 78 |
$operator = '!='; |
| 79 |
$status = 'trashed'; |
| 80 |
} |
| 81 |
|
| 82 |
return $q->where('status', $operator, $status); |
| 83 |
}) |
| 84 |
->when($startDate && $endDate, function ($q) use ($startDate, $endDate) { |
| 85 |
$endDate .= ' 23:59:59'; |
| 86 |
|
| 87 |
return $q->where('created_at', '>=', $startDate) |
| 88 |
->where('created_at', '<=', $endDate); |
| 89 |
}) |
| 90 |
->when($search, function ($q) use ($search) { |
| 91 |
return $q->where(function ($q) use ($search) { |
| 92 |
return $q->where('id', 'LIKE', "%{$search}%") |
| 93 |
->orWhere('response', 'LIKE', "%{$search}%") |
| 94 |
->orWhere('status', 'LIKE', "%{$search}%") |
| 95 |
->orWhere('created_at', 'LIKE', "%{$search}%"); |
| 96 |
}); |
| 97 |
}) |
| 98 |
->when($wheres, function ($q) use ($wheres) { |
| 99 |
foreach ($wheres as $where) { |
| 100 |
if (is_array($where) && count($where) > 1) { |
| 101 |
if (count($where) > 2) { |
| 102 |
$column = $where[0]; |
| 103 |
$operator = $where[1]; |
| 104 |
$value = $where[2]; |
| 105 |
} else { |
| 106 |
$column = $where[0]; |
| 107 |
$operator = '='; |
| 108 |
$value = $where[1]; |
| 109 |
} |
| 110 |
|
| 111 |
if (is_array($value)) { |
| 112 |
return $q->whereIn($column, $value); |
| 113 |
} else { |
| 114 |
return $q->where($column, $operator, $value); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
}); |
| 119 |
|
| 120 |
$response = $query->paginate(); |
| 121 |
$response = apply_filters_deprecated( |
| 122 |
'fluentform_get_raw_responses', |
| 123 |
[ |
| 124 |
$response, |
| 125 |
$formId |
| 126 |
], |
| 127 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 128 |
'fluentform/get_raw_responses', |
| 129 |
'Use fluentform/get_raw_responses instead of fluentform_get_raw_responses.' |
| 130 |
); |
| 131 |
|
| 132 |
return apply_filters('fluentform/get_raw_responses', $response, $formId); |
| 133 |
} |
| 134 |
|
| 135 |
public function countByGroup($formId) |
| 136 |
{ |
| 137 |
$statuses = $this->selectRaw('status, COUNT(*) as count') |
| 138 |
->where('form_id', $formId) |
| 139 |
->groupBy('status') |
| 140 |
->get(); |
| 141 |
|
| 142 |
$counts = []; |
| 143 |
|
| 144 |
foreach ($statuses as $status) { |
| 145 |
$counts[$status->status] = (int) $status->count; |
| 146 |
} |
| 147 |
|
| 148 |
$counts['all'] = array_sum($counts); |
| 149 |
|
| 150 |
if (isset($counts['trashed'])) { |
| 151 |
$counts['all'] -= $counts['trashed']; |
| 152 |
} |
| 153 |
|
| 154 |
$favorites = $this->where('form_id', $formId) |
| 155 |
->where('is_favourite', 1) |
| 156 |
->where('status', '!=', 'trashed') |
| 157 |
->count(); |
| 158 |
|
| 159 |
$counts['favorites'] = $favorites; |
| 160 |
|
| 161 |
return array_merge([ |
| 162 |
'unread' => 0, |
| 163 |
'read' => 0, |
| 164 |
'trashed' => 0, |
| 165 |
], $counts); |
| 166 |
} |
| 167 |
|
| 168 |
public function amend($id, $data = []) |
| 169 |
{ |
| 170 |
$this->where('id', $id)->update($data); |
| 171 |
} |
| 172 |
|
| 173 |
public static function remove($entryIds) |
| 174 |
{ |
| 175 |
static::whereIn('id', $entryIds)->delete(); |
| 176 |
|
| 177 |
EntryMeta::whereIn('response_id', $entryIds)->delete(); |
| 178 |
|
| 179 |
Log::whereIn('source_id', $entryIds) |
| 180 |
->where('source_type', 'submission_item') |
| 181 |
->delete(); |
| 182 |
|
| 183 |
EntryDetails::whereIn('submission_id', $entryIds)->delete(); |
| 184 |
|
| 185 |
//delete the pro models this way for now |
| 186 |
// todo: handle these pro models deletion |
| 187 |
// todo: delete files |
| 188 |
try { |
| 189 |
if(defined('FLUENTFORMPRO_VERSION')) { |
| 190 |
wpFluent()->table('fluentform_order_items') |
| 191 |
->whereIn('submission_id', $entryIds) |
| 192 |
->delete(); |
| 193 |
|
| 194 |
wpFluent()->table('fluentform_transactions') |
| 195 |
->whereIn('submission_id', $entryIds) |
| 196 |
->delete(); |
| 197 |
|
| 198 |
wpFluent()->table('fluentform_subscriptions') |
| 199 |
->whereIn('submission_id', $entryIds) |
| 200 |
->delete(); |
| 201 |
} |
| 202 |
|
| 203 |
wpFluent()->table('ff_scheduled_actions') |
| 204 |
->whereIn('origin_id', $entryIds) |
| 205 |
->where('type', 'submission_action') |
| 206 |
->delete(); |
| 207 |
} catch (Exception $exception) { |
| 208 |
// ... |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|