<?php
namespace SabaiApps\Directories\Component\Display\Controller\Admin;

use SabaiApps\Directories\Controller;
use SabaiApps\Directories\Context;
use SabaiApps\Directories\Exception;

class ListElements extends Controller {

	protected $_defaultType = 'field';

	protected function _doExecute( Context $context ) {
		$display = $context->alt_display ?: $context->display;
		if ( ! $bundle = $this->Entity_Bundle( $display->bundle_name ) ) {
			$context->setError();
			return;
		}
		$requested_types = array_filter( $context->getRequest()->asArray( 'type' ) );
		$element_types   = $this->Display_Elements_types( $bundle, $display->type );
		$elements        = array();
		foreach ( array_keys( $this->Display_Elements( $bundle, false ) ) as $element_name ) {
			try {
				if ( ( ! $element = $this->Display_Elements_impl( $bundle, $element_name ) )
					|| ! $element->displayElementSupports( $bundle, $display )
					|| ( ! $element_info = $element->displayElementInfo( $bundle ) )
					|| ( isset( $element_info['listable'] ) && ! $element_info['listable'] )
					|| ( $requested_types && ! in_array( $element_info['type'], $requested_types ) )
					|| ( ! empty( $element_info['displays'] ) && ! in_array( $display->name, (array) $element_info['displays'] ) )
					|| ! $this->Filter( 'display_admin_list_elements', true, array( $bundle, $display, $element_name, $element_info ) )
				) {
					continue;
				}
			} catch ( Exception\IException $e ) {
				$this->logError( $e );
				continue;
			}

			$info = $element->displayElementInfo( $bundle );
			if ( ! empty( $info['parent_element_name'] ) ) {
				continue;
			}

			$elements[ (string) @$info['type'] ][ $element_name ] = $info;
		}
		$sorter = function ( $a, $b ) {
			return strnatcmp( $a['label'], $b['label'] );
		};
		foreach ( array_keys( $elements ) as $element_type ) {
			uasort( $elements[ $element_type ], $sorter );
		}
		foreach ( array_keys( $element_types ) as $element_type ) {
			if ( empty( $elements[ $element_type ] ) ) {
				unset( $element_types[ $element_type ] );
			}
		}
		$context->addTemplate( 'display_admin_elements' )
			->setAttributes(
				array(
					'element_types' => $element_types,
					'elements'      => $elements,
					'default_type'  => $this->_defaultType,
				)
			);
	}
}
