<?php
/**
 * Class: Eac_Acf_Relational
 * Slug: eac-addon-relational-acf-values
 *
 * Méthode 'get_acf_supported_fields' pour le type de champ
 *
 * @return Les trois premiers articles de types Relationship et Post_object mis en forme HTML
 * extracts and formats the content of the first three articles
 * @since 1.8.0
 */

namespace EACCustomWidgets\Includes\Acf\DynamicTags\Tags;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

use EACCustomWidgets\Core\Utils\Eac_Tools_Util;
use EACCustomWidgets\Includes\Acf\Eac_Acf_Lib;
use Elementor\Controls_Manager;
use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module as TagsModule;

class Eac_Acf_Relational extends Tag {
	use \EACCustomWidgets\Includes\Traits\Panel_Template_Trait;
	use \EACCustomWidgets\Includes\Traits\Post_Main_Id_Trait;

	/**
	 * @const EXCERPT_LENGTH
	 *
	 * Nombre de caractères maximum pour le résumé
	 */
	private const EXCERPT_LENGTH = 30;

	/**
	 * @const KEY_NUMBER
	 *
	 * Nombre d'articles à afficher
	 */
	private const KEY_NUMBER = 2;

	public function get_name(): string {
		return 'eac-addon-relational-acf-values';
	}

	public function get_title(): string {
		return esc_html__( 'Relational', 'eac-components' );
	}

	public function get_group(): array {
		return array( 'eac-acf-groupe' );
	}

	public function get_categories(): array {
		return array(
			TagsModule::TEXT_CATEGORY,
		);
	}

	public function get_panel_template_setting_key(): string {
		return 'acf_relational_key';
	}

	protected function register_controls(): void {
		$this->add_control(
			'acf_relational_key',
			array(
				'label'       => esc_html__( 'Field', 'eac-components' ),
				'type'        => Controls_Manager::SELECT,
				'groups'      => Eac_Acf_Lib::get_acf_fields_options( $this->get_acf_supported_fields() ),
				'label_block' => true,
			)
		);
	}

	public function render(): void {
		$field_value = '';
		$key         = $this->get_settings( 'acf_relational_key' );

		if ( ! empty( $key ) ) {
			list($field_key, $meta_key) = array_pad( explode( '::', $key ), 2, '' );
			// Fonction du trait Post_Main_Id_Trait
			$real_id = $this->get_post_template_id( $field_key );

			// Récupère l'objet Field
			$field = get_field_object( $field_key, $real_id );

			if ( $field && ! empty( $field['value'] ) ) {
				$field_value = $field['value'];

				switch ( $field['type'] ) {
					case 'relationship':
					case 'post_object':
						$values   = array();
						$featured = true;
						$img      = '';
						if ( 'relationship' === $field['type'] ) {
							$featured = is_array( $field['elements'] ) && ! empty( $field['elements'][0] ) && 'featured_image' === $field['elements'][0] ? true : false;
						}
						/** @since 1.8.5 Fix cast $field_value dans le type tableau */
						$field_value = is_array( $field_value ) ? $field_value : array( $field_value );

						foreach ( $field_value as $key => $value ) {
							// Pas plus de trois articles
							if ( $key > self::KEY_NUMBER ) {
								break;
							}

							$id = 'object' === $field['return_format'] ? absint( $value->ID ) : absint( $value );

							$title = 'object' === $field['return_format'] ? esc_html( $value->post_title ) : esc_html( get_post( $id )->post_title );

							if ( $featured ) {
								$img = "<div class='acf-relational_img'><a href='" . esc_url( get_permalink( get_post( $id )->ID ) ) . "'>" . get_the_post_thumbnail( $id, 'thumbnail' ) . '</a></div>';
							}

							$title_link = "<div class='acf-relational_content'><div class='acf-relational_title'><a href='" . esc_url( get_permalink( get_post( $id )->ID ) ) . "'><h3>" . esc_html( $title ) . '</h3></a></div>';

							$date_modif = "<div class='acf-relational_date'>" . esc_html( get_the_modified_date( get_option( 'date_format' ), $id ) ) . '</div>';

							$excerpt = "<div class='acf-relational_excerpt'>" . Eac_Tools_Util::get_post_excerpt( $id, self::EXCERPT_LENGTH ) . '</div>';

							$classes = implode( ' ', get_post_class( '', $id ) );

							$article = "<article id='post-" . esc_attr( $id ) . "' class='" . esc_attr( $classes ) . "'>";

							$values[] = "<div class='acf-relational_post'>" . $article . $img . $title_link . $date_modif . $excerpt . '</article></div>';
						}

						$field_value = '<div class="acf-relational_container">' . implode( ' ', $values ) . '</div>';
						break;
				}
			} else {
				$field_value = get_post_meta( $real_id, $meta_key, true );
				if ( is_array( $field_value ) ) {
					$field_value = implode( ', ', $field_value ); }
			}
		}

		echo wp_kses_post( $field_value );
	}

	protected function get_acf_supported_fields(): array {
		return array(
			'relationship',
			'post_object',
		);
	}

	public function print_panel_template() {
		$this->fix_print_panel_template();
	}
}
