<?php
/**
 * Frontend output for the AUX Testimonial component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-testimonial-collection-about-testimonial-collections--docs
 *
 * Outline:
 * 1. Set variables to null
 * 2. Get fields from the post, if they have values, wrap in html
 * 3. Build the component html
 * 4. Sanitize and output
 *
 * @author Hailey Sanders
 * @since 1.0.0
 */

use WP_AUX\Entities\AuxFramework\Partials\TestimonialEntity;
use WP_AUX\Services\AuxCF\BlocksService;

$section = BlocksService::getSection('testimonial-card-group');

// Testing if testimonials are non-empty before doing a count
$testimonials = aux_get_field('testimonial');
$testimonialsCount = !empty($testimonials) ? count($testimonials) : 0;

// Loop through testimonials to get HTML for each
$testimonialsHtml = '';
if ($testimonials) {
  foreach ($testimonials as $testimonial) {
    $testimonialsHtml .= TestimonialEntity::create($testimonial['testimonial_quote'])
    ->setContact(
      $testimonial['testimonial_name'],
      $testimonial['testimonial_title'],
      $testimonial['testimonial_org']
    )->setImage($testimonial['testimonial_image']);
  }
}

$section->setContent(<<<HTML
  <div class="testimonial-card-container" data-cards="$testimonialsCount">
    $testimonialsHtml
  </div>
HTML);

echo $section;
