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

  /**
   * Register widget with WordPress.
   */ 
  function __construct() {
    parent::__construct(
      'chContact',                             // Base ID
      __('(ch)Widget Contact'),                        // Name
      array( 'description' => __( 'The Widget with Contact.' ), ),         // Args
      array( 
        'title'        => array('label'        => 'Display Name <small>All settings are in admin menu, Options > Contact tab</small>',
                                'input'         => '<input id="[%ID%]" name="[%NAME%]" class="[%CLASS%] widefat" type="text" value="[%VALUE%]" [%PLACEHOLDER%] />',
                                'defaultValue'  => 'Display Name',
                       ),
        'show_image'        => array('label'        => 'Show Image',
                                'input'         => '<input id="[%ID%]" name="[%NAME%]" class="[%CLASS%] widefat" type="checkbox" value="checked" [%VALUE%] />',
                                'defaultValue'  => 'on',
                       ),
        )
    );
  }

  /**
   * 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']);
    $show_image   = $instance['show_image'];

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

     echo $title.$args['after_title']; ?>
    <div class="contact-entry">
      <?php

      if ( $show_image ) {
        $img = get_field('contact_image', 'options');

        ?><figure><img src="<?php echo $img['url']; ?>" alt="<?php echo esc_attr($title); ?>" /></figure><?php
      }

      ?>
      <div class="address"><?php echo get_field('address', 'options'); ?></div>
      <div class="phones"><?php echo get_field('phones', 'options'); ?></div>
      <div class="mails"><?php 
      
          $mails_txt = get_field('e-mails', 'options');
          $mails = split('<br />', $mails_txt);

          $before = "";

          foreach ($mails as $mail) {
            if ( $mail ) {

              echo $before; ?><a href="mailto: <?php echo $mail; ?>" alt="Send mail"><?php echo $mail; ?></a>

              <?php
              $before = "<br />";
            }
          }
      
      ?></div>
    </div><?php

    echo $args['after_widget'];

  }

} // class chContact


// register widget
function register_chContact() {
    register_widget( 'chContact' );
}
add_action( 'widgets_init', 'register_chContact' );