<?php
/**
 * Postmag widget Titles
 * @package Postmagthemes
 * @subpackage Postmag
 */
if ( ! class_exists( 'Postmag_Titles_Widget' ) ) :
	class Postmag_Titles_Widget extends WP_Widget{
		function __construct() {
			parent::__construct(
				'custom_titles_widget', // Base ID
				__( 'Built Style 2 - Titles display', 'postmag' ), // Name
				array( 'description' => __( 'Display posts with Titlles only for consequential posts', 'postmag' ) )
			);
		}
		private function defaults() {
			$defaults = array( 
				'NoofListings'      => 1,
				'categoryListings' 	=> '',
				'categryorformat'   => 3,
				'postformat'	    => '',
				'shadow'			=> 0,
			);
			return $defaults;
		}
		public function form( $instance ) {
			$instance = wp_parse_args( (array) $instance, $this->defaults() );
			$NoofListings = $instance[ 'NoofListings' ];
			$categoryListings = $instance[ 'categoryListings' ];
			$categryorformat = $instance[ 'categryorformat' ];
			$postformat = $instance[ 'postformat' ];
			$shadow = $instance[ 'shadow' ];
			// validating the user input for number of post to show
			if ( $NoofListings == 0 || strlen( $NoofListings ) > 2 ) {
				$NoofListings = 1 ;
			}
			?>
			<!-- 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 POST TO SHOW -->
			<label><?php echo esc_html__( 'C. Number of posts to display: (1 - 99)', 'postmag' ) ?></label>
			<input id = "<?php echo absint( $this -> get_field_id( 'NoofListings' ) );?>" name = "<?php echo esc_attr( $this -> get_field_name( 'NoofListings' ) );?>" type = "number" value = "<?php echo absint( $NoofListings );?>" size = "2" min = "1" max = "99" class = "widefat" />
			<br />
			<?php
		}
		public function update( $new_instance, $old_instance) {
			$instance = $old_instance;
			$instance[ 'NoofListings' ] = absint( ( $new_instance[ 'NoofListings' ] ) );
			$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['shadow'] = isset($new_instance['shadow'])? 1 : 0;
			return $instance;
		}
		public function widget( $args, $instance) {
			$instance = wp_parse_args( (array) $instance, $this->defaults() );
			$NoofListings = $instance[ 'NoofListings' ];
			$categoryListings = $instance[ 'categoryListings' ];
			$categryorformat = $instance[ 'categryorformat' ];
			$postformat = $instance[ 'postformat' ];
			$shadow = $instance[ 'shadow' ];
			echo $args['before_widget'];
			$this->getRealtyListings( $NoofListings, $categoryListings,$categryorformat,$postformat,$shadow );
			echo $args['after_widget'];
		}
		private function getRealtyListings( $NoofListings,$categoryListings,$categryorformat,$postformat,$shadow ) {
			if ( absint( $categryorformat == 2 ) ) {
			?>
				<div class = "catagoryline" >
					<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 myrow5'  >
				<?php
				// this is just initilize $args to fix issue in customizer view
				$args = array(
					'post_type' 	 => 'post',
					'posts_per_page' => absint( $NoofListings ),
				);
				if ( absint( $categryorformat ) == 1 ) {
					$args = array(
						'post_format'    => 'post-format-'.esc_html( $postformat ),
						'posts_per_page' => absint( $NoofListings ),
					);
				}
				if ( absint( $categryorformat ) == 2 ) {
					$args = array(
						'category_name'  => esc_html( $categoryListings ),
						'posts_per_page' => absint( $NoofListings ),
					);
				 }
				if ( absint( $categryorformat ) == 3 ) {
					$args = array(
						'post_type' => 'post',
						'posts_per_page' => absint( $NoofListings ),
					);
				}
				$ii = 1;
				$listings = new WP_Query( $args );
				if ( $listings->have_posts() ) {
					while ( $listings-> have_posts() ) {
						$listings->the_post();
				?>
						<div class = "col-12 myrow4 " > 
							<?php if ( absint( $categryorformat ) == 1 or absint( $categryorformat ) == 3 ) { ?>
								<div class = "catagoryline" >
									<?php the_category( ', ' ); ?>
								</div>
							<?php
							} 
							if ( $ii == 1) {
								if ( absint( $shadow ) == 1 ) {
							?>
									<div class = "shadow" >
										<?php get_template_part( 'content-allin1' ); ?>
									</div>
								<?php } else { ?>
									<div>
									<?php get_template_part( 'content-allin1' ); ?>
									</div>
								<?php }
							}
							if ( $ii > 1) {
								?>
								<div>
									<a  href = "<?php the_permalink(); ?>" title = "<?php the_title_attribute(); ?>" ><h5 class = "title-border" ><?php the_title();?></h5></a>
								</div>
								<div class = "paddingdate ">
									<?php postmag_date_comments_left(); ?>
								</div>
								<?php
							}
								?>
						</div>
						<?php
						$ii = $ii+1;
					}
				}
						?>
			</div>
		<?php }
	} //end class titles_widget
endif;