1: <?php
2:
3: 4: 5:
6: class Quform_Admin_Page_Entries_List extends Quform_Admin_Page_Entries
7: {
8: 9: 10:
11: protected $formFactory;
12:
13: 14: 15:
16: protected $options;
17:
18: 19: 20: 21: 22: 23:
24: public function __construct(Quform_ViewFactory $viewFactory, Quform_Repository $repository,
25: Quform_Form_Factory $formFactory, Quform_Options $options)
26: {
27: parent::__construct($viewFactory, $repository);
28:
29: $this->formFactory = $formFactory;
30: $this->options = $options;
31: }
32:
33: public function init()
34: {
35: $this->template = QUFORM_TEMPLATE_PATH . '/admin/entries/list.php';
36: }
37:
38: 39: 40:
41: protected function enqueueStyles()
42: {
43: wp_enqueue_style('qtip2', Quform::url('css/jquery.qtip.min.css'), array(), '3.0.4');
44: wp_enqueue_style('spectrum', Quform::adminUrl('css/spectrum.min.css'), array(), '1.8.1');
45:
46: parent::enqueueStyles();
47: }
48:
49: 50: 51:
52: protected function enqueueScripts()
53: {
54: wp_enqueue_script('jeditable', Quform::adminUrl('js/jquery.jeditable.min.js'), array('jquery'), '2.0.19', true);
55: wp_enqueue_script('qtip2', Quform::url('js/jquery.qtip.min.js'), array('jquery'), '3.0.4', true);
56: wp_enqueue_script('spectrum', Quform::adminUrl('js/spectrum.min.js'), array(), '1.8.1', true);
57:
58: parent::enqueueScripts();
59:
60: wp_enqueue_script('sortablejs', Quform::adminUrl('js/Sortable.min.js'), array(), '1.15.7', true);
61: wp_enqueue_script('quform-entries-list', Quform::adminUrl('js/entries.list.min.js'), array('jquery', 'sortablejs'), QUFORM_VERSION, true);
62:
63: wp_localize_script('quform-entries-list', 'quformEntriesListL10n', array(
64: 'singleDeleteEntryMessage' => __('Are you sure you want to delete this entry? All data for this entry will be lost and this cannot be undone.', 'quform'),
65: 'pluralDeleteEntryMessage' => __('Are you sure you want to delete these entries? All data for these entries will be lost and this cannot be undone.', 'quform'),
66: 'saveEntriesTableSettingsNonce' => wp_create_nonce('quform_save_entries_table_settings'),
67: 'entryLabelEditHtml' => $this->getEntryLabelEditHtml()
68: ));
69: }
70:
71: 72: 73:
74: public function process()
75: {
76: if ( ! current_user_can('quform_view_entries')) {
77: wp_die(__( 'You do not have sufficient permissions to access this page.', 'quform'), 403);
78: }
79:
80:
81: $this->processActions();
82:
83: if (isset($_GET['id']) && Quform::isNonEmptyString($_GET['id'])) {
84: $config = $this->repository->getConfig((int) $_GET['id']);
85:
86: if ($config === null) {
87: wp_die(esc_html__('The form could not be found. Perhaps it was deleted?', 'quform'));
88: }
89: } else {
90: $defaultId = (int) apply_filters('quform_entries_default_form_id', 0);
91: $config = $defaultId > 0 ? $this->repository->getConfig($defaultId) : null;
92:
93: if ($config === null) {
94: $config = $this->repository->firstConfig();
95: }
96:
97: if ($config === null) {
98:
99: wp_safe_redirect(admin_url('admin.php?page=quform.forms'));
100: exit;
101: }
102: }
103:
104: $config['environment'] = 'listEntry';
105:
106: $form = $this->formFactory->create($config);
107:
108: $this->addPageMessages();
109:
110: $table = new Quform_Entry_List_Table($this, $form, $this->repository, $this->options);
111: $table->prepare_items();
112:
113: $perPage = get_user_meta(get_current_user_id(), 'quform_entries_per_page', true);
114: if ( ! is_numeric($perPage)) {
115: $perPage = 20;
116: }
117:
118: $this->view->with(array(
119: 'form' => $form,
120: 'table' => $table,
121: 'perPage' => $perPage,
122: 'labels' => $this->repository->getFormEntryLabels($form->getId())
123: ));
124:
125: add_filter('removable_query_args', array($this, 'removableQueryArgs'));
126: }
127:
128: protected function processActions()
129: {
130: $nonce = Quform::get($_GET, '_wpnonce');
131: $action = null;
132: $ids = array();
133:
134: if (isset($_GET['eid'])) {
135: $action = Quform::get($_GET, 'action');
136: $ids = (int) $_GET['eid'];
137: } elseif (isset($_GET['eids'])) {
138: $action = $this->getBulkAction();
139: $ids = (array) Quform::get($_GET, 'eids');
140: $ids = array_map('intval', $ids);
141: } elseif (isset($_GET['delete_all'])) {
142: $action = 'delete_all';
143: }
144:
145: if ($action == null) {
146: if (Quform::get($_GET, '_wp_http_referer')) {
147: wp_safe_redirect(esc_url_raw(remove_query_arg(array('_wp_http_referer', '_wpnonce'), wp_unslash($_SERVER['REQUEST_URI']))));
148: exit;
149: }
150:
151: return;
152: }
153:
154: do_action('quform_pre_process_entries_list_action', $action, $ids);
155:
156: $returnUrl = remove_query_arg(array('action', 'action2', 'eid', 'eids', 'read', 'unread', 'trashed', 'deleted', 'error'), wp_get_referer());
157:
158: switch ($action) {
159: case 'read':
160: $result = $this->processReadAction($ids, $nonce);
161: $returnUrl = add_query_arg($result, $returnUrl);
162: break;
163: case 'unread':
164: $result = $this->processUnreadAction($ids, $nonce);
165: $returnUrl = add_query_arg($result, $returnUrl);
166: break;
167: case 'trash':
168: $result = $this->processTrashAction($ids, $nonce);
169: $returnUrl = add_query_arg($result, $returnUrl);
170: break;
171: case 'untrash':
172: $result = $this->processUntrashAction($ids, $nonce);
173: $returnUrl = add_query_arg($result, $returnUrl);
174: break;
175: case 'delete':
176: $result = $this->processDeleteAction($ids, $nonce);
177: $returnUrl = add_query_arg($result, $returnUrl);
178: break;
179: case 'delete_all':
180: $ids = $this->repository->getEntryIdsByStatus((int) Quform::get($_GET, 'id'), 'trash');
181: $result = $this->processDeleteAction($ids, $nonce);
182: $returnUrl = add_query_arg($result, $returnUrl);
183: break;
184: }
185:
186: wp_safe_redirect(esc_url_raw($returnUrl));
187: exit;
188: }
189:
190: 191: 192: 193: 194: 195: 196:
197: protected function processReadAction($entryIds, $nonce)
198: {
199: if (is_array($entryIds)) {
200: $nonceAction = 'bulk-qfb-entries';
201: } else {
202: $nonceAction = 'quform_read_entry_' . $entryIds;
203: $entryIds = array($entryIds);
204: }
205:
206: if ( ! $nonce || ! count($entryIds)) {
207: return array('error' => self::BAD_REQUEST);
208: }
209:
210: if ( ! current_user_can('quform_view_entries')) {
211: return array('error' => self::NO_PERMISSION);
212: }
213:
214: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
215: return array('error' => self::NONCE_CHECK_FAILED);
216: }
217:
218: $count = $this->repository->readEntries($entryIds);
219:
220: return array('read' => $count);
221: }
222:
223: 224: 225: 226: 227: 228: 229:
230: protected function processUnreadAction($entryIds, $nonce)
231: {
232: if (is_array($entryIds)) {
233: $nonceAction = 'bulk-qfb-entries';
234: } else {
235: $nonceAction = 'quform_unread_entry_' . $entryIds;
236: $entryIds = array($entryIds);
237: }
238:
239: if ( ! $nonce || ! count($entryIds)) {
240: return array('error' => self::BAD_REQUEST);
241: }
242:
243: if ( ! current_user_can('quform_view_entries')) {
244: return array('error' => self::NO_PERMISSION);
245: }
246:
247: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
248: return array('error' => self::NONCE_CHECK_FAILED);
249: }
250:
251: $count = $this->repository->unreadEntries($entryIds);
252:
253: return array('unread' => $count);
254: }
255:
256: 257: 258: 259: 260: 261: 262:
263: protected function processTrashAction($entryIds, $nonce)
264: {
265: if (is_array($entryIds)) {
266: $nonceAction = 'bulk-qfb-entries';
267: } else {
268: $nonceAction = 'quform_trash_entry_' . $entryIds;
269: $entryIds = array($entryIds);
270: }
271:
272: if ( ! $nonce || ! count($entryIds)) {
273: return array('error' => self::BAD_REQUEST);
274: }
275:
276: if ( ! current_user_can('quform_delete_entries')) {
277: return array('error' => self::NO_PERMISSION);
278: }
279:
280: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
281: return array('error' => self::NONCE_CHECK_FAILED);
282: }
283:
284: $count = $this->repository->trashEntries($entryIds);
285:
286: return array('trashed' => $count);
287: }
288:
289: 290: 291: 292: 293: 294: 295:
296: protected function processUntrashAction($entryIds, $nonce)
297: {
298: if (is_array($entryIds)) {
299: $nonceAction = 'bulk-qfb-entries';
300: } else {
301: $nonceAction = 'quform_untrash_entry_' . $entryIds;
302: $entryIds = array($entryIds);
303: }
304:
305: if ( ! $nonce || ! count($entryIds)) {
306: return array('error' => self::BAD_REQUEST);
307: }
308:
309: if ( ! current_user_can('quform_delete_entries')) {
310: return array('error' => self::NO_PERMISSION);
311: }
312:
313: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
314: return array('error' => self::NONCE_CHECK_FAILED);
315: }
316:
317: $count = $this->repository->untrashEntries($entryIds);
318:
319: return array('untrashed' => $count);
320: }
321:
322: 323: 324: 325: 326: 327: 328:
329: protected function processDeleteAction($entryIds, $nonce)
330: {
331: if (is_array($entryIds)) {
332: $nonceAction = 'bulk-qfb-entries';
333: } else {
334: $nonceAction = 'quform_delete_entry_' . $entryIds;
335: $entryIds = array($entryIds);
336: }
337:
338: if ( ! $nonce || ! count($entryIds)) {
339: return array('error' => self::BAD_REQUEST);
340: }
341:
342: if ( ! current_user_can('quform_delete_entries')) {
343: return array('error' => self::NO_PERMISSION);
344: }
345:
346: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
347: return array('error' => self::NONCE_CHECK_FAILED);
348: }
349:
350: $count = $this->repository->deleteEntries($entryIds);
351:
352: return array('deleted' => $count);
353: }
354:
355: 356: 357: 358: 359:
360: protected function getBulkAction()
361: {
362: $action = null;
363:
364: $a1 = Quform::get($_GET, 'action', '-1');
365: $a2 = Quform::get($_GET, 'action2', '-1');
366:
367: if ($a1 != '-1') {
368: $action = $a1;
369: } elseif ($a2 != '-1') {
370: $action = $a2;
371: }
372:
373: return $action;
374: }
375:
376: 377: 378:
379: protected function addPageMessages()
380: {
381: $read = (int) Quform::get($_GET, 'read');
382: if ($read > 0) {
383:
384: $this->addMessage('success', sprintf(_n('%s entry marked as read', '%s entries marked as read', $read, 'quform'), number_format_i18n($read)));
385: }
386:
387: $unread = (int) Quform::get($_GET, 'unread');
388: if ($unread > 0) {
389:
390: $this->addMessage('success', sprintf(_n('%s entry marked as unread', '%s entries marked as unread', $unread, 'quform'), number_format_i18n($unread)));
391: }
392:
393: $trashed = (int) Quform::get($_GET, 'trashed');
394: if ($trashed > 0) {
395:
396: $this->addMessage('success', sprintf(_n('%s entry moved to the Trash', '%s entries moved to the Trash', $trashed, 'quform'), number_format_i18n($trashed)));
397: }
398:
399: $untrashed = (int) Quform::get($_GET, 'untrashed');
400: if ($untrashed > 0) {
401:
402: $this->addMessage('success', sprintf(_n('%s entry restored', '%s entries restored', $untrashed, 'quform'), number_format_i18n($untrashed)));
403: }
404:
405: $deleted = (int) Quform::get($_GET, 'deleted');
406: if ($deleted > 0) {
407:
408: $this->addMessage('success', sprintf(_n('%s entry deleted', '%s entries deleted', $deleted, 'quform'), number_format_i18n($deleted)));
409: }
410:
411: switch ((int) Quform::get($_GET, 'error')) {
412: case self::BAD_REQUEST:
413: $this->addMessage('error', __('Bad request.', 'quform'));
414: break;
415: case self::NO_PERMISSION:
416: $this->addMessage('error', __('You do not have permission to do this.', 'quform'));
417: break;
418: case self::NONCE_CHECK_FAILED:
419: $this->addMessage('error', __('Nonce check failed.', 'quform'));
420: break;
421: }
422: }
423:
424: 425: 426: 427: 428: 429:
430: public function removableQueryArgs($args)
431: {
432: $args[] = 'read';
433: $args[] = 'unread';
434:
435: return $args;
436: }
437:
438: 439: 440: 441: 442: 443: 444:
445: public function getNavHtml(?array $currentForm = null, array $extra = array())
446: {
447: $extra[40] = sprintf(
448: '<div class="qfb-nav-item qfb-nav-page-info"><i class="qfb-nav-page-icon qfb-mdi qfb-mdi-message"></i><span class="qfb-nav-page-title">%s</span></div>',
449:
450: Quform::escape(sprintf(__('Entries for %s', 'quform'), $currentForm['name']))
451: );
452:
453: $extra[50] = '<div class="qfb-nav-item qfb-nav-item-right"><a id="qfb-show-entries-table-settings" class="qfb-nav-item-link"><i class="qfb-mdi qfb-mdi-settings"></i></a></div>';
454:
455: return parent::getNavHtml($currentForm, $extra);
456: }
457:
458: 459: 460: 461: 462: 463:
464: public function getEntryLabelEditHtml(?array $label = null)
465: {
466: $output = sprintf(
467: '<div class="qfb-entry-label-edit qfb-cf"%s%s>',
468: is_array($label) ? sprintf(' data-label="%s"', Quform::escape(wp_json_encode($label))) : '',
469: is_array($label) ? sprintf(' style="background-color: %s;"', Quform::escape($label['color'])) : ''
470: );
471:
472: $output .= sprintf(
473: '<span class="qfb-entry-label-edit-name" title="%s">%s</span>',
474: esc_attr__('Click to edit name', 'quform'),
475: is_array($label) ? Quform::escape($label['name']) : ''
476: );
477:
478: $output .= '<div class="qfb-entry-label-edit-actions">';
479: $output .= '<span class="qfb-entry-label-edit-action-color"><i class="qfb-mdi qfb-mdi-format_color_fill"></i></span>';
480: $output .= '<span class="qfb-entry-label-edit-action-duplicate"><i class="qfb-mdi qfb-mdi-content_copy"></i></span>';
481: $output .= '<span class="qfb-entry-label-edit-action-remove"><i class="qfb-icon qfb-icon-trash"></i></span>';
482: $output .= '</div></div>';
483:
484: return $output;
485: }
486: }
487: