<?php
/**
 * Widget: Carousel
 *
 * Widget show the posts from selected categories in carousel layouts.
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */

if( !class_exists( 'Trainify_Site_Carousel_Widget' ) ):

	class Trainify_Site_Carousel_Widget extends TrainifyFramework_Master_Widget{

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {

			$widget_ops = array(
				'classname'   => 'trainify_site_carousel_widget tff-full-widget',
				'description' => esc_html__( 'Displays posts from selected categories in carousel layouts.', 'trainify' )
			);
			parent::__construct( 'trainify_site_carousel_widget', esc_html__( 'TF - Site Carousel', 'trainify' ), $widget_ops );

		}

		/**
		 * Helper function that holds widget fields
		 * Array is used in update and form functions
		 */
		public function widget_fields( $instance = array() ) {

			$fields = array(
				'trainify_framework_widget_tab'       => array(
					'trainify_framework_widget_field_name'     => 'trainify_framework_widget_tab',
					'trainify_framework_widget_field_title'    => esc_html__( 'General', 'trainify' ),
					'trainify_framework_widget_field_default'  => 'general',
					'trainify_framework_widget_field_type'     => 'tabgroup',
					'trainify_framework_widget_field_options'  => array(
						'general'=>array(
							'trainify_framework_widget_field_title'=>esc_html__('General', 'trainify'),
							'trainify_framework_widget_field_options'=> array(
								'title' => array(
									'trainify_framework_widget_field_name'        => 'title',
									'trainify_framework_widget_field_title'       => esc_html__( 'Block title', 'trainify' ),
									'trainify_framework_widget_field_description' => esc_html__( 'Enter your block title. (Optional - Leave blank to hide title.)', 'trainify' ),
									'trainify_framework_widget_field_type'  => 'text'
								),
								'title_target'    => array(
									'trainify_framework_widget_field_name'     => 'title_target',
									'trainify_framework_widget_field_wraper'   => 'title-target',
									'trainify_framework_widget_field_title'    => esc_html__( 'Title Target', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type'     => 'select',
									'trainify_framework_widget_field_options'  => trainify_framework_link_target(),
									'trainify_framework_widget_field_relation' => array(
										'exist' => array(
											'show_fields'   => array(
												'title-link', 
											),
										),
										'empty' => array(
											'hide_fields'   => array(
												'title-link', 
											),
										),
									),
								),
								'title_link'    => array(
									'trainify_framework_widget_field_name'     => 'title_link',
									'trainify_framework_widget_field_wraper'   => 'title-link',
									'trainify_framework_widget_field_title'    => esc_html__( 'Title link', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type'     => 'text',
								),
								'block_term_ids' => array(
									'trainify_framework_widget_field_name'          => 'block_term_ids',
									'trainify_framework_widget_field_title'         => esc_html__( 'Block Categories', 'trainify' ),
									'trainify_framework_widget_field_type'   => 'multitermlist',
									'trainify_framework_widget_taxonomy_type' => 'category',
								),
								'total_posts_show' => array(
									'trainify_framework_widget_field_name'          => 'total_posts_show',
									'trainify_framework_widget_field_title'         => esc_html__( 'No of post to show', 'trainify' ),
									'trainify_framework_widget_field_default'  => 6,
									'trainify_framework_widget_field_type'   => 'number',
								),
								'no_of_column' => array(
									'trainify_framework_widget_field_name'          => 'no_of_column',
									'trainify_framework_widget_field_title'         => esc_html__( 'No of column', 'trainify' ),
									'trainify_framework_widget_field_default'  => 3,
									'trainify_framework_widget_field_type'   => 'select',
									'trainify_framework_widget_field_options' => array(
										'1' => esc_html__('Column One', 'trainify'),
										'2' => esc_html__('Column Two', 'trainify'),
										'3' => esc_html__('Column Three', 'trainify'),
										'4' => esc_html__('Column Four', 'trainify'),
									),
								),
								'excerpt_length' => array(
                                	'trainify_framework_widget_field_name'         => 'excerpt_length',
                                	'trainify_framework_widget_field_title'        => esc_html__( 'Description Length', 'trainify' ),
                                	'trainify_framework_widget_field_default'      => '150',
                                	'trainify_framework_widget_field_type'   => 'number',
                                	'trainify_framework_widget_field_description'  => esc_html__( 'Enter the short description length in character.', 'trainify'),
                                ),
                                'thumbnail_size' => array(
									'trainify_framework_widget_field_name'         => 'thumbnail_size',
									'trainify_framework_widget_field_title'        => esc_html__( 
										'Thumbnail Image Size', 'trainify' ),
									'trainify_framework_widget_field_default'=> 'trainify-thumb-600x600',
									'trainify_framework_widget_field_type' => 'select',
									'trainify_framework_widget_field_options'   => trainify_framework_get_image_sizes(),
								),
							)
						),
						'layout'=>array(
							'trainify_framework_widget_field_title'=>esc_html__('Layout', 'trainify'),
							'trainify_framework_widget_field_options'=> array(
								'carousel_layout' => array(
									'trainify_framework_widget_field_name'          => 'carousel_layout',
									'trainify_framework_widget_field_title'         => esc_html__( 'Carousel Layout', 'trainify' ),
									'trainify_framework_widget_field_type'   => 'select',
									'trainify_framework_widget_field_default'  => 'layout2',
									'trainify_framework_widget_field_options' => array(
										'layout1' => esc_html__('Layout One', 'trainify'),
										'layout2' => esc_html__('Layout Two', 'trainify'),
									),
								),
								'enable_navigation' => array(
									'trainify_framework_widget_field_name'          => 'enable_navigation',
									'trainify_framework_widget_field_title'         => esc_html__( 'Enable Navigation', 'trainify' ),
									'trainify_framework_widget_field_type'   => 'checkbox',
									'trainify_framework_widget_field_default'  => 1
								),
							)
						),
					)
				)
			);

			$widget_fields_key = 'fields_'.$this->id_base;
			$widgets_fields = apply_filters( $widget_fields_key, $fields );
			return $widgets_fields;

		}

		/**
		 * Front-end display of widget.
		 *
		 * @see WP_Widget::widget()
		 *
		 * @param array $args Widget arguments.
		 * @param array $instance Saved values from database.
		 */
		public function widget( $args, $instance ) {
	
			/*
			 * Title Section
			 */			
			$before_title = (isset($args['before_title'])) ? $args['before_title'] : '';
			$after_title = (isset($args['after_title'])) ? $args['after_title'] : '';

			/*
			 * General Tab
			 */
			$title  = isset( $instance['title'] ) ? esc_html( $instance['title'] ) : '';
            $title  = apply_filters( 'widget_title', $title, $instance, $this->id_base );
            $title_target      = isset( $instance['title_target'] ) ? esc_html( $instance['title_target'] ) : '';
            $title_link  = isset( $instance['title_link'] ) ? esc_html( $instance['title_link'] ) : '';
            $block_term_ids  = isset( $instance['block_term_ids'] ) ? $instance['block_term_ids'] : '';
            $total_posts_show  = isset( $instance['total_posts_show'] ) ? absint($instance['total_posts_show']) : 6;
            $no_of_column  = isset( $instance['no_of_column'] ) ? absint($instance['no_of_column']) : 3;
            $excerpt_length  = isset( $instance['excerpt_length'] ) ? absint($instance['excerpt_length']) : 0;
            $thumbnail_size  = isset( $instance['thumbnail_size'] ) ? esc_attr($instance['thumbnail_size']) : 'trainify-thumb-600x600';

            /*
			 * Layout Tab
			 */
            $carousel_layout  = isset( $instance['carousel_layout'] ) ? $instance['carousel_layout'] : 'layout2';
            $enable_navigation  = isset( $instance['enable_navigation'] ) ? absint($instance['enable_navigation']) : 0;

			$trainify_block_args   = array(
				'query_args' => array(
					'post_type'	=> 'post',
					'posts_per_page' => absint( $total_posts_show )
				),
				'carousel_settings'		=> array(
					'thumbnail_size'	=> $thumbnail_size,
					'no_of_column'		=> $no_of_column,
					'excerpt_length'	=> $excerpt_length,
				),
			);
			if($block_term_ids){
				$trainify_block_args['query_args']['tax_query'][] = array(
					array(
						'taxonomy' => 'category',
						'field'    => 'term_id',
						'terms'    => $block_term_ids,
					),
				);
			}

			trainify_framework_before_widget($args);
			?>
			<div class="tff-container">
				<div data-layout="<?php echo esc_attr( $carousel_layout ); ?>"
					class="tff-block-wrapper tff-carousel-posts tff-clearfix <?php echo esc_attr( $carousel_layout ); ?>">
					<div class="tff-block-title-nav-wrap">
						<?php
						$title_args = array(
							'title' => $title,
							'title_target'	=> $title_target,
							'title_link'	=> $title_link,
							'before_title'	=> $before_title,
							'after_title'	=> $after_title,
						);
						do_action('trainify_framework_widget_title', $title_args);
						?>
					</div> <!-- tff-full-width-title-nav-wrap -->
					<div class="tff-block-posts-wrapper">
						<?php trainify_carousel_layout_section( $trainify_block_args ); ?>
						<?php if($enable_navigation): ?>
							<div class="carousel-nav-action">
								<div class="tff-navPrev carousel-controls"><i class="fa fa-angle-left"></i></div>
								<div class="tff-navNext carousel-controls"><i class="fa fa-angle-right"></i></div>
							</div>
						<?php endif; ?>
					</div><!-- .tff-block-posts-wrapper -->
				</div><!--- .tff-block-wrapper -->
			</div>
			<?php
			trainify_framework_after_widget($args);
		}

	}

endif;