<?php
/**
 * Gespreksverslagen Column Configuration
 *
 * @author Visual Media
 */

namespace VisualM\ManagementBundle\AdminList\Column;


use VisualM\ManagementBundle\AdminList\Column\ColumnConfigurationAbstract;
use VisualM\ManagementBundle\AdminList\Column\ColumnConfigurationInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyPath;

/**
 * Presentie Column Configuration
 * Zelfde als LinkColumn maar om historische redenen hebben de template parameters geen defaults
 *
 * @author Visual Media
 */
class LinkCustomViewTitelColumn extends LinkColumn implements ColumnConfigurationInterface
{
    /**

     * Constructor
     * 
     * @param string                $field      Field (null for no field binding)
     * @param string                $title      Title
     * @param string|callable       $route      Route or callable that returns route
     * @param string|array|callable $parameters Parameter value, array with parameters or callable that returns parameters
     * @param boolean               $sortable   Sortable
     * @param string                $templateitem view path
     * @param string                $templateheader view path
     */
    public function __construct($field, $title, $route, $parameters = [], $sortable = false, $templateitem, $templateheader)
    {
       
        parent::__construct(
            $field,
            $title,
            $route,
            $parameters,
            $sortable,
            $templateitem,
            $templateheader,
            null
        );

        $this->route = $route;
        $this->parameters = $parameters;
    }

    
    /**
     * Get Route for Action
     *
     * @param object $item Item
     *
     * @return string Route
     */
    public function getRoute($item)
    {
        if (is_callable($this->route)) {
            return call_user_func($this->route, $item);
        } else {
            return $this->route;
        }
    }

    /**
     * Get Parameters for Action
     *
     * @param object $item Item
     *
     * @return array Parameters
     */
    public function getParameters($item)
    {
      
        if ($this->parameters === null) {
            return array();
        } elseif (is_string($this->parameters)) {
            $accessor = PropertyAccess::createPropertyAccessor();
            $propertyPath = new PropertyPath($this->parameters);
            $key = $propertyPath->getLength() == 1 ? $this->parameters : 'id';
            return array( $key => $accessor->getValue($item, $propertyPath) );
        } elseif (is_callable($this->parameters)) {
            return call_user_func($this->parameters, $item);
        } else {
            return $this->parameters;
        }
    }

}