<?php
namespace SabaiApps\Directories\Component\WordPressContent\FieldType;

use SabaiApps\Directories\Component\Entity\FieldType\ParentIdFieldType;
use SabaiApps\Directories\Component\Field\Type\IColumnable;
use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class PostParentFieldType extends ParentIdFieldType implements IColumnable {

	protected function _fieldTypeInfo() {
		return array(
			'label'        => __( 'Parent Post', 'directories-pro' ),
			'entity_types' => array( 'post' ),
		) + parent::_fieldTypeInfo();
	}

	public function fieldColumnableInfo( IField $field ) {
		if ( ( ! $bundle = $field->Bundle )
			|| empty( $bundle->info['parent'] )
			|| ( ! $parent_bundle = $this->_application->Entity_Bundle( $bundle->info['parent'] ) )
		) {
			return;
		}

		return array(
			'' => array(
				'label' => $parent_bundle->getLabel( 'singular' ),
			),
		);
	}

	public function fieldColumnableColumn( IField $field, IEntity $entity, $column = '' ) {
		if ( ! $parent_id = $entity->getParentId() ) {
			return;
		}

		return '<a href="' . admin_url( 'edit.php?post_type=' . $field->Bundle->name . '&drts_wp_post_parent=' . $parent_id ) . '">'
			. esc_html( get_the_title( $parent_id ) ) . '</a>';
	}
}
