<?php
/**
 * Widget: Block Posts
 *
 * Widget show the block posts from selected category in different layouts.
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */
if(!class_exists( 'Trainify_Block_Posts_Widget' ) ):

	class Trainify_Block_Posts_Widget extends TrainifyFramework_Master_Widget{

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {
			$widget_ops = array(
				'classname'   => 'trainify_block_posts_widget tff-full-widget',
				'description' => esc_html__( 'Displays block posts from selected category in different layouts.', 'trainify' )
			);
			parent::__construct( 'trainify_block_posts_widget', esc_html__( 'TF - Block Posts', '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_default'  => esc_html__( 'Block Posts', 'trainify' ),
									'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 Category', 'trainify' ),
									'trainify_framework_widget_field_default'       => 0,
									'trainify_framework_widget_field_type'   => 'multitermlist',
									'trainify_framework_widget_taxonomy_type' => 'category',
								),
								'tab_term_list' => array(
									'trainify_framework_widget_field_name'         => 'tab_term_list',
									'trainify_framework_widget_field_title'        => esc_html__( 'Tab Category List', 'trainify' ),
									'trainify_framework_widget_field_default'      => 'none',
									'trainify_framework_widget_field_type'   => 'select',
									'trainify_framework_widget_field_options' => array(
										'none' => esc_html__( 'None', 'trainify' ),
										'selected' => esc_html__( 'Block Category List', 'trainify' ),
										'otherterm' => esc_html__( 'Others Category List', 'trainify' ),
									),
									'trainify_framework_widget_field_relation' => array(
										'values' => array(
											'otherterm' => array(
												'show_fields'   => array(
													'tabs-terms', 
													'default-tablabel',
												),
											),
											'none' => array(
												'hide_fields'   => array(
													'tabs-terms', 
													'default-tablabel',
												),
											),
											'selected' => array(
												'hide_fields'   => array(
													'tabs-terms', 
												),
												'show_fields'   => array(
													'default-tablabel',
												),
											),
										),
									),
								),
								'tabs_terms' => array(
									'trainify_framework_widget_field_name'         => 'tabs_terms',
									'trainify_framework_widget_field_wraper'       => 'tabs-terms',
									'trainify_framework_widget_field_title'        => esc_html__( 'Tab Categories', 'trainify' ),
									'trainify_framework_widget_field_default'      => 0,
									'trainify_framework_widget_field_type'   => 'multitermlist',
									'trainify_framework_widget_taxonomy_type' => 'category',
								),
								'default_tablabel' => array(
									'trainify_framework_widget_field_name'         => 'default_tablabel',
									'trainify_framework_widget_field_wraper'       => 'default-tablabel',
									'trainify_framework_widget_field_title'        => esc_html__( 'All Tab Label', 'trainify' ),
									'trainify_framework_widget_field_default'      => esc_html__('All', 'trainify'),
									'trainify_framework_widget_field_type'         => 'text',
								),
								
							),
						),
						'layout'=>array(
							'trainify_framework_widget_field_title'=>esc_html__('Layout', 'trainify'),
							'trainify_framework_widget_field_options'=> array(
								'block_layout' => array(
									'trainify_framework_widget_field_name'         => 'block_layout',
									'trainify_framework_widget_field_title'        => esc_html__( 'Block Layouts', 'trainify' ),
									'trainify_framework_widget_field_default'      => 'layout5',
									'trainify_framework_widget_field_type'   => 'select',
									'trainify_framework_widget_field_options' => array(
										'layout1' => esc_html__( 'Layout 1 - (LeftBig - Right list)', 'trainify' ),
										'layout2' => esc_html__( 'Layout 2 - (TwoBit - List bottom)', 'trainify' ),
										'layout3' => esc_html__( 'Layout 3 - (BigTop - three small)', 'trainify' ),
										'layout4' => esc_html__( 'Layout 4 - (Alternate Grid)', 'trainify' ),
										'layout5' => esc_html__( 'Layout 5 - (Blog Style)', 'trainify' ),
									),
								),
								'largeimg_size' => array(
									'trainify_framework_widget_field_name'         => 'largeimg_size',
									'trainify_framework_widget_field_title'        => esc_html__( 
										'Large Image Size', 'trainify' ),
									'trainify_framework_widget_field_default'=> 'trainify-thumb-622x420',
									'trainify_framework_widget_field_type' => 'select',
									'trainify_framework_widget_field_options'   => trainify_framework_get_image_sizes(),
								),
								'large_excerpt_length' => array(
									'trainify_framework_widget_field_name'         => 'large_excerpt_length',
									'trainify_framework_widget_field_title'        => esc_html__( 'Large Description Length', 'trainify' ),
									'trainify_framework_widget_field_default'      => '200',
									'trainify_framework_widget_field_type'   => 'number',
									'trainify_framework_widget_field_description'  => esc_html__( 'Enter the large 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-136x102',
									'trainify_framework_widget_field_type' => 'select',
									'trainify_framework_widget_field_options'   => trainify_framework_get_image_sizes(),
								),
								'small_excerpt_length' => array(
									'trainify_framework_widget_field_name'         => 'small_excerpt_length',
									'trainify_framework_widget_field_title'        => esc_html__( 'Small Description Length', 'trainify' ),
									'trainify_framework_widget_field_default'      => '100',
									'trainify_framework_widget_field_type'   => 'number',
									'trainify_framework_widget_field_description'  => esc_html__( 'Enter the short description length in character.', 'trainify'),
								),
							),
						),
					)
				)
			);

			$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'] ) ? $instance['title'] : '';
			$title 	= apply_filters( 'widget_title', $title, $instance, $this->id_base );
			$title_target      = isset( $instance['title_target'] ) ? $instance['title_target'] : '';
			$title_link  = isset( $instance['title_link'] ) ? $instance['title_link'] : '';
			$block_term_ids = isset( $instance['block_term_ids'] ) ? $instance['block_term_ids'] : '';
			$default_tablabel   = isset( $instance['default_tablabel'] ) ? $instance['default_tablabel'] : '';
			$tab_term_list   = isset( $instance['tab_term_list'] ) ? $instance['tab_term_list'] : 'none';
			$tabs_terms   = isset( $instance['tabs_terms'] ) ? $instance['tabs_terms'] : '';

	        /*
	         * Layout Tab
	         */
	        $block_layout   = isset( $instance['block_layout'] ) ? $instance['block_layout'] : 'layout1';
	        $thumbnail_size = isset( $instance['thumbnail_size'] ) ? $instance['thumbnail_size'] : 'thumbnail';
	        $small_excerpt_length   = isset( $instance['small_excerpt_length'] ) ? $instance['small_excerpt_length'] : 100;
	        $largeimg_size = isset( $instance['largeimg_size'] ) ? $instance['largeimg_size'] : 'full';
	        $large_excerpt_length   = isset( $instance['large_excerpt_length'] ) ? $instance['large_excerpt_length'] : 200;

	        trainify_framework_before_widget($args);

	        $title_args = array(
	        	'title' => $title,
	        	'title_target'=> $title_target,
	        	'title_link' => $title_link,
	        	'before_title'=>$before_title,
	        	'after_title'=>$after_title
	        );
	        if($tab_term_list!='none'){
	        	$title_args['title_terms'] = ($tab_term_list=='otherterm') ? $tabs_terms : $block_term_ids;
	        	$title_args['default_tablabel'] = isset( $instance['default_tablabel'] ) ? esc_attr($instance['default_tablabel']) : '';
	        	$title_args['tab_ajax_data'] = array(
	        		'type'      => 'POST',
	        		'dataType'  => 'json',
	        		'url'       => admin_url( 'admin-ajax.php' ),
	        		'data'      => array(
	        			'action'                => 'trainify_block_posts_tabs',
	        			'block_layout'          => $block_layout,
	        			'thumbnail_size'        => $thumbnail_size,
	        			'largeimg_size'         => $largeimg_size,
	        			'small_excerpt_length'  => $small_excerpt_length,
	        			'large_excerpt_length'  => $large_excerpt_length,
	        			'posts_per_page'        => 6,
	        			'block_posts_nonce'    => wp_create_nonce( 'trainify_block_post_tabs_nonce' )
	        		),
	        	);
	        }
	        ?>
	        <div class="tff-container">
	        	<?php
	        	do_action('trainify_framework_widget_title', $title_args);
	        	?>
	        	<div class="tff-block-wrapper block-posts tff-clearfix <?php echo esc_attr( $block_layout ); ?>">
	        		<div class="tff-block-posts-wrapper trainify-framework-tab-alldata tab-active">
	        			<?php
	        			$trainify_args = array(
	        				'terms_ids' => $block_term_ids,
	        				'thumbnail_size' => $thumbnail_size,
	        				'largeimg_size' => $largeimg_size,
	        				'small_excerpt_length'  => $small_excerpt_length,
	        				'large_excerpt_length' => $large_excerpt_length,
	        			);
	        			switch ( $block_layout ) {
	        				case 'layout2':
	        				trainify_block_second_layout_section( $trainify_args );
	        				break;
	        				case 'layout3':
	        				trainify_block_box_layout_section( $trainify_args );
	        				break;
	        				case 'layout4':
	        				trainify_block_alternate_grid_section( $trainify_args );
	        				break;
	        				case 'layout5':
	        				trainify_block_blog_style_section( $trainify_args );
	        				break;
	        				default:
	        				trainify_block_one_layout_section( $trainify_args );
	        				break;
	        			}
	        			?>
	        			<?php do_action( 'trainify_widget_blockposts_pagination', $trainify_args ); ?>
	        		</div><!-- .tff-block-posts-wrapper -->
	        		<figure class="hmrcls-wdgt-preloader hidden">
	        			<span class="helper"></span>
	        			<img src="<?php echo esc_url( trainify_framework_directory_uri('assets/img/preloader/loader3.gif') ); ?>" height="100" width="100" alt="Preloader" title="Preloader" />
	        		</figure>
	        	</div><!--- .tff-block-wrapper -->
	        </div>
	        <?php
	        trainify_framework_after_widget($args);

	    }

	}

endif;