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