<?php
/**
 * Postmag widget Cross
 * @package Postmagthemes
 * @subpackage Postmag
 */
if ( ! class_exists( 'Postmag_Cross_Widget' ) ) :
	class Postmag_Cross_Widget extends WP_Widget{
		function __construct() {
			parent::__construct(
				'custom_cross_widget', // Base ID
				__( 'Built Style 1 - Cross display', 'postmag' ), // Name
				array( 'description' => __( 'Displays posts in Grid', 'postmag' ) )
			);
		}
		private function defaults(){
			$defaults = array( 
				'NoofListings'     => 6,
				'categoryListings' => '',
				'categryorformat'   => 3,
				'postformat'	    => '',
			);
			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' ];
			// validating the user input for number of post to show
			if ( $NoofListings == 0 || strlen( $NoofListings ) > 2 ) {
				$NoofListings = 1;
			}
			?>
			<!-- CODE FOR CHOSING FORMAT -->
			<label><?php esc_attr_e( 'A. 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 TYPE OF LAYOUT DEPENDS UPON POST NO -->
			<label><?php esc_attr_e( 'B. Choose different Layout', 'postmag' ); ?> </label> 
			<br />
			<label><?php esc_attr_e( 'Types of layout:', 'postmag' ) ?></label>
			<br />
			<label><?php esc_attr_e( ' I.     Layout with 3 posts', 'postmag' ); ?></label>
			<input id = "<?php echo absint( $this -> get_field_id( 'NoofListings' ) ); ?>" name = "<?php echo esc_attr( $this -> get_field_name( 'NoofListings' ) ); ?>" type = "radio"  value = "3"
			<?php checked( 3, absint( $NoofListings ) ); ?> size = "1" />
			<br />
			<img src = "<?php echo esc_url( get_template_directory_uri() ); ?>/images/style-3post.png " >
			<br />
			<label> <?php esc_attr_e( 'II.     Layout with 6 posts', 'postmag' ); ?> </label>
			<input id = "<?php echo absint( $this -> get_field_id( 'NoofListings' ) );?>" name = "<?php echo esc_attr( $this -> get_field_name( 'NoofListings' ) );?>" type = "radio"  value =  "6" 
			<?php checked( 6, absint( $NoofListings ) ); ?> size = "1" />
			<br />
			<img src = "<?php echo esc_url( get_template_directory_uri() ); ?>/images/style-6post.png " >
			<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' ]) : '';
			return $instance;
		}
		public function widget( $args, $instance) {
			$instance = wp_parse_args( (array) $instance, $this->defaults() );
			$NoofListings = ! empty( $instance[ 'NoofListings' ] ) ? $instance[ 'NoofListings' ] : 6;
			$categoryListings = $instance[ 'categoryListings' ];
			$categryorformat = $instance[ 'categryorformat' ];
			$postformat = $instance[ 'postformat' ];
			echo $args['before_widget'];
			$this->getRealtyListings( $NoofListings, $categoryListings,$categryorformat,$postformat);
			echo $args['after_widget'];
		}
		private function getRealtyListings( $NoofListings,$categoryListings,$categryorformat,$postformat) {
			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'>
				<?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();
						// this is first box display
						if ( $ii == 1) { 
				?> 
							<div class = "col-lg-6 myrow4 paddingright" > 
								<?php if ( absint( $categryorformat ) == 1  or absint( $categryorformat ) == 3 ) { ?>
									<div class = "formatline" >
										<?php the_category( ', ' ); ?> 
									</div>
								<?php } ?>
								<div class = "parrent shadow " > 
									<?php get_template_part( 'nocontent-coursel' ); ?>
								</div>
							</div> 
						<?php } 
						if ( $ii == 2) { 
							?>
							<div id = "2ndcolume" class = "col-lg-6 myrow4" >
								<?php if ( absint( $categryorformat ) == 1 or absint( $categryorformat ) == 3 ) { ?>
									<div class = "formatline" >
										<?php the_category( ', ' ); ?> 
									</div>
								<?php } ?>											
								<div class = "row" >
									<div class = "col-lg-6  myrow4 paddingleft" > 
										<?php get_template_part( 'content-titles-1' ); ?>
									</div>
									<div class = "col-lg-6 myrow4 paddingleft" >
										<?php get_template_part( 'content-titles-2' ); ?>
									</div>
								</div>
						<?php }
						if ( $ii == 3) { 
							if ( absint( $categryorformat ) == 1 or absint( $categryorformat == 3 ) ) {
							?>
								<div class = "formatline" >
									<?php the_category( ', ' ); ?> 
								</div>
							<?php } ?>
								<div class = "row"  >
									<div class = " col-lg-6 myrow4 paddingleft ">
										<?php get_template_part( 'content-titles-1' ); ?>
									</div>
									<div class = "col-lg-6 myrow4 paddingleft" >
										<?php get_template_part( 'content-titles-2' ); ?>
									</div>
								</div>
							</div> <!-- this div is for 2nd colume -->
						<?php }
						if ( $ii == 4) { 
							?>		
							<div class = "col-lg-6 myrow4 margin-top" > 
								<?php if ( absint( $categryorformat )== 1 or absint( $categryorformat ) == 3 ) { ?>
									<div class = "formatline" >
										<?php the_category( ', ' ); ?> 
									</div>
								<?php } ?>
								<div class = "row" >
									<div class = "col-lg-6 myrow4 paddingright" >
										<?php get_template_part( 'content-titles-2' ); ?>
									</div>
									<div class = "col-lg-6 myrow4 paddingright" >
									<?php get_template_part( 'content-titles-1' ); ?>
								</div>
							</div>
						<?php }
						if ( $ii == 5) { 
							if ( absint( $categryorformat ) == 1 or absint( $categryorformat ) == 3 ) {
							?>
								<div class = "formatline" >
									<?php the_category( ', ' ); ?> 
								</div>
							<?php } ?>	
								<div class = "row" >
									<div class = "col-lg-6 myrow4 paddingright" >
										<?php get_template_part( 'content-titles-2' ); ?>
									</div>
									<div class = "col-lg-6 myrow4 paddingright" >
										<?php get_template_part( 'content-titles-1' ); ?>
									</div>
								</div>
							</div> <!-- this div is for 2nd colume -->
						<?php }
						if ( $ii == 6) { 
							?> 
							<div class = "col-lg-6 myrow4 paddingleft margin-top" > 
								<?php if ( absint( $categryorformat ) == 1 or absint( $categryorformat ) == 3 ) { ?>
									<div class = "formatline" >
										<?php the_category( ', ' ); ?> 
									</div>
								<?php } ?>
								<div class = "parrent shadow " >
									<?php get_template_part( 'nocontent-coursel' ); ?>
								</div>
							</div>
						<?php } 
						$ii = $ii+1;
					}
				}
						?>
			</div>	
		<?php }
	} //end class cross_widget USED
endif;