<?php
/**
 * Template for the m2m post selector by post relationship.
 *
 * @var string|null $current_post_type
 * @var array $relationship_definitions
 * @var array $relationship_section_title_per_cardinality
 * @since Views 2.9.3
 */

$context = isset( $context ) ? $context : array();
$current_post_type = toolset_getarr( $context, 'current_post_type', null );
$relationship_definitions = toolset_getarr( $context, 'relationship_definitions', null );
$relationship_section_title_per_cardinality = toolset_getarr( $context, 'relationship_section_title_per_cardinality', null );

if ( empty( $relationship_definitions ) ) {
	return;
}
?>
<li class="toolset-shortcode-gui-item-selector-option toolset-shortcode-gui-item-selector-has-related js-toolset-shortcode-gui-item-selector-has-related">
	<label for="toolset-shortcode-gui-item-selector-post-id-related">
		<input type="radio" class="js-toolset-shortcode-gui-item-selector" id="toolset-shortcode-gui-item-selector-post-id-related" name="toolset_shortcode_gui_object_id" value="related" />
		<?php echo __( 'A post related to the current post, set by a Types relationship', 'wpv-views' ); ?>
	</label>
	<div class="toolset-advanced-setting toolset-shortcode-gui-item-selector-is-related js-toolset-shortcode-gui-item-selector-is-related" style="display:none">
		<?php
		foreach ( $relationship_definitions as $relationship_definition  ) {
			$cardinality = $relationship_definition->get_cardinality()->get_type();
			$relationship_selectors_factory = new Toolset_Shortcode_Attr_Item_Gui_Factory(
				$relationship_definition, $current_post_type, 'related_object'
			);
			$relationship_selectors = $relationship_selectors_factory->get_options();
			?>
			<div style="margin:5px 0 0;">
			<h3><?php echo sprintf(
				$relationship_section_title_per_cardinality[ $cardinality ],
				$relationship_definition->get_display_name()
			); ?></h3>
			<ul>
			<?php
			foreach ( $relationship_selectors as $relationship_selector_option ) {
				// Escaped when genetaring the options: the end result here is valid HTML.
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo '<li style="display:inline-block;width:31%;vertical-align:top;margin-right:1%;>' . $relationship_selector_option . '</li>';
			}
			?>
			</ul>
			</div>
			<?php
		}
		?>
	</div>
</li>
