Created
March 26, 2011 13:57
-
-
Save anonymous/888297 to your computer and use it in GitHub Desktop.
TCA options viewHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*************************************************************** | |
* Copyright notice | |
* | |
* (c) 2010 Franz Koch <[email protected]>, Koch & Koch GbR | |
* | |
* All rights reserved | |
* | |
* This script is part of the TYPO3 project. The TYPO3 project is | |
* free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* The GNU General Public License can be found at | |
* http://www.gnu.org/copyleft/gpl.html. | |
* | |
* This script is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* This copyright notice MUST APPEAR in all copies of the script! | |
***************************************************************/ | |
/** | |
* Is fetching property values from TCA | |
* | |
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later | |
*/ | |
class Tx_ElementsExtbase_ViewHelpers_Tca_OptionsViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper { | |
/** | |
* local cache for dataMaps | |
* @var array | |
*/ | |
protected $dataMaps = array(); | |
/** | |
* @var Tx_Extbase_Persistence_Mapper_DataMapper | |
*/ | |
protected $dataMapper; | |
/** | |
* @var Tx_Extbase_Object_ObjectManager | |
*/ | |
protected $objectManager; | |
/** | |
* Injects the dataMapper | |
* @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper | |
* @return void | |
*/ | |
public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) { | |
$this->dataMapper = $dataMapper; | |
} | |
/** | |
* Injects the objectManager | |
* @param Tx_Extbase_Object_ObjectManager $objectManager | |
* @return void | |
*/ | |
public function injectObjectManager(Tx_Extbase_Object_ObjectManager $objectManager) { | |
$this->objectManager = $objectManager; | |
} | |
/** | |
* Is returning select values for a property or propertyPath of a given object or className | |
* @param mixed $subject The object or className the property belongs to | |
* @param string $property The property itself | |
* @return array The select options as array | |
*/ | |
public function render($subject = NULL, $property = '') { | |
if (!is_object($subject) && is_string($subject) && class_exists($subject)) { | |
$subject= $this->objectManager->create($subject); | |
} | |
return self::getSelectOptions($property, $subject); | |
} | |
/** | |
* Is resolving the select values for a property or propertyPath of a given object or className | |
* @param string $propertyPath The property itself | |
* @param object $subject The object or className the property belongs to | |
* @return array The select options as array | |
*/ | |
private function getSelectOptions($propertyPath, $subject) { | |
$selectOptions = array(); | |
if (is_object($subject)) { | |
$propertyPathSegments = t3lib_div::trimExplode('.', $propertyPath); | |
$object = clone($subject); | |
$propertyName = $propertyPath; | |
// resolve a property path | |
if (count($propertyPathSegments) > 1) { | |
foreach($propertyPathSegments as $key => $propertyName) { | |
$propertyType = $this->dataMapper->getType(get_class($tempObject), $propertyName); | |
if (strpos($propertyType,'_')) { | |
$object = $this->objectManager->create($propertyType); | |
} else { | |
break; | |
} | |
} | |
} | |
$dataMap = self::getDataMap($object); | |
if ($dataMap == NULL || !$dataMap->isPersistableProperty($propertyName)) { | |
return $selectOptions; | |
} | |
$tableName = $this->dataMapper->convertClassNameToTableName(get_class($object)); | |
$columnName = $dataMap->getColumnMap($propertyName)->getColumnName(); | |
// only convert select items which do not have a DB relation | |
$columnConfig = $GLOBALS['TCA'][$tableName]['columns'][$columnName]['config']; | |
if ($columnConfig['type'] == 'select' && count($columnConfig['items']) && !$columnConfig['foreign_table']) { | |
foreach ($columnConfig['items'] as $option) { | |
$selectOptions[$option[1]] = Tx_Extbase_Utility_Localization::translate($option[0], $this->controllerContext->getRequest()->getControllerExtensionName()); | |
} | |
} | |
} | |
return $selectOptions; | |
} | |
/** | |
* Resolve the dataMap for the given object | |
* @param object $object The domain object to get the dataMap for | |
* @return Tx_Extbase_Persistence_Mapper_DataMap | |
*/ | |
private function getDataMap($object) { | |
$class = is_object($object) ? get_class($object) : $object; | |
if (!isset($this->dataMaps[$class])) { | |
$this->dataMaps[$class] = $this->dataMapper->getDataMap($class); | |
} | |
return $this->dataMaps[$class]; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment