<?php
/**
 * Widget: Causes
 *
 * @package WordPress
 * @subpackage Charity
 * @since Charity 1.0
 */
?>
<?php


add_action('widgets_init', function(){return register_widget("wpl_home_projects_widget");});
class wpl_home_projects_widget extends WP_Widget {


	/*-----------------------------------------------------------------------------------*/
	/*	Widget actual processes
	/*-----------------------------------------------------------------------------------*/

	public function __construct() {
		parent::__construct(
	 		'wpl_home_projects_widget',
			__( 'WPL Projects (HomePage)', 'charity-wpl' ),
			array( 'description' => __( 'A widget to displaying Projects', 'charity-wpl' ), )
		);
	}

	/*-----------------------------------------------------------------------------------*/
	/*	Outputs the options form on admin
	/*-----------------------------------------------------------------------------------*/

	public function form( $instance ) {

		if ( $instance ) {
			$title = esc_attr( $instance[ 'title' ] );
			$subtitle = esc_attr( $instance[ 'subtitle' ] );
			$nr_posts = esc_attr( $instance[ 'nr_posts' ] );
			$display_type = esc_attr( $instance[ 'display_type' ] );
		}
		else {
			$title = __( 'Projects', 'charity-wpl' );
			$subtitle = __( 'Subtitle', 'charity-wpl' );
			$nr_posts = 8;
			$display_type = 'rand';
		}
		 ?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> <?php _e('Title:', 'charity-wpl'); ?> </label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_html( $title ); ?>" />
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'subtitle' ) ); ?>"> <?php _e('Subtitle:', 'charity-wpl'); ?> </label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subtitle' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'subtitle' ) ); ?>" type="text" value="<?php echo esc_html( $subtitle ); ?>" />
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('nr_posts') ); ?>"> <?php _e('Number of Projects:', 'charity-wpl'); ?> </label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('nr_posts') ); ?>" name="<?php echo esc_attr( $this->get_field_name('nr_posts') ); ?>" type="text" value="<?php echo esc_attr( $nr_posts ); ?>" />
				<p style="font-size: 10px; color: #999; margin: -10px 0 0 0px; padding: 0px;"> <?php _e('Number of Projects you want to display', 'charity-wpl'); ?></p>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('display_type') ); ?>"><?php _e('Order by:', 'charity-wpl'); ?> <br /> </label>
				<select id="<?php echo esc_attr( $this->get_field_id('display_type') ); ?>" name="<?php echo esc_attr( $this->get_field_name('display_type') ); ?>">
					<option value="rand" <?php selected( 'rand', $display_type ); ?>><?php _e('Random', 'charity-wpl'); ?></option>
					<option value="date" <?php selected( 'date', $display_type ); ?>><?php _e('Latest', 'charity-wpl'); ?></option>
				</select>
			</p>
		<?php
	}


	/*-----------------------------------------------------------------------------------*/
	/*	Processes widget options to be saved
	/*-----------------------------------------------------------------------------------*/

	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = sanitize_text_field($new_instance['title']);
		$instance['subtitle'] = sanitize_text_field($new_instance['subtitle']);
		$instance['nr_posts'] = sanitize_text_field($new_instance['nr_posts']);
		$instance['display_type'] = sanitize_text_field($new_instance['display_type']);
		return $instance;
	}


	/*-----------------------------------------------------------------------------------*/
	/*	Outputs the content of the widget
	/*-----------------------------------------------------------------------------------*/

	public function widget( $args, $instance ) {
		global $post;
		extract( $args );
		$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
		$subtitle = apply_filters( 'widget_title', empty($instance['subtitle']) ? '' : $instance['subtitle'], $instance );
		$nr_posts = isset( $instance['nr_posts'] ) ? esc_attr( $instance['nr_posts'] ) : '';
		$display_type = isset( $instance['display_type'] ) ? esc_attr( $instance['display_type'] ) : '';

		// WP_Query arguments
		$args = array(
			'post_type'              => 'post_projects',
			'post_status'            => 'publish',
			'pagination'             => false,
			'posts_per_page'         => $nr_posts,
			'orderby'                => $display_type
			 );

		// The Query
		$the_query = null;
		$the_query = new WP_Query( $args ); ?>

		<section class="section-featured projects">
			<div class="grid-container">
				<div class="grid-x grid-margin-x">
					<div class="cell small-12">
						<header class="section-head">
							<h2 class="section-title"><?php echo esc_html( $title ); ?></h2><!-- /.section-title -->
							<p><?php echo esc_html( $subtitle ); ?></p>
						</header><!-- /.section-head -->
						<div class="section-body">
							<div class="slider-featured">
								<div class="slider-clip">
									<ul class="slides owl-carousel">
										<?php

										if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

											$wplook_pid = $post->ID;
											$wplook_project_meta = get_post_meta($wplook_pid);
											$wplook_project_status 	=  isset($wplook_project_meta['wpl_project_status'][0]) ? $wplook_project_meta['wpl_project_status'][0] : false;
											$wpl_project_link = isset($wplook_project_meta['wpl_project_link'][0]) ? $wplook_project_meta['wpl_project_link'][0] : false;
											$wpl_project_custom_url = isset($wplook_project_meta['wpl_project_custom_url'][0]) ? $wplook_project_meta['wpl_project_custom_url'][0] : false;
											$wpl_project_custom_url_target = isset($wplook_project_meta['wpl_project_custom_url_target'][0]) ? $wplook_project_meta['wpl_project_custom_url_target'][0] : false;


										?>
											<li class="slide">
												<div class="slide-image">
													<?php
														if ( has_post_thumbnail() ) {
															the_post_thumbnail('medium-event');
														}
													 ?>

													<div class="slide-overlay">
														<div class="slide-overlay-inner">

															<?php if ($wpl_project_link == 'off') { ?>
																<!-- no url -->
															<?php } else { ?>

																<?php if ($wpl_project_custom_url !='') { ?>
																	<a href="<?php echo $wpl_project_custom_url; ?>" target="<?php echo $wpl_project_custom_url_target; ?>"><i class="far fa-file-alt"></i></a>
																<?php } else { ?>
																	<a href="<?php the_permalink(); ?>"><i class="far fa-file-alt"></i></a>
																<?php }
															} ?>

														</div><!-- /.slide-overlay-inner -->
													</div><!-- /.slide-overlay -->
												</div><!-- /.slide-image -->

												<div class="slide-content">
													<?php if ($wpl_project_link == 'off') { ?>
														<h3><?php the_title(); ?></h3>
														<?php } else { ?>

															<?php if ($wpl_project_custom_url !='') { ?>
																<h3><a href="<?php echo $wpl_project_custom_url; ?>" target="<?php echo $wpl_project_custom_url_target; ?>"><?php the_title(); ?></a>
																</h3>
															<?php } else { ?>
																<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
															<?php }
														} ?>

													<div class="cptlist-list-meta">
														<?php
															if ($wplook_project_status) {
																echo '<span><i class="fas fa-bullhorn"></i> '.esc_attr( $wplook_project_status ).'</span>';
															}
														?>
													</div><!-- /.cptlist-list-meta -->

													<p><?php echo wplook_short_excerpt(ot_get_option('wpl_cause_excerpt_limit')); ?></p>
												</div><!-- /.slide-content -->

											</li><!-- /.slide -->
											<?php endwhile; ?>
											<?php else : ?>
												<li><?php _e('Sorry, no posts matched your criteria.', 'charity-wpl'); ?></li>
											<?php endif; ?>
										<?php wp_reset_postdata(); ?>
									</ul><!-- /.slides -->
								</div><!-- /.slider-clip -->
								<div class="slider-actions"></div><!-- /.slider-actions -->
							</div><!-- /.slider-featured -->
						</div><!-- /.section-body -->
					</div><!-- /.cell small-12 -->
				</div><!-- /.grid-x grid-margin-x -->
			</div><!-- /.grid-container -->
		</section><!-- /.section-featured --><!-- <?php echo esc_attr($this->id); ?> -->
		<?php
	}
}
?>
