<?php
/**
 * Adds CallOut_Quote widget.
 * make sure to include in lib/widgets.php
 */
class chTestimonials extends CustomWP_Widget {

  /**
   * Register widget with WordPress.
   */ 
  function __construct() {
    parent::__construct(
      'chTestimonials',                             // Base ID
      __('(ch)Widget Testimonials'),                        // Name
      array( 'description' => __( 'The Widget with Testimonials.' ), ),         // Args
      array( 
        'title'        => array('label'        => 'Display Name',
                                'input'         => '<input id="[%ID%]" name="[%NAME%]" class="[%CLASS%] widefat" type="text" value="[%VALUE%]" [%PLACEHOLDER%] />',
                                'defaultValue'  => 'Display Name',
                       ),
        'categories'     => array('label'        => 'Choose Category',
                                'input'         => '<select id="[%ID%]" name="[%NAME%]" class="[%CLASS%] widefat">[%SELECT_VALUES%]</select>',
                                'defaultValue'  => '',
                       ),
        'no'        => array('label'        => 'Number of records to display',
                                'input'         => '<input id="[%ID%]" name="[%NAME%]" class="[%CLASS%] widefat" type="text" value="[%VALUE%]" [%PLACEHOLDER%] />',
                                'defaultValue'  => 1,
                       ),
        )
    );

    $taxonomies = array( 
      'testimonial-types',
    );

    $args = array(
        'orderby'       => 'name', 
        'order'         => 'ASC',
        'hide_empty'    => false, 
    ); 

    $tax = get_taxonomies();
    // var_dump($tax);
    $categories = get_terms( 'testimonial-types', $args );
    // var_dump($categories);
    $this->fields['categories']['select-values'][-1] = 'All';
    foreach ($categories as $categ) {
      $this->fields['categories']['select-values'][$categ->slug] = $categ->name;
    }
  }

  /**
   * 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        = nl2br($instance['title']);
    $categories   = ( $instance['categories'] ) ? $instance['categories'] : -1;
    $no  = ($instance['no'])? $instance['no'] : 1;

      // echo '<pre>';
      // var_dump( $args );


    $qargs = array(
      'posts_per_page'   => $no,
      'offset'           => 0,
      'orderby'          => 'post_date',
      'order'            => 'DESC',
      'include'          => '',
      'exclude'          => '',
      'meta_key'         => '',
      'meta_value'       => '',
      'post_type'        => 'testimonial',
      'post_mime_type'   => '',
      'post_parent'      => '',
      'post_status'      => 'publish',
      'suppress_filters' => true );

    if ( $categories != -1 ) {
      $qargs['testimonial-types'] = $categories;
    } 

    $testis = get_posts($qargs); 

    echo $args['before_widget'].$args['before_title'];

    echo $title.$args['after_title']; ?>
    <div class="testimonials"><ul><?php

    foreach ($testis as $testi) {

      ?>
      <li>        
        <div class="content">"<?php echo $testi->post_content; ?>"</div>
        <div class="title">- <?php echo $testi->post_title; ?></div>
      </li>
      <?php
    }

    ?></ul></div><?php

    echo $args['after_widget'];

  }

} // class chTestimonials


// register widget
function register_chTestimonials() {
    register_widget( 'chTestimonials' );
}
add_action( 'widgets_init', 'register_chTestimonials' );