<?php

/**
 * Displays the Email Petitions table page
 */
function dk_speakout_petitions_page() {
	// check security: ensure user has authority
	if ( ! current_user_can( 'publish_posts' ) ) wp_die( 'Insufficient privileges: You need to be an editor to do that.' );

	include_once( 'class.speakout.php' );
	include_once( 'class.petition.php' );
	include_once( 'class.wpml.php' );
	$the_petitions = new dk_speakout_Petition();
	$wpml          = new dk_speakout_WPML();
	$action        = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : '';
	$id            = isset( $_REQUEST['id'] ) ? sanitize_text_field( $_REQUEST['id'] ) : '';
	$options       = get_option( 'dk_speakout_options' );

	// set variables for paged record display and limit values in db query
	// request values may be submitted either by html links (pagination.php) or by javascript (admin.js)
	$paged        = isset( $_REQUEST['paged'] ) ? sanitize_text_field( $_REQUEST['paged'] ): '1';
	$total_pages  = isset ( $_REQUEST['total_pages'] ) ? sanitize_text_field( $_REQUEST['total_pages'] ) : '1';
	$current_page = dk_speakout_SpeakOut::current_paged( $paged, $total_pages );
	$query_limit  = $options['petitions_rows'] > "" ? $options['petitions_rows'] : 50 ;
	$query_start  = ( $current_page * $query_limit ) - $query_limit;

	// link URL for "Add New" button in header
	$addnew_url = esc_url( admin_url( 'admin.php?page=dk_speakout_addnew_page' ) );
	
	// define a single, reliable base_url for pagination links
	$base_url = remove_query_arg( array( 'paged', 'total_pages', 'action', 'id', '_wpnonce' ) );

	switch ( $action ) {

		case 'delete' :
			// security: ensure user has intention
			check_admin_referer( 'dk_speakout-delete_petition' . $id );

			// delete the petition and its signatures
			$the_petitions->delete( $id );
			$wpml->unregister_petition( $id );

			// get petitions
			$petitions = $the_petitions->all( $query_start, $query_limit );

			// set up page display variables
			$page_title     = __( 'Petitions', 'speakout' );
			$count          = $the_petitions->count();
			$message_update = __( 'Petition deleted.', 'speakout' );

			break;

		default :
			// get petitions
			//if Pro, put newest at top, if free, put 1st (and only editable) at top
			$listOrder = get_option( 'dk_speakout_license_key_verified' ) == 1 ?"DESC":"ASC";
			$petitions = $the_petitions->all( $query_start, $query_limit, $listOrder );

			// set up page display variables
			$page_title     = __( 'Petitions', 'speakout' );
			$count          = $the_petitions->count();
			$message_update = '';
	}

	// display the Petitions table
	include_once( 'petitions.view.php' );
}

?>