|
TYPO3 API
SVNRelease
|
00001 <?php 00002 00003 /* * 00004 * This script belongs to the FLOW3 package "Fluid". * 00005 * * 00006 * It is free software; you can redistribute it and/or modify it under * 00007 * the terms of the GNU Lesser General Public License as published by the * 00008 * Free Software Foundation, either version 3 of the License, or (at your * 00009 * option) any later version. * 00010 * * 00011 * This script is distributed in the hope that it will be useful, but * 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- * 00013 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * 00014 * General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with the script. * 00018 * If not, see http://www.gnu.org/licenses/lgpl.html * 00019 * * 00020 * The TYPO3 project - inspiring people to share! * 00021 * */ 00022 00023 /** 00024 * Grouped loop view helper. 00025 * Loops through the specified values 00026 * 00027 * = Examples = 00028 * 00029 * <code title="Simple"> 00030 * <f:groupedFor each="{0: {name: 'apple', color: 'green'}, 1: {name: 'cherry', color: 'red'}, 2: {name: 'banana', color: 'yellow'}, 3: {name: 'strawberry', color: 'red'}}" as="fruitsOfThisColor" groupBy="color"> 00031 * <f:for each="{fruitsOfThisColor}" as="fruit"> 00032 * {fruit.name} 00033 * </f:for> 00034 * </f:groupedFor> 00035 * </code> 00036 * 00037 * Output: 00038 * apple cherry strawberry banana 00039 * 00040 * <code title="Two dimensional list"> 00041 * <ul> 00042 * <f:groupedFor each="{0: {name: 'apple', color: 'green'}, 1: {name: 'cherry', color: 'red'}, 2: {name: 'banana', color: 'yellow'}, 3: {name: 'strawberry', color: 'red'}}" as="fruitsOfThisColor" groupBy="color" groupKey="color"> 00043 * <li> 00044 * {color} fruits: 00045 * <ul> 00046 * <f:for each="{fruitsOfThisColor}" as="fruit" key="label"> 00047 * <li>{label}: {fruit.name}</li> 00048 * </f:for> 00049 * </ul> 00050 * </li> 00051 * </f:groupedFor> 00052 * </ul> 00053 * </code> 00054 * <output> 00055 * <ul> 00056 * <li>green fruits 00057 * <ul> 00058 * <li>0: apple</li> 00059 * </ul> 00060 * </li> 00061 * <li>red fruits 00062 * <ul> 00063 * <li>1: cherry</li> 00064 * </ul> 00065 * <ul> 00066 * <li>3: strawberry</li> 00067 * </ul> 00068 * </li> 00069 * <li>yellow fruits 00070 * <ul> 00071 * <li>2: banana</li> 00072 * </ul> 00073 * </li> 00074 * </ul> 00075 * </output> 00076 * 00077 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later 00078 * @api 00079 */ 00080 class Tx_Fluid_ViewHelpers_GroupedForViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper { 00081 00082 /** 00083 * Iterates through elements of $each and renders child nodes 00084 * 00085 * @param array $each The array or Tx_Extbase_Persistence_ObjectStorage to iterated over 00086 * @param string $as The name of the iteration variable 00087 * @param string $groupBy Group by this property 00088 * @param string $groupKey The name of the variable to store the current group 00089 * @return string Rendered string 00090 * @author Bastian Waidelich <bastian@typo3.org> 00091 * @api 00092 */ 00093 public function render($each, $as, $groupBy, $groupKey = 'groupKey') { 00094 $output = ''; 00095 if ($each === NULL) { 00096 return ''; 00097 } 00098 if (is_object($each)) { 00099 if (!$each instanceof Traversable) { 00100 throw new Tx_Fluid_Core_ViewHelper_Exception('GroupedForViewHelper only supports arrays and objects implementing Traversable interface' , 1253108907); 00101 } 00102 $each = iterator_to_array($each); 00103 } 00104 00105 $groups = $this->groupElements($each, $groupBy); 00106 00107 foreach ($groups['values'] as $currentGroupIndex => $group) { 00108 $this->templateVariableContainer->add($groupKey, $groups['keys'][$currentGroupIndex]); 00109 $this->templateVariableContainer->add($as, $group); 00110 $output .= $this->renderChildren(); 00111 $this->templateVariableContainer->remove($groupKey); 00112 $this->templateVariableContainer->remove($as); 00113 } 00114 return $output; 00115 } 00116 00117 /** 00118 * Groups the given array by the specified groupBy property. 00119 * 00120 * @param array $elements The array / traversable object to be grouped 00121 * @param string $groupBy Group by this property 00122 * @return array The grouped array in the form array('keys' => array('key1' => [key1value], 'key2' => [key2value], ...), 'values' => array('key1' => array([key1value] => [element1]), ...), ...) 00123 * @author Bastian Waidelich <bastian@typo3.org> 00124 */ 00125 protected function groupElements(array $elements, $groupBy) { 00126 $groups = array('keys' => array(), 'values' => array()); 00127 foreach ($elements as $key => $value) { 00128 if (is_array($value)) { 00129 $currentGroupIndex = isset($value[$groupBy]) ? $value[$groupBy] : NULL; 00130 } elseif (is_object($value)) { 00131 $currentGroupIndex = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $groupBy); 00132 } else { 00133 throw new Tx_Fluid_Core_ViewHelper_Exception('GroupedForViewHelper only supports multi-dimensional arrays and objects' , 1253120365); 00134 } 00135 $currentGroupKeyValue = $currentGroupIndex; 00136 if (is_object($currentGroupIndex)) { 00137 $currentGroupIndex = spl_object_hash($currentGroupIndex); 00138 } 00139 $groups['keys'][$currentGroupIndex] = $currentGroupKeyValue; 00140 $groups['values'][$currentGroupIndex][$key] = $value; 00141 } 00142 return $groups; 00143 } 00144 } 00145 00146 ?>
1.8.0