<?php
namespace SabaiApps\Directories\Component\Entity\CSVExporter;

use SabaiApps\Directories\Component\CSV\Exporter\AbstractExporter;
use SabaiApps\Directories\Component\Entity\Model\Field;

class ReferenceCSVExporter extends AbstractExporter {

	public function csvExporterSettingsForm( Field $field, array $settings, $column, $enclosure, array $parents = array() ) {
		return array(
			'type' => array(
				'#type'          => 'select',
				'#title'         => __( 'Content item ID type', 'directories-pro' ),
				'#description'   => __( 'Select the type of data used to specify exported items.', 'directories-pro' ),
				'#options'       => array(
					'id'   => __( 'ID', 'directories-pro' ),
					'slug' => __( 'Slug', 'directories-pro' ),
				),
				'#default_value' => 'slug',
				'#horizontal'    => true,
			),
		) + $this->_acceptMultipleValues( $field, $enclosure, $parents );
	}

	public function csvExporterDoExport( Field $field, array $settings, $value, array $columns, array &$formStorage, array &$logs ) {
		switch ( $settings['type'] ) {
			case 'slug':
				$method = 'getSlug';
				break;
			case 'id':
			default:
				$method = 'getId';
				break;
		}
		$ret = array();
		if ( is_array( $value ) ) { // a user has reported an error $value not being an array for some reason
			foreach ( $value as $_value ) {
				$ret[] = $_value->$method();
			}
		}
		return isset( $settings['_separator'] ) ? implode( $settings['_separator'], $ret ) : $ret[0];
	}
}
