Jafix

InArrayViewHelper.php

Mar 8th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2. /***************************************************************
  3. *  Copyright notice
  4. *
  5. *  (c) 2013 Jan-Philipp Greth <[email protected]>
  6. *  Based on View Helpers by:
  7. *  - Dimitri König <[email protected]>, cab services ag ( http://forge.typo3.org/attachments/18001/IfContainsViewHelper.php )
  8. *  - Benjamin Rau <[email protected]>, codearts ( https://gist.github.com/benjaminrau/4674180 )
  9. *  
  10. *  All rights reserved
  11. *
  12. *  This script is part of the TYPO3 project. The TYPO3 project is
  13. *  free software; you can redistribute it and/or modify
  14. *  it under the terms of the GNU General Public License as published by
  15. *  the Free Software Foundation; either version 3 of the License, or
  16. *  (at your option) any later version.
  17. *
  18. *  The GNU General Public License can be found at
  19. *  http://www.gnu.org/copyleft/gpl.html.
  20. *
  21. *  This script is distributed in the hope that it will be useful,
  22. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. *  GNU General Public License for more details.
  25. *
  26. *  This copyright notice MUST APPEAR in all copies of the script!
  27. ***************************************************************/
  28. namespace Jpg\Events\ViewHelpers;
  29.  
  30. use TYPO3\Flow\Annotations as Flow;
  31.  
  32. /**
  33.  * In Array View Helper
  34.  *
  35.  * For Example you could use it with multiple checkboxes to check the selected one:
  36.  *
  37.  *<f:for each="{persons}" as="person">
  38.  *  <ev:inArray haystack="{event.persons}" needle="{person}" >
  39.  *      <f:then>
  40.  *          <f:form.checkbox property="persons" value="{person}" checked="true" /><label>{person.name}</label><br/>
  41.  *      </f:then>
  42.  *      <f:else>
  43.  *          <f:form.checkbox property="persons" value="{person}" /><label>{person.name}</label><br/>
  44.  *      </f:else>
  45.  *  </ev:inArray>
  46.  *</f:for>
  47.  *
  48.  *
  49.  * @Flow\Scope("prototype")
  50.  */
  51. class InArrayViewHelper extends \TYPO3\Fluid\Core\ViewHelper\AbstractConditionViewHelper {
  52.  
  53.     public function initializeArguments() {
  54.     parent::initializeArguments();
  55.     $this->registerArgument('haystack', 'mixed', 'View helper haystack ', TRUE);
  56.     $this->registerArgument('needle', 'string', 'View helper needle', TRUE);
  57.     }
  58.  
  59.     /**
  60.     * Check if value is in array
  61.     *
  62.     *
  63.     * @param string $needle
  64.     * @param array  $haystack
  65.     *
  66.     * @return boolean
  67.     */
  68.     public function render() {
  69.  
  70.         $needle = $this->arguments['needle'];
  71.         $haystack = $this->arguments['haystack'];
  72.  
  73.         if(is_string($haystack)) {
  74.             $haystack = strpos($haystack, ',') ? explode(',',$haystack) : $haystack;
  75.         }
  76.         if(is_object($haystack)) {
  77.             $haystack = $haystack->toArray();
  78.         }
  79.  
  80.         if(in_array($needle, $haystack) && is_array($haystack)) {
  81.             return $this->renderThenChild();
  82.         } else {
  83.             return $this->renderElseChild();
  84.         }
  85.  
  86.     /*************************
  87.  
  88.         $output = '';
  89.                         if ($value === NULL || $in === NULL) {
  90.                                 return '';
  91.                         }
  92.                 if (is_string($in)) {
  93.                     $in = t3lib_div::trimExplode(',', $in, true);
  94.                 }
  95.                 if (is_object($in)) {
  96.                     $in = $in->toArray();
  97.                 }
  98.                 if (is_array($in) && in_array($value, $in)) {
  99.                         $output = $this->renderChildren();
  100.                 }
  101.                 return $output;
  102.      */
  103.  
  104.     }
  105.  
  106. }
  107.  
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment