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