<?php
/**
 * Template: Jobs Listings Content - Jobs Table
 *
 * Override this in your theme by copying it to wp-content/themes/{yourtheme}/matador/jobs-listings-table.php.
 *
 * @link        https://matadorjobs.com/
 * @since       1.0.0
 *
 * @package     Matador Jobs
 * @subpackage  Templates
 * @author      Matador Software LLC, Jeremy Scott, Paul Bearne
 * @copyright   (c) 2017-2021, Matador Software, LLC
 *
 * @license     https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Defined before include:
 *
 * @var string   $class
 * @var int      $index
 * @var array    $args
 * @var WP_Query $jobs
 * @var array    $fields
 */

/**
 * Action Matador Jobs Before
 *
 * Runs before everything else at the start of a "Matador Jobs" function or shortcode call.
 *
 * @since 3.0.0
 * @since 3.4.0 added $jobs, $context and $args parameters.
 *
 * @param WP_Query $jobs
 * @param string $context
 * @param array  $args
 */
do_action( 'matador_jobs_before', $jobs, 'table', $args );
?>

<table class="<?php matador_build_classes( 'matador-jobs', 'matador-jobs-table', $class ); ?>">

	<thead>

		<tr>

			<?php foreach ( $fields as $field => $label ) : ?>

				<th aria-label="<?php echo esc_attr( $label . ' column' ); ?>" role="columnheader">
					<?php echo esc_html( $label ); ?>
				<?php
				// @todo - Discuss if table sorting should be off/on by default when released (it's potentially breaking change for some clients existing layouts, but off by default means strong risk clients with non-technical background aren't aware of thos functionality & how to activate it).
				if ( ! empty( $args['column_orderby'] ) && $args['column_orderby'] ) :
					$orderby_key = '';
					// Map table field names to sorting option keys.
					switch ( $field ) {
						case 'salary_string':
							$orderby_key = 'salary';
							break;
						case 'bullhorn_id':
							$orderby_key = 'source_id';
							break;
						case 'date':
							$orderby_key = 'publication_date_newest';
							break;
						case 'dateEnd':
							$orderby_key = 'closing_date_newest';
							break;
						default:
							$job_orderby_options = \matador\Job_Listing::job_orderby_options();
							if ( array_key_exists( $field, $job_orderby_options ) ) {
								$orderby_key = apply_filters( 'matador_jobs_table_orderby_key', $field );
							}
							break;
					}
					?>
					<?php if ( ! empty( $orderby_key ) ) : ?>

						<?php
							global $wp;
							$current_url = home_url( add_query_arg( array(), $wp->request ) );
						?>

						<span class="matador-jobs-table-orderby-indicators">

							<a 
								class="matador-jobs-table-orderby-indicator asc"
								href="<?php echo $current_url . '/?matador-jobs-orderby=' . $orderby_key . '&matador-jobs-order=asc'; ?>"
								aria-label="<?php printf( __( 'Sort the %s column in ascending order', 'matador-jobs' ), esc_attr( $label ) ); ?>"
							>
								<span class="dashicons dashicons-arrow-up <?php echo ( ! empty( $_GET['matador-jobs-orderby'] ) && $orderby_key === $_GET['matador-jobs-orderby'] && ! empty( $_GET['matador-jobs-order'] ) && 'asc' === $_GET['matador-jobs-order'] ) ? 'active' : 'inactive' ?>"></span>
							</a>

							<a 
								class="matador-jobs-table-orderby-indicator desc"
								href="<?php echo $current_url . '/?matador-jobs-orderby=' . $orderby_key . '&matador-jobs-order=desc';?>"
								aria-label="<?php printf( __( 'Sort the %s column in descending order', 'matador-jobs' ), esc_attr( $label ) ); ?>"
							>
								<span class="dashicons dashicons-arrow-down <?php echo ( ! empty( $_GET['matador-jobs-orderby'] ) && $orderby_key === $_GET['matador-jobs-orderby'] && ! empty( $_GET['matador-jobs-order'] ) && 'desc' === $_GET['matador-jobs-order'] ) ? 'active' : 'inactive' ?>"></span>
							</a>

						</span>

					<?php endif; ?>
				<?php endif; ?>
				</th>
			<?php endforeach ?>

		</tr>

	</thead>

	<tbody>

	<?php
	/**
	 * Action: Template Before Jobs
	 *
	 * Runs after the jobs loop in templates that feature a loop of jobs.
	 *
	 * @since 3.0.0
	 * @since 3.4.0 added the $context parameter
	 *
	 * @param WP_Query $jobs
	 * @param string   $context
	 */
	do_action( 'matador_jobs_before_jobs', $jobs, 'table' );

	while ( $jobs->have_posts() ) :

		$jobs->the_post();

		matador_get_template_part( 'jobs-table', 'job', array_merge( $args, array( 'index' => $index++ ) ) );

	endwhile;

	wp_reset_postdata();

	/**
	 * Action: Matador Jobs After Jobs
	 *
	 * Runs after the jobs loop but before the container (ie: <ul>, <table>) in templates that feature a loop of jobs.
	 *
	 * @since 3.0.0
	 * @since 3.4.0 added the $context parameter
	 *
	 * @param WP_Query $jobs
	 * @param string   $context
	 */
	do_action( 'matador_jobs_after_jobs', $jobs, 'listing' );
	?>

	</tbody>

</table>

<?php
/**
 * Action Matador Jobs After
 *
 * Runs after everything else at the end of a "Matador Jobs" function or shortcode call.
 *
 * @since 3.0.0
 * @since 3.4.0 added $jobs, $context and $args parameters.
 *
 * @param WP_Query $jobs
 * @param string $context
 * @param array  $args
 */
do_action( 'matador_jobs_after', $jobs, 'table', $args );

