<?php
/**
 * Apptivo Testimonials Widget
 * @package apptivo-business-site
 * @author  RajKumar <rmohanasundaram[at]apptivo[dot]com>
 */
class AWP_Testimonials_Widget extends WP_Widget {
    /** constructor */
		var $widget_name;
		var $widget_description;
		
      //  function AWP_Testimonials_Widget() {
        // change of depreciated method balaji

   function __construct()
   {    
        $this->widget_description = __( 'Display customer testimonials', 'apptivo-business-site' );
		$this->widget_name = __('[Apptivo] Testimonials', 'apptivo-business-site' );
        $widget_ops = array('description' => $this->widget_description );
        parent::__construct('awp_testimonials_widget', $this->widget_name, $widget_ops, '');
        }

        function widget($args, $instance) {
                    extract($args);

                    $instance = wp_parse_args( (array) $instance, array(
                            'title' => '',
                            'style' => '',
                            'page_id' => '',
                            'custom_css' => '',
                            'order' => '',
                            'itemstoshow' => AWP_DEFAULT_ITEM_SHOW,
                            'more_text' => AWP_DEFAULT_MORE_TEXT,
                            'content_limit' => '',
                            'awp_widget_templatelayout' => ''
                    ) );

                    echo wp_kses_post($before_widget);
                    $_template_file = AWP_TESTIMONIALS_TEMPLATEPATH."/".$instance['awp_widget_templatelayout'];
                    $awp_testimonials = getAllTestimonials();
                  
                      if(!empty($awp_testimonials))
                        {
                            include $_template_file;
                        } else {
                          echo wp_kses( awp_messagelist('testimonials-display-page'), array( 'span' => array('class' => array()),  ));
                          //echo esc_html(awp_messagelist('testimonials-display-page'));
                        }
              
                    echo wp_kses_post($after_widget);
                    // wp_reset_query();
                    wp_reset_postdata();
            }

            function update($new_instance, $old_instance) {
                  $new_instance['more_text']=(trim($new_instance['more_text'])!="")?$new_instance['more_text']:AWP_DEFAULT_MORE_TEXT;
                    $new_instance['itemstoshow'] = is_numeric($new_instance['itemstoshow'])?$new_instance['itemstoshow']:AWP_DEFAULT_ITEM_SHOW;
                    return $new_instance;
            }

            function form($instance) {

                    $instance = wp_parse_args( (array)$instance, array(
                            'title' => '',
                            'style' => '',
                            'page_id' => '',
                            'custom_css' => '',
                            'order' => '',
                            'itemstoshow' => AWP_DEFAULT_ITEM_SHOW,
                            'more_text' => AWP_DEFAULT_MORE_TEXT,
                            'content_limit' => '',
                            'awp_widget_templatelayout' => ''
                            ) );

                        ?>
    <p>
			<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title', 'apptivo-business-site'); ?>:</label>
			<input type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
		</p>

            <p>
              <label for="<?php echo esc_attr($this->get_field_id('custom_css')); ?>"><?php esc_html_e('Custom CSS:', 'apptivo-business-site'); ?></label>
              <textarea id="<?php echo esc_attr($this->get_field_id('custom_css')); ?>" name="<?php echo esc_attr($this->get_field_name('custom_css')); ?>" class="widefat" rows="6" cols="4"><?php echo esc_attr($instance['custom_css']); ?></textarea>
            </p>
            <p><label for="<?php echo esc_attr($this->get_field_id('order')); ?>"><?php esc_html_e('Order:', 'apptivo-business-site'); ?></label>
                <select id="<?php echo esc_attr($this->get_field_id('order')); ?>" name="<?php echo esc_attr($this->get_field_name('order')); ?>">
                            <option value="1" <?php selected('1', $instance['order']); ?>><?php esc_html_e('Newest First', 'apptivo-business-site'); ?></option>
                            <option value="2" <?php selected('2', $instance['order']); ?>><?php esc_html_e('Oldest First', 'apptivo-business-site'); ?></option>
                            <option value="3" <?php selected('3', $instance['order']); ?>><?php esc_html_e('Random Order', 'apptivo-business-site'); ?></option>
                            <option value="4" <?php selected('4', $instance['order']); ?>><?php esc_html_e('Custom Order', 'apptivo-business-site'); ?></option>
            </select>
          </p>
            <p>
              <label for="<?php echo esc_attr($this->get_field_id('itemstoshow')); ?>"><?php esc_html_e('Items to show:', 'apptivo-business-site'); ?></label>
              <input  id="<?php echo esc_attr($this->get_field_id('itemstoshow')); ?>" name="<?php echo esc_attr($this->get_field_name('itemstoshow')); ?>" size="3" type="text" value="<?php echo esc_attr($instance['itemstoshow']); ?>" />
            </p>
            <p>
              <label for="<?php echo esc_attr($this->get_field_id('more_text')); ?>"><?php esc_html_e('More Items Link Title:', 'apptivo-business-site'); ?></label>
              <input class="widefat" id="<?php echo esc_attr($this->get_field_id('more_text')); ?>" name="<?php echo esc_attr($this->get_field_name('more_text')); ?>" type="text" value="<?php echo esc_attr($instance['more_text']); ?>" />
            </p>
            <p>
            <label for="<?php echo esc_attr($this->get_field_id('awp_widget_templatelayout')); ?>"><?php esc_html_e('Select Template', 'apptivo-business-site'); ?>:</label>
            <?php
            $plugintemplates = get_awpTemplates(AWP_TESTIMONIALS_TEMPLATEPATH,'widget');
           ?>

                  <select id="<?php echo esc_attr($this->get_field_id('awp_widget_templatelayout')); ?>" name="<?php echo esc_attr($this->get_field_name('awp_widget_templatelayout')); ?>" >
						<?php
						foreach (array_keys( $plugintemplates ) as $template )
						{
							?>
							<option value="<?php echo esc_attr($plugintemplates[$template])?>"  <?php selected($plugintemplates[$template], $instance['awp_widget_templatelayout']); ?> >
							<?php echo esc_attr($template)?>
							</option>
							<?php }?>
				  </select>
		    </p>
            <p><label for="<?php echo esc_attr($this->get_field_id('page_id')); ?>"><?php esc_html_e('Page', 'apptivo-business-site'); ?>:</label>
                    <?php wp_dropdown_pages(array('name' => esc_attr($this->get_field_name('page_id')), 'selected' => absint($instance['page_id']))); ?></p>
            

            <?php
            }

}
?>