1: <?php
2:
3: 4: 5:
6: class Quform_Admin_Page_Forms_List extends Quform_Admin_Page
7: {
8: 9: 10:
11: protected $scriptLoader;
12:
13: 14: 15:
16: protected $table;
17:
18: 19: 20: 21: 22: 23:
24: public function __construct(Quform_ViewFactory $viewFactory, Quform_Repository $repository,
25: Quform_ScriptLoader $scriptLoader, Quform_Form_List_Table $table)
26: {
27: parent::__construct($viewFactory, $repository);
28:
29: $this->scriptLoader = $scriptLoader;
30: $this->table = $table;
31: }
32:
33: public function init()
34: {
35: $this->template = QUFORM_TEMPLATE_PATH . '/admin/forms/list.php';
36: }
37:
38: protected function enqueueScripts()
39: {
40: parent::enqueueScripts();
41:
42: wp_enqueue_script('quform-forms', Quform::adminUrl('js/forms.list.min.js'), array('jquery'), QUFORM_VERSION, true);
43:
44: wp_localize_script('quform-forms', 'quformFormsListL10n', array(
45: 'singleConfirmDelete' => __('Are you sure you want to delete this form? All saved settings, elements and entries for this form will be lost and this cannot be undone.', 'quform'),
46: 'pluralConfirmDelete' => __('Are you sure you want to delete these forms? All saved settings, elements and entries for these forms will be lost and this cannot be undone.', 'quform'),
47: 'saveFormsTableSettingsNonce' => wp_create_nonce('quform_save_forms_table_settings'),
48: 'addFormNonce' => wp_create_nonce('quform_add_form'),
49: 'errorAddingForm' => __('An error occurred adding the form', 'quform')
50: ));
51: }
52:
53: 54: 55:
56: public function process()
57: {
58: if ( ! current_user_can('quform_list_forms')) {
59: wp_die(__( 'You do not have sufficient permissions to access this page.', 'quform'), 403);
60: }
61:
62: $this->processActions();
63:
64: $this->addPageMessages();
65:
66: $this->table->prepare_items();
67:
68: $perPage = get_user_meta(get_current_user_id(), 'quform_forms_per_page', true);
69: if ( ! is_numeric($perPage)) {
70: $perPage = 20;
71: }
72:
73: $this->view->with(array(
74: 'table' => $this->table,
75: 'perPage' => $perPage
76: ));
77:
78: add_filter('removable_query_args', array($this, 'removableQueryArgs'));
79: }
80:
81: 82: 83:
84: protected function processActions()
85: {
86: $nonce = Quform::get($_GET, '_wpnonce');
87: $action = null;
88: $ids = array();
89:
90: if (isset($_GET['id'])) {
91: $action = Quform::get($_GET, 'action');
92: $ids = (int) $_GET['id'];
93: } elseif (isset($_GET['ids'])) {
94: $action = $this->getBulkAction();
95: $ids = (array) Quform::get($_GET, 'ids');
96: $ids = array_map('intval', $ids);
97: } elseif (isset($_GET['delete_all'])) {
98: $action = 'delete_all';
99: }
100:
101: if ($action == null) {
102: if (Quform::get($_GET, '_wp_http_referer')) {
103: wp_safe_redirect(esc_url_raw(remove_query_arg(array('_wp_http_referer', '_wpnonce'), wp_unslash($_SERVER['REQUEST_URI']))));
104: exit;
105: }
106:
107: return;
108: }
109:
110: $returnUrl = remove_query_arg(array('action', 'action2', 'id', 'ids', 'activated', 'deactivated', 'duplicated', 'trashed', 'restored', 'deleted', 'error'), wp_get_referer());
111:
112: switch ($action) {
113: case 'activate':
114: $result = $this->processActivateAction($ids, $nonce);
115: $returnUrl = add_query_arg($result, $returnUrl);
116: break;
117: case 'deactivate':
118: $result = $this->processDeactivateAction($ids, $nonce);
119: $returnUrl = add_query_arg($result, $returnUrl);
120: break;
121: case 'duplicate':
122: $result = $this->processDuplicateAction($ids, $nonce);
123: $returnUrl = add_query_arg($result, $returnUrl);
124: break;
125: case 'trash':
126: $result = $this->processTrashAction($ids, $nonce);
127: $returnUrl = add_query_arg($result, $returnUrl);
128: break;
129: case 'untrash':
130: $result = $this->processUntrashAction($ids, $nonce);
131: $returnUrl = add_query_arg($result, $returnUrl);
132: break;
133: case 'delete':
134: $result = $this->processDeleteAction($ids, $nonce);
135: $returnUrl = add_query_arg($result, $returnUrl);
136: break;
137: case 'delete_all':
138: $ids = $this->repository->getTrashedFormIds();
139: $result = $this->processDeleteAction($ids, $nonce);
140: $returnUrl = add_query_arg($result, $returnUrl);
141: break;
142: }
143:
144: wp_safe_redirect(esc_url_raw($returnUrl));
145: exit;
146: }
147:
148: 149: 150: 151: 152: 153: 154:
155: protected function processActivateAction($ids, $nonce)
156: {
157: if (is_array($ids)) {
158: $nonceAction = 'bulk-qfb-forms';
159: } else {
160: $nonceAction = 'quform_activate_form_' . $ids;
161: $ids = array($ids);
162: }
163:
164: if ( ! $nonce || ! count($ids)) {
165: return array('error' => self::BAD_REQUEST);
166: }
167:
168: if ( ! current_user_can('quform_edit_forms')) {
169: return array('error' => self::NO_PERMISSION);
170: }
171:
172: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
173: return array('error' => self::NONCE_CHECK_FAILED);
174: }
175:
176: $count = $this->repository->activateForms($ids);
177:
178: $this->scriptLoader->handleActivateForms($ids);
179:
180: return array('activated' => $count);
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: protected function processDeactivateAction($ids, $nonce)
191: {
192: if (is_array($ids)) {
193: $nonceAction = 'bulk-qfb-forms';
194: } else {
195: $nonceAction = 'quform_deactivate_form_' . $ids;
196: $ids = array($ids);
197: }
198:
199: if ( ! $nonce || ! count($ids)) {
200: return array('error' => self::BAD_REQUEST);
201: }
202:
203: if ( ! current_user_can('quform_edit_forms')) {
204: return array('error' => self::NO_PERMISSION);
205: }
206:
207: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
208: return array('error' => self::NONCE_CHECK_FAILED);
209: }
210:
211: $count = $this->repository->deactivateForms($ids);
212:
213: $this->scriptLoader->handleDeactivateForms($ids);
214:
215: return array('deactivated' => $count);
216: }
217:
218: 219: 220: 221: 222: 223: 224:
225: protected function processDuplicateAction($ids, $nonce)
226: {
227: if (is_array($ids)) {
228: $nonceAction = 'bulk-qfb-forms';
229: } else {
230: $nonceAction = 'quform_duplicate_form_' . $ids;
231: $ids = array($ids);
232: }
233:
234: if ( ! $nonce || ! count($ids)) {
235: return array('error' => self::BAD_REQUEST);
236: }
237:
238: if ( ! current_user_can('quform_add_forms')) {
239: return array('error' => self::NO_PERMISSION);
240: }
241:
242: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
243: return array('error' => self::NONCE_CHECK_FAILED);
244: }
245:
246: $newIds = $this->repository->duplicateForms($ids);
247:
248: $this->scriptLoader->handleDuplicateForms($newIds);
249:
250: return array('duplicated' => count($newIds));
251: }
252:
253: 254: 255: 256: 257: 258: 259:
260: protected function processTrashAction($ids, $nonce)
261: {
262: if (is_array($ids)) {
263: $nonceAction = 'bulk-qfb-forms';
264: } else {
265: $nonceAction = 'quform_trash_form_' . $ids;
266: $ids = array($ids);
267: }
268:
269: if ( ! $nonce || ! count($ids)) {
270: return array('error' => self::BAD_REQUEST);
271: }
272:
273: if ( ! current_user_can('quform_delete_forms')) {
274: return array('error' => self::NO_PERMISSION);
275: }
276:
277: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
278: return array('error' => self::NONCE_CHECK_FAILED);
279: }
280:
281: $count = $this->repository->trashForms($ids);
282:
283: $this->scriptLoader->handleTrashForms($ids);
284:
285: return array('trashed' => $count);
286: }
287:
288: 289: 290: 291: 292: 293: 294:
295: protected function processUntrashAction($ids, $nonce)
296: {
297: if (is_array($ids)) {
298: $nonceAction = 'bulk-qfb-forms';
299: } else {
300: $nonceAction = 'quform_untrash_form_' . $ids;
301: $ids = array($ids);
302: }
303:
304: if ( ! $nonce || ! count($ids)) {
305: return array('error' => self::BAD_REQUEST);
306: }
307:
308: if ( ! current_user_can('quform_delete_forms')) {
309: return array('error' => self::NO_PERMISSION);
310: }
311:
312: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
313: return array('error' => self::NONCE_CHECK_FAILED);
314: }
315:
316: $count = $this->repository->untrashForms($ids);
317:
318: $this->scriptLoader->handleUntrashForms($ids);
319:
320: return array('untrashed' => $count);
321: }
322:
323: 324: 325: 326: 327: 328: 329:
330: protected function processDeleteAction($ids, $nonce)
331: {
332: if (is_array($ids)) {
333: $nonceAction = 'bulk-qfb-forms';
334: } else {
335: $nonceAction = 'quform_delete_form_' . $ids;
336: $ids = array($ids);
337: }
338:
339: if ( ! $nonce || ! count($ids)) {
340: return array('error' => self::BAD_REQUEST);
341: }
342:
343: if ( ! current_user_can('quform_delete_forms')) {
344: return array('error' => self::NO_PERMISSION);
345: }
346:
347: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
348: return array('error' => self::NONCE_CHECK_FAILED);
349: }
350:
351: $count = $this->repository->deleteForms($ids);
352:
353: return array('deleted' => $count);
354: }
355:
356: 357: 358:
359: protected function getBulkAction()
360: {
361: $action = null;
362:
363: $a1 = Quform::get($_GET, 'action', '-1');
364: $a2 = Quform::get($_GET, 'action2', '-1');
365:
366: if ($a1 != '-1') {
367: $action = $a1;
368: } elseif ($a2 != '-1') {
369: $action = $a2;
370: }
371:
372: return $action;
373: }
374:
375: 376: 377:
378: protected function addPageMessages()
379: {
380: $activated = (int) Quform::get($_GET, 'activated');
381: if ($activated > 0) {
382:
383: $this->addMessage('success', sprintf(_n('%s form activated', '%s forms activated', $activated, 'quform'), number_format_i18n($activated)));
384: }
385:
386: $deactivated = (int) Quform::get($_GET, 'deactivated');
387: if ($deactivated > 0) {
388:
389: $this->addMessage('success', sprintf(_n('%s form deactivated', '%s forms deactivated', $deactivated, 'quform'), number_format_i18n($deactivated)));
390: }
391:
392: $duplicated = (int) Quform::get($_GET, 'duplicated');
393: if ($duplicated > 0) {
394:
395: $this->addMessage('success', sprintf(_n('%s form duplicated', '%s forms duplicated', $duplicated, 'quform'), number_format_i18n($duplicated)));
396: }
397:
398: $trashed = (int) Quform::get($_GET, 'trashed');
399: if ($trashed > 0) {
400:
401: $this->addMessage('success', sprintf(_n('%s form moved to the Trash', '%s forms moved to the Trash', $trashed, 'quform'), number_format_i18n($trashed)));
402: }
403:
404: $untrashed = (int) Quform::get($_GET, 'untrashed');
405: if ($untrashed > 0) {
406:
407: $this->addMessage('success', sprintf(_n('%s form restored', '%s forms restored', $untrashed, 'quform'), number_format_i18n($untrashed)));
408: }
409:
410: $deleted = (int) Quform::get($_GET, 'deleted');
411: if ($deleted > 0) {
412:
413: $this->addMessage('success', sprintf(_n('%s form deleted', '%s forms deleted', $deleted, 'quform'), number_format_i18n($deleted)));
414: }
415:
416: switch ((int) Quform::get($_GET, 'error')) {
417: case self::BAD_REQUEST:
418: $this->addMessage('error', __('Bad request.', 'quform'));
419: break;
420: case self::NO_PERMISSION:
421: $this->addMessage('error', __('You do not have permission to do this.', 'quform'));
422: break;
423: case self::NONCE_CHECK_FAILED:
424: $this->addMessage('error', __('Nonce check failed.', 'quform'));
425: break;
426: }
427: }
428:
429: 430: 431: 432: 433: 434:
435: public function removableQueryArgs($args)
436: {
437: $args[] = 'deactivated';
438: $args[] = 'duplicated';
439:
440: return $args;
441: }
442:
443: 444: 445: 446: 447: 448: 449:
450: public function getNavHtml(array $currentForm = null, array $extra = array())
451: {
452: $extra[40] = sprintf('<div class="qfb-nav-item qfb-nav-page-info"><i class="qfb-nav-page-icon qfb-mdi qfb-mdi-view_stream"></i><span class="qfb-nav-page-title">%s</span></div>', esc_html__('Forms', 'quform'));
453: $extra[50] = '<div class="qfb-nav-item qfb-nav-item-right"><a id="qfb-show-forms-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: