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: $ids = array();
127:
128: if (isset($_GET['eid'])) {
129: $action = Quform::get($_GET, 'action');
130: $ids = (int) $_GET['eid'];
131: } elseif (isset($_GET['eids'])) {
132: $action = $this->getBulkAction();
133: $ids = (array) Quform::get($_GET, 'eids');
134: $ids = array_map('intval', $ids);
135: } elseif (isset($_GET['delete_all'])) {
136: $action = 'delete_all';
137: }
138:
139: if ($action == null) {
140: if (Quform::get($_GET, '_wp_http_referer')) {
141: wp_safe_redirect(esc_url_raw(remove_query_arg(array('_wp_http_referer', '_wpnonce'), wp_unslash($_SERVER['REQUEST_URI']))));
142: exit;
143: }
144:
145: return;
146: }
147:
148: $returnUrl = remove_query_arg(array('action', 'action2', 'eid', 'eids', 'read', 'unread', 'trashed', 'deleted', 'error'), wp_get_referer());
149:
150: switch ($action) {
151: case 'read':
152: $result = $this->processReadAction($ids, $nonce);
153: $returnUrl = add_query_arg($result, $returnUrl);
154: break;
155: case 'unread':
156: $result = $this->processUnreadAction($ids, $nonce);
157: $returnUrl = add_query_arg($result, $returnUrl);
158: break;
159: case 'trash':
160: $result = $this->processTrashAction($ids, $nonce);
161: $returnUrl = add_query_arg($result, $returnUrl);
162: break;
163: case 'untrash':
164: $result = $this->processUntrashAction($ids, $nonce);
165: $returnUrl = add_query_arg($result, $returnUrl);
166: break;
167: case 'delete':
168: $result = $this->processDeleteAction($ids, $nonce);
169: $returnUrl = add_query_arg($result, $returnUrl);
170: break;
171: case 'delete_all':
172: $ids = $this->repository->getEntryIdsByStatus((int) Quform::get($_GET, 'id'), 'trash');
173: $result = $this->processDeleteAction($ids, $nonce);
174: $returnUrl = add_query_arg($result, $returnUrl);
175: break;
176: }
177:
178: wp_safe_redirect(esc_url_raw($returnUrl));
179: exit;
180: }
181:
182: 183: 184: 185: 186: 187: 188:
189: protected function processReadAction($entryIds, $nonce)
190: {
191: if (is_array($entryIds)) {
192: $nonceAction = 'bulk-qfb-entries';
193: } else {
194: $nonceAction = 'quform_read_entry_' . $entryIds;
195: $entryIds = array($entryIds);
196: }
197:
198: if ( ! $nonce || ! count($entryIds)) {
199: return array('error' => self::BAD_REQUEST);
200: }
201:
202: if ( ! current_user_can('quform_view_entries')) {
203: return array('error' => self::NO_PERMISSION);
204: }
205:
206: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
207: return array('error' => self::NONCE_CHECK_FAILED);
208: }
209:
210: $count = $this->repository->readEntries($entryIds);
211:
212: return array('read' => $count);
213: }
214:
215: 216: 217: 218: 219: 220: 221:
222: protected function processUnreadAction($entryIds, $nonce)
223: {
224: if (is_array($entryIds)) {
225: $nonceAction = 'bulk-qfb-entries';
226: } else {
227: $nonceAction = 'quform_unread_entry_' . $entryIds;
228: $entryIds = array($entryIds);
229: }
230:
231: if ( ! $nonce || ! count($entryIds)) {
232: return array('error' => self::BAD_REQUEST);
233: }
234:
235: if ( ! current_user_can('quform_view_entries')) {
236: return array('error' => self::NO_PERMISSION);
237: }
238:
239: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
240: return array('error' => self::NONCE_CHECK_FAILED);
241: }
242:
243: $count = $this->repository->unreadEntries($entryIds);
244:
245: return array('unread' => $count);
246: }
247:
248: 249: 250: 251: 252: 253: 254:
255: protected function processTrashAction($entryIds, $nonce)
256: {
257: if (is_array($entryIds)) {
258: $nonceAction = 'bulk-qfb-entries';
259: } else {
260: $nonceAction = 'quform_trash_entry_' . $entryIds;
261: $entryIds = array($entryIds);
262: }
263:
264: if ( ! $nonce || ! count($entryIds)) {
265: return array('error' => self::BAD_REQUEST);
266: }
267:
268: if ( ! current_user_can('quform_delete_entries')) {
269: return array('error' => self::NO_PERMISSION);
270: }
271:
272: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
273: return array('error' => self::NONCE_CHECK_FAILED);
274: }
275:
276: $count = $this->repository->trashEntries($entryIds);
277:
278: return array('trashed' => $count);
279: }
280:
281: 282: 283: 284: 285: 286: 287:
288: protected function processUntrashAction($entryIds, $nonce)
289: {
290: if (is_array($entryIds)) {
291: $nonceAction = 'bulk-qfb-entries';
292: } else {
293: $nonceAction = 'quform_untrash_entry_' . $entryIds;
294: $entryIds = array($entryIds);
295: }
296:
297: if ( ! $nonce || ! count($entryIds)) {
298: return array('error' => self::BAD_REQUEST);
299: }
300:
301: if ( ! current_user_can('quform_delete_entries')) {
302: return array('error' => self::NO_PERMISSION);
303: }
304:
305: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
306: return array('error' => self::NONCE_CHECK_FAILED);
307: }
308:
309: $count = $this->repository->untrashEntries($entryIds);
310:
311: return array('untrashed' => $count);
312: }
313:
314: 315: 316: 317: 318: 319: 320:
321: protected function processDeleteAction($entryIds, $nonce)
322: {
323: if (is_array($entryIds)) {
324: $nonceAction = 'bulk-qfb-entries';
325: } else {
326: $nonceAction = 'quform_delete_entry_' . $entryIds;
327: $entryIds = array($entryIds);
328: }
329:
330: if ( ! $nonce || ! count($entryIds)) {
331: return array('error' => self::BAD_REQUEST);
332: }
333:
334: if ( ! current_user_can('quform_delete_entries')) {
335: return array('error' => self::NO_PERMISSION);
336: }
337:
338: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
339: return array('error' => self::NONCE_CHECK_FAILED);
340: }
341:
342: $count = $this->repository->deleteEntries($entryIds);
343:
344: return array('deleted' => $count);
345: }
346:
347: 348: 349: 350: 351:
352: protected function getBulkAction()
353: {
354: $action = null;
355:
356: $a1 = Quform::get($_GET, 'action', '-1');
357: $a2 = Quform::get($_GET, 'action2', '-1');
358:
359: if ($a1 != '-1') {
360: $action = $a1;
361: } elseif ($a2 != '-1') {
362: $action = $a2;
363: }
364:
365: return $action;
366: }
367:
368: 369: 370:
371: protected function addPageMessages()
372: {
373: $read = (int) Quform::get($_GET, 'read');
374: if ($read > 0) {
375:
376: $this->addMessage('success', sprintf(_n('%s entry marked as read', '%s entries marked as read', $read, 'quform'), number_format_i18n($read)));
377: }
378:
379: $unread = (int) Quform::get($_GET, 'unread');
380: if ($unread > 0) {
381:
382: $this->addMessage('success', sprintf(_n('%s entry marked as unread', '%s entries marked as unread', $unread, 'quform'), number_format_i18n($unread)));
383: }
384:
385: $trashed = (int) Quform::get($_GET, 'trashed');
386: if ($trashed > 0) {
387:
388: $this->addMessage('success', sprintf(_n('%s entry moved to the Trash', '%s entries moved to the Trash', $trashed, 'quform'), number_format_i18n($trashed)));
389: }
390:
391: $untrashed = (int) Quform::get($_GET, 'untrashed');
392: if ($untrashed > 0) {
393:
394: $this->addMessage('success', sprintf(_n('%s entry restored', '%s entries restored', $untrashed, 'quform'), number_format_i18n($untrashed)));
395: }
396:
397: $deleted = (int) Quform::get($_GET, 'deleted');
398: if ($deleted > 0) {
399:
400: $this->addMessage('success', sprintf(_n('%s entry deleted', '%s entries deleted', $deleted, 'quform'), number_format_i18n($deleted)));
401: }
402:
403: switch ((int) Quform::get($_GET, 'error')) {
404: case self::BAD_REQUEST:
405: $this->addMessage('error', __('Bad request.', 'quform'));
406: break;
407: case self::NO_PERMISSION:
408: $this->addMessage('error', __('You do not have permission to do this.', 'quform'));
409: break;
410: case self::NONCE_CHECK_FAILED:
411: $this->addMessage('error', __('Nonce check failed.', 'quform'));
412: break;
413: }
414: }
415:
416: 417: 418: 419: 420: 421:
422: public function removableQueryArgs($args)
423: {
424: $args[] = 'read';
425: $args[] = 'unread';
426:
427: return $args;
428: }
429:
430: 431: 432: 433: 434: 435: 436:
437: public function getNavHtml(array $currentForm = null, array $extra = array())
438: {
439: $extra[40] = sprintf(
440: '<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>',
441:
442: Quform::escape(sprintf(__('Entries for %s', 'quform'), $currentForm['name']))
443: );
444:
445: $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>';
446:
447: return parent::getNavHtml($currentForm, $extra);
448: }
449:
450: 451: 452: 453: 454: 455:
456: public function getEntryLabelEditHtml(array $label = null)
457: {
458: $output = sprintf(
459: '<div class="qfb-entry-label-edit qfb-cf"%s%s>',
460: is_array($label) ? sprintf(' data-label="%s"', Quform::escape(wp_json_encode($label))) : '',
461: is_array($label) ? sprintf(' style="background-color: %s;"', Quform::escape($label['color'])) : ''
462: );
463:
464: $output .= sprintf(
465: '<span class="qfb-entry-label-edit-name" title="%s">%s</span>',
466: esc_attr__('Click to edit name', 'quform'),
467: is_array($label) ? Quform::escape($label['name']) : ''
468: );
469:
470: $output .= '<div class="qfb-entry-label-edit-actions">';
471: $output .= '<span class="qfb-entry-label-edit-action-color"><i class="qfb-mdi qfb-mdi-format_color_fill"></i></span>';
472: $output .= '<span class="qfb-entry-label-edit-action-duplicate"><i class="qfb-mdi qfb-mdi-content_copy"></i></span>';
473: $output .= '<span class="qfb-entry-label-edit-action-remove"><i class="qfb-icon qfb-icon-trash"></i></span>';
474: $output .= '</div></div>';
475:
476: return $output;
477: }
478: }
479: