| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Api; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
|
| 8 |
class Form |
| 9 |
{ |
| 10 |
public function forms($atts = [], $withFields = false) |
| 11 |
{ |
| 12 |
$defaultAtts = [ |
| 13 |
'search' => '', |
| 14 |
'status' => 'all', |
| 15 |
'sort_column' => 'id', |
| 16 |
'sort_by' => 'DESC', |
| 17 |
'per_page' => 10, |
| 18 |
'page' => 1 |
| 19 |
]; |
| 20 |
|
| 21 |
$atts = wp_parse_args($atts, $defaultAtts); |
| 22 |
|
| 23 |
$perPage = ArrayHelper::get($atts, 'per_page', 10); |
| 24 |
$search = ArrayHelper::get($atts, 'search', ''); |
| 25 |
$status = ArrayHelper::get($atts, 'status', 'all'); |
| 26 |
|
| 27 |
$shortColumn = ArrayHelper::get($atts, 'sort_column', 'id'); |
| 28 |
$sortBy = ArrayHelper::get($atts, 'sort_by', 'DESC'); |
| 29 |
|
| 30 |
$query = wpFluent()->table('fluentform_forms') |
| 31 |
->orderBy($shortColumn, $sortBy); |
| 32 |
|
| 33 |
if ($status && $status != 'all') { |
| 34 |
$query->where('status', $status); |
| 35 |
} |
| 36 |
|
| 37 |
if ($search) { |
| 38 |
$query->where(function ($q) use ($search) { |
| 39 |
$q->where('id', 'LIKE', '%' . $search . '%'); |
| 40 |
$q->orWhere('title', 'LIKE', '%' . $search . '%'); |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
$currentPage = intval(ArrayHelper::get($atts, 'page', 1)); |
| 45 |
$total = $query->count(); |
| 46 |
$skip = $perPage * ($currentPage - 1); |
| 47 |
$data = (array) $query->select('*')->limit($perPage)->offset($skip)->get(); |
| 48 |
|
| 49 |
$dataCount = count($data); |
| 50 |
|
| 51 |
$from = $dataCount > 0 ? ($currentPage - 1) * $perPage + 1 : null; |
| 52 |
|
| 53 |
$to = $dataCount > 0 ? $from + $dataCount - 1 : null; |
| 54 |
$lastPage = (int) ceil($total / $perPage); |
| 55 |
|
| 56 |
$forms = array( |
| 57 |
'current_page' => $currentPage, |
| 58 |
'per_page' => $perPage, |
| 59 |
'from' => $from, |
| 60 |
'to' => $to, |
| 61 |
'last_page' => $lastPage, |
| 62 |
'total' => $total, |
| 63 |
'data' => $data, |
| 64 |
); |
| 65 |
|
| 66 |
foreach ($forms['data'] as $form) { |
| 67 |
$formInstance = $this->form($form); |
| 68 |
$form->preview_url = Helper::getPreviewUrl($form->id, 'classic'); |
| 69 |
$form->edit_url = Helper::getFormAdminPermalink('editor', $form); |
| 70 |
$form->settings_url = Helper::getFormSettingsUrl($form); |
| 71 |
$form->entries_url = Helper::getFormAdminPermalink('entries', $form); |
| 72 |
$form->analytics_url = Helper::getFormAdminPermalink('analytics', $form); |
| 73 |
$form->total_views = Helper::getFormMeta($form->id, '_total_views', 0); |
| 74 |
$form->total_Submissions = $formInstance->submissionCount(); |
| 75 |
$form->unread_count = $formInstance->unreadCount(); |
| 76 |
$form->conversion = $formInstance->conversionRate(); |
| 77 |
if(Helper::isConversionForm($form->id)) { |
| 78 |
$form->conversion_preview = Helper::getPreviewUrl($form->id, 'conversational'); |
| 79 |
} |
| 80 |
|
| 81 |
if(!$withFields) { |
| 82 |
unset($form->form_fields); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $forms; |
| 87 |
} |
| 88 |
|
| 89 |
public function find($formId) |
| 90 |
{ |
| 91 |
return wpFluent()->table('fluentform_forms')->where('id', $formId)->first(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* get Form Properties instance |
| 96 |
* @param int|object $form |
| 97 |
* @return \FluentForm\App\Api\FormProperties |
| 98 |
*/ |
| 99 |
public function form($form) |
| 100 |
{ |
| 101 |
if(is_numeric($form)) { |
| 102 |
$form = $this->find($form); |
| 103 |
} |
| 104 |
|
| 105 |
return (new FormProperties($form)); |
| 106 |
} |
| 107 |
|
| 108 |
public function entryInstance($form) |
| 109 |
{ |
| 110 |
if(is_numeric($form)) { |
| 111 |
$form = $this->find($form); |
| 112 |
} |
| 113 |
|
| 114 |
return (new Entry($form)); |
| 115 |
} |
| 116 |
|
| 117 |
} |