Skip to content

Instantly share code, notes, and snippets.

@dimaip
Created June 25, 2012 13:20
Show Gist options
  • Select an option

  • Save dimaip/2988519 to your computer and use it in GitHub Desktop.

Select an option

Save dimaip/2988519 to your computer and use it in GitHub Desktop.
Draft of the typo3 fluid viewhelper to layout photos like in google plus gallery. Based on the for viewhelper
<?php
/* *
* This script is backported from the FLOW3 package "TYPO3.Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
*/
class Tx_Lister_ViewHelpers_LayoutGalleryViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper implements Tx_Fluid_Core_ViewHelper_Facets_CompilableInterface {
/**
* 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 $key The name of the variable to store the current array key
* @param boolean $reverse If enabled, the iterator will start with the last element and proceed reversely
* @param string $iteration The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)
* @param integer $containerWidth
* @param integer $height
* @param integer $gutter
* @return string Rendered string
* @api
*/
public function render($each, $as, $key = '', $reverse = FALSE, $iteration = NULL, $containerWidth = 1165, $height = 233, $gutter = 6) {
return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
}
/**
* @param array $arguments
* @param Closure $renderChildrenClosure
* @param Tx_Fluid_Core_Rendering_RenderingContextInterface $renderingContext
* @return string
*/
static public function renderStatic(array $arguments, Closure $renderChildrenClosure, Tx_Fluid_Core_Rendering_RenderingContextInterface $renderingContext) {
$templateVariableContainer = $renderingContext->getTemplateVariableContainer();
if ($arguments['each'] === NULL) {
return '';
}
if (is_object($arguments['each']) && !$arguments['each'] instanceof Traversable) {
throw new Tx_Fluid_Core_ViewHelper_Exception('ForViewHelper only supports arrays and objects implementing Traversable interface', 1248728393);
}
if ($arguments['reverse'] === TRUE) {
// array_reverse only supports arrays
if (is_object($arguments['each'])) {
$arguments['each'] = iterator_to_array($arguments['each']);
}
$arguments['each'] = array_reverse($arguments['each']);
}
$iterationData = array(
'index' => 0,
'cycle' => 1,
'total' => count($arguments['each'])
);
$output = '';
$containerWidth = $arguments['containerWidth'];
$height = 233;
$gutter_width = 6;
$width_so_far = 0;
$index_begin = 0;
$index_end = 0;
foreach ($arguments['each'] as $keyValue => $singleElement) {
$img_array = getimagesize("uploads/pics/$singleElement");
$img_ration = $img_array[0] / $img_array[1];
$width_arr_pre[$keyValue] = $img_ration * $height;
$width_so_far += $img_ration * $height + $gutter_width;
$index_end++;
if ($width_so_far > $containerWidth) {
$scale_factor =( $containerWidth - $row_counter * $gutter_width) / ($width_so_far - $row_counter * $gutter_width);
for ($i = $index_begin; $i < $index_end; $i++) {
$width_arr[$i] = floor($width_arr_pre[$i] * $scale_factor);
}
$index_begin = $index_end;
$width_so_far = 0;
}
}
foreach ($arguments['each'] as $keyValue => $singleElement) {
$templateVariableContainer->add($arguments['as'], $singleElement);
$templateVariableContainer->add('width_crop', $width_arr[$keyValue]);
if ($arguments['key'] !== '') {
$templateVariableContainer->add($arguments['key'], $keyValue);
}
if ($arguments['iteration'] !== NULL) {
$iterationData['isFirst'] = $iterationData['cycle'] === 1;
$iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
$iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
$iterationData['isOdd'] = !$iterationData['isEven'];
$templateVariableContainer->add($arguments['iteration'], $iterationData);
$iterationData['index']++;
$iterationData['cycle']++;
}
$output .= $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
$templateVariableContainer->remove('width_crop');
if ($arguments['key'] !== '') {
$templateVariableContainer->remove($arguments['key']);
}
if ($arguments['iteration'] !== NULL) {
$templateVariableContainer->remove($arguments['iteration']);
}
}
return $output;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment