1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Form_List_Table extends WP_List_Table
  7: {
  8:       9:  10: 
 11:     protected $repository;
 12: 
 13:      14:  15: 
 16:     protected $options;
 17: 
 18:      19:  20: 
 21:     protected $view;
 22: 
 23:      24:  25:  26: 
 27:     public function __construct(Quform_Repository $repository, Quform_Options $options)
 28:     {
 29:         parent::__construct(array(
 30:             'singular' => 'qfb-form',
 31:             'plural' => 'qfb-forms'
 32:         ));
 33: 
 34:         $this->repository = $repository;
 35:         $this->options = $options;
 36:     }
 37: 
 38:      39:  40: 
 41:     public function prepare_items()
 42:     {
 43:         $this->view = Quform::get($_GET, 'view');
 44:         $perPage = $this->get_items_per_page('quform_forms_per_page');
 45: 
 46:         $args = array(
 47:             'active' => null,
 48:             'orderby' => $this->getOrderBy(strtolower((string) Quform::get($_GET, 'orderby'))),
 49:             'order' => $this->getOrder(strtolower((string) Quform::get($_GET, 'order'))),
 50:             'trashed' => false,
 51:             'limit' => $perPage,
 52:             'offset' => ($this->get_pagenum() - 1) * $perPage,
 53:             'search' => isset($_GET['s']) && Quform::isNonEmptyString($_GET['s']) ? wp_unslash($_GET['s']) : ''
 54:         );
 55: 
 56:         switch ($this->view) {
 57:             case 'active':
 58:                 $args['active'] = true;
 59:                 break;
 60:             case 'inactive':
 61:                 $args['active'] = false;
 62:                 break;
 63:             case 'trashed':
 64:                 $args['trashed'] = true;
 65:                 break;
 66:         }
 67: 
 68:         $this->items = $this->repository->getForms($args);
 69: 
 70:         $foundItems = $this->repository->getFoundRows();
 71: 
 72:         $this->set_pagination_args(array(
 73:             'total_items' => $foundItems,
 74:             'total_pages' => ceil($foundItems / $args['limit']),
 75:             'per_page' => $args['limit']
 76:         ));
 77:     }
 78: 
 79:      80:  81: 
 82:     public function views()
 83:     {
 84:         $views = $this->get_views();
 85: 
 86:         if (empty($views)) {
 87:             return;
 88:         }
 89: 
 90:         echo '<div class="qfb-sub-nav qfb-cf">';
 91:         echo '<ul class="qfb-sub-nav-ul">';
 92: 
 93:         foreach ($views as $class => $view) {
 94:             printf('<li class="qfb-view-%s">%s</li>', $class, $view);
 95:         }
 96: 
 97:         echo '</ul>';
 98:         echo '</div>';
 99:     }
100: 
101:     102: 103: 104: 105: 
106:     protected function get_views()
107:     {
108:         $isSearch = isset($_GET['s']) && Quform::isNonEmptyString($_GET['s']);
109:         $views = array();
110: 
111:         $views['all'] = sprintf(
112:             '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>',
113:             esc_url(admin_url('admin.php?page=quform.forms')),
114:             $this->view === null && !$isSearch ? 'qfb-current' : '',
115:             esc_html__('All', 'quform'),
116:             number_format_i18n($this->repository->count())
117:         );
118: 
119:         $views['active'] = sprintf(
120:             '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>',
121:             esc_url(admin_url('admin.php?page=quform.forms&view=active')),
122:             $this->view === 'active' && !$isSearch ? 'qfb-current' : '',
123:             esc_html__('Active', 'quform'),
124:             number_format_i18n($this->repository->count(true))
125:         );
126: 
127:         $views['inactive'] = sprintf(
128:             '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>',
129:             esc_url(admin_url('admin.php?page=quform.forms&view=inactive')),
130:             $this->view === 'inactive' && !$isSearch ? 'qfb-current' : '',
131:             esc_html__('Inactive', 'quform'),
132:             number_format_i18n($this->repository->count(false))
133:         );
134: 
135:         $views['trash'] = sprintf(
136:             '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>',
137:             esc_url(admin_url('admin.php?page=quform.forms&view=trashed')),
138:             $this->view === 'trashed' && !$isSearch ? 'qfb-current' : '',
139:             esc_html__('Trash', 'quform'),
140:             number_format_i18n($this->repository->count(null, true))
141:         );
142: 
143:         if ($isSearch) {
144:             $views['search'] = sprintf(
145:                 '<a class="qfb-current">%s <span class="count">(%s)</span></a>',
146:                 esc_html(sprintf(__('Search results for “%s”', 'quform'), wp_unslash($_GET['s']))),
147:                 number_format_i18n($this->_pagination_args['total_items'])
148:             );
149:         }
150: 
151:         return $views;
152:     }
153: 
154:     155: 156: 157: 158: 
159:     public function get_columns()
160:     {
161:         return array(
162:             'cb' => '<input type="checkbox" />',
163:             'name' => esc_html__('Name', 'quform'),
164:             'shortcode' => '',
165:             'entries' => esc_html__('Entries', 'quform'),
166:             'active' => esc_html__('Active', 'quform'),
167:             'updated_at' => esc_html__('Last modified', 'quform')
168:         );
169:     }
170: 
171:     172: 173: 174: 175: 176: 
177:     protected function column_cb($item)
178:     {
179:         return sprintf('<input type="checkbox" name="ids[]" value="%s" />', $item['id']);
180:     }
181: 
182:     183: 184: 185: 186: 187: 
188:     protected function column_name($item)
189:     {
190:         $output = '<strong>';
191: 
192:         if (current_user_can('quform_edit_forms') && $item['trashed'] != '1') {
193:             $output .= sprintf(
194:                 '<a href="%s" aria-label="%s">%s</a>',
195:                 esc_url(add_query_arg(array('id' => $item['id']), admin_url('admin.php?page=quform.forms&sp=edit'))),
196:                 
197:                 sprintf(esc_attr__('Edit form “%s”', 'quform'), Quform::escape($item['name'])),
198:                 Quform::escape($item['name'])
199:             );
200:         } else {
201:             $output .= Quform::escape($item['name']);
202:         }
203: 
204:         $output .= '</strong>';
205: 
206:         return $output;
207:     }
208: 
209:     210: 211: 212: 213: 214: 
215:     protected function column_shortcode($item)
216:     {
217:         $shortcode = sprintf('[quform id="%s" name="%s"]', $item['id'], $item['name']);
218: 
219:         $output = sprintf(
220:             '<input type="text" value="%s" size="%s" readonly>',
221:             Quform::escape($shortcode),
222:             esc_attr(Quform::strlen($shortcode))
223:         );
224: 
225:         return $output;
226:     }
227: 
228:     229: 230: 231: 232: 233: 234: 235: 
236:     protected function handle_row_actions($item, $column_name, $primary)
237:     {
238:         if ($column_name != $primary) {
239:             return '';
240:         }
241: 
242:         $actions = array();
243: 
244:         if ($item['trashed'] == '0') {
245:             if (current_user_can('quform_edit_forms')) {
246:                 $actions['edit'] = sprintf(
247:                     '<a href="%s" aria-label="%s">%s</a>',
248:                     esc_url(add_query_arg(array('id' => $item['id']), admin_url('admin.php?page=quform.forms&sp=edit'))),
249:                     sprintf(esc_attr__('Edit form “%s”', 'quform'), Quform::escape($item['name'])),
250:                     esc_html__('Edit', 'quform')
251:                 );
252:             }
253: 
254:             if (current_user_can('quform_view_entries')) {
255:                 $actions['entries'] = sprintf(
256:                     '<a href="%s" aria-label="%s">%s</a>',
257:                     esc_url(add_query_arg(array('id' => $item['id']), admin_url('admin.php?page=quform.entries'))),
258:                     
259:                     sprintf(esc_attr__('View submitted entries for “%s”', 'quform'), Quform::escape($item['name'])),
260:                     esc_html__('Entries', 'quform')
261:                 );
262:             }
263: 
264:             if (current_user_can('quform_edit_forms')) {
265:                 if ($item['active'] == '1') {
266:                     $deactivateUrl = admin_url('admin.php?page=quform.forms&action=deactivate');
267:                     $deactivateNonce = wp_create_nonce('quform_deactivate_form_' . $item['id']);
268: 
269:                     $actions['deactivate'] = sprintf(
270:                         '<a href="%s" aria-label="%s">%s</a>',
271:                         esc_url(add_query_arg(array('id' => $item['id'], '_wpnonce' => $deactivateNonce), $deactivateUrl)),
272:                         
273:                         sprintf(esc_attr__('Deactivate form “%s”', 'quform'), Quform::escape($item['name'])),
274:                         esc_html__('Deactivate', 'quform')
275:                     );
276:                 } else {
277:                     $activateUrl = admin_url('admin.php?page=quform.forms&action=activate');
278:                     $activateNonce = wp_create_nonce('quform_activate_form_' . $item['id']);
279: 
280:                     $actions['activate'] = sprintf(
281:                         '<a href="%s" aria-label="%s">%s</a>',
282:                         esc_url(add_query_arg(array('id' => $item['id'], '_wpnonce' => $activateNonce), $activateUrl)),
283:                         
284:                         sprintf(esc_attr__('Activate form “%s”', 'quform'), Quform::escape($item['name'])),
285:                         esc_html__('Activate', 'quform')
286:                     );
287:                 }
288:             }
289: 
290:             if (current_user_can('quform_add_forms')) {
291:                 $duplicateUrl = admin_url('admin.php?page=quform.forms&action=duplicate');
292:                 $duplicateNonce = wp_create_nonce('quform_duplicate_form_' . $item['id']);
293: 
294:                 $actions['duplicate'] = sprintf(
295:                     '<a href="%s" aria-label="%s">%s</a>',
296:                     esc_url(add_query_arg(array('id' => $item['id'], '_wpnonce' => $duplicateNonce), $duplicateUrl)),
297:                     
298:                     sprintf(esc_attr__('Duplicate form “%s”', 'quform'), Quform::escape($item['name'])),
299:                     esc_html__('Duplicate', 'quform')
300:                 );
301:             }
302: 
303:             if (current_user_can('quform_delete_forms')) {
304:                 $trashUrl = admin_url('admin.php?page=quform.forms&action=trash');
305:                 $trashNonce = wp_create_nonce('quform_trash_form_' . $item['id']);
306: 
307:                 $actions['trash'] = sprintf(
308:                     '<a href="%s" aria-label="%s">%s</a>',
309:                     esc_url(add_query_arg(array('id' => $item['id'], '_wpnonce' => $trashNonce), $trashUrl)),
310:                     
311:                     sprintf(esc_attr__('Move form “%s” to the Trash', 'quform'), Quform::escape($item['name'])),
312:                     esc_html__('Trash', 'quform')
313:                 );
314:             }
315:         } else {
316:             if (current_user_can('quform_delete_forms')) {
317:                 $untrashUrl = admin_url('admin.php?page=quform.forms&action=untrash');
318:                 $untrashNonce = wp_create_nonce('quform_untrash_form_' . $item['id']);
319: 
320:                 $actions['untrash'] = sprintf(
321:                     '<a href="%s" aria-label="%s">%s</a>',
322:                     esc_url(add_query_arg(array('id' => $item['id'], '_wpnonce' => $untrashNonce), $untrashUrl)),
323:                     
324:                     sprintf(esc_attr__('Restore form “%s” from the Trash', 'quform'), Quform::escape($item['name'])),
325:                     esc_html__('Restore', 'quform')
326:                 );
327: 
328:                 $deleteUrl = admin_url('admin.php?page=quform.forms&action=delete');
329:                 $deleteNonce = wp_create_nonce('quform_delete_form_' . $item['id']);
330: 
331:                 $actions['delete'] = sprintf(
332:                     '<a href="%s" aria-label="%s">%s</a>',
333:                     esc_url(add_query_arg(array('id' => $item['id'], '_wpnonce' => $deleteNonce), $deleteUrl)),
334:                     
335:                     sprintf(esc_attr__('Delete form “%s” permanently', 'quform'), Quform::escape($item['name'])),
336:                     esc_html__('Delete permanently', 'quform')
337:                 );
338:             }
339:         }
340: 
341:         return $this->row_actions($actions);
342:     }
343: 
344:     345: 346: 347: 348: 349: 
350:     protected function column_entries($item)
351:     {
352:         if ($item['unread'] > 0) {
353:             $count = sprintf(
354:                 '<strong>%s (%s)</strong>',
355:                 
356:                 esc_html(sprintf(_n('%s unread', '%s unread', $item['unread'], 'quform'), number_format_i18n($item['unread']))),
357:                 
358:                 esc_html(sprintf(_n('%s entry', '%s entries', $item['entries'], 'quform'), number_format_i18n($item['entries'])))
359:             );
360:         } else {
361:             
362:             $count = esc_html(sprintf(_n('%s entry', '%s entries', $item['entries'], 'quform'), number_format_i18n($item['entries'])));
363:         }
364: 
365:         if (current_user_can('quform_view_entries')) {
366:             $output = sprintf(
367:                 '<a href="%s">%s</a>',
368:                 esc_url(admin_url('admin.php?page=quform.entries&id=' . $item['id'])),
369:                 $count
370:             );
371:         } else {
372:             $output = $count;
373:         }
374: 
375:         return $output;
376:     }
377: 
378:     379: 380: 381: 382: 383: 
384:     protected function column_active($item)
385:     {
386:         return $item['active'] == '1' ? esc_html__('Yes', 'quform') : esc_html__('No', 'quform');
387:     }
388: 
389:     390: 391: 392: 393: 394: 
395:     protected function column_updated_at($item)
396:     {
397:         return esc_html($this->options->formatDate($item['updated_at'], true));
398:     }
399: 
400:     401: 402: 403: 404: 
405:     protected function get_sortable_columns()
406:     {
407:         $orderBy = $this->getOrderBy();
408:         $isAsc = $this->getOrder() == 'asc';
409: 
410:         return array(
411:             'name' => array('name', $orderBy == 'name' && $isAsc),
412:             'shortcode' => array('id', $orderBy == 'id' && $isAsc),
413:             'entries' => array('entries', ! ($orderBy == 'entries' && ! $isAsc)), 
414:             'active' => array('active', $orderBy == 'active' && $isAsc),
415:             'updated_at' => array('updated_at', ! ($orderBy == 'updated_at' && ! $isAsc)) 
416:         );
417:     }
418: 
419:     420: 421: 422: 423: 424: 
425:     protected function get_bulk_actions()
426:     {
427:         $actions = array();
428: 
429:         if ($this->view == 'trashed') {
430:             if (current_user_can('quform_delete_forms')) {
431:                 $actions['untrash'] = __('Restore', 'quform');
432:                 $actions['delete'] = __('Delete permanently', 'quform');
433:             }
434:         } else {
435:             if (current_user_can('quform_edit_forms')) {
436:                 $actions['activate'] = __('Activate', 'quform');
437:                 $actions['deactivate'] = __('Deactivate', 'quform');
438:             }
439: 
440:             if (current_user_can('quform_add_forms')) {
441:                 $actions['duplicate'] = __('Duplicate', 'quform');
442:             }
443: 
444:             if (current_user_can('quform_delete_forms')) {
445:                 $actions['trash'] = __('Move to Trash', 'quform');
446:             }
447:         }
448: 
449:         return $actions;
450:     }
451: 
452:     453: 454: 
455:     public function no_items() {
456:         if (isset($_GET['s']) && Quform::isNonEmptyString($_GET['s'])) {
457:             esc_html_e('Your search did not match any forms.', 'quform');
458:         } elseif ($this->view == 'trashed') {
459:             esc_html_e('No forms found in Trash.', 'quform');
460:         } elseif (current_user_can('quform_add_forms')) {
461:             printf(
462:                 
463:                 esc_html__('No forms found, %1$sclick here%2$s to create one.', 'quform'),
464:                 sprintf('<a href="%s">', esc_url(admin_url('admin.php?page=quform.forms&sp=add'))),
465:                 '</a>'
466:             );
467:         } else {
468:             esc_html_e('No forms found.', 'quform');
469:         }
470:     }
471: 
472:     473: 474: 475: 476: 477: 478: 479: 
480:     public function search_box( $text, $input_id ) {
481:         $input_id = $input_id . '-search-input';
482: 
483:         if ( ! empty( $_REQUEST['orderby'] ) )
484:             echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
485:         if ( ! empty( $_REQUEST['order'] ) )
486:             echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
487:         if ( ! empty( $_REQUEST['post_mime_type'] ) )
488:             echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
489:         if ( ! empty( $_REQUEST['detached'] ) )
490:             echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
491:         ?>
492:         <p class="search-box">
493:             <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
494:             <input type="search" id="<?php echo esc_attr( $input_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" />
495:             <?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
496:         </p>
497:         <?php
498:     }
499: 
500:     501: 502: 503: 504: 505: 506: 507: 
508:     protected function getOrderBy($requestedOrderBy = '')
509:     {
510:         $currentUserId = get_current_user_id();
511:         $userOrderBy = get_user_meta($currentUserId, 'quform_forms_order_by', true);
512: 
513:         if (Quform::isNonEmptyString($requestedOrderBy)) {
514:             $orderBy = $requestedOrderBy;
515: 
516:             if ($requestedOrderBy != $userOrderBy) {
517:                 update_user_meta($currentUserId, 'quform_forms_order_by', $requestedOrderBy);
518:             }
519:         } elseif (Quform::isNonEmptyString($userOrderBy)) {
520:             $orderBy = $userOrderBy;
521:         } else {
522:             $orderBy = 'updated_at';
523:         }
524: 
525:         return $orderBy;
526:     }
527: 
528:     529: 530: 531: 532: 533: 534: 535: 
536:     protected function getOrder($requestedOrder = '')
537:     {
538:         $currentUserId = get_current_user_id();
539:         $userOrder = get_user_meta($currentUserId, 'quform_forms_order', true);
540: 
541:         if (Quform::isNonEmptyString($requestedOrder)) {
542:             $order = $requestedOrder;
543: 
544:             if ($requestedOrder != $userOrder) {
545:                 update_user_meta($currentUserId, 'quform_forms_order', $requestedOrder);
546:             }
547:         } elseif (Quform::isNonEmptyString($userOrder)) {
548:             $order = $userOrder;
549:         } else {
550:             $order = 'desc';
551:         }
552: 
553:         return $order;
554:     }
555: 
556:     557: 558: 559: 560: 
561:     protected function extra_tablenav($which)
562:     {
563:         if ($which == 'top' && $this->view == 'trashed' && count($this->items) && current_user_can('quform_delete_forms')) {
564:             submit_button(__('Empty Trash', 'quform'), 'apply', 'delete_all', false);
565:         }
566:     }
567: }
568: