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:
89: if (isset($_GET['id'])) {
90: $action = Quform::get($_GET, 'action');
91: $ids = (int) $_GET['id'];
92: } elseif (isset($_GET['ids'])) {
93: $action = $this->getBulkAction();
94: $ids = (array) Quform::get($_GET, 'ids');
95: $ids = array_map('intval', $ids);
96: }
97:
98: if ($action == null) {
99: if (Quform::get($_GET, '_wp_http_referer')) {
100: wp_safe_redirect(esc_url_raw(remove_query_arg(array('_wp_http_referer', '_wpnonce'), wp_unslash($_SERVER['REQUEST_URI']))));
101: exit;
102: }
103:
104: return;
105: }
106:
107: $returnUrl = remove_query_arg(array('action', 'action2', 'id', 'ids', 'activated', 'deactivated', 'duplicated', 'trashed', 'restored', 'deleted', 'error'), wp_get_referer());
108:
109: switch ($action) {
110: case 'activate':
111: $result = $this->processActivateAction($ids, $nonce);
112: $returnUrl = add_query_arg($result, $returnUrl);
113: break;
114: case 'deactivate':
115: $result = $this->processDeactivateAction($ids, $nonce);
116: $returnUrl = add_query_arg($result, $returnUrl);
117: break;
118: case 'duplicate':
119: $result = $this->processDuplicateAction($ids, $nonce);
120: $returnUrl = add_query_arg($result, $returnUrl);
121: break;
122: case 'trash':
123: $result = $this->processTrashAction($ids, $nonce);
124: $returnUrl = add_query_arg($result, $returnUrl);
125: break;
126: case 'untrash':
127: $result = $this->processUntrashAction($ids, $nonce);
128: $returnUrl = add_query_arg($result, $returnUrl);
129: break;
130: case 'delete':
131: $result = $this->processDeleteAction($ids, $nonce);
132: $returnUrl = add_query_arg($result, $returnUrl);
133: break;
134: }
135:
136: wp_safe_redirect(esc_url_raw($returnUrl));
137: exit;
138: }
139:
140: 141: 142: 143: 144: 145: 146:
147: protected function processActivateAction($ids, $nonce)
148: {
149: if (is_array($ids)) {
150: $nonceAction = 'bulk-qfb-forms';
151: } else {
152: $nonceAction = 'quform_activate_form_' . $ids;
153: $ids = array($ids);
154: }
155:
156: if ( ! $nonce || ! count($ids)) {
157: return array('error' => self::BAD_REQUEST);
158: }
159:
160: if ( ! current_user_can('quform_edit_forms')) {
161: return array('error' => self::NO_PERMISSION);
162: }
163:
164: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
165: return array('error' => self::NONCE_CHECK_FAILED);
166: }
167:
168: $count = $this->repository->activateForms($ids);
169:
170: $this->scriptLoader->handleActivateForms($ids);
171:
172: return array('activated' => $count);
173: }
174:
175: 176: 177: 178: 179: 180: 181:
182: protected function processDeactivateAction($ids, $nonce)
183: {
184: if (is_array($ids)) {
185: $nonceAction = 'bulk-qfb-forms';
186: } else {
187: $nonceAction = 'quform_deactivate_form_' . $ids;
188: $ids = array($ids);
189: }
190:
191: if ( ! $nonce || ! count($ids)) {
192: return array('error' => self::BAD_REQUEST);
193: }
194:
195: if ( ! current_user_can('quform_edit_forms')) {
196: return array('error' => self::NO_PERMISSION);
197: }
198:
199: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
200: return array('error' => self::NONCE_CHECK_FAILED);
201: }
202:
203: $count = $this->repository->deactivateForms($ids);
204:
205: $this->scriptLoader->handleDeactivateForms($ids);
206:
207: return array('deactivated' => $count);
208: }
209:
210: 211: 212: 213: 214: 215: 216:
217: protected function processDuplicateAction($ids, $nonce)
218: {
219: if (is_array($ids)) {
220: $nonceAction = 'bulk-qfb-forms';
221: } else {
222: $nonceAction = 'quform_duplicate_form_' . $ids;
223: $ids = array($ids);
224: }
225:
226: if ( ! $nonce || ! count($ids)) {
227: return array('error' => self::BAD_REQUEST);
228: }
229:
230: if ( ! current_user_can('quform_add_forms')) {
231: return array('error' => self::NO_PERMISSION);
232: }
233:
234: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
235: return array('error' => self::NONCE_CHECK_FAILED);
236: }
237:
238: $newIds = $this->repository->duplicateForms($ids);
239:
240: $this->scriptLoader->handleDuplicateForms($newIds);
241:
242: return array('duplicated' => count($newIds));
243: }
244:
245: 246: 247: 248: 249: 250: 251:
252: protected function processTrashAction($ids, $nonce)
253: {
254: if (is_array($ids)) {
255: $nonceAction = 'bulk-qfb-forms';
256: } else {
257: $nonceAction = 'quform_trash_form_' . $ids;
258: $ids = array($ids);
259: }
260:
261: if ( ! $nonce || ! count($ids)) {
262: return array('error' => self::BAD_REQUEST);
263: }
264:
265: if ( ! current_user_can('quform_delete_forms')) {
266: return array('error' => self::NO_PERMISSION);
267: }
268:
269: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
270: return array('error' => self::NONCE_CHECK_FAILED);
271: }
272:
273: $count = $this->repository->trashForms($ids);
274:
275: $this->scriptLoader->handleTrashForms($ids);
276:
277: return array('trashed' => $count);
278: }
279:
280: 281: 282: 283: 284: 285: 286:
287: protected function processUntrashAction($ids, $nonce)
288: {
289: if (is_array($ids)) {
290: $nonceAction = 'bulk-qfb-forms';
291: } else {
292: $nonceAction = 'quform_untrash_form_' . $ids;
293: $ids = array($ids);
294: }
295:
296: if ( ! $nonce || ! count($ids)) {
297: return array('error' => self::BAD_REQUEST);
298: }
299:
300: if ( ! current_user_can('quform_delete_forms')) {
301: return array('error' => self::NO_PERMISSION);
302: }
303:
304: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
305: return array('error' => self::NONCE_CHECK_FAILED);
306: }
307:
308: $count = $this->repository->untrashForms($ids);
309:
310: $this->scriptLoader->handleUntrashForms($ids);
311:
312: return array('untrashed' => $count);
313: }
314:
315: 316: 317: 318: 319: 320: 321:
322: protected function processDeleteAction($ids, $nonce)
323: {
324: if (is_array($ids)) {
325: $nonceAction = 'bulk-qfb-forms';
326: } else {
327: $nonceAction = 'quform_delete_form_' . $ids;
328: $ids = array($ids);
329: }
330:
331: if ( ! $nonce || ! count($ids)) {
332: return array('error' => self::BAD_REQUEST);
333: }
334:
335: if ( ! current_user_can('quform_delete_forms')) {
336: return array('error' => self::NO_PERMISSION);
337: }
338:
339: if ( ! wp_verify_nonce($nonce, $nonceAction)) {
340: return array('error' => self::NONCE_CHECK_FAILED);
341: }
342:
343: $count = $this->repository->deleteForms($ids);
344:
345: return array('deleted' => $count);
346: }
347:
348: 349: 350:
351: protected function getBulkAction()
352: {
353: $action = null;
354:
355: $a1 = Quform::get($_GET, 'action', '-1');
356: $a2 = Quform::get($_GET, 'action2', '-1');
357:
358: if ($a1 != '-1') {
359: $action = $a1;
360: } elseif ($a2 != '-1') {
361: $action = $a2;
362: }
363:
364: return $action;
365: }
366:
367: 368: 369:
370: protected function addPageMessages()
371: {
372: $activated = (int) Quform::get($_GET, 'activated');
373: if ($activated > 0) {
374:
375: $this->addMessage('success', sprintf(_n('%s form activated', '%s forms activated', $activated, 'quform'), number_format_i18n($activated)));
376: }
377:
378: $deactivated = (int) Quform::get($_GET, 'deactivated');
379: if ($deactivated > 0) {
380:
381: $this->addMessage('success', sprintf(_n('%s form deactivated', '%s forms deactivated', $deactivated, 'quform'), number_format_i18n($deactivated)));
382: }
383:
384: $duplicated = (int) Quform::get($_GET, 'duplicated');
385: if ($duplicated > 0) {
386:
387: $this->addMessage('success', sprintf(_n('%s form duplicated', '%s forms duplicated', $duplicated, 'quform'), number_format_i18n($duplicated)));
388: }
389:
390: $trashed = (int) Quform::get($_GET, 'trashed');
391: if ($trashed > 0) {
392:
393: $this->addMessage('success', sprintf(_n('%s form moved to the Trash', '%s forms moved to the Trash', $trashed, 'quform'), number_format_i18n($trashed)));
394: }
395:
396: $untrashed = (int) Quform::get($_GET, 'untrashed');
397: if ($untrashed > 0) {
398:
399: $this->addMessage('success', sprintf(_n('%s form restored', '%s forms restored', $untrashed, 'quform'), number_format_i18n($untrashed)));
400: }
401:
402: $deleted = (int) Quform::get($_GET, 'deleted');
403: if ($deleted > 0) {
404:
405: $this->addMessage('success', sprintf(_n('%s form deleted', '%s forms deleted', $deleted, 'quform'), number_format_i18n($deleted)));
406: }
407:
408: switch ((int) Quform::get($_GET, 'error')) {
409: case self::BAD_REQUEST:
410: $this->addMessage('error', __('Bad request.', 'quform'));
411: break;
412: case self::NO_PERMISSION:
413: $this->addMessage('error', __('You do not have permission to do this.', 'quform'));
414: break;
415: case self::NONCE_CHECK_FAILED:
416: $this->addMessage('error', __('Nonce check failed.', 'quform'));
417: break;
418: }
419: }
420:
421: 422: 423: 424: 425: 426:
427: public function removableQueryArgs($args)
428: {
429: $args[] = 'deactivated';
430: $args[] = 'duplicated';
431:
432: return $args;
433: }
434:
435: 436: 437: 438: 439: 440: 441:
442: public function getNavHtml(array $currentForm = null, array $extra = array())
443: {
444: $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'));
445: $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>';
446:
447: return parent::getNavHtml($currentForm, $extra);
448: }
449: }
450: