Last active
July 13, 2016 06:52
-
-
Save johnyaucc/cdd1ea6b8bf8a3fddf8254c4e1bd39be to your computer and use it in GitHub Desktop.
MODX Snippet - Group items into columns from placeholders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @modxDescription Group a list of items into columns where the items are stored in separate placeholders (zero-based). | |
| */ | |
| /** | |
| * getColumnized | |
| * | |
| * Group a list of items into columns where the items are stored in separate placeholders (zero-based). | |
| * | |
| * @author John Yau | |
| * @copyright Copyright © 2016 | |
| * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License | |
| * version 2 or (at your option) any later version. | |
| */ | |
| /** | |
| * Example usage #1: | |
| * [[ getColumnized? | |
| * &placeholder=`item` | |
| * &columns=`3` | |
| * &total=`10` | |
| * &colTpl=`columnTpl` | |
| * ]] | |
| * | |
| * Result #1: | |
| * | item0 | item1 | item2 | | |
| * | item3 | item4 | item5 | | |
| * | item6 | item7 | item8 | | |
| * | item9 | | | | |
| * | |
| * @TODO | |
| * Example usage #2: | |
| * [[ getColumnized? | |
| * &placeholder=`item` | |
| * &columns=`3` | |
| * &offset=`4` | |
| * &total=`5` | |
| * &colTpl=`columnTpl` | |
| * ]] | |
| * | |
| * Result #2: | |
| * | item4 | item5 | item6 | | |
| * | item7 | item8 | | | |
| * | |
| * Note: | |
| * - Default number of columns: 2 | |
| * - Template 'columnTpl' should contain a placeholder [[+items]] | |
| * | |
| **/ | |
| // placeholder prefix | |
| $placeholder = $modx->getOption('placeholder', $scriptProperties, ''); | |
| // number of columns | |
| $numOfCols = $modx->getOption('columns', $scriptProperties, 2); | |
| // offset (skip ahead a number of items) | |
| $offset = $modx->getOption('offset', $scriptProperties, 0); | |
| // total number of items | |
| $totalItems = $modx->getOption('total', $scriptProperties, 0); | |
| // column template | |
| $colTpl = $modx->getOption('colTpl', $scriptProperties, ''); | |
| if ( $numOfCols < 2 ) { | |
| $modx->log(modX::LOG_LEVEL_ERROR, '[getColumnized] `columns` requires more than 2.'); | |
| return ''; | |
| } | |
| if ( $offset < 0 ) { | |
| $modx->log(modX::LOG_LEVEL_ERROR, '[getColumnized] `offset` requires no less than 0.'); | |
| return ''; | |
| } | |
| if ( empty($totalItems) || $totalItems < 0 ) { | |
| $modx->log(modX::LOG_LEVEL_ERROR, '[getColumnized] Please specify the total number of items.'); | |
| return ''; | |
| } | |
| if ( empty($placeholder) ) { | |
| $modx->log(modX::LOG_LEVEL_ERROR, '[getColumnized] Please specify the prefix of the separate placeholders.'); | |
| return ''; | |
| } | |
| if ( empty($colTpl) ) { | |
| $modx->log(modX::LOG_LEVEL_ERROR, '[getColumnized] Please specify the param `colTpl`.'); | |
| return ''; | |
| } | |
| $columnsOutput = ''; | |
| for ( $c = 0; $c < $numOfCols; $c++ ) { | |
| $indices = range($c + $offset, $totalItems - 1 + $offset, $numOfCols); | |
| // @TODO: Fix warning: range(): step exceeds the specified range | |
| $items = ''; | |
| foreach ($indices as $idx) { | |
| $items .= $modx->getPlaceholder($placeholder.$idx); | |
| } | |
| if ( ! empty($items) ) { | |
| $columnsOutput .= $modx->getChunk($colTpl, array('items' => $items)); | |
| } | |
| } | |
| return $columnsOutput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment