<?php
/**
 * Postmag widget News
 * @package Postmagthemes
 * @subpackage Postmag
 */
if ( ! class_exists( 'Postmag_News_Widget' ) ) :
	class Postmag_News_Widget extends WP_Widget{
		function __construct() {
			parent::__construct(
				'custom_news_widget', // Base ID
				__( 'Built Style 4 - News display', 'postmag' ), // Name
				array( 'description' =>__( 'Displays posts with Titles underneath thumbnail image', 'postmag' ) )
			);
		}
		private function defaults(){
			$defaults = array( 
				'categoryListings' => '',
				'categryorformat'   => 3,
				'postformat'	    => '',
				'content' 			=> 0,
				'noofcolume'	    => 1,
				'shadow'			=> 0,
			);
			return $defaults;
		}
		public function form( $instance) {
			$instance = wp_parse_args( (array) $instance, $this->defaults() );
			$categoryListings = $instance[ 'categoryListings' ];
			$categryorformat = $instance[ 'categryorformat' ];
			$postformat = $instance[ 'postformat' ];
			$content = $instance[ 'content' ];
			$noofcolume = $instance[ 'noofcolume' ];
			$shadow = $instance[ 'shadow' ];
			?>
			<!-- CODE FOR CHOOSING CONTENT DISPLAY  -->
			<label><?php esc_attr_e( 'A. Show Post Excerpt:', 'postmag' ); ?></label>
			<input type = "checkbox" <?php checked( 1, absint( $content ) ); ?> id = "<?php echo absint( $this->get_field_id( 'content' ) ); ?>" name = "<?php echo esc_attr( $this->get_field_name( 'content' ) ); ?>" /> 
			<br />
			<!-- CODE FOR CHOOSING BOX SHADOW DISPLAY  -->
			<label><?php esc_attr_e( 'B. Choose to drop Box Shadow:', 'postmag' ); ?></label>
			<input type = "checkbox" <?php checked( 1, absint( $shadow ) ); ?> id = "<?php echo absint( $this->get_field_id( 'shadow' ) ); ?>" name = "<?php echo esc_attr( $this->get_field_name( 'shadow' ) ); ?>" /> 
			<br />
			<!-- CODE FOR CHOSING FORMAT -->
			<label ><?php esc_attr_e( 'C. Choose Type for display:', 'postmag' ); ?></label><br>
			<input id = "<?php echo absint( $this -> get_field_id( 'categryorformat' ) ); ?>" name = "<?php echo esc_attr( $this -> get_field_name( 'categryorformat' ) ); ?>" type = "radio" value = "1"
			<?php checked( 1, absint( $categryorformat ) ); ?> size = "1" />
			<label><?php esc_attr_e( 'Choose Format type display: (Image or Video)', 'postmag' ); ?></label>
			<?php
                $postmag_postformat = array(
					'show_option_none'   => __('Select post by format','postmag'),
                    'orderby'            => 'name',
                    'order'              => 'asc',
                    'show_count'         => 1,
                    'hide_empty'         => 1,
                    'echo'               => 1,
                    'selected'           => $postformat,
                    'hierarchical'       => 1,
                    'name'               => esc_html( $this->get_field_name('postformat') ),
                    'id'                 => absint( $this->get_field_id('postformat') ),
                    'class'              => 'widefat',
                    'taxonomy'           => 'post_format',
					'value_field'	     => 'name',
                );
                wp_dropdown_categories($postmag_postformat);
            ?>
			<!-- CODE FOR CHOSING RECENT POST -->
			<input id = "<?php echo absint( $this -> get_field_id( 'categryorformat' ) );?>" name = "<?php echo esc_attr( $this -> get_field_name( 'categryorformat' ) );?>" type = "radio"  value = "3"
			<?php checked( 3, absint( $categryorformat ) ); ?> size = "1" />
			<label><?php esc_attr_e( 'All formats type display:', 'postmag' ); ?></label>
			<br />
			<!-- CODE FOR CHOSING CATEGORY -->
			<input id = "<?php echo absint( $this -> get_field_id( 'categryorformat' ) ); ?>" name = "<?php echo esc_attr( $this -> get_field_name( 'categryorformat' ) ); ?>" type = "radio"  value = "2" 
			<?php checked( 2, absint( $categryorformat ) ); ?> size = "1" />
			<label><?php esc_attr_e( 'Choose Category type display:', 'postmag' ); ?></label>
			<?php
                $postmag_categoryListings = array(
					'show_option_none'   => __( 'Select post by category','postmag' ),
					'option_none_value'  => __( 'none', 'postmag' ),
                    'orderby'            => 'name',
                    'order'              => 'asc',
                    'show_count'         => 1,
                    'hide_empty'         => 1,
                    'echo'               => 1,
                    'selected'           => $categoryListings,
                    'hierarchical'       => 1,
                    'name'               => esc_html( $this->get_field_name('categoryListings') ),
                    'id'                 => absint( $this->get_field_id('categoryListings') ),
                    'class'              => 'widefat',
                    'taxonomy'           => 'category',
					'value_field'	     => 'name',
                );
                wp_dropdown_categories($postmag_categoryListings);
            ?>
			<!-- CODE FOR NUMBER OF COLUME IN LAYOUT -->
			<div class = "padborder " >
				<label><?php esc_attr_e( 'D. Choose Layout type', 'postmag' ); ?></label> <br>
				<br />
				<input id = "<?php echo esc_attr( $this -> get_field_id( 'noofcolume' ) ); ?>" name = "<?php echo esc_attr( $this -> get_field_name( 'noofcolume' ));?>" type = "radio"  value = "4"
				<?php checked( 4, absint( $noofcolume ) ); ?> size = "1" />
				<?php esc_html_e( '4 Colume layout with 4 posts to display ', 'postmag' ); ?>
				<div>
					<img src = "<?php echo esc_url( get_template_directory_uri() )?>/images/style-4overlaycolume.png "  >
				</div>
				<input id = "<?php echo absint( $this -> get_field_id( 'noofcolume' ) );?>" name = "<?php echo esc_attr( $this -> get_field_name( 'noofcolume' ) );?>" type = "radio"  value = "3"
				<?php checked( 3, absint( $noofcolume ) ); ?> size = "1" />
				<?php esc_html_e( '3 Colume layout with 3 posts to display ', 'postmag' ); ?>
				<div>
					<img src = "<?php echo esc_url( get_template_directory_uri() )?>/images/style-3overlaycolume.png " >
				</div>
				<label> <?php esc_attr_e( 'Simlarly other types are  :', 'postmag' ); ?></label>
				<input id = "<?php echo absint( $this -> get_field_id( 'noofcolume' ) );?>" name = "<?php echo esc_attr( $this -> get_field_name( 'noofcolume' ) );?>" type = "radio"  value = "2" 
				<?php checked( 2, absint( $noofcolume ) ); ?> size = "1" /><?php esc_html_e( '2', 'postmag' ); ?>
				<input id = "<?php echo absint( $this -> get_field_id( 'noofcolume' ) );?>" name = "<?php echo esc_attr( $this -> get_field_name( 'noofcolume' ) );?>" type = "radio"  value = "1"
				<?php checked( 1, absint( $noofcolume ) ); ?> size = "1" /><?php esc_html_e( '1', 'postmag' );?>
				<input id = "<?php echo absint( $this -> get_field_id( 'noofcolume' ) );?>" name = "<?php echo esc_attr( $this -> get_field_name( 'noofcolume' ) );?>" type = "radio"  value = "6" 
				<?php checked( 6, absint( $noofcolume ) ); ?> size = "1" /><?php esc_html_e( '6', 'postmag' ); ?>
				<br />
			</div>
			<br />
			<?php
		}
		public function update( $new_instance, $old_instance) {
			$instance = $old_instance;			
			$instance['categoryListings'] = ( isset( $new_instance['categoryListings'] ) ) ? sanitize_text_field( $new_instance['categoryListings'] ) : '';
			$instance[ 'categryorformat' ] = absint( ( $new_instance[ 'categryorformat' ] ) );
			$instance[ 'postformat' ] = ( isset( $new_instance['postformat'] ) ) ? sanitize_text_field( $new_instance[ 'postformat' ]) : '';
			$instance['content'] = isset($new_instance['content'])? 1 : 0;
			$instance['shadow'] = isset($new_instance['shadow'])? 1 : 0;
			$instance[ 'noofcolume' ] = absint( $new_instance[ 'noofcolume' ]);
			return $instance;
		}
		public function widget( $args, $instance) {
			$instance = wp_parse_args( (array) $instance, $this->defaults());
			$categoryListings = $instance[ 'categoryListings' ];
			$categryorformat = $instance[ 'categryorformat' ];
			$postformat = $instance[ 'postformat' ];
			$content = $instance[ 'content' ];
			$shadow = $instance[ 'shadow' ];
			$noofcolume = $instance[ 'noofcolume' ];
			echo $args['before_widget'];
			$this->getRealtyListings( $categoryListings,$categryorformat,$postformat,$content,$noofcolume,$shadow );
			echo $args['after_widget'];
		}
		private function getRealtyListings( $categoryListings,$categryorformat,$postformat,$content,$noofcolume,$shadow ) {
			if ( absint( $categryorformat ) == 2 ) {
			?>
				<div class = "catagoryline" id = "category" class = "bypostauthor">
					<a href = "<?php echo esc_url( home_url() ); ?>/category/<?php echo esc_attr( $categoryListings )?>" > <?php echo esc_html( $categoryListings )?></a>
				</div>
			<?php } ?>
			<div class = 'row'>
				<?php
				// this is just initilize $args to fix issue in customizer view
				$args = array(
					'post_type' 	 => 'post',
					'posts_per_page' => absint( $noofcolume ),
				);
				if ( absint( $categryorformat ) == 1 ) {
					$args = array(
						'post_format'    => 'post-format-'.esc_html( $postformat ),
						'posts_per_page' => absint( $noofcolume ),
					);
				}
				if ( absint( $categryorformat ) == 2 ) {
					$args = array(
						'category_name'  => esc_html( $categoryListings ),
						'posts_per_page' => absint( $noofcolume ),
					);	
				}
				if ( absint( $categryorformat ) == 3 ) {
					$args = array(
						'post_type' 	 => 'post',
						'posts_per_page' => absint( $noofcolume ),
					);
				}
				$listings = new WP_Query( $args );
				$noofcolume = 12/absint( $noofcolume ) ;
				if ( $listings->have_posts() ) {
					while ( $listings-> have_posts() ) {
						$listings->the_post();
				?>
						<div class = "col-lg-<?php echo absint( $noofcolume ) ?> col-md-<?php echo absint( $noofcolume )*2 ?> myrow4 mb-4"> <?php
							if ( absint( $categryorformat ) == 1 or absint( $categryorformat ) == 3 ) { ?>
								<div class = "catagoryline" >
									<?php the_category( ', ' ); ?> 
								</div>
							<?php
							}
							if ( absint( $shadow ) == 1 ) :
							?>
								<div class = "shadow blog-head" >
								<?php get_template_part( 'content-allin1' ); ?>
								</div>
							<?php else: ?>
								<div class = "blog-head marginb15">
								<?php get_template_part( 'content-allin1' ); ?>
								</div>
							<?php
							endif;
							if( absint( $content ) == 1) :
							?>
								<div class = "conetentline" >
									<?php the_excerpt(); ?>
									<a href="<?php the_permalink(); ?>"><p><?php esc_html_e( 'Read More', 'postmag'); ?></p></a>
									</div>
							<?php endif; ?>
						</div>
					<?php }
				}
					?>
			</div>
		<?php }
	} //end class custom_news_Widget USED
endif;
