Skip to content

Instantly share code, notes, and snippets.

@j6s
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save j6s/d035157cc3f66f5dd4f0 to your computer and use it in GitHub Desktop.

Select an option

Save j6s/d035157cc3f66f5dd4f0 to your computer and use it in GitHub Desktop.
InSetViewHelper.php
{namespace seminar=NIM\Nimseminar\ViewHelpers}
<f:comment>
<!--
Auflistung von Seminaren
-->
</f:comment>
<ul class="ns-list-eventList">
<f:if condition="{events}">
<f:then>
<f:for each="{events}" as="parent">
<f:if condition="{seminar:fe.inSet(object: parent)}">
<f:else> (... template stuff...) </f:else>
</f:if>
</f:for>
</f:then>
</f:if>
</ul>
<?php
namespace NIM\Nimseminar\ViewHelpers\Fe;
use NIM\Nimseminar\NIMGeneral;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* Created by thephpjo.
* Date: 28.10.14
* Time: 16:56
*/
/**
* Class IsInListViewHelper
*
* Checks if an Element is already in the list. returns true, if it is, returns false and adds it to the list, if not
* Usage:
*
* <f:for each="{events}" as="event">
* <f:if condition="{seminar:fe.isInList(uid: event.uid)}">
* <f:else>
* <!-- only display it, if it wasn't already displayed -->
* </f:else>
* </f:if>
* </f:for>
*
* @package NIM\Nimseminar\ViewHelpers\FE
*/
class InSetViewHelper extends AbstractViewHelper {
protected static $set = array();
/**
* @param object $object
* @param null $set
*
* @internal param int $uid
*
* @return bool
*/
public function render($object, $set = NULL){
$set = $this->getSet($object,$set);
if(!is_array(self::$set[$set])){
self::$set[$set] = array();
}
if(is_object($object)){
$hash = spl_object_hash($object);
} else {
$hash = md5((string)$object);
}
NIMGeneral::log(print_r(self::$set,true),LOG_DEBUG);
if(in_array($hash,self::$set[$set])){
return true;
} else {
self::$set[$set][] = $hash;
return false;
}
}
private function getSet($object,$set){
if(is_string($set) || is_int($set)){
return $set;
}
if($set === NULL){
if(is_object($object)){
return get_class($object);
}
}
if(is_string($object)){
return "string";
}
if(is_array($object)){
return "array";
}
if(is_int($object)){
return "integer";
}
return rand(0,100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment