All pastes #2090041 Raw Edit

Dmitri Pisarev

public php v1 · immutable
#2090041 ·published 2011-10-14 12:10 UTC
rendered paste body
<?phpclass Tx_Rmap_ViewHelpers_GroupedForViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {	/**	 * Iterates through elements of $each and renders child nodes	 *	 * @param array $each The array or Tx_Extbase_Persistence_ObjectStorage to iterated over	 * @param string $as The name of the iteration variable	 * @param string $groupBy Group by this property	 * @param string $groupKey The name of the variable to store the current group	 * @return string Rendered string	 * @author Bastian Waidelich <bastian@typo3.org>	 * @api	 */	public function render($each, $as, $groupBy, $groupKey = 'groupKey') {		$output = '';		if ($each === NULL) {			return '';		}		if (is_object($each)) {			if (!$each instanceof Traversable) {				throw new Tx_Fluid_Core_ViewHelper_Exception('GroupedForViewHelper only supports arrays and objects implementing Traversable interface', 1253108907);			}			$each = iterator_to_array($each);		}		$groups = $this->groupElements($each, $groupBy);		foreach ($groups['values'] as $currentGroupIndex => $group) {			$this->templateVariableContainer->add($groupKey, $groups['keys'][$currentGroupIndex]);			$this->templateVariableContainer->add($as, $group);			$output .= $this->renderChildren();			$this->templateVariableContainer->remove($groupKey);			$this->templateVariableContainer->remove($as);		}		return $output;	}	/**	 * Groups the given array by the specified groupBy property.	 *	 * @param array $elements The array / traversable object to be grouped	 * @param string $groupBy Group by this property	 * @return array The grouped array in the form array('keys' => array('key1' => [key1value], 'key2' => [key2value], ...), 'values' => array('key1' => array([key1value] => [element1]), ...), ...)	 * @author Bastian Waidelich <bastian@typo3.org>	 */	protected function groupElements(array $elements, $groupBy) {		$groups = array('keys' => array(), 'values' => array());		foreach ($elements as $key => $value) {			if (is_array($value)) {				$currentGroupIndexs = isset($value[$groupBy]) ? $value[$groupBy] : NULL;			} elseif (is_object($value)) {				$currentGroupIndexs = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $groupBy);			} else {				throw new Tx_Fluid_Core_ViewHelper_Exception('GroupedForViewHelper only supports multi-dimensional arrays and objects', 1253120365);			}			if ($currentGroupIndexs instanceof Traversable) {				$currentGroupIndexs = iterator_to_array($currentGroupIndexs);			}			if (is_array($currentGroupIndexs)) {				foreach ($currentGroupIndexs as $cGI => $currentGroupIndex) {					$currentGroupKeyValue = $currentGroupIndex;					if (is_object($currentGroupIndex)) {						$currentGroupIndex = spl_object_hash($currentGroupIndex);					}					$groups['keys'][$currentGroupIndex] = $currentGroupKeyValue;					$groups['values'][$currentGroupIndex][$key] = $value;				}			} else {				$currentGroupIndex = $currentGroupIndexs;				$currentGroupKeyValue = $currentGroupIndex;				if (is_object($currentGroupIndex)) {					$currentGroupIndex = spl_object_hash($currentGroupIndex);				}				$groups['keys'][$currentGroupIndex] = $currentGroupKeyValue;				$groups['values'][$currentGroupIndex][$key] = $value;			}		}		return $groups;	}}?>