| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Models; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 7 |
use FluentForm\Framework\Support\Arr; |
| 8 |
|
| 9 |
/** |
| 10 |
* Legacy duplicate of the Submission model (deprecated code). |
| 11 |
* |
| 12 |
* @deprecated Dead pair with EntryMeta: they reference only each other (a circular |
| 13 |
* relation that is never invoked) plus Form. No external code in free or |
| 14 |
* Pro uses either class, and remove() has no callers. The live "Entry" |
| 15 |
* API is \FluentForm\App\Api\Entry; submissions go through Submission. |
| 16 |
* TODO: delete Entry AND EntryMeta together; remove() kept fail-closed meanwhile. |
| 17 |
*/ |
| 18 |
class Entry extends Model |
| 19 |
{ |
| 20 |
/** |
| 21 |
* The table associated with the model. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $table = 'fluentform_submissions'; |
| 26 |
|
| 27 |
/** |
| 28 |
* A formMeta is owned by a form. |
| 29 |
* |
| 30 |
* @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo |
| 31 |
*/ |
| 32 |
public function form() |
| 33 |
{ |
| 34 |
return $this->belongsTo(Form::class, 'form_id', 'id'); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* An entry has many meta. |
| 39 |
* |
| 40 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 41 |
*/ |
| 42 |
public function entryMeta() |
| 43 |
{ |
| 44 |
return $this->hasMany(EntryMeta::class, 'response_id', 'id'); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* An entry has many logs. |
| 49 |
* |
| 50 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 51 |
*/ |
| 52 |
public function logs() |
| 53 |
{ |
| 54 |
return $this->hasMany(Log::class, 'source_id', 'id'); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* An entry has many entry details. |
| 59 |
* |
| 60 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 61 |
*/ |
| 62 |
public function entryDetails() |
| 63 |
{ |
| 64 |
return $this->hasMany(EntryDetails::class, 'submission_id', 'id'); |
| 65 |
} |
| 66 |
|
| 67 |
public function paginateEntries($attributes = []) |
| 68 |
{ |
| 69 |
$formId = Arr::get($attributes, 'form_id'); |
| 70 |
$isFavourite = Arr::get($attributes, 'is_favourite'); |
| 71 |
$status = Arr::get($attributes, 'status'); |
| 72 |
$startDate = Arr::get($attributes, 'start_date'); |
| 73 |
$endDate = Arr::get($attributes, 'end_date'); |
| 74 |
$search = Arr::get($attributes, 'search'); |
| 75 |
$wheres = Arr::get($attributes, 'wheres'); |
| 76 |
|
| 77 |
$sortBy = \FluentForm\App\Helpers\Helper::sanitizeOrderValue(Arr::get($attributes, 'sort_by', 'DESC')); |
| 78 |
$query = $this->orderBy('id', $sortBy) |
| 79 |
->when($formId, function ($q) use ($formId) { |
| 80 |
return $q->where('form_id', $formId); |
| 81 |
}) |
| 82 |
->when($isFavourite, function ($q) { |
| 83 |
return $q->where('is_favourite', true); |
| 84 |
}) |
| 85 |
->where(function ($q) use ($status) { |
| 86 |
$operator = '='; |
| 87 |
|
| 88 |
if (!$status) { |
| 89 |
$operator = '!='; |
| 90 |
$status = 'trashed'; |
| 91 |
} |
| 92 |
|
| 93 |
return $q->where('status', $operator, $status); |
| 94 |
}) |
| 95 |
->when($startDate && $endDate, function ($q) use ($startDate, $endDate) { |
| 96 |
$endDate .= ' 23:59:59'; |
| 97 |
|
| 98 |
return $q->where('created_at', '>=', $startDate) |
| 99 |
->where('created_at', '<=', $endDate); |
| 100 |
}) |
| 101 |
->when($search, function ($q) use ($search) { |
| 102 |
global $wpdb; |
| 103 |
$escaped = $wpdb->esc_like($search); |
| 104 |
return $q->where(function ($q) use ($escaped) { |
| 105 |
return $q->where('id', 'LIKE', "%{$escaped}%") |
| 106 |
->orWhere('response', 'LIKE', "%{$escaped}%") |
| 107 |
->orWhere('status', 'LIKE', "%{$escaped}%") |
| 108 |
->orWhere('created_at', 'LIKE', "%{$escaped}%"); |
| 109 |
}); |
| 110 |
}) |
| 111 |
->when($wheres, function ($q) use ($wheres) { |
| 112 |
foreach ($wheres as $where) { |
| 113 |
if (is_array($where) && count($where) > 1) { |
| 114 |
if (count($where) > 2) { |
| 115 |
$column = $where[0]; |
| 116 |
$operator = $where[1]; |
| 117 |
$value = $where[2]; |
| 118 |
} else { |
| 119 |
$column = $where[0]; |
| 120 |
$operator = '='; |
| 121 |
$value = $where[1]; |
| 122 |
} |
| 123 |
|
| 124 |
if (is_array($value)) { |
| 125 |
return $q->whereIn($column, $value); |
| 126 |
} else { |
| 127 |
return $q->where($column, $operator, $value); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
$response = $query->paginate(); |
| 134 |
$response = apply_filters_deprecated( |
| 135 |
'fluentform_get_raw_responses', |
| 136 |
[ |
| 137 |
$response, |
| 138 |
$formId |
| 139 |
], |
| 140 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 141 |
'fluentform/get_raw_responses', |
| 142 |
'Use fluentform/get_raw_responses instead of fluentform_get_raw_responses.' |
| 143 |
); |
| 144 |
|
| 145 |
return apply_filters('fluentform/get_raw_responses', $response, $formId); |
| 146 |
} |
| 147 |
|
| 148 |
public function countByGroup($formId) |
| 149 |
{ |
| 150 |
$statuses = $this->selectRaw('status, COUNT(*) as count') |
| 151 |
->where('form_id', $formId) |
| 152 |
->groupBy('status') |
| 153 |
->get(); |
| 154 |
|
| 155 |
$counts = []; |
| 156 |
|
| 157 |
foreach ($statuses as $status) { |
| 158 |
$counts[$status->status] = (int) $status->count; |
| 159 |
} |
| 160 |
|
| 161 |
$counts['all'] = array_sum($counts); |
| 162 |
|
| 163 |
if (isset($counts['trashed'])) { |
| 164 |
$counts['all'] -= $counts['trashed']; |
| 165 |
} |
| 166 |
|
| 167 |
$favorites = $this->where('form_id', $formId) |
| 168 |
->where('is_favourite', 1) |
| 169 |
->where('status', '!=', 'trashed') |
| 170 |
->count(); |
| 171 |
|
| 172 |
$counts['favorites'] = $favorites; |
| 173 |
|
| 174 |
return array_merge([ |
| 175 |
'unread' => 0, |
| 176 |
'read' => 0, |
| 177 |
'spam' => 0, |
| 178 |
'trashed' => 0, |
| 179 |
], $counts); |
| 180 |
} |
| 181 |
|
| 182 |
public function amend($id, $data = []) |
| 183 |
{ |
| 184 |
$this->where('id', $id)->update($data); |
| 185 |
} |
| 186 |
|
| 187 |
public static function remove($entryIds, $formId = null) |
| 188 |
{ |
| 189 |
// Fail-closed scope guard: $formId scopes every delete to its owning form; |
| 190 |
// a missing scope throws rather than ever deleting unscoped. |
| 191 |
if (empty($formId)) { |
| 192 |
throw new \InvalidArgumentException('Entry::remove() requires a form id to scope the deletion.'); |
| 193 |
} |
| 194 |
|
| 195 |
$entryIds = static::where('form_id', $formId) |
| 196 |
->whereIn('id', (array) $entryIds) |
| 197 |
->pluck('id') |
| 198 |
->all(); |
| 199 |
|
| 200 |
if (!$entryIds) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
static::whereIn('id', $entryIds)->delete(); |
| 205 |
|
| 206 |
EntryMeta::whereIn('response_id', $entryIds)->delete(); |
| 207 |
|
| 208 |
Log::whereIn('source_id', $entryIds) |
| 209 |
->where('source_type', 'submission_item') |
| 210 |
->delete(); |
| 211 |
|
| 212 |
EntryDetails::whereIn('submission_id', $entryIds)->delete(); |
| 213 |
|
| 214 |
try { |
| 215 |
if (PaymentHelper::hasPaymentSettings()) { |
| 216 |
OrderItem::whereIn('submission_id', $entryIds)->delete(); |
| 217 |
Transaction::whereIn('submission_id', $entryIds)->delete(); |
| 218 |
Subscription::whereIn('submission_id', $entryIds)->delete(); |
| 219 |
} |
| 220 |
|
| 221 |
wpFluent()->table('ff_scheduled_actions') |
| 222 |
->whereIn('origin_id', $entryIds) |
| 223 |
->where('type', 'submission_action') |
| 224 |
->delete(); |
| 225 |
} catch (Exception $exception) { |
| 226 |
// ... |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|