| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Entries; |
| 4 |
|
| 5 |
use FluentForm\View; |
| 6 |
use FluentForm\App\Modules\Acl\Acl; |
| 7 |
use FluentForm\App\Modules\Form\FormDataParser; |
| 8 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 9 |
|
| 10 |
class Entries extends EntryQuery |
| 11 |
{ |
| 12 |
protected $responseMetaModel; |
| 13 |
|
| 14 |
/** |
| 15 |
* Entries constructor. |
| 16 |
* @throws \Exception |
| 17 |
*/ |
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
Acl::verify('fluentform_entries_viewer'); |
| 21 |
|
| 22 |
parent::__construct(); |
| 23 |
$this->responseMetaModel = wpFluent()->table( 'fluentform_submission_meta' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function renderEntries($form_id) |
| 27 |
{ |
| 28 |
wp_enqueue_script('fluentform_form_entries', fluentformMix("js/form_entries.js")); |
| 29 |
|
| 30 |
$forms = wpFluent() |
| 31 |
->select(array('id', 'title')) |
| 32 |
->table('fluentform_forms') |
| 33 |
->get(); |
| 34 |
|
| 35 |
$form = wpFluent()->table('fluentform_forms')->find($form_id); |
| 36 |
|
| 37 |
wp_localize_script('fluentform_form_entries', 'fluent_form_entries_vars', array( |
| 38 |
'all_forms_url' => admin_url('admin.php?page=fluent_forms'), |
| 39 |
'forms' => $forms, |
| 40 |
'form_id' => $form->id, |
| 41 |
'current_form_title' => $form->title, |
| 42 |
'entries_url_base' => admin_url('admin.php?page=fluent_forms&route=entries&form_id='), |
| 43 |
'no_found_text' => __('Sorry! No entries found. All your entries will be shown here once you start getting form submissions', 'fluentform') |
| 44 |
)); |
| 45 |
View::render('admin.form.entries', array( |
| 46 |
'form_id' => $form_id |
| 47 |
)); |
| 48 |
} |
| 49 |
|
| 50 |
public function getEntriesGroup() { |
| 51 |
$formId = intval($this->request->get('form_id')); |
| 52 |
$counts = $this->groupCount($formId); |
| 53 |
wp_send_json_success([ |
| 54 |
'counts' => $counts |
| 55 |
], 200); |
| 56 |
} |
| 57 |
|
| 58 |
public function getEntries() { |
| 59 |
$formId = intval($this->request->get('form_id')); |
| 60 |
$currentPage = intval($this->request->get('current_page', 1)); |
| 61 |
$perPage = intval($this->request->get('per_page', 10)); |
| 62 |
$sortBy = sanitize_text_field($this->request->get('sort_by', 'ASC')); |
| 63 |
$entry_type = sanitize_text_field($this->request->get('entry_type', 'all')); |
| 64 |
$search = sanitize_text_field($this->request->get('search')); |
| 65 |
|
| 66 |
$this->formId = $formId; |
| 67 |
$this->per_page = $perPage; |
| 68 |
$this->sort_by = $sortBy; |
| 69 |
$this->page_number = $currentPage; |
| 70 |
$this->search = $search; |
| 71 |
|
| 72 |
if($entry_type == 'favorite') { |
| 73 |
$this->is_favourite = true; |
| 74 |
} else if($entry_type != 'all') { |
| 75 |
$this->status = $entry_type; |
| 76 |
} |
| 77 |
|
| 78 |
$form = $this->formModel->find($formId); |
| 79 |
$formMeta = $this->getFormInputsAndLabels($form); |
| 80 |
|
| 81 |
$submissions = $this->getResponses(); |
| 82 |
$submissions['data'] = FormDataParser::parseFormEntries($submissions['data'], $form); |
| 83 |
|
| 84 |
wp_send_json_success([ |
| 85 |
'submissions' => $submissions, |
| 86 |
'labels' => $formMeta['labels'] |
| 87 |
], 200); |
| 88 |
exit(); |
| 89 |
} |
| 90 |
|
| 91 |
public function getEntry() |
| 92 |
{ |
| 93 |
$formId = intval($this->request->get('form_id')); |
| 94 |
$entryId = intval($this->request->get('entry_id')); |
| 95 |
$entry_type = sanitize_text_field($this->request->get('entry_type', 'all')); |
| 96 |
$operator = sanitize_text_field($this->request->get('operator')); |
| 97 |
// Ensures the next operator is in recognized format. |
| 98 |
$operator = in_array($operator, ['+', '-']) ? $operator : null; |
| 99 |
|
| 100 |
$currentSerialNo = sanitize_text_field($this->request->get('currentSerialNo')); |
| 101 |
|
| 102 |
$this->formId = $formId; |
| 103 |
$this->sort_by = sanitize_text_field($this->request->get('sort_by', 'ASC')); |
| 104 |
$this->search = sanitize_text_field($this->request->get('search')); |
| 105 |
|
| 106 |
if ($entry_type === 'favorite') { |
| 107 |
$this->is_favourite = true; |
| 108 |
} else if ($entry_type !== 'all') { |
| 109 |
$this->status = $entry_type; |
| 110 |
} |
| 111 |
|
| 112 |
$form = $this->formModel->find($formId); |
| 113 |
$formMeta = $this->getFormInputsAndLabels($form); |
| 114 |
|
| 115 |
$submission = $this->getResponse($entryId, $operator, $currentSerialNo); |
| 116 |
$submission = FormDataParser::parseFormEntry($submission, $form, $formMeta['inputs']); |
| 117 |
|
| 118 |
if($submission->user_id) { |
| 119 |
$user = get_user_by('ID', $submission->user_id); |
| 120 |
$user_data = array( |
| 121 |
'name' => $user->display_name, |
| 122 |
'email' => $user->user_email, |
| 123 |
'ID' => $user->ID, |
| 124 |
'permalink' => get_edit_user_link($user->ID) |
| 125 |
); |
| 126 |
$submission->user = $user_data; |
| 127 |
} |
| 128 |
|
| 129 |
$submission = apply_filters('fluentform_single_response_data', $submission, $this->formId); |
| 130 |
|
| 131 |
wp_send_json_success(array( |
| 132 |
'submission' => $submission, |
| 133 |
'labels' => $formMeta['labels'], |
| 134 |
'fields' => $formMeta['inputs'] |
| 135 |
), 200); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @param $formId |
| 140 |
* @todo: Implement Caching mechanism so we don't have to parse these things for every request |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
private function getFormInputsAndLabels($form) |
| 144 |
{ |
| 145 |
$formInputs = FormFieldsParser::getEntryInputs($form); |
| 146 |
$inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs); |
| 147 |
return array( |
| 148 |
'inputs' => $formInputs, |
| 149 |
'labels' => $inputLabels |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
public function getNotes() { |
| 154 |
$formId = intval($this->request->get('form_id')); |
| 155 |
$entry_id = intval($this->request->get('entry_id')); |
| 156 |
|
| 157 |
$notes = $this->responseMetaModel |
| 158 |
->where('form_id', $formId) |
| 159 |
->where('response_id', $entry_id) |
| 160 |
->where('meta_key','_notes') |
| 161 |
->get(); |
| 162 |
|
| 163 |
foreach ( $notes as $note ) { |
| 164 |
if($note->user_id) { |
| 165 |
$note->pemalink = get_edit_user_link($note->user_id); |
| 166 |
} else { |
| 167 |
$note->pemalink = false; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
$notes = apply_filters('fluentform_entry_notes', $notes, $entry_id, $formId); |
| 172 |
|
| 173 |
wp_send_json_success([ |
| 174 |
'notes' => $notes |
| 175 |
], 200); |
| 176 |
} |
| 177 |
|
| 178 |
public function addNote() { |
| 179 |
$entryId = intval($this->request->get('entry_id')); |
| 180 |
$formId = intval($this->request->get('form_id')); |
| 181 |
$note = $this->request->get('note'); |
| 182 |
$note_content = sanitize_textarea_field($note['content']); |
| 183 |
$note_status = sanitize_text_field($note['status']); |
| 184 |
$user = get_user_by('ID', get_current_user_id()); |
| 185 |
|
| 186 |
$response_note = array( |
| 187 |
'response_id' => $entryId, |
| 188 |
'form_id' => $formId, |
| 189 |
'meta_key' => '_notes', |
| 190 |
'value' => $note_content, |
| 191 |
'status' => $note_status, |
| 192 |
'user_id' => $user->ID, |
| 193 |
'name' => $user->display_name, |
| 194 |
'created_at' => date('Y-m-d H:i:s'), |
| 195 |
'updated_at' => date('Y-m-d H:i:s') |
| 196 |
); |
| 197 |
$response_note = apply_filters('fluentform_add_response_note', $response_note); |
| 198 |
|
| 199 |
$insertId = $this->responseMetaModel->insert($response_note); |
| 200 |
$added_note = $this->responseMetaModel->find($insertId); |
| 201 |
do_action('fluentform_new_response_note_added', $insertId, $added_note); |
| 202 |
wp_send_json_success([ |
| 203 |
'message' => __('Note has been successfully added', 'fluentform'), |
| 204 |
'note' => $added_note, |
| 205 |
'insert_id' => $insertId |
| 206 |
], 200); |
| 207 |
} |
| 208 |
|
| 209 |
public function changeEntryStatus() { |
| 210 |
$formId = intval($this->request->get('form_id')); |
| 211 |
$entryId = intval($this->request->get('entry_id')); |
| 212 |
$newStatus = sanitize_text_field($this->request->get('status')); |
| 213 |
|
| 214 |
$this->responseModel |
| 215 |
->where('form_id', $formId) |
| 216 |
->where('id', $entryId) |
| 217 |
->update(array('status' => $newStatus)); |
| 218 |
|
| 219 |
wp_send_json_success(array( |
| 220 |
'message' => __('Item has been marked as '.$newStatus, 'fluentform'), |
| 221 |
'status' => $newStatus |
| 222 |
), 200); |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
public function deleteEntry() { |
| 227 |
$formId = intval($this->request->get('form_id')); |
| 228 |
$entryId = intval($this->request->get('entry_id')); |
| 229 |
$newStatus = sanitize_text_field($this->request->get('status')); |
| 230 |
|
| 231 |
$this->responseModel |
| 232 |
->where('form_id', $formId) |
| 233 |
->where('id', $entryId) |
| 234 |
->delete(); |
| 235 |
|
| 236 |
wp_send_json_success(array( |
| 237 |
'message' => __('Item Successfully deleted', 'fluentform'), |
| 238 |
'status' => $newStatus |
| 239 |
), 200); |
| 240 |
} |
| 241 |
|
| 242 |
public function favoriteChange() { |
| 243 |
$formId = intval($this->request->get('form_id')); |
| 244 |
$entryId = intval($this->request->get('entry_id')); |
| 245 |
$newStatus = intval($this->request->get('is_favourite')); |
| 246 |
if($newStatus) { |
| 247 |
$message = __('Item has been added to favorites', 'fluentform'); |
| 248 |
} else { |
| 249 |
$message = __('Item has been removed from favorites', 'fluentform'); |
| 250 |
} |
| 251 |
$this->responseModel |
| 252 |
->where('form_id', $formId) |
| 253 |
->where('id', $entryId) |
| 254 |
->update(array('is_favourite' => $newStatus)); |
| 255 |
|
| 256 |
wp_send_json_success(array( |
| 257 |
'message' => $message, |
| 258 |
'is_favourite' => $newStatus |
| 259 |
), 200); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
public function handleBulkAction() |
| 264 |
{ |
| 265 |
$formId = intval($this->request->get('form_id')); |
| 266 |
$entries = fluentFormSanitizer($this->request->get('entries', array())); |
| 267 |
|
| 268 |
$actionType = sanitize_text_field($this->request->get('action_type')); |
| 269 |
|
| 270 |
if(!$formId || !count($entries)) { |
| 271 |
wp_send_json_error(array( |
| 272 |
'message' => __('Please select entries first', 'fluentform') |
| 273 |
), 400); |
| 274 |
} |
| 275 |
|
| 276 |
$message = __('Action Successfully executed', 'fluentform'); |
| 277 |
|
| 278 |
$bulkQuery = wpFluent()->table('fluentform_submissions') |
| 279 |
->where('form_id', $formId) |
| 280 |
->whereIn('id', $entries); |
| 281 |
if($actionType == 'trashed') { |
| 282 |
$updateData = array( |
| 283 |
'status' => 'trashed' |
| 284 |
); |
| 285 |
$bulkQuery->update($updateData); |
| 286 |
$message = __('Selected entries successfully trashed', 'fluentform'); |
| 287 |
} else if($actionType == 'delete') { |
| 288 |
$bulkQuery->delete(); |
| 289 |
$message = __('Selected entries successfully deleted', 'fluentform'); |
| 290 |
} else if($actionType == 'read') { |
| 291 |
$updateData = array( |
| 292 |
'status' => 'read' |
| 293 |
); |
| 294 |
$bulkQuery->update($updateData); |
| 295 |
$message = __('Selected entries successfully marked as read', 'fluentform'); |
| 296 |
} else if($actionType == 'unread') { |
| 297 |
$updateData = array( |
| 298 |
'status' => 'unread' |
| 299 |
); |
| 300 |
$bulkQuery->update($updateData); |
| 301 |
$message = __('Selected entries successfully marked as unread', 'fluentform'); |
| 302 |
} else if($actionType == 'favorite') { |
| 303 |
$updateData = array( |
| 304 |
'is_favourite' => 1 |
| 305 |
); |
| 306 |
$bulkQuery->update($updateData); |
| 307 |
$message = __('Selected entries successfully marked as Favorite', 'fluentform'); |
| 308 |
} else if($actionType == 'remove-favorite') { |
| 309 |
$updateData = array( |
| 310 |
'is_favourite' => 0 |
| 311 |
); |
| 312 |
$bulkQuery->update($updateData); |
| 313 |
$message = __('Selected entries successfully remove from favorite', 'fluentform'); |
| 314 |
} |
| 315 |
|
| 316 |
wp_send_json_success(array( |
| 317 |
'message' => $message |
| 318 |
), 200); |
| 319 |
} |
| 320 |
|
| 321 |
} |